MDL-65069 mod_forum: inline 'add discussion' form
[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');
7fb35af6 28require_once($CFG->dirroot . '/repository/lib.php');
2ecd2847
FM
29
30/**
31 * Class to post in a forum.
32 *
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
36 */
37class mod_forum_post_form extends moodleform {
38
39 /**
40 * Returns the options array to use in filemanager for forum attachments
41 *
42 * @param stdClass $forum
43 * @return array
44 */
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' => '*',
151b0f94 53 'return_types' => FILE_INTERNAL | FILE_CONTROLLED_LINK
2ecd2847
FM
54 );
55 }
56
57 /**
58 * Returns the options array to use in forum text editor
59 *
5e95223e
PS
60 * @param context_module $context
61 * @param int $postid post id, use null when adding new post
2ecd2847
FM
62 * @return array
63 */
5e95223e 64 public static function editor_options(context_module $context, $postid) {
2ecd2847
FM
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,
5e95223e
PS
72 'return_types'=> FILE_INTERNAL | FILE_EXTERNAL,
73 'subdirs' => file_area_contains_subdirs($context, 'mod_forum', 'post', $postid)
2ecd2847
FM
74 );
75 }
76
77 /**
78 * Form definition
79 *
80 * @return void
81 */
82 function definition() {
83 global $CFG, $OUTPUT;
84
85 $mform =& $this->_form;
86
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'];
ebfb73db 93 $subscribe = $this->_customdata['subscribe'];
2ecd2847
FM
94 $edit = $this->_customdata['edit'];
95 $thresholdwarning = $this->_customdata['thresholdwarning'];
bc4c7337 96 $canreplyprivately = $this->_customdata['canreplyprivately'];
2ecd2847
FM
97
98 $mform->addElement('header', 'general', '');//fill in the data depending on page params later using set_data
99
100 // If there is a warning message and we are not editing a post we need to handle the warning.
101 if (!empty($thresholdwarning) && !$edit) {
102 // Here we want to display a warning if they can still post but have reached the warning threshold.
103 if ($thresholdwarning->canpost) {
104 $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
105 $mform->addElement('html', $OUTPUT->notification($message));
106 }
107 }
108
109 $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
110 $mform->setType('subject', PARAM_TEXT);
111 $mform->addRule('subject', get_string('required'), 'required', null, 'client');
112 $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
113
5e95223e 114 $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
2ecd2847
FM
115 $mform->setType('message', PARAM_RAW);
116 $mform->addRule('message', get_string('required'), 'required', null, 'client');
117
ebfb73db
AN
118 $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
119
120 if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
be773fd6
AN
121 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
122 $mform->freeze('discussionsubscribe');
123 $mform->setDefaults('discussionsubscribe', 0);
124 $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum');
ebfb73db
AN
125
126 } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
be773fd6
AN
127 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
128 $mform->freeze('discussionsubscribe');
129 $mform->setDefaults('discussionsubscribe', 0);
130 $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum');
2ecd2847 131
ebfb73db 132 } else {
be773fd6 133 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
ebfb73db 134 $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
59075a43 135 }
2ecd2847 136
581e75bf 137 if (forum_can_create_attachment($forum, $modcontext)) {
2ecd2847
FM
138 $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null, self::attachment_options($forum));
139 $mform->addHelpButton('attachments', 'attachment', 'forum');
140 }
141
5f219cf1
BK
142 if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) {
143 $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum'));
144 $mform->addHelpButton('pinned', 'discussionpinned', 'forum');
145 }
146
ebfb73db 147 if (empty($post->id) && $manageactivities) {
2ecd2847
FM
148 $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
149 }
150
bc4c7337 151 if ((empty($post->id) && $canreplyprivately) || (!empty($post) && !empty($post->privatereplyto))) {
e782253c 152 // Only show the option to change private reply settings if this is a new post and the user can reply
bc4c7337
AN
153 // privately, or if this is already private reply, in which case the state is shown but is not editable.
154 $mform->addElement('checkbox', 'isprivatereply', get_string('privatereply', 'forum'));
155 $mform->addHelpButton('isprivatereply', 'privatereply', 'forum');
156 if (!empty($post->privatereplyto)) {
157 $mform->setDefault('isprivatereply', 1);
158 $mform->freeze('isprivatereply');
159 }
160 }
161
344e1263 162 if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
2ecd2847 163 $groupdata = groups_get_activity_allowed_groups($cm);
344e1263 164
f2946a54 165 $groupinfo = array();
344e1263
AN
166 foreach ($groupdata as $groupid => $group) {
167 // Check whether this user can post in this group.
168 // We must make this check because all groups are returned for a visible grouped activity.
169 if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
170 // Build the data for the groupinfo select.
171 $groupinfo[$groupid] = $group->name;
172 } else {
173 unset($groupdata[$groupid]);
174 }
175 }
176 $groupcount = count($groupinfo);
177
178 // Check whether a user can post to all of their own groups.
179
180 // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
181 // 1) It only makes sense to allow this when a user is in more than one group.
182 // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
183 $canposttoowngroups = empty($post->edit) && $groupcount > 1;
184
185 // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
186 $canposttoowngroups = $canposttoowngroups && empty($post->parent);
187
188 // 3) You also need the canposttoowngroups capability.
189 $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
190 if ($canposttoowngroups) {
191 // This user is in multiple groups, and can post to all of their own groups.
192 // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
193 // user is a member of.
194 $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
195 $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
196 $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
197 }
f2946a54 198
344e1263
AN
199 // Check whether this user can post to all groups.
200 // Posts to the 'All participants' group go to all groups, not to each group in a list.
201 // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
202 if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) {
203 // Note: We must reverse in this manner because array_unshift renumbers the array.
204 $groupinfo = array_reverse($groupinfo, true );
205 $groupinfo[-1] = get_string('allparticipants');
206 $groupinfo = array_reverse($groupinfo, true );
f2946a54
AN
207 $groupcount++;
208 }
209
344e1263
AN
210 // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
211 // 1) This is a new post (not an edit), and there are at least two groups to choose from.
212 $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
2587313a 213
344e1263
AN
214 // 2) This is editing of an existing post and the user is allowed to movediscussions.
215 // We allow this because the post may have been moved from another forum where groups are not available.
216 // We show this even if no groups are available as groups *may* have been available but now are not.
217 $canselectgroupformove = $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext);
218
219 // Important: You can *only* change the group for a top level post. Never any reply.
220 $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
221
222 if ($canselectgroup) {
2ecd2847
FM
223 $mform->addElement('select','groupinfo', get_string('group'), $groupinfo);
224 $mform->setDefault('groupinfo', $post->groupid);
fe484811 225 $mform->setType('groupinfo', PARAM_INT);
2ecd2847
FM
226 } else {
227 if (empty($post->groupid)) {
228 $groupname = get_string('allparticipants');
229 } else {
230 $groupname = format_string($groupdata[$post->groupid]->name);
231 }
232 $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
233 }
234 }
608edec5
AN
235
236 if (!empty($CFG->forum_enabletimedposts) && !$post->parent && has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
237 $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
238
239 $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'), array('optional' => true));
240 $mform->addHelpButton('timestart', 'displaystart', 'forum');
241
242 $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'), array('optional' => true));
243 $mform->addHelpButton('timeend', 'displayend', 'forum');
244
245 } else {
246 $mform->addElement('hidden', 'timestart');
247 $mform->setType('timestart', PARAM_INT);
248 $mform->addElement('hidden', 'timeend');
249 $mform->setType('timeend', PARAM_INT);
250 $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
251 }
252
d2ba493c
AH
253 if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) {
254 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
255
256 $mform->addElement('tags', 'tags', get_string('tags'),
257 array('itemtype' => 'forum_posts', 'component' => 'mod_forum'));
258 }
259
2ecd2847
FM
260 //-------------------------------------------------------------------------------
261 // buttons
262 if (isset($post->edit)) { // hack alert
263 $submit_string = get_string('savechanges');
264 } else {
265 $submit_string = get_string('posttoforum', 'forum');
266 }
2587313a 267
f3206dfc 268 $this->add_action_buttons(true, $submit_string);
2ecd2847
FM
269
270 $mform->addElement('hidden', 'course');
271 $mform->setType('course', PARAM_INT);
272
273 $mform->addElement('hidden', 'forum');
274 $mform->setType('forum', PARAM_INT);
275
276 $mform->addElement('hidden', 'discussion');
277 $mform->setType('discussion', PARAM_INT);
278
279 $mform->addElement('hidden', 'parent');
280 $mform->setType('parent', PARAM_INT);
281
2ecd2847
FM
282 $mform->addElement('hidden', 'groupid');
283 $mform->setType('groupid', PARAM_INT);
284
285 $mform->addElement('hidden', 'edit');
286 $mform->setType('edit', PARAM_INT);
287
288 $mform->addElement('hidden', 'reply');
289 $mform->setType('reply', PARAM_INT);
290 }
291
292 /**
293 * Form validation
294 *
295 * @param array $data data from the form.
296 * @param array $files files uploaded.
297 * @return array of errors.
298 */
299 function validation($data, $files) {
300 $errors = parent::validation($data, $files);
301 if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
302 $errors['timeend'] = get_string('timestartenderror', 'forum');
303 }
304 if (empty($data['message']['text'])) {
305 $errors['message'] = get_string('erroremptymessage', 'forum');
306 }
307 if (empty($data['subject'])) {
308 $errors['subject'] = get_string('erroremptysubject', 'forum');
309 }
310 return $errors;
311 }
312}