PageRenderTime 77ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/classes/post_form.php

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