MDL-66461 forum: Use correct component for forum strings
[moodle.git] / mod / forum / post.php
CommitLineData
cd4e6b17 1<?php
8f685009
SH
2// This file is part of Moodle - http://moodle.org/
3//
4// Moodle is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// Moodle is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16
17/**
18 * Edit and save a new post to a discussion
19 *
01030f1b 20 * @package mod_forum
8f685009
SH
21 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
2f67a9b3 23 */
501cdbd8 24
cd4e6b17 25require_once('../../config.php');
26require_once('lib.php');
516c5eca 27require_once($CFG->libdir.'/completionlib.php');
cd4e6b17 28
29$reply = optional_param('reply', 0, PARAM_INT);
30$forum = optional_param('forum', 0, PARAM_INT);
31$edit = optional_param('edit', 0, PARAM_INT);
32$delete = optional_param('delete', 0, PARAM_INT);
33$prune = optional_param('prune', 0, PARAM_INT);
34$name = optional_param('name', '', PARAM_CLEAN);
35$confirm = optional_param('confirm', 0, PARAM_INT);
36$groupid = optional_param('groupid', null, PARAM_INT);
9b4f09ba 37$subject = optional_param('subject', '', PARAM_TEXT);
9c3e3a90
P
38
39// Values posted via the inpage reply form.
9b4f09ba 40$prefilledpost = optional_param('post', '', PARAM_TEXT);
f6548dfb 41$prefilledpostformat = optional_param('postformat', FORMAT_MOODLE, PARAM_INT);
56e9b6d6 42$prefilledprivatereply = optional_param('privatereply', false, PARAM_BOOL);
cd4e6b17 43
44$PAGE->set_url('/mod/forum/post.php', array(
707dfd0c
JM
45 'reply' => $reply,
46 'forum' => $forum,
47 'edit' => $edit,
48 'delete' => $delete,
49 'prune' => $prune,
50 'name' => $name,
51 'confirm' => $confirm,
52 'groupid' => $groupid,
53));
54// These page_params will be passed as hidden variables later in the form.
55$pageparams = array('reply' => $reply, 'forum' => $forum, 'edit' => $edit);
cd4e6b17 56
bf0f06b1 57$sitecontext = context_system::instance();
cd4e6b17 58
15dc885a
AN
59$entityfactory = mod_forum\local\container::get_entity_factory();
60$vaultfactory = mod_forum\local\container::get_vault_factory();
61$managerfactory = mod_forum\local\container::get_manager_factory();
62$legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
63$urlfactory = mod_forum\local\container::get_url_factory();
64
65$forumvault = $vaultfactory->get_forum_vault();
66$forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
64f93798 67
15dc885a
AN
68$discussionvault = $vaultfactory->get_discussion_vault();
69$discussiondatamapper = $legacydatamapperfactory->get_discussion_data_mapper();
70
71$postvault = $vaultfactory->get_post_vault();
72$postdatamapper = $legacydatamapperfactory->get_post_data_mapper();
73
74if (!isloggedin() or isguestuser()) {
dcee0b94 75 if (!isloggedin() and !get_local_referer()) {
707dfd0c 76 // No referer+not logged in - probably coming in via email See MDL-9052.
4f0c2d00
PS
77 require_login();
78 }
cd4e6b17 79
15dc885a
AN
80 if (!empty($forum)) {
81 // User is starting a new discussion in a forum.
82 $forumentity = $forumvault->get_from_id($forum);
83 if (empty($forumentity)) {
cd4e6b17 84 print_error('invalidforumid', 'forum');
556963f5 85 }
15dc885a
AN
86 } else if (!empty($reply)) {
87 // User is writing a new reply.
88 $forumentity = $forumvault->get_from_post_id($reply);
89 if (empty($forumentity)) {
cd4e6b17 90 print_error('invalidparentpostid', 'forum');
556963f5 91 }
cd4e6b17 92 }
65bcf17b 93
15dc885a
AN
94 $forum = $forumdatamapper->to_legacy_object($forumentity);
95 $modcontext = $forumentity->get_context();
96 $course = $forumentity->get_course_record();
97 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
98 print_error("invalidcoursemodule");
cd4e6b17 99 }
d3558659 100
184bcf11
SH
101 $PAGE->set_cm($cm, $course, $forum);
102 $PAGE->set_context($modcontext);
cd4e6b17 103 $PAGE->set_title($course->shortname);
104 $PAGE->set_heading($course->fullname);
dcee0b94 105 $referer = get_local_referer(false);
48d38fad 106
cd4e6b17 107 echo $OUTPUT->header();
dd0607b7 108 echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), $referer);
cd4e6b17 109 echo $OUTPUT->footer();
110 exit;
111}
65bcf17b 112
707dfd0c 113require_login(0, false); // Script is useless unless they're logged in.
65bcf17b 114
bc4c7337 115$canreplyprivately = false;
313d8b6e 116
15dc885a
AN
117if (!empty($forum)) {
118 // User is starting a new discussion in a forum.
119 $forumentity = $forumvault->get_from_id($forum);
120 if (empty($forumentity)) {
cd4e6b17 121 print_error('invalidforumid', 'forum');
122 }
15dc885a
AN
123
124 $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
125 $forum = $forumdatamapper->to_legacy_object($forumentity);
126 $course = $forumentity->get_course_record();
127 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
cd4e6b17 128 print_error("invalidcoursemodule");
129 }
2b63df96 130
f67b5c99 131 // Retrieve the contexts.
15dc885a 132 $modcontext = $forumentity->get_context();
bf0f06b1 133 $coursecontext = context_course::instance($course->id);
80602101 134
15dc885a
AN
135 if ($forumentity->is_in_group_mode() && null === $groupid) {
136 $groupid = groups_get_activity_group($cm);
137 }
138
139 if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
4f0c2d00
PS
140 if (!isguestuser()) {
141 if (!is_enrolled($coursecontext)) {
45ff8a80 142 if (enrol_selfenrol_available($course->id)) {
f0202ae9 143 $SESSION->wantsurl = qualified_me();
dcee0b94 144 $SESSION->enrolcancel = get_local_referer(false);
9542b156
MN
145 redirect(new moodle_url('/enrol/index.php', array('id' => $course->id,
146 'returnurl' => '/mod/forum/view.php?f=' . $forum->id)),
147 get_string('youneedtoenrol'));
54ab8769 148 }
4f0c2d00 149 }
3b27b0fe 150 }
4f0c2d00 151 print_error('nopostforum', 'forum');
cd4e6b17 152 }
2b63df96 153
f67b5c99 154 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
15dc885a
AN
155 redirect(
156 $urlfactory->get_course_url_from_forum($forumentity),
157 get_string('activityiscurrentlyhidden'),
158 null,
b0c785e2 159 \core\output\notification::NOTIFY_ERROR
15dc885a 160 );
cd4e6b17 161 }
2b63df96 162
dcee0b94 163 $SESSION->fromurl = get_local_referer(false);
501cdbd8 164
cd4e6b17 165 // Load up the $post variable.
89d35c49 166
39790bd8 167 $post = new stdClass();
cd4e6b17 168 $post->course = $course->id;
169 $post->forum = $forum->id;
707dfd0c 170 $post->discussion = 0; // Ie discussion # not defined yet.
cd4e6b17 171 $post->parent = 0;
9b4f09ba 172 $post->subject = $subject;
cd4e6b17 173 $post->userid = $USER->id;
9b4f09ba 174 $post->message = $prefilledpost;
20e5da7d 175 $post->messageformat = editors_get_preferred_format();
cd4e6b17 176 $post->messagetrust = 0;
15dc885a 177 $post->groupid = $groupid;
501cdbd8 178
46691973
EM
179 // Unsetting this will allow the correct return URL to be calculated later.
180 unset($SESSION->fromdiscussion);
bd4128e9 181
15dc885a
AN
182} else if (!empty($reply)) {
183 // User is writing a new reply.
4cabf99f 184
15dc885a
AN
185 $parententity = $postvault->get_from_id($reply);
186 if (empty($parententity)) {
cd4e6b17 187 print_error('invalidparentpostid', 'forum');
188 }
15dc885a
AN
189
190 $discussionentity = $discussionvault->get_from_id($parententity->get_discussion_id());
191 if (empty($discussionentity)) {
cd4e6b17 192 print_error('notpartofdiscussion', 'forum');
193 }
15dc885a
AN
194
195 $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
196 if (empty($forumentity)) {
cd4e6b17 197 print_error('invalidforumid', 'forum');
198 }
15dc885a
AN
199
200 $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
201 $parent = $postdatamapper->to_legacy_object($parententity);
202 $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
203 $forum = $forumdatamapper->to_legacy_object($forumentity);
204 $course = $forumentity->get_course_record();
205 $modcontext = $forumentity->get_context();
206 $coursecontext = context_course::instance($course->id);
207
208 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
cd4e6b17 209 print_error('invalidcoursemodule');
210 }
ea3caf69 211
707dfd0c 212 // Ensure lang, theme, etc. is set up properly. MDL-6926.
34563b35 213 $PAGE->set_cm($cm, $course, $forum);
cd4e6b17 214
15dc885a 215 if (!$capabilitymanager->can_reply_to_post($USER, $discussionentity, $parententity)) {
6a176a74 216 if (!isguestuser()) {
4f0c2d00 217 if (!is_enrolled($coursecontext)) { // User is a guest here!
f0202ae9 218 $SESSION->wantsurl = qualified_me();
dcee0b94 219 $SESSION->enrolcancel = get_local_referer(false);
9542b156
MN
220 redirect(new moodle_url('/enrol/index.php', array('id' => $course->id,
221 'returnurl' => '/mod/forum/view.php?f=' . $forum->id)),
222 get_string('youneedtoenrol'));
4f0c2d00 223 }
bdb4a87d
P
224
225 // The forum has been locked. Just redirect back to the discussion page.
226 if (forum_discussion_is_locked($forum, $discussion)) {
227 redirect(new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
228 }
6c506ca7 229 }
4f0c2d00 230 print_error('nopostforum', 'forum');
cd4e6b17 231 }
2b63df96 232
707dfd0c 233 // Make sure user can post here.
cd4e6b17 234 if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
707dfd0c 235 $groupmode = $cm->groupmode;
cd4e6b17 236 } else {
237 $groupmode = $course->groupmode;
238 }
239 if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
240 if ($discussion->groupid == -1) {
241 print_error('nopostforum', 'forum');
242 } else {
243 if (!groups_is_member($discussion->groupid)) {
89d35c49 244 print_error('nopostforum', 'forum');
80602101 245 }
02509fe6 246 }
cd4e6b17 247 }
4136c239 248
f67b5c99 249 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
cd4e6b17 250 print_error("activityiscurrentlyhidden");
251 }
02509fe6 252
15dc885a 253 if ($parententity->is_private_reply()) {
bc4c7337
AN
254 print_error('cannotreplytoprivatereply', 'forum');
255 }
256
743d66bf 257 // We always are going to honor the preferred format. We are creating a new post.
f6548dfb 258 $preferredformat = editors_get_preferred_format();
743d66bf
EL
259
260 // Only if there are prefilled contents coming.
261 if (!empty($prefilledpost)) {
262 // If the prefilled post is not HTML and the preferred format is HTML, convert to it.
263 if ($prefilledpostformat != FORMAT_HTML and $preferredformat == FORMAT_HTML) {
264 $prefilledpost = format_text($prefilledpost, $prefilledpostformat, ['context' => $modcontext]);
265 }
f6548dfb
SA
266 }
267
cd4e6b17 268 // Load up the $post variable.
39790bd8 269 $post = new stdClass();
cd4e6b17 270 $post->course = $course->id;
271 $post->forum = $forum->id;
272 $post->discussion = $parent->discussion;
273 $post->parent = $parent->id;
9b4f09ba 274 $post->subject = $subject ? $subject : $parent->subject;
cd4e6b17 275 $post->userid = $USER->id;
bc4c7337 276 $post->parentpostauthor = $parent->userid;
9b4f09ba 277 $post->message = $prefilledpost;
743d66bf 278 $post->messageformat = $preferredformat;
56e9b6d6 279 $post->isprivatereply = $prefilledprivatereply;
15dc885a 280 $canreplyprivately = $capabilitymanager->can_reply_privately_to_post($USER, $parententity);
501cdbd8 281
cd4e6b17 282 $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
4136c239 283
cd4e6b17 284 $strre = get_string('re', 'forum');
285 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
286 $post->subject = $strre.' '.$post->subject;
287 }
501cdbd8 288
46691973 289 // Unsetting this will allow the correct return URL to be calculated later.
cd4e6b17 290 unset($SESSION->fromdiscussion);
0e52f01f 291
15dc885a
AN
292} else if (!empty($edit)) {
293 // User is editing their own post.
b8be40ce 294
15dc885a
AN
295 $postentity = $postvault->get_from_id($edit);
296 if (empty($postentity)) {
cd4e6b17 297 print_error('invalidpostid', 'forum');
298 }
15dc885a
AN
299 if ($postentity->has_parent()) {
300 $parententity = $postvault->get_from_id($postentity->get_parent_id());
301 $parent = $postdatamapper->to_legacy_object($parententity);
cd4e6b17 302 }
2b63df96 303
15dc885a
AN
304 $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
305 if (empty($discussionentity)) {
cd4e6b17 306 print_error('notpartofdiscussion', 'forum');
307 }
15dc885a
AN
308
309 $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
310 if (empty($forumentity)) {
cd4e6b17 311 print_error('invalidforumid', 'forum');
312 }
15dc885a
AN
313
314 $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
315 $post = $postdatamapper->to_legacy_object($postentity);
316 $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
317 $forum = $forumdatamapper->to_legacy_object($forumentity);
318 $course = $forumentity->get_course_record();
319 $modcontext = $forumentity->get_context();
320 $coursecontext = context_course::instance($course->id);
321
cd4e6b17 322 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
323 print_error('invalidcoursemodule');
cd4e6b17 324 }
34563b35
SH
325
326 $PAGE->set_cm($cm, $course, $forum);
54ab8769 327
cd4e6b17 328 if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
329 if (((time() - $post->created) > $CFG->maxeditingtime) and
707dfd0c 330 !has_capability('mod/forum:editanypost', $modcontext)) {
cd4e6b17 331 print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
6e89ca55 332 }
cd4e6b17 333 }
334 if (($post->userid <> $USER->id) and
707dfd0c 335 !has_capability('mod/forum:editanypost', $modcontext)) {
cd4e6b17 336 print_error('cannoteditposts', 'forum');
337 }
501cdbd8 338
cd4e6b17 339 // Load up the $post variable.
340 $post->edit = $edit;
341 $post->course = $course->id;
342 $post->forum = $forum->id;
343 $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
15dc885a 344 if ($postentity->has_parent()) {
bc4c7337
AN
345 $canreplyprivately = forum_user_can_reply_privately($modcontext, $parent);
346 }
501cdbd8 347
cd4e6b17 348 $post = trusttext_pre_edit($post, 'message', $modcontext);
501cdbd8 349
46691973 350 // Unsetting this will allow the correct return URL to be calculated later.
cd4e6b17 351 unset($SESSION->fromdiscussion);
64f93798 352
15dc885a
AN
353} else if (!empty($delete)) {
354 // User is deleting a post.
dcf6d93c 355
15dc885a
AN
356 $postentity = $postvault->get_from_id($delete);
357 if (empty($postentity)) {
cd4e6b17 358 print_error('invalidpostid', 'forum');
359 }
15dc885a
AN
360
361 $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
362 if (empty($discussionentity)) {
cd4e6b17 363 print_error('notpartofdiscussion', 'forum');
364 }
15dc885a
AN
365
366 $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
367 if (empty($forumentity)) {
cd4e6b17 368 print_error('invalidforumid', 'forum');
369 }
15dc885a
AN
370
371 $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
372 $post = $postdatamapper->to_legacy_object($postentity);
373 $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
374 $forum = $forumdatamapper->to_legacy_object($forumentity);
375 $course = $forumentity->get_course_record();
376 $modcontext = $forumentity->get_context();
377 $coursecontext = context_course::instance($course->id);
378
379 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
cd4e6b17 380 print_error('invalidcoursemodule');
381 }
501cdbd8 382
cd4e6b17 383 require_login($course, false, $cm);
2b63df96 384
15dc885a
AN
385 if (!$capabilitymanager->can_delete_post($USER, $discussionentity, $postentity)) {
386 redirect(
387 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
388 get_string('cannotdeletepost', 'forum'),
389 null,
b0c785e2 390 \core\output\notification::NOTIFY_ERROR
15dc885a 391 );
cd4e6b17 392 }
64f93798 393
15dc885a
AN
394 $replycount = $postvault->get_reply_count_for_post_id_in_discussion_id(
395 $USER, $postentity->get_id(), $discussionentity->get_id(), true);
64f93798 396
15dc885a
AN
397 if (!empty($confirm) && confirm_sesskey()) {
398 // User has confirmed the delete.
707dfd0c 399 // Check user capability to delete post.
b855512a 400 $timepassed = time() - $post->created;
cd4e6b17 401 if ($post->totalscore) {
15dc885a
AN
402 redirect(
403 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
404 get_string('couldnotdeleteratings', 'rating'),
405 null,
b0c785e2 406 \core\output\notification::NOTIFY_ERROR
15dc885a 407 );
cd4e6b17 408 } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
15dc885a
AN
409 redirect(
410 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
10b1b829 411 get_string('couldnotdeletereplies', 'forum'),
15dc885a 412 null,
b0c785e2 413 \core\output\notification::NOTIFY_ERROR
15dc885a 414 );
cd4e6b17 415 } else {
15dc885a
AN
416 if (!$postentity->has_parent()) {
417 // Post is a discussion topic as well, so delete discussion.
cd4e6b17 418 if ($forum->type == 'single') {
15dc885a
AN
419 redirect(
420 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
10b1b829 421 get_string('cannotdeletediscussioninsinglediscussion', 'forum'),
15dc885a 422 null,
b0c785e2 423 \core\output\notification::NOTIFY_ERROR
15dc885a 424 );
cd4e6b17 425 }
426 forum_delete_discussion($discussion, false, $course, $cm, $forum);
501cdbd8 427
71595d00
DP
428 $params = array(
429 'objectid' => $discussion->id,
430 'context' => $modcontext,
431 'other' => array(
432 'forumid' => $forum->id,
433 )
434 );
435
436 $event = \mod_forum\event\discussion_deleted::create($params);
437 $event->add_record_snapshot('forum_discussions', $discussion);
438 $event->trigger();
2b63df96 439
9f832722 440 redirect(
15dc885a
AN
441 $urlfactory->get_forum_view_url_from_forum($forumentity),
442 get_string('eventdiscussiondeleted', 'forum'),
9f832722
DW
443 null,
444 \core\output\notification::NOTIFY_SUCCESS
445 );
2b63df96 446
15dc885a
AN
447 } else {
448 $deleted = forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext), $course, $cm, $forum);
449
450 if (!$deleted) {
451 redirect(
452 $urlfactory->get_discussion_view_url_from_post($postentity),
453 get_string('errorwhiledelete', 'forum'),
454 null,
b0c785e2 455 \core\output\notification::NOTIFY_ERROR
15dc885a
AN
456 );
457 }
69d79bc3 458
cd4e6b17 459 if ($forum->type == 'single') {
15dc885a
AN
460 // Single discussion forums are an exception.
461 // We show the forum itself since it only has one discussion thread.
462 $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
501cdbd8 463 } else {
15dc885a 464 $discussionurl = $urlfactory->get_discussion_view_url_from_discussion($discussionentity);
501cdbd8 465 }
501cdbd8 466
9f832722
DW
467 redirect(
468 forum_go_back_to($discussionurl),
15dc885a 469 get_string('eventpostdeleted', 'forum'),
9f832722
DW
470 null,
471 \core\output\notification::NOTIFY_SUCCESS
472 );
cd4e6b17 473 }
474 }
501cdbd8 475
501cdbd8 476
15dc885a
AN
477 } else {
478 // User just asked to delete something.
cd4e6b17 479 forum_set_return();
b4c07395
RW
480 $PAGE->navbar->add(get_string('delete', 'forum'));
481 $PAGE->set_title($course->shortname);
482 $PAGE->set_heading($course->fullname);
09d40d65 483
cd4e6b17 484 if ($replycount) {
485 if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
15dc885a
AN
486 redirect(
487 forum_go_back_to($urlfactory->get_view_post_url_from_post($postentity)),
10b1b829 488 get_string('couldnotdeletereplies', 'forum'),
15dc885a 489 null,
b0c785e2 490 \core\output\notification::NOTIFY_ERROR
15dc885a 491 );
b82faacd 492 }
15dc885a 493
cd4e6b17 494 echo $OUTPUT->header();
66e2b9f8 495 echo $OUTPUT->heading(format_string($forum->name), 2);
707dfd0c
JM
496 echo $OUTPUT->confirm(get_string("deletesureplural", "forum", $replycount + 1),
497 "post.php?delete=$delete&confirm=$delete",
498 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
8f0cd6ef 499
313d8b6e 500 $postentities = [$postentity];
cd4e6b17 501 if (empty($post->edit)) {
313d8b6e 502 $postvault = $vaultfactory->get_post_vault();
bc4c7337
AN
503 $replies = $postvault->get_replies_to_post(
504 $USER,
505 $postentity,
15dc885a
AN
506 // Note: All replies are fetched here as the user has deleteanypost.
507 true,
bc4c7337
AN
508 'created ASC'
509 );
313d8b6e 510 $postentities = array_merge($postentities, $replies);
cd4e6b17 511 }
313d8b6e
RW
512
513 $rendererfactory = mod_forum\local\container::get_renderer_factory();
514 $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_NESTED, true);
15dc885a 515 echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
cd4e6b17 516 } else {
517 echo $OUTPUT->header();
66e2b9f8 518 echo $OUTPUT->heading(format_string($forum->name), 2);
cd4e6b17 519 echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
707dfd0c
JM
520 "post.php?delete=$delete&confirm=$delete",
521 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
313d8b6e
RW
522
523 $rendererfactory = mod_forum\local\container::get_renderer_factory();
524 $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
525 echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
501cdbd8 526 }
501cdbd8 527
cd4e6b17 528 }
529 echo $OUTPUT->footer();
530 die;
501cdbd8 531
15dc885a
AN
532} else if (!empty($prune)) {
533 // Pruning.
8f0cd6ef 534
15dc885a
AN
535 $postentity = $postvault->get_from_id($prune);
536 if (empty($postentity)) {
cd4e6b17 537 print_error('invalidpostid', 'forum');
538 }
15dc885a
AN
539
540 $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
541 if (empty($discussionentity)) {
cd4e6b17 542 print_error('notpartofdiscussion', 'forum');
543 }
15dc885a
AN
544
545 $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
546 if (empty($forumentity)) {
cd4e6b17 547 print_error('invalidforumid', 'forum');
548 }
15dc885a
AN
549
550 $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
551 $post = $postdatamapper->to_legacy_object($postentity);
552 $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
553 $forum = $forumdatamapper->to_legacy_object($forumentity);
554 $course = $forumentity->get_course_record();
555 $modcontext = $forumentity->get_context();
556 $coursecontext = context_course::instance($course->id);
557
558 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
cd4e6b17 559 print_error('invalidcoursemodule');
cd4e6b17 560 }
15dc885a
AN
561
562 if (!$postentity->has_parent()) {
563 redirect(
564 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
565 get_string('alreadyfirstpost', 'forum'),
566 null,
567 \core\output\notification::NOTIFY_ERROR
568 );
569 }
570 if (!$capabilitymanager->can_split_post($USER, $discussionentity, $postentity)) {
571 redirect(
572 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
573 get_string('cannotsplit', 'forum'),
574 null,
575 \core\output\notification::NOTIFY_ERROR
576 );
cd4e6b17 577 }
8f0cd6ef 578
069d0f17
MM
579 $PAGE->set_cm($cm);
580 $PAGE->set_context($modcontext);
581
582 $prunemform = new mod_forum_prune_form(null, array('prune' => $prune, 'confirm' => $prune));
8f0cd6ef 583
069d0f17 584 if ($prunemform->is_cancelled()) {
15dc885a 585 redirect(forum_go_back_to($urlfactory->get_discussion_view_url_from_discussion($discussionentity)));
069d0f17
MM
586 } else if ($fromform = $prunemform->get_data()) {
587 // User submits the data.
39790bd8 588 $newdiscussion = new stdClass();
cd4e6b17 589 $newdiscussion->course = $discussion->course;
590 $newdiscussion->forum = $discussion->forum;
591 $newdiscussion->name = $name;
592 $newdiscussion->firstpost = $post->id;
593 $newdiscussion->userid = $discussion->userid;
594 $newdiscussion->groupid = $discussion->groupid;
595 $newdiscussion->assessed = $discussion->assessed;
596 $newdiscussion->usermodified = $post->userid;
597 $newdiscussion->timestart = $discussion->timestart;
598 $newdiscussion->timeend = $discussion->timeend;
8f0cd6ef 599
cd4e6b17 600 $newid = $DB->insert_record('forum_discussions', $newdiscussion);
d078ee9b 601
39790bd8 602 $newpost = new stdClass();
cd4e6b17 603 $newpost->id = $post->id;
604 $newpost->parent = 0;
605 $newpost->subject = $name;
d078ee9b 606
cd4e6b17 607 $DB->update_record("forum_posts", $newpost);
15dc885a 608 $postentity = $postvault->get_from_id($postentity->get_id());
8f0cd6ef 609
cd4e6b17 610 forum_change_discussionid($post->id, $newid);
cf84431b 611
069d0f17 612 // Update last post in each discussion.
cd4e6b17 613 forum_discussion_update_last_post($discussion->id);
614 forum_discussion_update_last_post($newid);
4e6a816a 615
2d3f692b 616 // Fire events to reflect the split..
97802bea
DP
617 $params = array(
618 'context' => $modcontext,
619 'objectid' => $discussion->id,
620 'other' => array(
621 'forumid' => $forum->id,
622 )
623 );
624 $event = \mod_forum\event\discussion_updated::create($params);
625 $event->trigger();
626
2d3f692b
DP
627 $params = array(
628 'context' => $modcontext,
629 'objectid' => $newid,
630 'other' => array(
631 'forumid' => $forum->id,
632 )
633 );
634 $event = \mod_forum\event\discussion_created::create($params);
635 $event->trigger();
636
637 $params = array(
638 'context' => $modcontext,
639 'objectid' => $post->id,
640 'other' => array(
641 'discussionid' => $newid,
642 'forumid' => $forum->id,
643 'forumtype' => $forum->type,
644 )
645 );
646 $event = \mod_forum\event\post_updated::create($params);
647 $event->add_record_snapshot('forum_discussions', $discussion);
648 $event->trigger();
cf84431b 649
9f832722 650 redirect(
15dc885a
AN
651 forum_go_back_to($urlfactory->get_discussion_view_url_from_post($postentity)),
652 get_string('discussionsplit', 'forum'),
9f832722
DW
653 null,
654 \core\output\notification::NOTIFY_SUCCESS
655 );
069d0f17
MM
656 } else {
657 // Display the prune form.
cd4e6b17 658 $course = $DB->get_record('course', array('id' => $forum->course));
707dfd0c
JM
659 $subjectstr = format_string($post->subject, true);
660 $PAGE->navbar->add($subjectstr, new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
cd4e6b17 661 $PAGE->navbar->add(get_string("prune", "forum"));
662 $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
b4c07395 663 $PAGE->set_heading($course->fullname);
cd4e6b17 664 echo $OUTPUT->header();
66e2b9f8
AD
665 echo $OUTPUT->heading(format_string($forum->name), 2);
666 echo $OUTPUT->heading(get_string('pruneheading', 'forum'), 3);
8f0cd6ef 667
069d0f17 668 $prunemform->display();
501cdbd8 669
313d8b6e
RW
670 $postentity = $entityfactory->get_post_from_stdclass($post);
671 $discussionentity = $entityfactory->get_discussion_from_stdclass($discussion);
672 $forumentity = $entityfactory->get_forum_from_stdclass($forum, $modcontext, $cm, $course);
673 $rendererfactory = mod_forum\local\container::get_renderer_factory();
674 $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
675 echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
501cdbd8 676 }
069d0f17 677
cd4e6b17 678 echo $OUTPUT->footer();
679 die;
680} else {
681 print_error('unknowaction');
501cdbd8 682
cd4e6b17 683}
684
707dfd0c 685// From now on user must be logged on properly.
12fab708 686
cd4e6b17 687require_login($course, false, $cm);
12fab708 688
cd4e6b17 689if (isguestuser()) {
707dfd0c 690 // Just in case.
cd4e6b17 691 print_error('noguest');
692}
693
f5ad424b 694$thresholdwarning = forum_check_throttling($forum, $cm);
bc4c7337
AN
695$mformpost = new mod_forum_post_form('post.php', [
696 'course' => $course,
697 'cm' => $cm,
698 'coursecontext' => $coursecontext,
699 'modcontext' => $modcontext,
700 'forum' => $forum,
701 'post' => $post,
702 'subscribe' => \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm),
703 'thresholdwarning' => $thresholdwarning,
704 'edit' => $edit,
705 'canreplyprivately' => $canreplyprivately,
706 ], 'post', '', array('id' => 'mformforum'));
cd4e6b17 707
708$draftitemid = file_get_submitted_draft_itemid('attachments');
707dfd0c
JM
709$postid = empty($post->id) ? null : $post->id;
710$attachoptions = mod_forum_post_form::attachment_options($forum);
711file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', $postid, $attachoptions);
cd4e6b17 712
707dfd0c 713// Load data into form NOW!
cd4e6b17 714
707dfd0c 715if ($USER->id != $post->userid) { // Not the original author, so add a message to the end.
fffd3703 716 $data = new stdClass();
6cab66bb 717 $data->date = userdate($post->created);
cd4e6b17 718 if ($post->messageformat == FORMAT_HTML) {
719 $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
707dfd0c 720 fullname($USER).'</a>';
dd4502af 721 $post->message .= '<p><span class="edited">('.get_string('editedby', 'forum', $data).')</span></p>';
cd4e6b17 722 } else {
723 $data->name = fullname($USER);
724 $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
12fab708 725 }
fffd3703 726 unset($data);
cd4e6b17 727}
c58efbf5 728
0da2ae21 729$formheading = '';
cd4e6b17 730if (!empty($parent)) {
731 $heading = get_string("yourreply", "forum");
0da2ae21 732 $formheading = get_string('reply', 'forum');
cd4e6b17 733} else {
734 if ($forum->type == 'qanda') {
735 $heading = get_string('yournewquestion', 'forum');
736 } else {
737 $heading = get_string('yournewtopic', 'forum');
738 }
739}
740
5e95223e 741$postid = empty($post->id) ? null : $post->id;
707dfd0c
JM
742$draftideditor = file_get_submitted_draft_itemid('message');
743$editoropts = mod_forum_post_form::editor_options($modcontext, $postid);
744$currenttext = file_prepare_draft_area($draftideditor, $modcontext->id, 'mod_forum', 'post', $postid, $editoropts, $post->message);
cb28132d
P
745$discussionid = isset($discussion) ? $discussion->id : null;
746$discussionsubscribe = \mod_forum\subscriptions::get_user_default_subscription($forum, $coursecontext, $cm, $discussionid);
be773fd6 747
707dfd0c
JM
748$mformpost->set_data(
749 array(
750 'attachments' => $draftitemid,
751 'general' => $heading,
752 'subject' => $post->subject,
753 'message' => array(
754 'text' => $currenttext,
b6c3b130
EL
755 'format' => !isset($post->messageformat) || !is_numeric($post->messageformat) ?
756 editors_get_preferred_format() : $post->messageformat,
707dfd0c
JM
757 'itemid' => $draftideditor
758 ),
759 'discussionsubscribe' => $discussionsubscribe,
760 'mailnow' => !empty($post->mailnow),
761 'userid' => $post->userid,
762 'parent' => $post->parent,
763 'discussion' => $post->discussion,
56e9b6d6 764 'course' => $course->id,
d7ddc44f 765 'isprivatereply' => $post->isprivatereply ?? false
707dfd0c
JM
766 ) +
767
768 $pageparams +
769
770 (isset($post->format) ? array('format' => $post->format) : array()) +
771
772 (isset($discussion->timestart) ? array('timestart' => $discussion->timestart) : array()) +
773
774 (isset($discussion->timeend) ? array('timeend' => $discussion->timeend) : array()) +
775
776 (isset($discussion->pinned) ? array('pinned' => $discussion->pinned) : array()) +
777
778 (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) +
779
780 (isset($discussion->id) ? array('discussion' => $discussion->id) : array())
781);
782
9c3e3a90
P
783// If we are being redirected via a no_submit_button press OR if the message is being prefilled.
784// then set the initial 'dirty' state.
785// - A prefilled post will exist when being redirected from the inpage reply form.
786// - A no_submit_button press occurs when being redirected from the inpage add new discussion post form.
787$dirty = $prefilledpost ? true : false;
788if ($mformpost->no_submit_button_pressed()) {
789 $data = $mformpost->get_submitted_data();
790
791 // If a no submit button has been pressed but the default values haven't been then reset the form change.
e7bf69e0 792 if (!$dirty && isset($data->message['text']) && !empty(trim($data->message['text']))) {
9c3e3a90
P
793 $dirty = true;
794 }
795
e7bf69e0 796 if (!$dirty && isset($data->message['message']) && !empty(trim($data->message['message']))) {
9c3e3a90
P
797 $dirty = true;
798 }
799}
800$mformpost->set_initial_dirty_state($dirty);
801
707dfd0c 802if ($mformpost->is_cancelled()) {
15dc885a
AN
803 if (!isset($discussion->id) || $forum->type === 'single') {
804 // Single forums don't have a discussion page.
805 redirect($urlfactory->get_forum_view_url_from_forum($forumentity));
f3206dfc 806 } else {
15dc885a 807 redirect($urlfactory->get_discussion_view_url_from_discussion($discussionentity));
f3206dfc 808 }
04cfb5c1 809} else if ($mformpost->is_submitted() && !$mformpost->no_submit_button_pressed() && $fromform = $mformpost->get_data()) {
cd4e6b17 810
811 if (empty($SESSION->fromurl)) {
15dc885a 812 $errordestination = $urlfactory->get_forum_view_url_from_forum($forumentity);
cd4e6b17 813 } else {
814 $errordestination = $SESSION->fromurl;
30a9aff5 815 }
04cfb5c1 816
cd4e6b17 817 $fromform->itemid = $fromform->message['itemid'];
818 $fromform->messageformat = $fromform->message['format'];
819 $fromform->message = $fromform->message['text'];
820 // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
821 $fromform->messagetrust = trusttext_trusted($modcontext);
4cabf99f 822
418831e0
MH
823 // Clean message text.
824 $fromform = trusttext_pre_edit($fromform, 'message', $modcontext);
825
15dc885a
AN
826 if ($fromform->edit) {
827 // Updating a post.
cd4e6b17 828 unset($fromform->groupid);
829 $fromform->id = $fromform->edit;
830 $message = '';
2b63df96 831
15dc885a
AN
832 if (!$capabilitymanager->can_edit_post($USER, $discussionentity, $postentity)) {
833 redirect(
834 $urlfactory->get_view_post_url_from_post($postentity),
835 get_string('cannotupdatepost', 'forum'),
836 null,
837 \core\output\notification::ERROR
838 );
12fab708 839 }
12fab708 840
15dc885a
AN
841 if (isset($fromform->groupinfo) && $capabilitymanager->can_move_discussions($USER)) {
842 // If the user has access to all groups and they are changing the group, then update the post.
a0243017
DM
843 if (empty($fromform->groupinfo)) {
844 $fromform->groupinfo = -1;
845 }
344e1263 846
15dc885a
AN
847 if (!$capabilitymanager->can_create_discussions($USER, $fromform->groupinfo)) {
848 redirect(
849 $urlfactory->get_view_post_url_from_post($postentity),
850 get_string('cannotupdatepost', 'forum'),
851 null,
852 \core\output\notification::ERROR
853 );
344e1263
AN
854 }
855
15dc885a
AN
856 if ($discussionentity->get_group_id() != $fromform->groupinfo) {
857 $DB->set_field('forum_discussions', 'groupid', $fromform->groupinfo, array('firstpost' => $fromform->id));
858 }
2edcf21a 859 }
15dc885a 860
5f219cf1 861 // When editing first post/discussion.
70ed77c5 862 if (!$postentity->has_parent()) {
15dc885a 863 if ($capabilitymanager->can_pin_discussions($USER)) {
5f219cf1
BK
864 // Can change pinned if we have capability.
865 $fromform->pinned = !empty($fromform->pinned) ? FORUM_DISCUSSION_PINNED : FORUM_DISCUSSION_UNPINNED;
866 } else {
867 // We don't have the capability to change so keep to previous value.
868 unset($fromform->pinned);
869 }
870 }
15dc885a 871 $updatepost = $fromform;
cd4e6b17 872 $updatepost->forum = $forum->id;
707dfd0c 873 if (!forum_update_post($updatepost, $mformpost)) {
cd4e6b17 874 print_error("couldnotupdate", "forum", $errordestination);
12fab708 875 }
12fab708 876
15dc885a 877 if ('single' == $forumentity->get_type() && !$postentity->has_parent()) {
707dfd0c 878 // Updating first post of single discussion type -> updating forum intro.
cd4e6b17 879 $forum->intro = $updatepost->message;
880 $forum->timemodified = time();
881 $DB->update_record("forum", $forum);
882 }
12fab708 883
15dc885a 884 if ($USER->id === $postentity->get_author_id()) {
2e48384d 885 $message .= get_string("postupdated", "forum");
c2c071a2 886 } else {
15dc885a 887 $realuser = \core_user::get_user($postentity->get_author_id());
2e48384d 888 $message .= get_string("editedpostupdated", "forum", fullname($realuser));
c2c071a2 889 }
12fab708 890
d954b54f 891 $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
15dc885a
AN
892 if ('single' == $forumentity->get_type()) {
893 // Single discussion forums are an exception.
894 // We show the forum itself since it only has one discussion thread.
895 $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
2b63df96 896 } else {
15dc885a 897 $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
2b63df96 898 }
2d3f692b
DP
899
900 $params = array(
901 'context' => $modcontext,
902 'objectid' => $fromform->id,
903 'other' => array(
904 'discussionid' => $discussion->id,
905 'forumid' => $forum->id,
906 'forumtype' => $forum->type,
907 )
908 );
909
15dc885a
AN
910 if ($USER->id !== $postentity->get_author_id()) {
911 $params['relateduserid'] = $postentity->get_author_id();
2d3f692b
DP
912 }
913
914 $event = \mod_forum\event\post_updated::create($params);
2d3f692b
DP
915 $event->add_record_snapshot('forum_discussions', $discussion);
916 $event->trigger();
2b63df96 917
d954b54f 918 redirect(
707dfd0c
JM
919 forum_go_back_to($discussionurl),
920 $message . $subscribemessage,
921 null,
922 \core\output\notification::NOTIFY_SUCCESS
923 );
65bcf17b 924
15dc885a
AN
925 } else if ($fromform->discussion) {
926 // Adding a new post to an existing discussion
34e29871
MN
927 // Before we add this we must check that the user will not exceed the blocking threshold.
928 forum_check_blocking_threshold($thresholdwarning);
929
cd4e6b17 930 unset($fromform->groupid);
931 $message = '';
932 $addpost = $fromform;
707dfd0c
JM
933 $addpost->forum = $forum->id;
934 if ($fromform->id = forum_add_new_post($addpost, $mformpost)) {
15dc885a 935 $postentity = $postvault->get_from_id($fromform->id);
3e95e09b 936 $fromform->deleted = 0;
d954b54f 937 $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
cd4e6b17 938
939 if (!empty($fromform->mailnow)) {
940 $message .= get_string("postmailnow", "forum");
cd4e6b17 941 } else {
942 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
943 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
944 }
945
2b63df96 946 if ($forum->type == 'single') {
15dc885a
AN
947 // Single discussion forums are an exception.
948 // We show the forum itself since it only has one discussion thread.
949 $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
2b63df96 950 } else {
15dc885a 951 $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
cd4e6b17 952 }
2d3f692b
DP
953
954 $params = array(
955 'context' => $modcontext,
956 'objectid' => $fromform->id,
957 'other' => array(
958 'discussionid' => $discussion->id,
959 'forumid' => $forum->id,
960 'forumtype' => $forum->type,
961 )
962 );
963 $event = \mod_forum\event\post_created::create($params);
964 $event->add_record_snapshot('forum_posts', $fromform);
965 $event->add_record_snapshot('forum_discussions', $discussion);
966 $event->trigger();
cd4e6b17 967
707dfd0c
JM
968 // Update completion state.
969 $completion = new completion_info($course);
970 if ($completion->is_enabled($cm) &&
cd4e6b17 971 ($forum->completionreplies || $forum->completionposts)) {
707dfd0c 972 $completion->update_state($cm, COMPLETION_COMPLETE);
2b63df96 973 }
2b63df96 974
d954b54f 975 redirect(
707dfd0c
JM
976 forum_go_back_to($discussionurl),
977 $message . $subscribemessage,
978 null,
979 \core\output\notification::NOTIFY_SUCCESS
980 );
2b63df96 981
cd4e6b17 982 } else {
983 print_error("couldnotadd", "forum", $errordestination);
984 }
985 exit;
2b63df96 986
15dc885a
AN
987 } else {
988 // Adding a new discussion.
344e1263 989 // The location to redirect to after successfully posting.
4626c848 990 $redirectto = new moodle_url('/mod/forum/view.php', array('f' => $fromform->forum));
344e1263 991
cd4e6b17 992 $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
6606c00f 993
cd4e6b17 994 $discussion = $fromform;
2587313a 995 $discussion->name = $fromform->subject;
2893812e 996 $discussion->timelocked = 0;
2b63df96 997
6606c00f 998 $newstopic = false;
cd4e6b17 999 if ($forum->type == 'news' && !$fromform->parent) {
1000 $newstopic = true;
1001 }
5f219cf1 1002
15dc885a 1003 if (!empty($fromform->pinned) && $capabilitymanager->can_pin_discussions($USER)) {
5f219cf1 1004 $discussion->pinned = FORUM_DISCUSSION_PINNED;
70ed77c5
MG
1005 } else {
1006 $discussion->pinned = FORUM_DISCUSSION_UNPINNED;
5f219cf1 1007 }
2b63df96 1008
2587313a
DC
1009 $allowedgroups = array();
1010 $groupstopostto = array();
2b63df96 1011
2587313a
DC
1012 // If we are posting a copy to all groups the user has access to.
1013 if (isset($fromform->posttomygroups)) {
344e1263 1014 // Post to each of my groups.
eabbc98c 1015 require_capability('mod/forum:canposttomygroups', $modcontext);
344e1263
AN
1016
1017 // Fetch all of this user's groups.
1018 // Note: all groups are returned when in visible groups mode so we must manually filter.
2587313a 1019 $allowedgroups = groups_get_activity_allowed_groups($cm);
344e1263 1020 foreach ($allowedgroups as $groupid => $group) {
15dc885a 1021 if ($capabilitymanager->can_create_discussions($USER, $groupid)) {
344e1263
AN
1022 $groupstopostto[] = $groupid;
1023 }
89d35c49 1024 }
344e1263
AN
1025 } else if (isset($fromform->groupinfo)) {
1026 // Use the value provided in the dropdown group selection.
1027 $groupstopostto[] = $fromform->groupinfo;
1028 $redirectto->param('group', $fromform->groupinfo);
1029 } else if (isset($fromform->groupid) && !empty($fromform->groupid)) {
1030 // Use the value provided in the hidden form element instead.
1031 $groupstopostto[] = $fromform->groupid;
1032 $redirectto->param('group', $fromform->groupid);
1033 } else {
1034 // Use the value for all participants instead.
1035 $groupstopostto[] = -1;
2587313a 1036 }
89d35c49 1037
2587313a
DC
1038 // Before we post this we must check that the user will not exceed the blocking threshold.
1039 forum_check_blocking_threshold($thresholdwarning);
2b63df96 1040
2587313a 1041 foreach ($groupstopostto as $group) {
15dc885a 1042 if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
2587313a 1043 print_error('cannotcreatediscussion', 'forum');
2b63df96 1044 }
1045
2587313a
DC
1046 $discussion->groupid = $group;
1047 $message = '';
707dfd0c 1048 if ($discussion->id = forum_add_discussion($discussion, $mformpost)) {
501cdbd8 1049
2587313a
DC
1050 $params = array(
1051 'context' => $modcontext,
1052 'objectid' => $discussion->id,
1053 'other' => array(
1054 'forumid' => $forum->id,
1055 )
1056 );
1057 $event = \mod_forum\event\discussion_created::create($params);
1058 $event->add_record_snapshot('forum_discussions', $discussion);
1059 $event->trigger();
1060
2587313a
DC
1061 if ($fromform->mailnow) {
1062 $message .= get_string("postmailnow", "forum");
2587313a
DC
1063 } else {
1064 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
1065 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
1066 }
1067
d954b54f 1068 $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
2587313a
DC
1069 } else {
1070 print_error("couldnotadd", "forum", $errordestination);
1071 }
501cdbd8 1072 }
501cdbd8 1073
2587313a
DC
1074 // Update completion status.
1075 $completion = new completion_info($course);
1076 if ($completion->is_enabled($cm) &&
707dfd0c 1077 ($forum->completiondiscussions || $forum->completionposts)) {
2587313a
DC
1078 $completion->update_state($cm, COMPLETION_COMPLETE);
1079 }
1080
344e1263 1081 // Redirect back to the discussion.
d954b54f 1082 redirect(
707dfd0c
JM
1083 forum_go_back_to($redirectto->out()),
1084 $message . $subscribemessage,
1085 null,
1086 \core\output\notification::NOTIFY_SUCCESS
1087 );
cef1ce6a 1088 }
cd4e6b17 1089}
8dec2253 1090
15ca5e5e 1091
15dc885a
AN
1092// This section is only shown after all checks are in place, and the forumentity and any relevant discussion and post
1093// entity are available.
65bcf17b 1094
15dc885a
AN
1095if (!empty($discussionentity)) {
1096 $titlesubject = format_string($discussionentity->get_name(), true);
1097} else if ('news' == $forumentity->get_type()) {
1098 $titlesubject = get_string("addanewtopic", "forum");
cd4e6b17 1099} else {
15dc885a 1100 $titlesubject = get_string("addanewdiscussion", "forum");
cd4e6b17 1101}
1102
1103if (empty($post->edit)) {
1104 $post->edit = '';
1105}
1106
1107if (empty($discussion->name)) {
1108 if (empty($discussion)) {
39790bd8 1109 $discussion = new stdClass();
098d27d4 1110 }
cd4e6b17 1111 $discussion->name = $forum->name;
1112}
15dc885a
AN
1113
1114$strdiscussionname = '';
1115if ('single' == $forumentity->get_type()) {
cd4e6b17 1116 // There is only one discussion thread for this forum type. We should
1117 // not show the discussion name (same as forum name in this case) in
1118 // the breadcrumbs.
1119 $strdiscussionname = '';
15dc885a 1120} else if (!empty($discussionentity)) {
cd4e6b17 1121 // Show the discussion name in the breadcrumbs.
15dc885a 1122 $strdiscussionname = format_string($discussionentity->get_name()) . ': ';
cd4e6b17 1123}
1124
707dfd0c 1125$forcefocus = empty($reply) ? null : 'message';
cd4e6b17 1126
1127if (!empty($discussion->id)) {
15dc885a 1128 $PAGE->navbar->add($titlesubject, $urlfactory->get_discussion_view_url_from_discussion($discussionentity));
cd4e6b17 1129}
1130
1131if ($post->parent) {
b4c07395
RW
1132 $PAGE->navbar->add(get_string('reply', 'forum'));
1133}
1134
1135if ($edit) {
1136 $PAGE->navbar->add(get_string('edit', 'forum'));
cd4e6b17 1137}
1138
15dc885a 1139$PAGE->set_title("{$course->shortname}: {$strdiscussionname}{$titlesubject}");
cd4e6b17 1140$PAGE->set_heading($course->fullname);
cd4e6b17 1141
1142echo $OUTPUT->header();
66e2b9f8 1143echo $OUTPUT->heading(format_string($forum->name), 2);
098d27d4 1144
707dfd0c 1145// Checkup.
15dc885a 1146if (!empty($parententity) && !$capabilitymanager->can_view_post($USER, $discussionentity, $parententity)) {
cd4e6b17 1147 print_error('cannotreply', 'forum');
1148}
15dc885a
AN
1149
1150if (empty($parententity) && empty($edit) && !$capabilitymanager->can_create_discussions($USER, $groupid)) {
cd4e6b17 1151 print_error('cannotcreatediscussion', 'forum');
1152}
1153
15dc885a
AN
1154if (!empty($discussionentity) && 'qanda' == $forumentity->get_type()) {
1155 $displaywarning = $capabilitymanager->must_post_before_viewing_discussion($USER, $discussionentity);
1156 $displaywarning = $displaywarning && !forum_user_has_posted($forumentity->get_id(), $discussionentity->get_id(), $USER->id);
1157 if ($displaywarning) {
1158 echo $OUTPUT->notification(get_string('qandanotify', 'forum'));
1159 }
cd4e6b17 1160}
1161
eaa8f5ad
MN
1162// If there is a warning message and we are not editing a post we need to handle the warning.
1163if (!empty($thresholdwarning) && !$edit) {
1164 // Here we want to throw an exception if they are no longer allowed to post.
34e29871 1165 forum_check_blocking_threshold($thresholdwarning);
f5ad424b 1166}
cd4e6b17 1167
15dc885a
AN
1168if (!empty($parententity)) {
1169 $postentities = [$parententity];
313d8b6e 1170
cd4e6b17 1171 if (empty($post->edit)) {
15dc885a 1172 if ('qanda' != $forumentity->get_type() || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
bc4c7337
AN
1173 $replies = $postvault->get_replies_to_post(
1174 $USER,
15dc885a 1175 $parententity,
bc4c7337
AN
1176 $capabilitymanager->can_view_any_private_reply($USER),
1177 'created ASC'
1178 );
313d8b6e 1179 $postentities = array_merge($postentities, $replies);
21da9db8 1180 }
501cdbd8 1181 }
313d8b6e
RW
1182
1183 $rendererfactory = mod_forum\local\container::get_renderer_factory();
1184 $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_THREADED, true);
15dc885a 1185 echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
cd4e6b17 1186} else {
1187 if (!empty($forum->intro)) {
1188 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
1189 }
1190}
501cdbd8 1191
f3bf0ed5
JM
1192// Call print disclosure for enabled plagiarism plugins.
1193if (!empty($CFG->enableplagiarism)) {
1194 require_once($CFG->libdir.'/plagiarismlib.php');
1195 echo plagiarism_print_disclosure($cm->id);
1196}
1197
0da2ae21
FM
1198if (!empty($formheading)) {
1199 echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide'));
1200}
d2ba493c 1201
15dc885a
AN
1202if (!empty($postentity)) {
1203 $data = (object) [
1204 'tags' => core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $postentity->get_id())
1205 ];
707dfd0c 1206 $mformpost->set_data($data);
d2ba493c
AH
1207}
1208
707dfd0c 1209$mformpost->display();
501cdbd8 1210
cd4e6b17 1211echo $OUTPUT->footer();