PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/classes/post_form.php

https://bitbucket.org/synergylearning/campusconnect
PHP | 250 lines | 153 code | 37 blank | 60 comment | 39 complexity | 2683a34ea8b2a1e18f1531e0c459c081 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, LGPL-2.1, Apache-2.0, BSD-3-Clause, AGPL-3.0
  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
  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. $edit = $this->_customdata['edit'];
  85. $thresholdwarning = $this->_customdata['thresholdwarning'];
  86. $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
  87. // If there is a warning message and we are not editing a post we need to handle the warning.
  88. if (!empty($thresholdwarning) && !$edit) {
  89. // Here we want to display a warning if they can still post but have reached the warning threshold.
  90. if ($thresholdwarning->canpost) {
  91. $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
  92. $mform->addElement('html', $OUTPUT->notification($message));
  93. }
  94. }
  95. $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
  96. $mform->setType('subject', PARAM_TEXT);
  97. $mform->addRule('subject', get_string('required'), 'required', null, 'client');
  98. $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  99. $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
  100. $mform->setType('message', PARAM_RAW);
  101. $mform->addRule('message', get_string('required'), 'required', null, 'client');
  102. if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
  103. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
  104. $mform->addElement('hidden', 'subscribe');
  105. $mform->setType('subscribe', PARAM_INT);
  106. $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
  107. } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
  108. has_capability('moodle/course:manageactivities', $coursecontext)) {
  109. $options = array();
  110. $options[0] = get_string('subscribestop', 'forum');
  111. $options[1] = get_string('subscribestart', 'forum');
  112. $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
  113. $mform->addHelpButton('subscribe', 'subscription', 'forum');
  114. } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
  115. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
  116. $mform->addElement('hidden', 'subscribe');
  117. $mform->setType('subscribe', PARAM_INT);
  118. $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
  119. }
  120. if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
  121. $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
  122. $mform->addHelpButton('attachments', 'attachment', 'forum');
  123. }
  124. if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
  125. $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
  126. }
  127. if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
  128. $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
  129. $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
  130. $mform->addHelpButton('timestart', 'displaystart', 'forum');
  131. $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
  132. $mform->addHelpButton('timeend', 'displayend', 'forum');
  133. } else {
  134. $mform->addElement('hidden', 'timestart');
  135. $mform->setType('timestart', PARAM_INT);
  136. $mform->addElement('hidden', 'timeend');
  137. $mform->setType('timeend', PARAM_INT);
  138. $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
  139. }
  140. if ($groupmode = groups_get_activity_groupmode($cm, $course)) { // hack alert
  141. $groupdata = groups_get_activity_allowed_groups($cm);
  142. $groupcount = count($groupdata);
  143. $groupinfo = array();
  144. $modulecontext = context_module::instance($cm->id);
  145. // Check whether the user has access to all groups in this forum from the accessallgroups cap.
  146. if ($groupmode == VISIBLEGROUPS || has_capability('moodle/site:accessallgroups', $modulecontext)) {
  147. // Only allow posting to all groups if the user has access to all groups.
  148. $groupinfo = array('0' => get_string('allparticipants'));
  149. $groupcount++;
  150. }
  151. $contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
  152. if ($contextcheck) {
  153. foreach ($groupdata as $grouptemp) {
  154. $groupinfo[$grouptemp->id] = $grouptemp->name;
  155. }
  156. $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
  157. $mform->setDefault('groupinfo', $post->groupid);
  158. $mform->setType('groupinfo', PARAM_INT);
  159. } else {
  160. if (empty($post->groupid)) {
  161. $groupname = get_string('allparticipants');
  162. } else {
  163. $groupname = format_string($groupdata[$post->groupid]->name);
  164. }
  165. $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
  166. }
  167. }
  168. //-------------------------------------------------------------------------------
  169. // buttons
  170. if (isset($post->edit)) { // hack alert
  171. $submit_string = get_string('savechanges');
  172. } else {
  173. $submit_string = get_string('posttoforum', 'forum');
  174. }
  175. $this->add_action_buttons(false, $submit_string);
  176. $mform->addElement('hidden', 'course');
  177. $mform->setType('course', PARAM_INT);
  178. $mform->addElement('hidden', 'forum');
  179. $mform->setType('forum', PARAM_INT);
  180. $mform->addElement('hidden', 'discussion');
  181. $mform->setType('discussion', PARAM_INT);
  182. $mform->addElement('hidden', 'parent');
  183. $mform->setType('parent', PARAM_INT);
  184. $mform->addElement('hidden', 'userid');
  185. $mform->setType('userid', PARAM_INT);
  186. $mform->addElement('hidden', 'groupid');
  187. $mform->setType('groupid', PARAM_INT);
  188. $mform->addElement('hidden', 'edit');
  189. $mform->setType('edit', PARAM_INT);
  190. $mform->addElement('hidden', 'reply');
  191. $mform->setType('reply', PARAM_INT);
  192. }
  193. /**
  194. * Form validation
  195. *
  196. * @param array $data data from the form.
  197. * @param array $files files uploaded.
  198. * @return array of errors.
  199. */
  200. function validation($data, $files) {
  201. $errors = parent::validation($data, $files);
  202. if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
  203. $errors['timeend'] = get_string('timestartenderror', 'forum');
  204. }
  205. if (empty($data['message']['text'])) {
  206. $errors['message'] = get_string('erroremptymessage', 'forum');
  207. }
  208. if (empty($data['subject'])) {
  209. $errors['subject'] = get_string('erroremptysubject', 'forum');
  210. }
  211. return $errors;
  212. }
  213. }