/mod/forum/post_form.php

https://bitbucket.org/andrewdavidson/sl-clone · PHP · 207 lines · 140 code · 34 blank · 33 comment · 33 complexity · e6dd5a504d7da61d688cd4ec17d52e95 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. * @package mod-forum
  18. * @copyright Jamie Pratt <me@jamiep.org>
  19. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  20. */
  21. if (!defined('MOODLE_INTERNAL')) {
  22. die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
  23. }
  24. require_once($CFG->libdir.'/formslib.php');
  25. class mod_forum_post_form extends moodleform {
  26. /**
  27. * Returns the options array to use in filemanager for forum attachments
  28. *
  29. * @param stdClass $forum
  30. * @return array
  31. */
  32. public static function attachment_options($forum) {
  33. global $COURSE, $PAGE, $CFG;
  34. $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
  35. return array(
  36. 'subdirs' => 0,
  37. 'maxbytes' => $maxbytes,
  38. 'maxfiles' => $forum->maxattachments,
  39. 'accepted_types' => '*',
  40. 'return_types' => FILE_INTERNAL
  41. );
  42. }
  43. /**
  44. * Returns the options array to use in forum text editor
  45. *
  46. * @return array
  47. */
  48. public static function editor_options() {
  49. global $COURSE, $PAGE, $CFG;
  50. // TODO: add max files and max size support
  51. $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
  52. return array(
  53. 'maxfiles' => EDITOR_UNLIMITED_FILES,
  54. 'maxbytes' => $maxbytes,
  55. 'trusttext'=> true,
  56. 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL
  57. );
  58. }
  59. function definition() {
  60. global $CFG;
  61. $mform =& $this->_form;
  62. $course = $this->_customdata['course'];
  63. $cm = $this->_customdata['cm'];
  64. $coursecontext = $this->_customdata['coursecontext'];
  65. $modcontext = $this->_customdata['modcontext'];
  66. $forum = $this->_customdata['forum'];
  67. $post = $this->_customdata['post'];
  68. $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
  69. $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
  70. $mform->setType('subject', PARAM_TEXT);
  71. $mform->addRule('subject', get_string('required'), 'required', null, 'client');
  72. $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  73. $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options());
  74. $mform->setType('message', PARAM_RAW);
  75. $mform->addRule('message', get_string('required'), 'required', null, 'client');
  76. if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
  77. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
  78. $mform->addElement('hidden', 'subscribe');
  79. $mform->setType('subscribe', PARAM_INT);
  80. $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
  81. } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
  82. has_capability('moodle/course:manageactivities', $coursecontext)) {
  83. $options = array();
  84. $options[0] = get_string('subscribestop', 'forum');
  85. $options[1] = get_string('subscribestart', 'forum');
  86. $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
  87. $mform->addHelpButton('subscribe', 'subscription', 'forum');
  88. } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
  89. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
  90. $mform->addElement('hidden', 'subscribe');
  91. $mform->setType('subscribe', PARAM_INT);
  92. $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
  93. }
  94. if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
  95. $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
  96. $mform->addHelpButton('attachments', 'attachment', 'forum');
  97. }
  98. if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
  99. $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
  100. }
  101. if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
  102. $mform->addElement('header', '', get_string('displayperiod', 'forum'));
  103. $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
  104. $mform->addHelpButton('timestart', 'displaystart', 'forum');
  105. $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
  106. $mform->addHelpButton('timeend', 'displayend', 'forum');
  107. } else {
  108. $mform->addElement('hidden', 'timestart');
  109. $mform->setType('timestart', PARAM_INT);
  110. $mform->addElement('hidden', 'timeend');
  111. $mform->setType('timeend', PARAM_INT);
  112. $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
  113. }
  114. if (groups_get_activity_groupmode($cm, $course)) { // hack alert
  115. $groupdata = groups_get_activity_allowed_groups($cm);
  116. $groupcount = count($groupdata);
  117. $modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
  118. $contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
  119. if ($contextcheck) {
  120. $groupinfo = array('0' => get_string('allparticipants'));
  121. foreach ($groupdata as $grouptemp) {
  122. $groupinfo[$grouptemp->id] = $grouptemp->name;
  123. }
  124. $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
  125. $mform->setDefault('groupinfo', $post->groupid);
  126. } else {
  127. if (empty($post->groupid)) {
  128. $groupname = get_string('allparticipants');
  129. } else {
  130. $groupname = format_string($groupdata[$post->groupid]->name);
  131. }
  132. $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
  133. }
  134. }
  135. //-------------------------------------------------------------------------------
  136. // buttons
  137. if (isset($post->edit)) { // hack alert
  138. $submit_string = get_string('savechanges');
  139. } else {
  140. $submit_string = get_string('posttoforum', 'forum');
  141. }
  142. $this->add_action_buttons(false, $submit_string);
  143. $mform->addElement('hidden', 'course');
  144. $mform->setType('course', PARAM_INT);
  145. $mform->addElement('hidden', 'forum');
  146. $mform->setType('forum', PARAM_INT);
  147. $mform->addElement('hidden', 'discussion');
  148. $mform->setType('discussion', PARAM_INT);
  149. $mform->addElement('hidden', 'parent');
  150. $mform->setType('parent', PARAM_INT);
  151. $mform->addElement('hidden', 'userid');
  152. $mform->setType('userid', PARAM_INT);
  153. $mform->addElement('hidden', 'groupid');
  154. $mform->setType('groupid', PARAM_INT);
  155. $mform->addElement('hidden', 'edit');
  156. $mform->setType('edit', PARAM_INT);
  157. $mform->addElement('hidden', 'reply');
  158. $mform->setType('reply', PARAM_INT);
  159. }
  160. function validation($data, $files) {
  161. $errors = parent::validation($data, $files);
  162. if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
  163. $errors['timeend'] = get_string('timestartenderror', 'forum');
  164. }
  165. if (empty($data['message']['text'])) {
  166. $errors['message'] = get_string('erroremptymessage', 'forum');
  167. }
  168. if (empty($data['subject'])) {
  169. $errors['subject'] = get_string('erroremptysubject', 'forum');
  170. }
  171. return $errors;
  172. }
  173. }