Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   1  <?php
   2  
   3  // This file is part of Moodle - http://moodle.org/
   4  //
   5  // Moodle is free software: you can redistribute it and/or modify
   6  // it under the terms of the GNU General Public License as published by
   7  // the Free Software Foundation, either version 3 of the License, or
   8  // (at your option) any later version.
   9  //
  10  // Moodle is distributed in the hope that it will be useful,
  11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13  // GNU General Public License for more details.
  14  //
  15  // You should have received a copy of the GNU General Public License
  16  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  17  
  18  /**
  19   * File containing the form definition to post in the forum.
  20   *
  21   * @package   mod_forum
  22   * @copyright Jamie Pratt <me@jamiep.org>
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  require_once($CFG->libdir . '/formslib.php');
  28  require_once($CFG->dirroot . '/repository/lib.php');
  29  
  30  /**
  31   * Class to post in a forum.
  32   *
  33   * @package   mod_forum
  34   * @copyright Jamie Pratt <me@jamiep.org>
  35   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class mod_forum_post_form extends moodleform {
  38  
  39      /**
  40       * Returns the options array to use in filemanager for forum attachments
  41       *
  42       * @param stdClass $forum
  43       * @return array
  44       */
  45      public static function attachment_options($forum) {
  46          global $COURSE, $PAGE, $CFG;
  47          $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
  48          return array(
  49              'subdirs' => 0,
  50              'maxbytes' => $maxbytes,
  51              'maxfiles' => $forum->maxattachments,
  52              'accepted_types' => '*',
  53              'return_types' => FILE_INTERNAL | FILE_CONTROLLED_LINK
  54          );
  55      }
  56  
  57      /**
  58       * Returns the options array to use in forum text editor
  59       *
  60       * @param context_module $context
  61       * @param int $postid post id, use null when adding new post
  62       * @return array
  63       */
  64      public static function editor_options(context_module $context, $postid) {
  65          global $COURSE, $PAGE, $CFG;
  66          // TODO: add max files and max size support
  67          $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
  68          return array(
  69              'maxfiles' => EDITOR_UNLIMITED_FILES,
  70              'maxbytes' => $maxbytes,
  71              'trusttext'=> true,
  72              'return_types'=> FILE_INTERNAL | FILE_EXTERNAL,
  73              'subdirs' => file_area_contains_subdirs($context, 'mod_forum', 'post', $postid)
  74          );
  75      }
  76  
  77      /**
  78       * Form definition
  79       *
  80       * @return void
  81       */
  82      function definition() {
  83          global $CFG, $OUTPUT;
  84  
  85          $mform =& $this->_form;
  86  
  87          $course = $this->_customdata['course'];
  88          $cm = $this->_customdata['cm'];
  89          $coursecontext = $this->_customdata['coursecontext'];
  90          $modcontext = $this->_customdata['modcontext'];
  91          $forum = $this->_customdata['forum'];
  92          $post = $this->_customdata['post'];
  93          $subscribe = $this->_customdata['subscribe'];
  94          $edit = $this->_customdata['edit'];
  95          $thresholdwarning = $this->_customdata['thresholdwarning'];
  96          $canreplyprivately = array_key_exists('canreplyprivately', $this->_customdata) ?
  97              $this->_customdata['canreplyprivately'] : false;
  98          $inpagereply = $this->_customdata['inpagereply'] ?? false;
  99  
 100          if (!$inpagereply) {
 101              // Fill in the data depending on page params later using set_data.
 102              $mform->addElement('header', 'general', '');
 103          }
 104  
 105          // If there is a warning message and we are not editing a post we need to handle the warning.
 106          if (!empty($thresholdwarning) && !$edit) {
 107              // Here we want to display a warning if they can still post but have reached the warning threshold.
 108              if ($thresholdwarning->canpost) {
 109                  $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
 110                  $mform->addElement('html', $OUTPUT->notification($message, \core\output\notification::NOTIFY_INFO));
 111              }
 112          }
 113  
 114          $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
 115          $mform->setType('subject', PARAM_TEXT);
 116          $mform->addRule('subject', get_string('required'), 'required', null, 'client');
 117          $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
 118  
 119          $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
 120          $mform->setType('message', PARAM_RAW);
 121          $mform->addRule('message', get_string('required'), 'required', null, 'client');
 122  
 123          if (!$inpagereply) {
 124              $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
 125  
 126              if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
 127                  $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
 128                  $mform->freeze('discussionsubscribe');
 129                  $mform->setDefaults('discussionsubscribe', 0);
 130                  $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum');
 131  
 132              } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
 133                  $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
 134                  $mform->freeze('discussionsubscribe');
 135                  $mform->setDefaults('discussionsubscribe', 0);
 136                  $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum');
 137  
 138              } else {
 139                  $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
 140                  $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
 141              }
 142  
 143              if (forum_can_create_attachment($forum, $modcontext)) {
 144                  $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
 145                      self::attachment_options($forum));
 146                  $mform->addHelpButton('attachments', 'attachment', 'forum');
 147              }
 148  
 149              if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) {
 150                  $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum'));
 151                  $mform->addHelpButton('pinned', 'discussionpinned', 'forum');
 152              }
 153  
 154              if (empty($post->id) && ($manageactivities ||
 155                      ($forum->type == 'qanda' && has_capability('mod/forum:canmailnow', $modcontext)))
 156              ) {
 157                  $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
 158              }
 159  
 160              if ((empty($post->id) && $canreplyprivately) || (!empty($post) && !empty($post->privatereplyto))) {
 161                  // Only show the option to change private reply settings if this is a new post and the user can reply
 162                  // privately, or if this is already private reply, in which case the state is shown but is not editable.
 163                  $mform->addElement('checkbox', 'isprivatereply', get_string('privatereply', 'forum'));
 164                  $mform->addHelpButton('isprivatereply', 'privatereply', 'forum');
 165                  if (!empty($post->privatereplyto)) {
 166                      $mform->setDefault('isprivatereply', 1);
 167                      $mform->freeze('isprivatereply');
 168                  }
 169              }
 170  
 171              if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
 172                  $groupdata = groups_get_activity_allowed_groups($cm);
 173                  $groupinfo = array();
 174                  foreach ($groupdata as $groupid => $group) {
 175                      // Check whether this user can post in this group.
 176                      // We must make this check because all groups are returned for a visible grouped activity.
 177                      if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
 178                          // Build the data for the groupinfo select.
 179                          $groupinfo[$groupid] = format_string($group->name, true, ['context' => $modcontext]);
 180                      } else {
 181                          unset($groupdata[$groupid]);
 182                      }
 183                  }
 184                  $groupcount = count($groupinfo);
 185  
 186                  // Check whether a user can post to all of their own groups.
 187  
 188                  // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
 189                  // 1) It only makes sense to allow this when a user is in more than one group.
 190                  // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
 191                  $canposttoowngroups = empty($post->edit) && $groupcount > 1;
 192  
 193                  // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
 194                  $canposttoowngroups = $canposttoowngroups && empty($post->parent);
 195  
 196                  // 3) You also need the canposttoowngroups capability.
 197                  $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
 198                  if ($canposttoowngroups) {
 199                      // This user is in multiple groups, and can post to all of their own groups.
 200                      // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
 201                      // user is a member of.
 202                      $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
 203                      $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
 204                      $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
 205                  }
 206  
 207                  // Check whether this user can post to all groups.
 208                  // Posts to the 'All participants' group go to all groups, not to each group in a list.
 209                  // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
 210                  if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) {
 211                      // Note: We must reverse in this manner because array_unshift renumbers the array.
 212                      $groupinfo = array_reverse($groupinfo, true);
 213                      $groupinfo[-1] = get_string('allparticipants');
 214                      $groupinfo = array_reverse($groupinfo, true);
 215                      $groupcount++;
 216                  }
 217  
 218                  // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
 219                  // 1) This is a new post (not an edit), and there are at least two groups to choose from.
 220                  $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
 221  
 222                  // 2) This is editing of an existing post and the user is allowed to movediscussions.
 223                  // We allow this because the post may have been moved from another forum where groups are not available.
 224                  // We show this even if no groups are available as groups *may* have been available but now are not.
 225                  $canselectgroupformove =
 226                      $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext);
 227  
 228                  // Important: You can *only* change the group for a top level post. Never any reply.
 229                  $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
 230  
 231                  if ($canselectgroup) {
 232                      $mform->addElement('select', 'groupinfo', get_string('group'), $groupinfo);
 233                      $mform->setDefault('groupinfo', $post->groupid);
 234                      $mform->setType('groupinfo', PARAM_INT);
 235                  } else {
 236                      if (empty($post->groupid)) {
 237                          $groupname = get_string('allparticipants');
 238                      } else {
 239                          $groupname = format_string($groupdata[$post->groupid]->name);
 240                      }
 241                      $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
 242                  }
 243              }
 244  
 245              if (!empty($CFG->forum_enabletimedposts) && !$post->parent &&
 246                  has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
 247                  $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
 248  
 249                  $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'),
 250                      array('optional' => true));
 251                  $mform->addHelpButton('timestart', 'displaystart', 'forum');
 252  
 253                  $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'),
 254                      array('optional' => true));
 255                  $mform->addHelpButton('timeend', 'displayend', 'forum');
 256  
 257              } else {
 258                  $mform->addElement('hidden', 'timestart');
 259                  $mform->setType('timestart', PARAM_INT);
 260                  $mform->addElement('hidden', 'timeend');
 261                  $mform->setType('timeend', PARAM_INT);
 262                  $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
 263              }
 264  
 265              if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) {
 266                  $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
 267  
 268                  $mform->addElement('tags', 'tags', get_string('tags'),
 269                      array('itemtype' => 'forum_posts', 'component' => 'mod_forum'));
 270              }
 271          }
 272  
 273          //-------------------------------------------------------------------------------
 274          // buttons
 275          if (isset($post->edit)) { // hack alert
 276              $submitstring = get_string('savechanges');
 277          } else {
 278              $submitstring = get_string('posttoforum', 'forum');
 279          }
 280  
 281          // Always register a no submit button so it can be picked up if redirecting to the original post form.
 282          $mform->registerNoSubmitButton('advancedadddiscussion');
 283  
 284          // This is an inpage add discussion which requires custom buttons.
 285          if ($inpagereply) {
 286              $mform->addElement('hidden', 'discussionsubscribe');
 287              $mform->setType('discussionsubscribe', PARAM_INT);
 288  
 289              $buttonarray = array();
 290              $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitstring);
 291              $buttonarray[] = &$mform->createElement('button', 'cancelbtn',
 292                  get_string('cancel', 'core'),
 293                  // Additional attribs to handle collapsible div.
 294                  ['data-toggle' => 'collapse', 'data-target' => "#collapseAddForm"]);
 295              $buttonarray[] = &$mform->createElement('submit', 'advancedadddiscussion',
 296                  get_string('showadvancededitor'), null, null, ['customclassoverride' => 'btn-link']);
 297  
 298              $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
 299              $mform->closeHeaderBefore('buttonar');
 300          } else {
 301              $this->add_action_buttons(true, $submitstring);
 302          }
 303  
 304          $mform->addElement('hidden', 'course');
 305          $mform->setType('course', PARAM_INT);
 306  
 307          $mform->addElement('hidden', 'forum');
 308          $mform->setType('forum', PARAM_INT);
 309  
 310          $mform->addElement('hidden', 'discussion');
 311          $mform->setType('discussion', PARAM_INT);
 312  
 313          $mform->addElement('hidden', 'parent');
 314          $mform->setType('parent', PARAM_INT);
 315  
 316          $mform->addElement('hidden', 'groupid');
 317          $mform->setType('groupid', PARAM_INT);
 318  
 319          $mform->addElement('hidden', 'edit');
 320          $mform->setType('edit', PARAM_INT);
 321  
 322          $mform->addElement('hidden', 'reply');
 323          $mform->setType('reply', PARAM_INT);
 324      }
 325  
 326      /**
 327       * Form validation
 328       *
 329       * @param array $data data from the form.
 330       * @param array $files files uploaded.
 331       * @return array of errors.
 332       */
 333      function validation($data, $files) {
 334          $errors = parent::validation($data, $files);
 335          if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
 336              $errors['timeend'] = get_string('timestartenderror', 'forum');
 337          }
 338          if (empty($data['message']['text'])) {
 339              $errors['message'] = get_string('erroremptymessage', 'forum');
 340          }
 341          if (empty($data['subject'])) {
 342              $errors['subject'] = get_string('erroremptysubject', 'forum');
 343          }
 344          return $errors;
 345      }
 346  }