MDL-41143 mod_forum: Prevent unnessary loading of formslib
[moodle.git] / mod / forum / classes / post_form.php
CommitLineData
2ecd2847
FM
1<?php
2
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.
14//
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/>.
17
18/**
19 * File containing the form definition to post in the forum.
20 *
21 * @package mod_forum
22 * @copyright Jamie Pratt <me@jamiep.org>
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26defined('MOODLE_INTERNAL') || die();
27require_once($CFG->libdir . '/formslib.php');
28
29/**
30 * Class to post in a forum.
31 *
32 * @package mod_forum
33 * @copyright Jamie Pratt <me@jamiep.org>
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class mod_forum_post_form extends moodleform {
37
38 /**
39 * Returns the options array to use in filemanager for forum attachments
40 *
41 * @param stdClass $forum
42 * @return array
43 */
44 public static function attachment_options($forum) {
45 global $COURSE, $PAGE, $CFG;
46 $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
47 return array(
48 'subdirs' => 0,
49 'maxbytes' => $maxbytes,
50 'maxfiles' => $forum->maxattachments,
51 'accepted_types' => '*',
52 'return_types' => FILE_INTERNAL
53 );
54 }
55
56 /**
57 * Returns the options array to use in forum text editor
58 *
59 * @return array
60 */
61 public static function editor_options() {
62 global $COURSE, $PAGE, $CFG;
63 // TODO: add max files and max size support
64 $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
65 return array(
66 'maxfiles' => EDITOR_UNLIMITED_FILES,
67 'maxbytes' => $maxbytes,
68 'trusttext'=> true,
69 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL
70 );
71 }
72
73 /**
74 * Form definition
75 *
76 * @return void
77 */
78 function definition() {
79 global $CFG, $OUTPUT;
80
81 $mform =& $this->_form;
82
83 $course = $this->_customdata['course'];
84 $cm = $this->_customdata['cm'];
85 $coursecontext = $this->_customdata['coursecontext'];
86 $modcontext = $this->_customdata['modcontext'];
87 $forum = $this->_customdata['forum'];
88 $post = $this->_customdata['post'];
89 $edit = $this->_customdata['edit'];
90 $thresholdwarning = $this->_customdata['thresholdwarning'];
91
92 $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
93
94 // If there is a warning message and we are not editing a post we need to handle the warning.
95 if (!empty($thresholdwarning) && !$edit) {
96 // Here we want to display a warning if they can still post but have reached the warning threshold.
97 if ($thresholdwarning->canpost) {
98 $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
99 $mform->addElement('html', $OUTPUT->notification($message));
100 }
101 }
102
103 $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
104 $mform->setType('subject', PARAM_TEXT);
105 $mform->addRule('subject', get_string('required'), 'required', null, 'client');
106 $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
107
108 $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options());
109 $mform->setType('message', PARAM_RAW);
110 $mform->addRule('message', get_string('required'), 'required', null, 'client');
111
112 if (isset($forum->id) && forum_is_forcesubscribed($forum)) {
113
114 $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('everyoneissubscribed', 'forum'));
115 $mform->addElement('hidden', 'subscribe');
116 $mform->setType('subscribe', PARAM_INT);
117 $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
118
119 } else if (isset($forum->forcesubscribe)&& $forum->forcesubscribe != FORUM_DISALLOWSUBSCRIBE ||
120 has_capability('moodle/course:manageactivities', $coursecontext)) {
121
122 $options = array();
123 $options[0] = get_string('subscribestop', 'forum');
124 $options[1] = get_string('subscribestart', 'forum');
125
126 $mform->addElement('select', 'subscribe', get_string('subscription', 'forum'), $options);
127 $mform->addHelpButton('subscribe', 'subscription', 'forum');
128 } else if ($forum->forcesubscribe == FORUM_DISALLOWSUBSCRIBE) {
129 $mform->addElement('static', 'subscribemessage', get_string('subscription', 'forum'), get_string('disallowsubscribe', 'forum'));
130 $mform->addElement('hidden', 'subscribe');
131 $mform->setType('subscribe', PARAM_INT);
132 $mform->addHelpButton('subscribemessage', 'subscription', 'forum');
133 }
134
135 if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
136 $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
137 $mform->addHelpButton('attachments', 'attachment', 'forum');
138 }
139
140 if (empty($post->id) && has_capability('moodle/course:manageactivities', $coursecontext)) { // hack alert
141 $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
142 }
143
144 if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) { // hack alert
145 $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
146
147 $mform->addElement('date_selector', 'timestart', get_string('displaystart', 'forum'), array('optional'=>true));
148 $mform->addHelpButton('timestart', 'displaystart', 'forum');
149
150 $mform->addElement('date_selector', 'timeend', get_string('displayend', 'forum'), array('optional'=>true));
151 $mform->addHelpButton('timeend', 'displayend', 'forum');
152
153 } else {
154 $mform->addElement('hidden', 'timestart');
155 $mform->setType('timestart', PARAM_INT);
156 $mform->addElement('hidden', 'timeend');
157 $mform->setType('timeend', PARAM_INT);
158 $mform->setConstants(array('timestart'=> 0, 'timeend'=>0));
159 }
160
161 if (groups_get_activity_groupmode($cm, $course)) { // hack alert
162 $groupdata = groups_get_activity_allowed_groups($cm);
163 $groupcount = count($groupdata);
164 $modulecontext = context_module::instance($cm->id);
165 $contextcheck = has_capability('mod/forum:movediscussions', $modulecontext) && empty($post->parent) && $groupcount > 1;
166 if ($contextcheck) {
167 $groupinfo = array('0' => get_string('allparticipants'));
168 foreach ($groupdata as $grouptemp) {
169 $groupinfo[$grouptemp->id] = $grouptemp->name;
170 }
171 $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
172 $mform->setDefault('groupinfo', $post->groupid);
173 } else {
174 if (empty($post->groupid)) {
175 $groupname = get_string('allparticipants');
176 } else {
177 $groupname = format_string($groupdata[$post->groupid]->name);
178 }
179 $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
180 }
181 }
182 //-------------------------------------------------------------------------------
183 // buttons
184 if (isset($post->edit)) { // hack alert
185 $submit_string = get_string('savechanges');
186 } else {
187 $submit_string = get_string('posttoforum', 'forum');
188 }
189 $this->add_action_buttons(false, $submit_string);
190
191 $mform->addElement('hidden', 'course');
192 $mform->setType('course', PARAM_INT);
193
194 $mform->addElement('hidden', 'forum');
195 $mform->setType('forum', PARAM_INT);
196
197 $mform->addElement('hidden', 'discussion');
198 $mform->setType('discussion', PARAM_INT);
199
200 $mform->addElement('hidden', 'parent');
201 $mform->setType('parent', PARAM_INT);
202
203 $mform->addElement('hidden', 'userid');
204 $mform->setType('userid', PARAM_INT);
205
206 $mform->addElement('hidden', 'groupid');
207 $mform->setType('groupid', PARAM_INT);
208
209 $mform->addElement('hidden', 'edit');
210 $mform->setType('edit', PARAM_INT);
211
212 $mform->addElement('hidden', 'reply');
213 $mform->setType('reply', PARAM_INT);
214 }
215
216 /**
217 * Form validation
218 *
219 * @param array $data data from the form.
220 * @param array $files files uploaded.
221 * @return array of errors.
222 */
223 function validation($data, $files) {
224 $errors = parent::validation($data, $files);
225 if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
226 $errors['timeend'] = get_string('timestartenderror', 'forum');
227 }
228 if (empty($data['message']['text'])) {
229 $errors['message'] = get_string('erroremptymessage', 'forum');
230 }
231 if (empty($data['subject'])) {
232 $errors['subject'] = get_string('erroremptysubject', 'forum');
233 }
234 return $errors;
235 }
236}
237