PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/mod/forum/post_form.php

http://github.com/moodle/moodle
PHP | 172 lines | 116 code | 33 blank | 23 comment | 31 complexity | 3bebf89e9b31da98c20056c4e4f85853 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. if (empty($post->groupid)) {
  94. $groupname = get_string('allparticipants');
  95. } else {
  96. $group = groups_get_group($post->groupid);
  97. $groupname = format_string($group->name);
  98. }
  99. $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
  100. }
  101. //-------------------------------------------------------------------------------
  102. // buttons
  103. if (isset($post->edit)) { // hack alert
  104. $submit_string = get_string('savechanges');
  105. } else {
  106. $submit_string = get_string('posttoforum', 'forum');
  107. }
  108. $this->add_action_buttons(false, $submit_string);
  109. $mform->addElement('hidden', 'course');
  110. $mform->setType('course', PARAM_INT);
  111. $mform->addElement('hidden', 'forum');
  112. $mform->setType('forum', PARAM_INT);
  113. $mform->addElement('hidden', 'discussion');
  114. $mform->setType('discussion', PARAM_INT);
  115. $mform->addElement('hidden', 'parent');
  116. $mform->setType('parent', PARAM_INT);
  117. $mform->addElement('hidden', 'userid');
  118. $mform->setType('userid', PARAM_INT);
  119. $mform->addElement('hidden', 'groupid');
  120. $mform->setType('groupid', PARAM_INT);
  121. $mform->addElement('hidden', 'edit');
  122. $mform->setType('edit', PARAM_INT);
  123. $mform->addElement('hidden', 'reply');
  124. $mform->setType('reply', PARAM_INT);
  125. }
  126. function validation($data, $files) {
  127. $errors = parent::validation($data, $files);
  128. if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
  129. $errors['timeend'] = get_string('timestartenderror', 'forum');
  130. }
  131. if (empty($data['message']['text'])) {
  132. $errors['message'] = get_string('erroremptymessage', 'forum');
  133. }
  134. if (empty($data['subject'])) {
  135. $errors['subject'] = get_string('erroremptysubject', 'forum');
  136. }
  137. return $errors;
  138. }
  139. }