MDL-65032 mod_forum: Updates based on Jun's feedback
[moodle.git] / mod / forum / post.php
1 <?php
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/>.
17 /**
18  * Edit and save a new post to a discussion
19  *
20  * @package   mod_forum
21  * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
22  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23  */
25 require_once('../../config.php');
26 require_once('lib.php');
27 require_once($CFG->libdir.'/completionlib.php');
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);
37 $subject = optional_param('subject', '', PARAM_TEXT);
38 $prefilledpost = optional_param('post', '', PARAM_TEXT);
40 $PAGE->set_url('/mod/forum/post.php', array(
41     'reply' => $reply,
42     'forum' => $forum,
43     'edit'  => $edit,
44     'delete' => $delete,
45     'prune' => $prune,
46     'name'  => $name,
47     'confirm' => $confirm,
48     'groupid' => $groupid,
49 ));
50 // These page_params will be passed as hidden variables later in the form.
51 $pageparams = array('reply' => $reply, 'forum' => $forum, 'edit' => $edit);
53 $sitecontext = context_system::instance();
55 $entityfactory = mod_forum\local\container::get_entity_factory();
56 $vaultfactory = mod_forum\local\container::get_vault_factory();
57 $managerfactory = mod_forum\local\container::get_manager_factory();
58 $legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
59 $urlfactory = mod_forum\local\container::get_url_factory();
61 $forumvault = $vaultfactory->get_forum_vault();
62 $forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
64 $discussionvault = $vaultfactory->get_discussion_vault();
65 $discussiondatamapper = $legacydatamapperfactory->get_discussion_data_mapper();
67 $postvault = $vaultfactory->get_post_vault();
68 $postdatamapper = $legacydatamapperfactory->get_post_data_mapper();
70 if (!isloggedin() or isguestuser()) {
71     if (!isloggedin() and !get_local_referer()) {
72         // No referer+not logged in - probably coming in via email  See MDL-9052.
73         require_login();
74     }
76     if (!empty($forum)) {
77         // User is starting a new discussion in a forum.
78         $forumentity = $forumvault->get_from_id($forum);
79         if (empty($forumentity)) {
80             print_error('invalidforumid', 'forum');
81         }
82     } else if (!empty($reply)) {
83         // User is writing a new reply.
84         $forumentity = $forumvault->get_from_post_id($reply);
85         if (empty($forumentity)) {
86             print_error('invalidparentpostid', 'forum');
87         }
88     }
90     $forum = $forumdatamapper->to_legacy_object($forumentity);
91     $modcontext = $forumentity->get_context();
92     $course = $forumentity->get_course_record();
93     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
94         print_error("invalidcoursemodule");
95     }
97     $PAGE->set_cm($cm, $course, $forum);
98     $PAGE->set_context($modcontext);
99     $PAGE->set_title($course->shortname);
100     $PAGE->set_heading($course->fullname);
101     $referer = get_local_referer(false);
103     echo $OUTPUT->header();
104     echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), $referer);
105     echo $OUTPUT->footer();
106     exit;
109 require_login(0, false);   // Script is useless unless they're logged in.
111 $canreplyprivately = false;
113 if (!empty($forum)) {
114     // User is starting a new discussion in a forum.
115     $forumentity = $forumvault->get_from_id($forum);
116     if (empty($forumentity)) {
117         print_error('invalidforumid', 'forum');
118     }
120     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
121     $forum = $forumdatamapper->to_legacy_object($forumentity);
122     $course = $forumentity->get_course_record();
123     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
124         print_error("invalidcoursemodule");
125     }
127     // Retrieve the contexts.
128     $modcontext = $forumentity->get_context();
129     $coursecontext = context_course::instance($course->id);
131     if ($forumentity->is_in_group_mode() && null === $groupid) {
132         $groupid = groups_get_activity_group($cm);
133     }
135     if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
136         if (!isguestuser()) {
137             if (!is_enrolled($coursecontext)) {
138                 if (enrol_selfenrol_available($course->id)) {
139                     $SESSION->wantsurl = qualified_me();
140                     $SESSION->enrolcancel = get_local_referer(false);
141                     redirect(new moodle_url('/enrol/index.php', array('id' => $course->id,
142                         'returnurl' => '/mod/forum/view.php?f=' . $forum->id)),
143                         get_string('youneedtoenrol'));
144                 }
145             }
146         }
147         print_error('nopostforum', 'forum');
148     }
150     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
151         redirect(
152                 $urlfactory->get_course_url_from_forum($forumentity),
153                 get_string('activityiscurrentlyhidden'),
154                 null,
155                 \core\output\notice::NOTIFY_ERROR
156             );
157     }
159     $SESSION->fromurl = get_local_referer(false);
161     // Load up the $post variable.
163     $post = new stdClass();
164     $post->course        = $course->id;
165     $post->forum         = $forum->id;
166     $post->discussion    = 0;           // Ie discussion # not defined yet.
167     $post->parent        = 0;
168     $post->subject       = $subject;
169     $post->userid        = $USER->id;
170     $post->message       = $prefilledpost;
171     $post->messageformat = editors_get_preferred_format();
172     $post->messagetrust  = 0;
173     $post->groupid = $groupid;
175     // Unsetting this will allow the correct return URL to be calculated later.
176     unset($SESSION->fromdiscussion);
178 } else if (!empty($reply)) {
179     // User is writing a new reply.
181     $parententity = $postvault->get_from_id($reply);
182     if (empty($parententity)) {
183         print_error('invalidparentpostid', 'forum');
184     }
186     $discussionentity = $discussionvault->get_from_id($parententity->get_discussion_id());
187     if (empty($discussionentity)) {
188         print_error('notpartofdiscussion', 'forum');
189     }
191     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
192     if (empty($forumentity)) {
193         print_error('invalidforumid', 'forum');
194     }
196     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
197     $parent = $postdatamapper->to_legacy_object($parententity);
198     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
199     $forum = $forumdatamapper->to_legacy_object($forumentity);
200     $course = $forumentity->get_course_record();
201     $modcontext = $forumentity->get_context();
202     $coursecontext = context_course::instance($course->id);
204     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
205         print_error('invalidcoursemodule');
206     }
208     // Ensure lang, theme, etc. is set up properly. MDL-6926.
209     $PAGE->set_cm($cm, $course, $forum);
211     if (!$capabilitymanager->can_reply_to_post($USER, $discussionentity, $parententity)) {
212         if (!isguestuser()) {
213             if (!is_enrolled($coursecontext)) {  // User is a guest here!
214                 $SESSION->wantsurl = qualified_me();
215                 $SESSION->enrolcancel = get_local_referer(false);
216                 redirect(new moodle_url('/enrol/index.php', array('id' => $course->id,
217                     'returnurl' => '/mod/forum/view.php?f=' . $forum->id)),
218                     get_string('youneedtoenrol'));
219             }
221             // The forum has been locked. Just redirect back to the discussion page.
222             if (forum_discussion_is_locked($forum, $discussion)) {
223                 redirect(new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
224             }
225         }
226         print_error('nopostforum', 'forum');
227     }
229     // Make sure user can post here.
230     if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
231         $groupmode = $cm->groupmode;
232     } else {
233         $groupmode = $course->groupmode;
234     }
235     if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
236         if ($discussion->groupid == -1) {
237             print_error('nopostforum', 'forum');
238         } else {
239             if (!groups_is_member($discussion->groupid)) {
240                 print_error('nopostforum', 'forum');
241             }
242         }
243     }
245     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
246         print_error("activityiscurrentlyhidden");
247     }
249     if ($parententity->is_private_reply()) {
250         print_error('cannotreplytoprivatereply', 'forum');
251     }
253     // Load up the $post variable.
254     $post = new stdClass();
255     $post->course      = $course->id;
256     $post->forum       = $forum->id;
257     $post->discussion  = $parent->discussion;
258     $post->parent      = $parent->id;
259     $post->subject     = $subject ? $subject : $parent->subject;
260     $post->userid      = $USER->id;
261     $post->parentpostauthor = $parent->userid;
262     $post->message     = $prefilledpost;
263     $canreplyprivately = $capabilitymanager->can_reply_privately_to_post($USER, $parententity);
265     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
267     $strre = get_string('re', 'forum');
268     if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
269         $post->subject = $strre.' '.$post->subject;
270     }
272     // Unsetting this will allow the correct return URL to be calculated later.
273     unset($SESSION->fromdiscussion);
275 } else if (!empty($edit)) {
276     // User is editing their own post.
278     $postentity = $postvault->get_from_id($edit);
279     if (empty($postentity)) {
280         print_error('invalidpostid', 'forum');
281     }
282     if ($postentity->has_parent()) {
283         $parententity = $postvault->get_from_id($postentity->get_parent_id());
284         $parent = $postdatamapper->to_legacy_object($parententity);
285     }
287     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
288     if (empty($discussionentity)) {
289         print_error('notpartofdiscussion', 'forum');
290     }
292     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
293     if (empty($forumentity)) {
294         print_error('invalidforumid', 'forum');
295     }
297     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
298     $post = $postdatamapper->to_legacy_object($postentity);
299     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
300     $forum = $forumdatamapper->to_legacy_object($forumentity);
301     $course = $forumentity->get_course_record();
302     $modcontext = $forumentity->get_context();
303     $coursecontext = context_course::instance($course->id);
305     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
306         print_error('invalidcoursemodule');
307     }
309     $PAGE->set_cm($cm, $course, $forum);
311     if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
312         if (((time() - $post->created) > $CFG->maxeditingtime) and
313             !has_capability('mod/forum:editanypost', $modcontext)) {
314             print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
315         }
316     }
317     if (($post->userid <> $USER->id) and
318         !has_capability('mod/forum:editanypost', $modcontext)) {
319         print_error('cannoteditposts', 'forum');
320     }
322     // Load up the $post variable.
323     $post->edit   = $edit;
324     $post->course = $course->id;
325     $post->forum  = $forum->id;
326     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
327     if ($postentity->has_parent()) {
328         $canreplyprivately = forum_user_can_reply_privately($modcontext, $parent);
329     }
331     $post = trusttext_pre_edit($post, 'message', $modcontext);
333     // Unsetting this will allow the correct return URL to be calculated later.
334     unset($SESSION->fromdiscussion);
336 } else if (!empty($delete)) {
337     // User is deleting a post.
339     $postentity = $postvault->get_from_id($delete);
340     if (empty($postentity)) {
341         print_error('invalidpostid', 'forum');
342     }
344     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
345     if (empty($discussionentity)) {
346         print_error('notpartofdiscussion', 'forum');
347     }
349     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
350     if (empty($forumentity)) {
351         print_error('invalidforumid', 'forum');
352     }
354     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
355     $post = $postdatamapper->to_legacy_object($postentity);
356     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
357     $forum = $forumdatamapper->to_legacy_object($forumentity);
358     $course = $forumentity->get_course_record();
359     $modcontext = $forumentity->get_context();
360     $coursecontext = context_course::instance($course->id);
362     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
363         print_error('invalidcoursemodule');
364     }
366     require_login($course, false, $cm);
368     if (!$capabilitymanager->can_delete_post($USER, $discussionentity, $postentity)) {
369         redirect(
370                 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
371                 get_string('cannotdeletepost', 'forum'),
372                 null,
373                 \core\output\notice::NOTIFY_ERROR
374             );
375     }
377     $replycount = $postvault->get_reply_count_for_post_id_in_discussion_id(
378         $USER, $postentity->get_id(), $discussionentity->get_id(), true);
380     if (!empty($confirm) && confirm_sesskey()) {
381         // User has confirmed the delete.
382         // Check user capability to delete post.
383         $timepassed = time() - $post->created;
384         if ($post->totalscore) {
385             redirect(
386                     $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
387                     get_string('couldnotdeleteratings', 'rating'),
388                     null,
389                     \core\output\notice::NOTIFY_ERROR
390                 );
391         } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
392             redirect(
393                     $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
394                     get_string('couldnotdeletereplies', 'rating'),
395                     null,
396                     \core\output\notice::NOTIFY_ERROR
397                 );
398         } else {
399             if (!$postentity->has_parent()) {
400                 // Post is a discussion topic as well, so delete discussion.
401                 if ($forum->type == 'single') {
402                     redirect(
403                             $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
404                             get_string('cannotdeletediscussioninsinglediscussion', 'rating'),
405                             null,
406                             \core\output\notice::NOTIFY_ERROR
407                         );
408                 }
409                 forum_delete_discussion($discussion, false, $course, $cm, $forum);
411                 $params = array(
412                     'objectid' => $discussion->id,
413                     'context' => $modcontext,
414                     'other' => array(
415                         'forumid' => $forum->id,
416                     )
417                 );
419                 $event = \mod_forum\event\discussion_deleted::create($params);
420                 $event->add_record_snapshot('forum_discussions', $discussion);
421                 $event->trigger();
423                 redirect(
424                     $urlfactory->get_forum_view_url_from_forum($forumentity),
425                     get_string('eventdiscussiondeleted', 'forum'),
426                     null,
427                     \core\output\notification::NOTIFY_SUCCESS
428                 );
430             } else {
431                 $deleted = forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext), $course, $cm, $forum);
433                 if (!$deleted) {
434                     redirect(
435                             $urlfactory->get_discussion_view_url_from_post($postentity),
436                             get_string('errorwhiledelete', 'forum'),
437                             null,
438                             \core\output\notice::NOTIFY_ERROR
439                         );
440                 }
442                 if ($forum->type == 'single') {
443                     // Single discussion forums are an exception.
444                     // We show the forum itself since it only has one discussion thread.
445                     $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
446                 } else {
447                     $discussionurl = $urlfactory->get_discussion_view_url_from_discussion($discussionentity);
448                 }
450                 redirect(
451                     forum_go_back_to($discussionurl),
452                     get_string('eventpostdeleted', 'forum'),
453                     null,
454                     \core\output\notification::NOTIFY_SUCCESS
455                 );
456             }
457         }
460     } else {
461         // User just asked to delete something.
462         forum_set_return();
463         $PAGE->navbar->add(get_string('delete', 'forum'));
464         $PAGE->set_title($course->shortname);
465         $PAGE->set_heading($course->fullname);
467         if ($replycount) {
468             if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
469                 redirect(
470                         forum_go_back_to($urlfactory->get_view_post_url_from_post($postentity)),
471                         get_string('couldnotdeletereplies', 'rating'),
472                         null,
473                         \core\output\notice::NOTIFY_ERROR
474                     );
475             }
477             echo $OUTPUT->header();
478             echo $OUTPUT->heading(format_string($forum->name), 2);
479             echo $OUTPUT->confirm(get_string("deletesureplural", "forum", $replycount + 1),
480                 "post.php?delete=$delete&confirm=$delete",
481                 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
483             $postentities = [$postentity];
484             if (empty($post->edit)) {
485                 $postvault = $vaultfactory->get_post_vault();
486                 $replies = $postvault->get_replies_to_post(
487                         $USER,
488                         $postentity,
489                         // Note: All replies are fetched here as the user has deleteanypost.
490                         true,
491                         'created ASC'
492                     );
493                 $postentities = array_merge($postentities, $replies);
494             }
496             $rendererfactory = mod_forum\local\container::get_renderer_factory();
497             $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_NESTED, true);
498             echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
499         } else {
500             echo $OUTPUT->header();
501             echo $OUTPUT->heading(format_string($forum->name), 2);
502             echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
503                 "post.php?delete=$delete&confirm=$delete",
504                 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
506             $rendererfactory = mod_forum\local\container::get_renderer_factory();
507             $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
508             echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
509         }
511     }
512     echo $OUTPUT->footer();
513     die;
515 } else if (!empty($prune)) {
516     // Pruning.
518     $postentity = $postvault->get_from_id($prune);
519     if (empty($postentity)) {
520         print_error('invalidpostid', 'forum');
521     }
523     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
524     if (empty($discussionentity)) {
525         print_error('notpartofdiscussion', 'forum');
526     }
528     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
529     if (empty($forumentity)) {
530         print_error('invalidforumid', 'forum');
531     }
533     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
534     $post = $postdatamapper->to_legacy_object($postentity);
535     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
536     $forum = $forumdatamapper->to_legacy_object($forumentity);
537     $course = $forumentity->get_course_record();
538     $modcontext = $forumentity->get_context();
539     $coursecontext = context_course::instance($course->id);
541     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
542         print_error('invalidcoursemodule');
543     }
545     if (!$postentity->has_parent()) {
546         redirect(
547                 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
548                 get_string('alreadyfirstpost', 'forum'),
549                 null,
550                 \core\output\notification::NOTIFY_ERROR
551             );
552     }
553     if (!$capabilitymanager->can_split_post($USER, $discussionentity, $postentity)) {
554         redirect(
555                 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
556                 get_string('cannotsplit', 'forum'),
557                 null,
558                 \core\output\notification::NOTIFY_ERROR
559             );
560     }
562     $PAGE->set_cm($cm);
563     $PAGE->set_context($modcontext);
565     $prunemform = new mod_forum_prune_form(null, array('prune' => $prune, 'confirm' => $prune));
567     if ($prunemform->is_cancelled()) {
568         redirect(forum_go_back_to($urlfactory->get_discussion_view_url_from_discussion($discussionentity)));
569     } else if ($fromform = $prunemform->get_data()) {
570         // User submits the data.
571         $newdiscussion = new stdClass();
572         $newdiscussion->course       = $discussion->course;
573         $newdiscussion->forum        = $discussion->forum;
574         $newdiscussion->name         = $name;
575         $newdiscussion->firstpost    = $post->id;
576         $newdiscussion->userid       = $discussion->userid;
577         $newdiscussion->groupid      = $discussion->groupid;
578         $newdiscussion->assessed     = $discussion->assessed;
579         $newdiscussion->usermodified = $post->userid;
580         $newdiscussion->timestart    = $discussion->timestart;
581         $newdiscussion->timeend      = $discussion->timeend;
583         $newid = $DB->insert_record('forum_discussions', $newdiscussion);
585         $newpost = new stdClass();
586         $newpost->id      = $post->id;
587         $newpost->parent  = 0;
588         $newpost->subject = $name;
590         $DB->update_record("forum_posts", $newpost);
591         $postentity = $postvault->get_from_id($postentity->get_id());
593         forum_change_discussionid($post->id, $newid);
595         // Update last post in each discussion.
596         forum_discussion_update_last_post($discussion->id);
597         forum_discussion_update_last_post($newid);
599         // Fire events to reflect the split..
600         $params = array(
601             'context' => $modcontext,
602             'objectid' => $discussion->id,
603             'other' => array(
604                 'forumid' => $forum->id,
605             )
606         );
607         $event = \mod_forum\event\discussion_updated::create($params);
608         $event->trigger();
610         $params = array(
611             'context' => $modcontext,
612             'objectid' => $newid,
613             'other' => array(
614                 'forumid' => $forum->id,
615             )
616         );
617         $event = \mod_forum\event\discussion_created::create($params);
618         $event->trigger();
620         $params = array(
621             'context' => $modcontext,
622             'objectid' => $post->id,
623             'other' => array(
624                 'discussionid' => $newid,
625                 'forumid' => $forum->id,
626                 'forumtype' => $forum->type,
627             )
628         );
629         $event = \mod_forum\event\post_updated::create($params);
630         $event->add_record_snapshot('forum_discussions', $discussion);
631         $event->trigger();
633         redirect(
634             forum_go_back_to($urlfactory->get_discussion_view_url_from_post($postentity)),
635             get_string('discussionsplit', 'forum'),
636             null,
637             \core\output\notification::NOTIFY_SUCCESS
638         );
639     } else {
640         // Display the prune form.
641         $course = $DB->get_record('course', array('id' => $forum->course));
642         $subjectstr = format_string($post->subject, true);
643         $PAGE->navbar->add($subjectstr, new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
644         $PAGE->navbar->add(get_string("prune", "forum"));
645         $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
646         $PAGE->set_heading($course->fullname);
647         echo $OUTPUT->header();
648         echo $OUTPUT->heading(format_string($forum->name), 2);
649         echo $OUTPUT->heading(get_string('pruneheading', 'forum'), 3);
651         $prunemform->display();
653         $postentity = $entityfactory->get_post_from_stdclass($post);
654         $discussionentity = $entityfactory->get_discussion_from_stdclass($discussion);
655         $forumentity = $entityfactory->get_forum_from_stdclass($forum, $modcontext, $cm, $course);
656         $rendererfactory = mod_forum\local\container::get_renderer_factory();
657         $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
658         echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
659     }
661     echo $OUTPUT->footer();
662     die;
663 } else {
664     print_error('unknowaction');
668 // From now on user must be logged on properly.
670 require_login($course, false, $cm);
672 if (isguestuser()) {
673     // Just in case.
674     print_error('noguest');
677 $thresholdwarning = forum_check_throttling($forum, $cm);
678 $mformpost = new mod_forum_post_form('post.php', [
679         'course' => $course,
680         'cm' => $cm,
681         'coursecontext' => $coursecontext,
682         'modcontext' => $modcontext,
683         'forum' => $forum,
684         'post' => $post,
685         'subscribe' => \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm),
686         'thresholdwarning' => $thresholdwarning,
687         'edit' => $edit,
688         'canreplyprivately' => $canreplyprivately,
689     ], 'post', '', array('id' => 'mformforum'));
691 $draftitemid = file_get_submitted_draft_itemid('attachments');
692 $postid = empty($post->id) ? null : $post->id;
693 $attachoptions = mod_forum_post_form::attachment_options($forum);
694 file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', $postid, $attachoptions);
696 // Load data into form NOW!
698 if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end.
699     $data = new stdClass();
700     $data->date = userdate($post->created);
701     if ($post->messageformat == FORMAT_HTML) {
702         $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
703             fullname($USER).'</a>';
704         $post->message .= '<p><span class="edited">('.get_string('editedby', 'forum', $data).')</span></p>';
705     } else {
706         $data->name = fullname($USER);
707         $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
708     }
709     unset($data);
712 $formheading = '';
713 if (!empty($parent)) {
714     $heading = get_string("yourreply", "forum");
715     $formheading = get_string('reply', 'forum');
716 } else {
717     if ($forum->type == 'qanda') {
718         $heading = get_string('yournewquestion', 'forum');
719     } else {
720         $heading = get_string('yournewtopic', 'forum');
721     }
724 $postid = empty($post->id) ? null : $post->id;
725 $draftideditor = file_get_submitted_draft_itemid('message');
726 $editoropts = mod_forum_post_form::editor_options($modcontext, $postid);
727 $currenttext = file_prepare_draft_area($draftideditor, $modcontext->id, 'mod_forum', 'post', $postid, $editoropts, $post->message);
729 $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
730 if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
731     // User does not have permission to subscribe to this discussion at all.
732     $discussionsubscribe = false;
733 } else if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
734     // User does not have permission to unsubscribe from this discussion at all.
735     $discussionsubscribe = true;
736 } else {
737     if (isset($discussion) && \mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id, $cm)) {
738         // User is subscribed to the discussion - continue the subscription.
739         $discussionsubscribe = true;
740     } else if (!isset($discussion) && \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm)) {
741         // Starting a new discussion, and the user is subscribed to the forum - subscribe to the discussion.
742         $discussionsubscribe = true;
743     } else {
744         // User is not subscribed to either forum or discussion. Follow user preference.
745         $discussionsubscribe = $USER->autosubscribe;
746     }
749 $mformpost->set_data(
750     array(
751         'attachments' => $draftitemid,
752         'general' => $heading,
753         'subject' => $post->subject,
754         'message' => array(
755             'text' => $currenttext,
756             'format' => empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat,
757             'itemid' => $draftideditor
758         ),
759         'discussionsubscribe' => $discussionsubscribe,
760         'mailnow' => !empty($post->mailnow),
761         'userid' => $post->userid,
762         'parent' => $post->parent,
763         'discussion' => $post->discussion,
764         'course' => $course->id
765     ) +
767     $pageparams +
769     (isset($post->format) ? array('format' => $post->format) : array()) +
771     (isset($discussion->timestart) ? array('timestart' => $discussion->timestart) : array()) +
773     (isset($discussion->timeend) ? array('timeend' => $discussion->timeend) : array()) +
775     (isset($discussion->pinned) ? array('pinned' => $discussion->pinned) : array()) +
777     (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) +
779     (isset($discussion->id) ? array('discussion' => $discussion->id) : array())
780 );
782 if ($mformpost->is_cancelled()) {
783     if (!isset($discussion->id) || $forum->type === 'single') {
784         // Single forums don't have a discussion page.
785         redirect($urlfactory->get_forum_view_url_from_forum($forumentity));
786     } else {
787         redirect($urlfactory->get_discussion_view_url_from_discussion($discussionentity));
788     }
789 } else if ($fromform = $mformpost->get_data()) {
791     if (empty($SESSION->fromurl)) {
792         $errordestination = $urlfactory->get_forum_view_url_from_forum($forumentity);
793     } else {
794         $errordestination = $SESSION->fromurl;
795     }
797     $fromform->itemid        = $fromform->message['itemid'];
798     $fromform->messageformat = $fromform->message['format'];
799     $fromform->message       = $fromform->message['text'];
800     // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
801     $fromform->messagetrust  = trusttext_trusted($modcontext);
803     // Clean message text.
804     $fromform = trusttext_pre_edit($fromform, 'message', $modcontext);
806     if ($fromform->edit) {
807         // Updating a post.
808         unset($fromform->groupid);
809         $fromform->id = $fromform->edit;
810         $message = '';
812         if (!$capabilitymanager->can_edit_post($USER, $discussionentity, $postentity)) {
813             redirect(
814                     $urlfactory->get_view_post_url_from_post($postentity),
815                     get_string('cannotupdatepost', 'forum'),
816                     null,
817                     \core\output\notification::ERROR
818                 );
819         }
821         if (isset($fromform->groupinfo) && $capabilitymanager->can_move_discussions($USER)) {
822             // If the user has access to all groups and they are changing the group, then update the post.
823             if (empty($fromform->groupinfo)) {
824                 $fromform->groupinfo = -1;
825             }
827             if (!$capabilitymanager->can_create_discussions($USER, $fromform->groupinfo)) {
828                 redirect(
829                         $urlfactory->get_view_post_url_from_post($postentity),
830                         get_string('cannotupdatepost', 'forum'),
831                         null,
832                         \core\output\notification::ERROR
833                     );
834             }
836             if ($discussionentity->get_group_id() != $fromform->groupinfo) {
837                 $DB->set_field('forum_discussions', 'groupid', $fromform->groupinfo, array('firstpost' => $fromform->id));
838             }
839         }
841         // When editing first post/discussion.
842         if ($postentity->has_parent()) {
843             if ($capabilitymanager->can_pin_discussions($USER)) {
844                 // Can change pinned if we have capability.
845                 $fromform->pinned = !empty($fromform->pinned) ? FORUM_DISCUSSION_PINNED : FORUM_DISCUSSION_UNPINNED;
846             } else {
847                 // We don't have the capability to change so keep to previous value.
848                 unset($fromform->pinned);
849             }
850         }
851         $updatepost = $fromform;
852         $updatepost->forum = $forum->id;
853         if (!forum_update_post($updatepost, $mformpost)) {
854             print_error("couldnotupdate", "forum", $errordestination);
855         }
857         if ('single' == $forumentity->get_type() && !$postentity->has_parent()) {
858             // Updating first post of single discussion type -> updating forum intro.
859             $forum->intro = $updatepost->message;
860             $forum->timemodified = time();
861             $DB->update_record("forum", $forum);
862         }
864         if ($USER->id === $postentity->get_author_id()) {
865             $message .= get_string("postupdated", "forum");
866         } else {
867             $realuser = \core_user::get_user($postentity->get_author_id());
868             $message .= get_string("editedpostupdated", "forum", fullname($realuser));
869         }
871         $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
872         if ('single' == $forumentity->get_type()) {
873             // Single discussion forums are an exception.
874             // We show the forum itself since it only has one discussion thread.
875             $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
876         } else {
877             $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
878         }
880         $params = array(
881             'context' => $modcontext,
882             'objectid' => $fromform->id,
883             'other' => array(
884                 'discussionid' => $discussion->id,
885                 'forumid' => $forum->id,
886                 'forumtype' => $forum->type,
887             )
888         );
890         if ($USER->id !== $postentity->get_author_id()) {
891             $params['relateduserid'] = $postentity->get_author_id();
892         }
894         $event = \mod_forum\event\post_updated::create($params);
895         $event->add_record_snapshot('forum_discussions', $discussion);
896         $event->trigger();
898         redirect(
899             forum_go_back_to($discussionurl),
900             $message . $subscribemessage,
901             null,
902             \core\output\notification::NOTIFY_SUCCESS
903         );
905     } else if ($fromform->discussion) {
906         // Adding a new post to an existing discussion
907         // Before we add this we must check that the user will not exceed the blocking threshold.
908         forum_check_blocking_threshold($thresholdwarning);
910         unset($fromform->groupid);
911         $message = '';
912         $addpost = $fromform;
913         $addpost->forum = $forum->id;
914         if ($fromform->id = forum_add_new_post($addpost, $mformpost)) {
915             $postentity = $postvault->get_from_id($fromform->id);
916             $fromform->deleted = 0;
917             $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
919             if (!empty($fromform->mailnow)) {
920                 $message .= get_string("postmailnow", "forum");
921             } else {
922                 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
923                 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
924             }
926             if ($forum->type == 'single') {
927                 // Single discussion forums are an exception.
928                 // We show the forum itself since it only has one discussion thread.
929                 $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
930             } else {
931                 $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
932             }
934             $params = array(
935                 'context' => $modcontext,
936                 'objectid' => $fromform->id,
937                 'other' => array(
938                     'discussionid' => $discussion->id,
939                     'forumid' => $forum->id,
940                     'forumtype' => $forum->type,
941                 )
942             );
943             $event = \mod_forum\event\post_created::create($params);
944             $event->add_record_snapshot('forum_posts', $fromform);
945             $event->add_record_snapshot('forum_discussions', $discussion);
946             $event->trigger();
948             // Update completion state.
949             $completion = new completion_info($course);
950             if ($completion->is_enabled($cm) &&
951                 ($forum->completionreplies || $forum->completionposts)) {
952                 $completion->update_state($cm, COMPLETION_COMPLETE);
953             }
955             redirect(
956                 forum_go_back_to($discussionurl),
957                 $message . $subscribemessage,
958                 null,
959                 \core\output\notification::NOTIFY_SUCCESS
960             );
962         } else {
963             print_error("couldnotadd", "forum", $errordestination);
964         }
965         exit;
967     } else {
968         // Adding a new discussion.
969         // The location to redirect to after successfully posting.
970         $redirectto = new moodle_url('/mod/forum/view.php', array('f' => $fromform->forum));
972         $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
974         $discussion = $fromform;
975         $discussion->name = $fromform->subject;
976         $discussion->timelocked = 0;
978         $newstopic = false;
979         if ($forum->type == 'news' && !$fromform->parent) {
980             $newstopic = true;
981         }
982         $discussion->timestart = $fromform->timestart;
983         $discussion->timeend = $fromform->timeend;
984         $discussion->pinned = FORUM_DISCUSSION_UNPINNED;
986         if (!empty($fromform->pinned) && $capabilitymanager->can_pin_discussions($USER)) {
987             $discussion->pinned = FORUM_DISCUSSION_PINNED;
988         }
990         $allowedgroups = array();
991         $groupstopostto = array();
993         // If we are posting a copy to all groups the user has access to.
994         if (isset($fromform->posttomygroups)) {
995             // Post to each of my groups.
996             require_capability('mod/forum:canposttomygroups', $modcontext);
998             // Fetch all of this user's groups.
999             // Note: all groups are returned when in visible groups mode so we must manually filter.
1000             $allowedgroups = groups_get_activity_allowed_groups($cm);
1001             foreach ($allowedgroups as $groupid => $group) {
1002                 if ($capabilitymanager->can_create_discussions($USER, $groupid)) {
1003                     $groupstopostto[] = $groupid;
1004                 }
1005             }
1006         } else if (isset($fromform->groupinfo)) {
1007             // Use the value provided in the dropdown group selection.
1008             $groupstopostto[] = $fromform->groupinfo;
1009             $redirectto->param('group', $fromform->groupinfo);
1010         } else if (isset($fromform->groupid) && !empty($fromform->groupid)) {
1011             // Use the value provided in the hidden form element instead.
1012             $groupstopostto[] = $fromform->groupid;
1013             $redirectto->param('group', $fromform->groupid);
1014         } else {
1015             // Use the value for all participants instead.
1016             $groupstopostto[] = -1;
1017         }
1019         // Before we post this we must check that the user will not exceed the blocking threshold.
1020         forum_check_blocking_threshold($thresholdwarning);
1022         foreach ($groupstopostto as $group) {
1023             if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
1024                 print_error('cannotcreatediscussion', 'forum');
1025             }
1027             $discussion->groupid = $group;
1028             $message = '';
1029             if ($discussion->id = forum_add_discussion($discussion, $mformpost)) {
1031                 $params = array(
1032                     'context' => $modcontext,
1033                     'objectid' => $discussion->id,
1034                     'other' => array(
1035                         'forumid' => $forum->id,
1036                     )
1037                 );
1038                 $event = \mod_forum\event\discussion_created::create($params);
1039                 $event->add_record_snapshot('forum_discussions', $discussion);
1040                 $event->trigger();
1042                 if ($fromform->mailnow) {
1043                     $message .= get_string("postmailnow", "forum");
1044                 } else {
1045                     $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
1046                     $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
1047                 }
1049                 $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
1050             } else {
1051                 print_error("couldnotadd", "forum", $errordestination);
1052             }
1053         }
1055         // Update completion status.
1056         $completion = new completion_info($course);
1057         if ($completion->is_enabled($cm) &&
1058             ($forum->completiondiscussions || $forum->completionposts)) {
1059             $completion->update_state($cm, COMPLETION_COMPLETE);
1060         }
1062         // Redirect back to the discussion.
1063         redirect(
1064             forum_go_back_to($redirectto->out()),
1065             $message . $subscribemessage,
1066             null,
1067             \core\output\notification::NOTIFY_SUCCESS
1068         );
1069     }
1073 // This section is only shown after all checks are in place, and the forumentity and any relevant discussion and post
1074 // entity are available.
1076 if (!empty($discussionentity)) {
1077     $titlesubject = format_string($discussionentity->get_name(), true);
1078 } else if ('news' == $forumentity->get_type()) {
1079     $titlesubject = get_string("addanewtopic", "forum");
1080 } else {
1081     $titlesubject = get_string("addanewdiscussion", "forum");
1084 if (empty($post->edit)) {
1085     $post->edit = '';
1088 if (empty($discussion->name)) {
1089     if (empty($discussion)) {
1090         $discussion = new stdClass();
1091     }
1092     $discussion->name = $forum->name;
1095 $strdiscussionname = '';
1096 if ('single' == $forumentity->get_type()) {
1097     // There is only one discussion thread for this forum type. We should
1098     // not show the discussion name (same as forum name in this case) in
1099     // the breadcrumbs.
1100     $strdiscussionname = '';
1101 } else if (!empty($discussionentity)) {
1102     // Show the discussion name in the breadcrumbs.
1103     $strdiscussionname = format_string($discussionentity->get_name()) . ': ';
1106 $forcefocus = empty($reply) ? null : 'message';
1108 if (!empty($discussion->id)) {
1109     $PAGE->navbar->add($titlesubject, $urlfactory->get_discussion_view_url_from_discussion($discussionentity));
1112 if ($post->parent) {
1113     $PAGE->navbar->add(get_string('reply', 'forum'));
1116 if ($edit) {
1117     $PAGE->navbar->add(get_string('edit', 'forum'));
1120 $PAGE->set_title("{$course->shortname}: {$strdiscussionname}{$titlesubject}");
1121 $PAGE->set_heading($course->fullname);
1123 echo $OUTPUT->header();
1124 echo $OUTPUT->heading(format_string($forum->name), 2);
1126 // Checkup.
1127 if (!empty($parententity) && !$capabilitymanager->can_view_post($USER, $discussionentity, $parententity)) {
1128     print_error('cannotreply', 'forum');
1131 if (empty($parententity) && empty($edit) && !$capabilitymanager->can_create_discussions($USER, $groupid)) {
1132     print_error('cannotcreatediscussion', 'forum');
1135 if (!empty($discussionentity) && 'qanda' == $forumentity->get_type()) {
1136     $displaywarning = $capabilitymanager->must_post_before_viewing_discussion($USER, $discussionentity);
1137     $displaywarning = $displaywarning && !forum_user_has_posted($forumentity->get_id(), $discussionentity->get_id(), $USER->id);
1138     if ($displaywarning) {
1139         echo $OUTPUT->notification(get_string('qandanotify', 'forum'));
1140     }
1143 // If there is a warning message and we are not editing a post we need to handle the warning.
1144 if (!empty($thresholdwarning) && !$edit) {
1145     // Here we want to throw an exception if they are no longer allowed to post.
1146     forum_check_blocking_threshold($thresholdwarning);
1149 if (!empty($parententity)) {
1150     $postentities = [$parententity];
1152     if (empty($post->edit)) {
1153         if ('qanda' != $forumentity->get_type() || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
1154             $replies = $postvault->get_replies_to_post(
1155                     $USER,
1156                     $parententity,
1157                     $capabilitymanager->can_view_any_private_reply($USER),
1158                     'created ASC'
1159                 );
1160             $postentities = array_merge($postentities, $replies);
1161         }
1162     }
1164     $rendererfactory = mod_forum\local\container::get_renderer_factory();
1165     $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_THREADED, true);
1166     echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
1167 } else {
1168     if (!empty($forum->intro)) {
1169         echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
1170     }
1173 // Call print disclosure for enabled plagiarism plugins.
1174 if (!empty($CFG->enableplagiarism)) {
1175     require_once($CFG->libdir.'/plagiarismlib.php');
1176     echo plagiarism_print_disclosure($cm->id);
1179 if (!empty($formheading)) {
1180     echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide'));
1183 if (!empty($postentity)) {
1184     $data = (object) [
1185         'tags' => core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $postentity->get_id())
1186     ];
1187     $mformpost->set_data($data);
1190 $mformpost->display();
1192 echo $OUTPUT->footer();