PageRenderTime 43ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/mod/forum/post_form.php

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