PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/classes/post_form.php

https://gitlab.com/unofficial-mirrors/moodle
PHP | 300 lines | 173 code | 47 blank | 80 comment | 43 complexity | f27a6834a06e5c900cb2e2b001e583ca MD5 | raw file
  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. $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
  88. // If there is a warning message and we are not editing a post we need to handle the warning.
  89. if (!empty($thresholdwarning) && !$edit) {
  90. // Here we want to display a warning if they can still post but have reached the warning threshold.
  91. if ($thresholdwarning->canpost) {
  92. $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
  93. $mform->addElement('html', $OUTPUT->notification($message));
  94. }
  95. }
  96. $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
  97. $mform->setType('subject', PARAM_TEXT);
  98. $mform->addRule('subject', get_string('required'), 'required', null, 'client');
  99. $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  100. $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
  101. $mform->setType('message', PARAM_RAW);
  102. $mform->addRule('message', get_string('required'), 'required', null, 'client');
  103. $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
  104. if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
  105. $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
  106. $mform->freeze('discussionsubscribe');
  107. $mform->setDefaults('discussionsubscribe', 0);
  108. $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum');
  109. } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
  110. $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
  111. $mform->freeze('discussionsubscribe');
  112. $mform->setDefaults('discussionsubscribe', 0);
  113. $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum');
  114. } else {
  115. $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
  116. $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
  117. }
  118. if (forum_can_create_attachment($forum, $modcontext)) {
  119. $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
  120. $mform->addHelpButton('attachments', 'attachment', 'forum');
  121. }
  122. if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) {
  123. $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum'));
  124. $mform->addHelpButton('pinned', 'discussionpinned', 'forum');
  125. }
  126. if (empty($post->id) && $manageactivities) {
  127. $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
  128. }
  129. if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
  130. $groupdata = groups_get_activity_allowed_groups($cm);
  131. $groupinfo = array();
  132. foreach ($groupdata as $groupid => $group) {
  133. // Check whether this user can post in this group.
  134. // We must make this check because all groups are returned for a visible grouped activity.
  135. if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
  136. // Build the data for the groupinfo select.
  137. $groupinfo[$groupid] = $group->name;
  138. } else {
  139. unset($groupdata[$groupid]);
  140. }
  141. }
  142. $groupcount = count($groupinfo);
  143. // Check whether a user can post to all of their own groups.
  144. // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
  145. // 1) It only makes sense to allow this when a user is in more than one group.
  146. // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
  147. $canposttoowngroups = empty($post->edit) && $groupcount > 1;
  148. // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
  149. $canposttoowngroups = $canposttoowngroups && empty($post->parent);
  150. // 3) You also need the canposttoowngroups capability.
  151. $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
  152. if ($canposttoowngroups) {
  153. // This user is in multiple groups, and can post to all of their own groups.
  154. // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
  155. // user is a member of.
  156. $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
  157. $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
  158. $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
  159. }
  160. // Check whether this user can post to all groups.
  161. // Posts to the 'All participants' group go to all groups, not to each group in a list.
  162. // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
  163. if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) {
  164. // Note: We must reverse in this manner because array_unshift renumbers the array.
  165. $groupinfo = array_reverse($groupinfo, true );
  166. $groupinfo[-1] = get_string('allparticipants');
  167. $groupinfo = array_reverse($groupinfo, true );
  168. $groupcount++;
  169. }
  170. // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
  171. // 1) This is a new post (not an edit), and there are at least two groups to choose from.
  172. $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
  173. // 2) This is editing of an existing post and the user is allowed to movediscussions.
  174. // We allow this because the post may have been moved from another forum where groups are not available.
  175. // We show this even if no groups are available as groups *may* have been available but now are not.
  176. $canselectgroupformove = $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext);
  177. // Important: You can *only* change the group for a top level post. Never any reply.
  178. $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
  179. if ($canselectgroup) {
  180. $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
  181. $mform->setDefault('groupinfo', $post->groupid);
  182. $mform->setType('groupinfo', PARAM_INT);
  183. } else {
  184. if (empty($post->groupid)) {
  185. $groupname = get_string('allparticipants');
  186. } else {
  187. $groupname = format_string($groupdata[$post->groupid]->name);
  188. }
  189. $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
  190. }
  191. }
  192. if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
  193. $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
  194. $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'), array('optional' => true));
  195. $mform->addHelpButton('timestart', 'displaystart', 'forum');
  196. $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'), array('optional' => true));
  197. $mform->addHelpButton('timeend', 'displayend', 'forum');
  198. } else {
  199. $mform->addElement('hidden', 'timestart');
  200. $mform->setType('timestart', PARAM_INT);
  201. $mform->addElement('hidden', 'timeend');
  202. $mform->setType('timeend', PARAM_INT);
  203. $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
  204. }
  205. if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) {
  206. $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
  207. $mform->addElement('tags', 'tags', get_string('tags'),
  208. array('itemtype' => 'forum_posts', 'component' => 'mod_forum'));
  209. }
  210. //-------------------------------------------------------------------------------
  211. // buttons
  212. if (isset($post->edit)) { // hack alert
  213. $submit_string = get_string('savechanges');
  214. } else {
  215. $submit_string = get_string('posttoforum', 'forum');
  216. }
  217. $this->add_action_buttons(true, $submit_string);
  218. $mform->addElement('hidden', 'course');
  219. $mform->setType('course', PARAM_INT);
  220. $mform->addElement('hidden', 'forum');
  221. $mform->setType('forum', PARAM_INT);
  222. $mform->addElement('hidden', 'discussion');
  223. $mform->setType('discussion', PARAM_INT);
  224. $mform->addElement('hidden', 'parent');
  225. $mform->setType('parent', PARAM_INT);
  226. $mform->addElement('hidden', 'groupid');
  227. $mform->setType('groupid', PARAM_INT);
  228. $mform->addElement('hidden', 'edit');
  229. $mform->setType('edit', PARAM_INT);
  230. $mform->addElement('hidden', 'reply');
  231. $mform->setType('reply', PARAM_INT);
  232. }
  233. /**
  234. * Form validation
  235. *
  236. * @param array $data data from the form.
  237. * @param array $files files uploaded.
  238. * @return array of errors.
  239. */
  240. function validation($data, $files) {
  241. $errors = parent::validation($data, $files);
  242. if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
  243. $errors['timeend'] = get_string('timestartenderror', 'forum');
  244. }
  245. if (empty($data['message']['text'])) {
  246. $errors['message'] = get_string('erroremptymessage', 'forum');
  247. }
  248. if (empty($data['subject'])) {
  249. $errors['subject'] = get_string('erroremptysubject', 'forum');
  250. }
  251. return $errors;
  252. }
  253. }