MDL-65069 mod_forum: Remove unused code and additional dev docs.
[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'];
2aff2c19
P
96 $canreplyprivately = array_key_exists('canreplyprivately', $this->_customdata) ?
97 $this->_customdata['canreplyprivately'] : false;
cb28132d 98 $inpagereply = $this->_customdata['inpagereply'] ?? false;
2ecd2847 99
cb28132d 100 if (!$inpagereply) {
2aff2c19
P
101 // Fill in the data depending on page params later using set_data.
102 $mform->addElement('header', 'general', '');
97babbf7 103 }
2ecd2847
FM
104
105 // If there is a warning message and we are not editing a post we need to handle the warning.
106 if (!empty($thresholdwarning) && !$edit) {
107 // Here we want to display a warning if they can still post but have reached the warning threshold.
108 if ($thresholdwarning->canpost) {
109 $message = get_string($thresholdwarning->errorcode, $thresholdwarning->module, $thresholdwarning->additional);
110 $mform->addElement('html', $OUTPUT->notification($message));
111 }
112 }
113
114 $mform->addElement('text', 'subject', get_string('subject', 'forum'), 'size="48"');
115 $mform->setType('subject', PARAM_TEXT);
116 $mform->addRule('subject', get_string('required'), 'required', null, 'client');
117 $mform->addRule('subject', get_string('maximumchars', '', 255), 'maxlength', 255, 'client');
118
5e95223e 119 $mform->addElement('editor', 'message', get_string('message', 'forum'), null, self::editor_options($modcontext, (empty($post->id) ? null : $post->id)));
2ecd2847
FM
120 $mform->setType('message', PARAM_RAW);
121 $mform->addRule('message', get_string('required'), 'required', null, 'client');
122
cb28132d 123 if (!$inpagereply) {
97babbf7 124 $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
2ecd2847 125
97babbf7 126 if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
cb28132d 127 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
97babbf7
P
128 $mform->freeze('discussionsubscribe');
129 $mform->setDefaults('discussionsubscribe', 0);
130 $mform->addHelpButton('discussionsubscribe', 'forcesubscribed', 'forum');
5f219cf1 131
97babbf7 132 } else if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
cb28132d 133 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
97babbf7
P
134 $mform->freeze('discussionsubscribe');
135 $mform->setDefaults('discussionsubscribe', 0);
136 $mform->addHelpButton('discussionsubscribe', 'disallowsubscription', 'forum');
2ecd2847 137
97babbf7 138 } else {
cb28132d 139 $mform->addElement('checkbox', 'discussionsubscribe', get_string('discussionsubscription', 'forum'));
97babbf7 140 $mform->addHelpButton('discussionsubscribe', 'discussionsubscription', 'forum');
bc4c7337 141 }
bc4c7337 142
97babbf7
P
143 if (forum_can_create_attachment($forum, $modcontext)) {
144 $mform->addElement('filemanager', 'attachments', get_string('attachment', 'forum'), null,
145 self::attachment_options($forum));
146 $mform->addHelpButton('attachments', 'attachment', 'forum');
344e1263 147 }
f2946a54 148
97babbf7
P
149 if (!$post->parent && has_capability('mod/forum:pindiscussions', $modcontext)) {
150 $mform->addElement('checkbox', 'pinned', get_string('discussionpinned', 'forum'));
151 $mform->addHelpButton('pinned', 'discussionpinned', 'forum');
f2946a54
AN
152 }
153
97babbf7
P
154 if ((empty($post->id) && $canreplyprivately) || (!empty($post) && !empty($post->privatereplyto))) {
155 // Only show the option to change private reply settings if this is a new post and the user can reply
156 // privately, or if this is already private reply, in which case the state is shown but is not editable.
157 $mform->addElement('checkbox', 'isprivatereply', get_string('privatereply', 'forum'));
158 $mform->addHelpButton('isprivatereply', 'privatereply', 'forum');
159 if (!empty($post->privatereplyto)) {
160 $mform->setDefault('isprivatereply', 1);
161 $mform->freeze('isprivatereply');
162 }
163 }
344e1263 164
97babbf7
P
165 if ($groupmode = groups_get_activity_groupmode($cm, $course)) {
166 $groupdata = groups_get_activity_allowed_groups($cm);
167 if (empty($post->id) && $manageactivities) {
168 $mform->addElement('checkbox', 'mailnow', get_string('mailnow', 'forum'));
169 }
344e1263 170
cb28132d
P
171 $groupinfo = array();
172 foreach ($groupdata as $groupid => $group) {
173 // Check whether this user can post in this group.
174 // We must make this check because all groups are returned for a visible grouped activity.
175 if (forum_user_can_post_discussion($forum, $groupid, null, $cm, $modcontext)) {
176 // Build the data for the groupinfo select.
177 $groupinfo[$groupid] = $group->name;
178 } else {
179 unset($groupdata[$groupid]);
97babbf7 180 }
cb28132d
P
181 }
182 $groupcount = count($groupinfo);
183
184 // Check whether a user can post to all of their own groups.
185
186 // Posts to all of my groups are copied to each group that the user is a member of. Certain conditions must be met.
187 // 1) It only makes sense to allow this when a user is in more than one group.
188 // Note: This check must come before we consider adding accessallgroups, because that is not a real group.
189 $canposttoowngroups = empty($post->edit) && $groupcount > 1;
190
191 // 2) Important: You can *only* post to multiple groups for a top level post. Never any reply.
192 $canposttoowngroups = $canposttoowngroups && empty($post->parent);
193
194 // 3) You also need the canposttoowngroups capability.
195 $canposttoowngroups = $canposttoowngroups && has_capability('mod/forum:canposttomygroups', $modcontext);
196 if ($canposttoowngroups) {
197 // This user is in multiple groups, and can post to all of their own groups.
198 // Note: This is not the same as accessallgroups. This option will copy a post to all groups that a
199 // user is a member of.
200 $mform->addElement('checkbox', 'posttomygroups', get_string('posttomygroups', 'forum'));
201 $mform->addHelpButton('posttomygroups', 'posttomygroups', 'forum');
202 $mform->disabledIf('groupinfo', 'posttomygroups', 'checked');
203 }
97babbf7 204
cb28132d
P
205 // Check whether this user can post to all groups.
206 // Posts to the 'All participants' group go to all groups, not to each group in a list.
207 // It makes sense to allow this, even if there currently aren't any groups because there may be in the future.
208 if (forum_user_can_post_discussion($forum, -1, null, $cm, $modcontext)) {
209 // Note: We must reverse in this manner because array_unshift renumbers the array.
210 $groupinfo = array_reverse($groupinfo, true);
211 $groupinfo[-1] = get_string('allparticipants');
212 $groupinfo = array_reverse($groupinfo, true);
213 $groupcount++;
214 }
97babbf7 215
cb28132d
P
216 // Determine whether the user can select a group from the dropdown. The dropdown is available for several reasons.
217 // 1) This is a new post (not an edit), and there are at least two groups to choose from.
218 $canselectgroupfornew = empty($post->edit) && $groupcount > 1;
219
220 // 2) This is editing of an existing post and the user is allowed to movediscussions.
221 // We allow this because the post may have been moved from another forum where groups are not available.
222 // We show this even if no groups are available as groups *may* have been available but now are not.
223 $canselectgroupformove =
224 $groupcount && !empty($post->edit) && has_capability('mod/forum:movediscussions', $modcontext);
225
226 // Important: You can *only* change the group for a top level post. Never any reply.
227 $canselectgroup = empty($post->parent) && ($canselectgroupfornew || $canselectgroupformove);
228
229 if ($canselectgroup) {
230 $mform->addElement('select', 'groupinfo', get_string('group'), $groupinfo);
231 $mform->setDefault('groupinfo', $post->groupid);
232 $mform->setType('groupinfo', PARAM_INT);
233 } else {
234 if (empty($post->groupid)) {
235 $groupname = get_string('allparticipants');
97babbf7 236 } else {
cb28132d 237 $groupname = format_string($groupdata[$post->groupid]->name);
97babbf7 238 }
cb28132d 239 $mform->addElement('static', 'groupinfo', get_string('group'), $groupname);
2ecd2847 240 }
2ecd2847 241 }
608edec5 242
97babbf7
P
243 if (!empty($CFG->forum_enabletimedposts) && !$post->parent &&
244 has_capability('mod/forum:viewhiddentimedposts', $coursecontext)) {
245 $mform->addElement('header', 'displayperiod', get_string('displayperiod', 'forum'));
608edec5 246
97babbf7
P
247 $mform->addElement('date_time_selector', 'timestart', get_string('displaystart', 'forum'),
248 array('optional' => true));
249 $mform->addHelpButton('timestart', 'displaystart', 'forum');
608edec5 250
97babbf7
P
251 $mform->addElement('date_time_selector', 'timeend', get_string('displayend', 'forum'),
252 array('optional' => true));
253 $mform->addHelpButton('timeend', 'displayend', 'forum');
608edec5 254
97babbf7
P
255 } else {
256 $mform->addElement('hidden', 'timestart');
257 $mform->setType('timestart', PARAM_INT);
258 $mform->addElement('hidden', 'timeend');
259 $mform->setType('timeend', PARAM_INT);
260 $mform->setConstants(array('timestart' => 0, 'timeend' => 0));
261 }
608edec5 262
97babbf7
P
263 if (core_tag_tag::is_enabled('mod_forum', 'forum_posts')) {
264 $mform->addElement('header', 'tagshdr', get_string('tags', 'tag'));
d2ba493c 265
97babbf7
P
266 $mform->addElement('tags', 'tags', get_string('tags'),
267 array('itemtype' => 'forum_posts', 'component' => 'mod_forum'));
268 }
d2ba493c
AH
269 }
270
2ecd2847
FM
271 //-------------------------------------------------------------------------------
272 // buttons
273 if (isset($post->edit)) { // hack alert
2aff2c19 274 $submitstring = get_string('savechanges');
2ecd2847 275 } else {
2aff2c19 276 $submitstring = get_string('posttoforum', 'forum');
2ecd2847 277 }
2587313a 278
97babbf7
P
279 // Always register a no submit button so it can be picked up if redirecting to the original post form.
280 $mform->registerNoSubmitButton('advancedadddiscussion');
281
282 // This is an inpage add discussion which requires custom buttons.
cb28132d
P
283 if ($inpagereply) {
284 $mform->addElement('hidden', 'discussionsubscribe');
285 $mform->setType('discussionsubscribe', PARAM_INT);
286 $mform->disable_form_change_checker();
97babbf7 287 $buttonarray = array();
2aff2c19 288 $buttonarray[] = &$mform->createElement('submit', 'submitbutton', $submitstring);
97babbf7
P
289 $buttonarray[] = &$mform->createElement('button', 'cancelbtn',
290 get_string('cancel', 'core'),
2aff2c19 291 // Additional attribs to handle collapsible div.
0d71eee8
P
292 ['data-toggle' => 'collapse', 'data-target' => "#collapseAddForm"]);
293 $buttonarray[] = &$mform->createElement('submit', 'advancedadddiscussion',
cb28132d 294 get_string('advanced'), null, null, ['customclassoverride' => 'btn-link']);
0d71eee8 295
97babbf7
P
296 $mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
297 $mform->closeHeaderBefore('buttonar');
298 } else {
2aff2c19 299 $this->add_action_buttons(true, $submitstring);
97babbf7 300 }
2ecd2847
FM
301
302 $mform->addElement('hidden', 'course');
303 $mform->setType('course', PARAM_INT);
304
305 $mform->addElement('hidden', 'forum');
306 $mform->setType('forum', PARAM_INT);
307
308 $mform->addElement('hidden', 'discussion');
309 $mform->setType('discussion', PARAM_INT);
310
311 $mform->addElement('hidden', 'parent');
312 $mform->setType('parent', PARAM_INT);
313
2ecd2847
FM
314 $mform->addElement('hidden', 'groupid');
315 $mform->setType('groupid', PARAM_INT);
316
317 $mform->addElement('hidden', 'edit');
318 $mform->setType('edit', PARAM_INT);
319
320 $mform->addElement('hidden', 'reply');
321 $mform->setType('reply', PARAM_INT);
322 }
323
324 /**
325 * Form validation
326 *
327 * @param array $data data from the form.
328 * @param array $files files uploaded.
329 * @return array of errors.
330 */
331 function validation($data, $files) {
332 $errors = parent::validation($data, $files);
333 if (($data['timeend']!=0) && ($data['timestart']!=0) && $data['timeend'] <= $data['timestart']) {
334 $errors['timeend'] = get_string('timestartenderror', 'forum');
335 }
336 if (empty($data['message']['text'])) {
337 $errors['message'] = get_string('erroremptymessage', 'forum');
338 }
339 if (empty($data['subject'])) {
340 $errors['subject'] = get_string('erroremptysubject', 'forum');
341 }
342 return $errors;
343 }
344}