Merge branch 'm21_MDL-28017' of git://github.com/danmarsden/moodle into MOODLE_21_STABLE
[moodle.git] / mod / forum / post_form.php
blobb26deafc561cbd9e7b752042c9c531478d414921
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
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
24 if (!defined('MOODLE_INTERNAL')) {
25 die('Direct access to this script is forbidden.'); /// It must be included from a Moodle page
28 require_once($CFG->libdir.'/formslib.php');
30 class mod_forum_post_form extends moodleform {
32 function definition() {
34 global $CFG;
35 $mform =& $this->_form;
37 $course = $this->_customdata['course'];
38 $cm = $this->_customdata['cm'];
39 $coursecontext = $this->_customdata['coursecontext'];
40 $modcontext = $this->_customdata['modcontext'];
41 $forum = $this->_customdata['forum'];
42 $post = $this->_customdata['post'];
43 // if $forum->maxbytes == '0' means we should use $course->maxbytes
44 if ($forum->maxbytes == '0') {
45 $forum->maxbytes = $course->maxbytes;
47 // TODO: add max files and max size support
48 $editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'trusttext'=>true, 'context'=>$modcontext);
50 $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
51 $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
52 $mform->setType('subject', PARAM_TEXT);
53 $mform->addRule('subject', get_string('required'), 'required', null, 'client');
54 $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
56 $mform->addElement('editor', 'message', get_string('message', 'forum'), null, $editoroptions);
57 $mform->setType('message', PARAM_RAW);
58 $mform->addRule('message', get_string('required'), 'required', null, 'client');
60 if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
62 $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
63 $mform->addElement('hidden', 'subscribe');
64 $mform->setType('subscribe', PARAM_INT);
65 $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
67 } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
68 has_capability('moodle/course:manageactivities', $coursecontext)) {
70 $options = array();
71 $options[0] = get_string('subscribestop', 'forum');
72 $options[1] = get_string('subscribestart', 'forum');
74 $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
75 $mform->addHelpButton('subscribe', 'subscription', 'forum');
76 } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
77 $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
78 $mform->addElement('hidden', 'subscribe');
79 $mform->setType('subscribe', PARAM_INT);
80 $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
83 if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
84 $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
85 array('subdirs'=>0,
86 'maxbytes'=>$forum->maxbytes,
87 'maxfiles'=>$forum->maxattachments,
88 'accepted_types'=>'*',
89 'return_types'=>FILE_INTERNAL));
90 $mform->addHelpButton('attachments', 'attachment', 'forum');
93 if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
94 $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
97 if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
98 $mform->addElement('header', '', get_string('displayperiod', 'forum'));
100 $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
101 $mform->addHelpButton('timestart', 'displaystart', 'forum');
103 $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
104 $mform->addHelpButton('timeend', 'displayend', 'forum');
106 } else {
107 $mform->addElement('hidden', 'timestart');
108 $mform->setType('timestart', PARAM_INT);
109 $mform->addElement('hidden', 'timeend');
110 $mform->setType('timeend', PARAM_INT);
111 $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
114 if (groups_get_activity_groupmode($cm, $course)) { // hack alert
115 if (empty($post->groupid)) {
116 $groupname = get_string('allparticipants');
117 } else {
118 $group = groups_get_group($post->groupid);
119 $groupname = format_string($group->name);
121 $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
124 //-------------------------------------------------------------------------------
125 // buttons
126 if (isset($post->edit)) { // hack alert
127 $submit_string = get_string('savechanges');
128 } else {
129 $submit_string = get_string('posttoforum', 'forum');
131 $this->add_action_buttons(false, $submit_string);
133 $mform->addElement('hidden', 'course');
134 $mform->setType('course', PARAM_INT);
136 $mform->addElement('hidden', 'forum');
137 $mform->setType('forum', PARAM_INT);
139 $mform->addElement('hidden', 'discussion');
140 $mform->setType('discussion', PARAM_INT);
142 $mform->addElement('hidden', 'parent');
143 $mform->setType('parent', PARAM_INT);
145 $mform->addElement('hidden', 'userid');
146 $mform->setType('userid', PARAM_INT);
148 $mform->addElement('hidden', 'groupid');
149 $mform->setType('groupid', PARAM_INT);
151 $mform->addElement('hidden', 'edit');
152 $mform->setType('edit', PARAM_INT);
154 $mform->addElement('hidden', 'reply');
155 $mform->setType('reply', PARAM_INT);
158 function validation($data, $files) {
159 $errors = parent::validation($data, $files);
160 if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
161 $errors['timeend'] = get_string('timestartenderror', 'forum');
163 if (empty($data['message']['text'])) {
164 $errors['message'] = get_string('erroremptymessage', 'forum');
166 if (empty($data['subject'])) {
167 $errors['subject'] = get_string('erroremptysubject', 'forum');
169 return $errors;