PageRenderTime 40ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/post_form.php

http://github.com/moodle/moodle
PHP | 183 lines | 128 code | 32 blank | 23 comment | 35 complexity | 760b285f9b61c4cd3d0c9c39f991886e 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. * @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. function definition() {
  27. global $CFG;
  28. $mform =& $this->_form;
  29. $course = $this->_customdata['course'];
  30. $cm = $this->_customdata['cm'];
  31. $coursecontext = $this->_customdata['coursecontext'];
  32. $modcontext = $this->_customdata['modcontext'];
  33. $forum = $this->_customdata['forum'];
  34. $post = $this->_customdata['post'];
  35. // if $forum->maxbytes == '0' means we should use $course->maxbytes
  36. if ($forum->maxbytes == '0') {
  37. $forum->maxbytes = $course->maxbytes;
  38. }
  39. // TODO: add max files and max size support
  40. $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true, 'context'=>$modcontext);
  41. $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
  42. $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
  43. $mform->setType('subject', PARAM_TEXT);
  44. $mform->addRule('subject', get_string('required'), 'required', null, 'client');
  45. $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
  46. $mform->addElement('editor', 'message', get_string('message', 'forum'), null, $editoroptions);
  47. $mform->setType('message', PARAM_RAW);
  48. $mform->addRule('message', get_string('required'), 'required', null, 'client');
  49. if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
  50. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
  51. $mform->addElement('hidden', 'subscribe');
  52. $mform->setType('subscribe', PARAM_INT);
  53. $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
  54. } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
  55. has_capability('moodle/course:manageactivities', $coursecontext)) {
  56. $options = array();
  57. $options[0] = get_string('subscribestop', 'forum');
  58. $options[1] = get_string('subscribestart', 'forum');
  59. $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
  60. $mform->addHelpButton('subscribe', 'subscription', 'forum');
  61. } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
  62. $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
  63. $mform->addElement('hidden', 'subscribe');
  64. $mform->setType('subscribe', PARAM_INT);
  65. $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
  66. }
  67. if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
  68. $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
  69. array('subdirs'=>0,
  70. 'maxbytes'=>$forum->maxbytes,
  71. 'maxfiles'=>$forum->maxattachments,
  72. 'accepted_types'=>'*',
  73. 'return_types'=>FILE_INTERNAL));
  74. $mform->addHelpButton('attachments', 'attachment', 'forum');
  75. }
  76. if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
  77. $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
  78. }
  79. if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
  80. $mform->addElement('header', '', get_string('displayperiod', 'forum'));
  81. $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
  82. $mform->addHelpButton('timestart', 'displaystart', 'forum');
  83. $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
  84. $mform->addHelpButton('timeend', 'displayend', 'forum');
  85. } else {
  86. $mform->addElement('hidden', 'timestart');
  87. $mform->setType('timestart', PARAM_INT);
  88. $mform->addElement('hidden', 'timeend');
  89. $mform->setType('timeend', PARAM_INT);
  90. $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
  91. }
  92. if (groups_get_activity_groupmode($cm, $course)) { // hack alert
  93. $groupdata = groups_get_activity_allowed_groups($cm);
  94. $groupcount = count($groupdata);
  95. $modulecontext = get_context_instance(CONTEXT_MODULE, $cm->id);
  96. $contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
  97. if ($contextcheck) {
  98. $groupinfo = array('0' => get_string('allparticipants'));
  99. foreach ($groupdata as $grouptemp) {
  100. $groupinfo[$grouptemp->id] = $grouptemp->name;
  101. }
  102. $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
  103. $mform->setDefault('groupinfo', $post->groupid);
  104. } else {
  105. if (empty($post->groupid)) {
  106. $groupname = get_string('allparticipants');
  107. } else {
  108. $groupname = format_string($groupdata[$post->groupid]->name);
  109. }
  110. $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
  111. }
  112. }
  113. //-------------------------------------------------------------------------------
  114. // buttons
  115. if (isset($post->edit)) { // hack alert
  116. $submit_string = get_string('savechanges');
  117. } else {
  118. $submit_string = get_string('posttoforum', 'forum');
  119. }
  120. $this->add_action_buttons(false, $submit_string);
  121. $mform->addElement('hidden', 'course');
  122. $mform->setType('course', PARAM_INT);
  123. $mform->addElement('hidden', 'forum');
  124. $mform->setType('forum', PARAM_INT);
  125. $mform->addElement('hidden', 'discussion');
  126. $mform->setType('discussion', PARAM_INT);
  127. $mform->addElement('hidden', 'parent');
  128. $mform->setType('parent', PARAM_INT);
  129. $mform->addElement('hidden', 'userid');
  130. $mform->setType('userid', PARAM_INT);
  131. $mform->addElement('hidden', 'groupid');
  132. $mform->setType('groupid', PARAM_INT);
  133. $mform->addElement('hidden', 'edit');
  134. $mform->setType('edit', PARAM_INT);
  135. $mform->addElement('hidden', 'reply');
  136. $mform->setType('reply', PARAM_INT);
  137. }
  138. function validation($data, $files) {
  139. $errors = parent::validation($data, $files);
  140. if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
  141. $errors['timeend'] = get_string('timestartenderror', 'forum');
  142. }
  143. if (empty($data['message']['text'])) {
  144. $errors['message'] = get_string('erroremptymessage', 'forum');
  145. }
  146. if (empty($data['subject'])) {
  147. $errors['subject'] = get_string('erroremptysubject', 'forum');
  148. }
  149. return $errors;
  150. }
  151. }