MDL-56225 mod_forum: Remove unnecessary attributes from update
[moodle.git] / mod / forum / classes / post_form.php
blob6ddbaee7472fbbb9a1fddb23aeb778f86c43f4b6
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 * File containing the form definition to post in the forum.
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
26 defined('MOODLE_INTERNAL') || die();
27 require_once($CFG->libdir . '/formslib.php');
28 require_once($CFG->dirroot . '/repository/lib.php');
30 /**
31 * Class to post in a forum.
33 * @package mod_forum
34 * @copyright Jamie Pratt <me@jamiep.org>
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
37 class mod_forum_post_form extends moodleform {
39 /**
40 * Returns the options array to use in filemanager for forum attachments
42 * @param stdClass $forum
43 * @return array
45 public static function attachment_options($forum) {
46 global $COURSE, $PAGE, $CFG;
47 $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes, $forum->maxbytes);
48 return array(
49 'subdirs' => 0,
50 'maxbytes' => $maxbytes,
51 'maxfiles' => $forum->maxattachments,
52 'accepted_types' => '*',
53 'return_types' => FILE_INTERNAL
57 /**
58 * Returns the options array to use in forum text editor
60 * @param context_module $context
61 * @param int $postid post id, use null when adding new post
62 * @return array
64 public static function editor_options(context_module $context, $postid) {
65 global $COURSE, $PAGE, $CFG;
66 // TODO: add max files and max size support
67 $maxbytes = get_user_max_upload_file_size($PAGE->context, $CFG->maxbytes, $COURSE->maxbytes);
68 return array(
69 'maxfiles' => EDITOR_UNLIMITED_FILES,
70 'maxbytes' => $maxbytes,
71 'trusttext'=> true,
72 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL,
73 'subdirs' => file_area_contains_subdirs($context, 'mod_forum', 'post', $postid)
77 /**
78 * Form definition
80 * @return void
82 function definition() {
83 global $CFG, $OUTPUT;
85 $mform =& $this->_form;
87 $course = $this->_customdata['course'];
88 $cm = $this->_customdata['cm'];
89 $coursecontext = $this->_customdata['coursecontext'];
90 $modcontext = $this->_customdata['modcontext'];
91 $forum = $this->_customdata['forum'];
92 $post = $this->_customdata['post'];
93 $subscribe = $this->_customdata['subscribe'];
94 $edit = $this->_customdata['edit'];
95 $thresholdwarning = $this->_customdata['thresholdwarning'];
97 $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
99 // If there is a warning message and we are not editing a post we need to handle the warning.
100 if (!empty($thresholdwarning) && !$edit) {
101 // Here we want to display a warning if they can still post but have reached the warning threshold.
102 if ($thresholdwarning->canpost) {
103 $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
104 $mform->addElement('html', $OUTPUT->notification($message));
108 $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
109 $mform->setType('subject', PARAM_TEXT);
110 $mform->addRule('subject', get_string('required'), 'required', null, 'client');
111 $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
113 $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
114 $mform->setType('message', PARAM_RAW);
115 $mform->addRule('message', get_string('required'), 'required', null, 'client');
117 $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
119 if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
120 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
121 $mform->freeze('discussionsubscribe');
122 $mform->setDefaults('discussionsubscribe', 0);
123 $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum');
125 } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
126 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
127 $mform->freeze('discussionsubscribe');
128 $mform->setDefaults('discussionsubscribe', 0);
129 $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum');
131 } else {
132 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
133 $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
136 if (!empty($forum->maxattachments) && $forum->maxbytes != 1 && has_capability('mod/forum:createattachment', $modcontext)) { // 1 = No attachments at all
137 $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
138 $mform->addHelpButton('attachments', 'attachment', 'forum');
141 if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) {
142 $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum'));
143 $mform->addHelpButton('pinned', 'discussionpinned', 'forum');
146 if (empty($post->id) && $manageactivities) {
147 $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
150 if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
151 $groupdata = groups_get_activity_allowed_groups($cm);
153 $groupinfo = array();
154 foreach ($groupdata as $groupid => $group) {
155 // Check whether this user can post in this group.
156 // We must make this check because all groups are returned for a visible grouped activity.
157 if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
158 // Build the data for the groupinfo select.
159 $groupinfo[$groupid] = $group->name;
160 } else {
161 unset($groupdata[$groupid]);
164 $groupcount = count($groupinfo);
166 // Check whether a user can post to all of their own groups.
168 // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
169 // 1) It only makes sense to allow this when a user is in more than one group.
170 // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
171 $canposttoowngroups = empty($post->edit) && $groupcount > 1;
173 // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
174 $canposttoowngroups = $canposttoowngroups && empty($post->parent);
176 // 3) You also need the canposttoowngroups capability.
177 $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
178 if ($canposttoowngroups) {
179 // This user is in multiple groups, and can post to all of their own groups.
180 // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
181 // user is a member of.
182 $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
183 $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
184 $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
187 // Check whether this user can post to all groups.
188 // Posts to the 'All participants' group go to all groups, not to each group in a list.
189 // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
190 if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) {
191 // Note: We must reverse in this manner because array_unshift renumbers the array.
192 $groupinfo = array_reverse($groupinfo, true );
193 $groupinfo[-1] = get_string('allparticipants');
194 $groupinfo = array_reverse($groupinfo, true );
195 $groupcount++;
198 // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
199 // 1) This is a new post (not an edit), and there are at least two groups to choose from.
200 $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
202 // 2) This is editing of an existing post and the user is allowed to movediscussions.
203 // We allow this because the post may have been moved from another forum where groups are not available.
204 // We show this even if no groups are available as groups *may* have been available but now are not.
205 $canselectgroupformove = $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext);
207 // Important: You can *only* change the group for a top level post. Never any reply.
208 $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
210 if ($canselectgroup) {
211 $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
212 $mform->setDefault('groupinfo', $post->groupid);
213 $mform->setType('groupinfo', PARAM_INT);
214 } else {
215 if (empty($post->groupid)) {
216 $groupname = get_string('allparticipants');
217 } else {
218 $groupname = format_string($groupdata[$post->groupid]->name);
220 $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
224 if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
225 $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
227 $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'), array('optional' => true));
228 $mform->addHelpButton('timestart', 'displaystart', 'forum');
230 $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'), array('optional' => true));
231 $mform->addHelpButton('timeend', 'displayend', 'forum');
233 } else {
234 $mform->addElement('hidden', 'timestart');
235 $mform->setType('timestart', PARAM_INT);
236 $mform->addElement('hidden', 'timeend');
237 $mform->setType('timeend', PARAM_INT);
238 $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
241 //-------------------------------------------------------------------------------
242 // buttons
243 if (isset($post->edit)) { // hack alert
244 $submit_string = get_string('savechanges');
245 } else {
246 $submit_string = get_string('posttoforum', 'forum');
249 $this->add_action_buttons(true, $submit_string);
251 $mform->addElement('hidden', 'course');
252 $mform->setType('course', PARAM_INT);
254 $mform->addElement('hidden', 'forum');
255 $mform->setType('forum', PARAM_INT);
257 $mform->addElement('hidden', 'discussion');
258 $mform->setType('discussion', PARAM_INT);
260 $mform->addElement('hidden', 'parent');
261 $mform->setType('parent', PARAM_INT);
263 $mform->addElement('hidden', 'groupid');
264 $mform->setType('groupid', PARAM_INT);
266 $mform->addElement('hidden', 'edit');
267 $mform->setType('edit', PARAM_INT);
269 $mform->addElement('hidden', 'reply');
270 $mform->setType('reply', PARAM_INT);
274 * Form validation
276 * @param array $data data from the form.
277 * @param array $files files uploaded.
278 * @return array of errors.
280 function validation($data, $files) {
281 $errors = parent::validation($data, $files);
282 if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
283 $errors['timeend'] = get_string('timestartenderror', 'forum');
285 if (empty($data['message']['text'])) {
286 $errors['message'] = get_string('erroremptymessage', 'forum');
288 if (empty($data['subject'])) {
289 $errors['subject'] = get_string('erroremptysubject', 'forum');
291 return $errors;