Merge branch 'MDL-66927-run-new-adhoc' of https://github.com/brendanheywood/moodle
[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);
39 // Values posted via the inpage reply form.
40 $prefilledpost = optional_param('post', '', PARAM_TEXT);
41 $prefilledpostformat = optional_param('postformat', FORMAT_MOODLE, PARAM_INT);
42 $prefilledprivatereply = optional_param('privatereply', false, PARAM_BOOL);
44 $PAGE->set_url('/mod/forum/post.php', array(
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);
57 $sitecontext = context_system::instance();
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();
65 $forumvault = $vaultfactory->get_forum_vault();
66 $forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
68 $discussionvault = $vaultfactory->get_discussion_vault();
69 $discussiondatamapper = $legacydatamapperfactory->get_discussion_data_mapper();
71 $postvault = $vaultfactory->get_post_vault();
72 $postdatamapper = $legacydatamapperfactory->get_post_data_mapper();
74 if (!isloggedin() or isguestuser()) {
75     if (!isloggedin() and !get_local_referer()) {
76         // No referer+not logged in - probably coming in via email  See MDL-9052.
77         require_login();
78     }
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)) {
84             print_error('invalidforumid', 'forum');
85         }
86     } else if (!empty($reply)) {
87         // User is writing a new reply.
88         $forumentity = $forumvault->get_from_post_id($reply);
89         if (empty($forumentity)) {
90             print_error('invalidparentpostid', 'forum');
91         }
92     }
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");
99     }
101     $PAGE->set_cm($cm, $course, $forum);
102     $PAGE->set_context($modcontext);
103     $PAGE->set_title($course->shortname);
104     $PAGE->set_heading($course->fullname);
105     $referer = get_local_referer(false);
107     echo $OUTPUT->header();
108     echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), $referer);
109     echo $OUTPUT->footer();
110     exit;
113 require_login(0, false);   // Script is useless unless they're logged in.
115 $canreplyprivately = false;
117 if (!empty($forum)) {
118     // User is starting a new discussion in a forum.
119     $forumentity = $forumvault->get_from_id($forum);
120     if (empty($forumentity)) {
121         print_error('invalidforumid', 'forum');
122     }
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)) {
128         print_error("invalidcoursemodule");
129     }
131     // Retrieve the contexts.
132     $modcontext = $forumentity->get_context();
133     $coursecontext = context_course::instance($course->id);
135     if ($forumentity->is_in_group_mode() && null === $groupid) {
136         $groupid = groups_get_activity_group($cm);
137     }
139     if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
140         if (!isguestuser()) {
141             if (!is_enrolled($coursecontext)) {
142                 if (enrol_selfenrol_available($course->id)) {
143                     $SESSION->wantsurl = qualified_me();
144                     $SESSION->enrolcancel = get_local_referer(false);
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'));
148                 }
149             }
150         }
151         print_error('nopostforum', 'forum');
152     }
154     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
155         redirect(
156                 $urlfactory->get_course_url_from_forum($forumentity),
157                 get_string('activityiscurrentlyhidden'),
158                 null,
159                 \core\output\notification::NOTIFY_ERROR
160             );
161     }
163     $SESSION->fromurl = get_local_referer(false);
165     // Load up the $post variable.
167     $post = new stdClass();
168     $post->course        = $course->id;
169     $post->forum         = $forum->id;
170     $post->discussion    = 0;           // Ie discussion # not defined yet.
171     $post->parent        = 0;
172     $post->subject       = $subject;
173     $post->userid        = $USER->id;
174     $post->message       = $prefilledpost;
175     $post->messageformat = editors_get_preferred_format();
176     $post->messagetrust  = 0;
177     $post->groupid = $groupid;
179     // Unsetting this will allow the correct return URL to be calculated later.
180     unset($SESSION->fromdiscussion);
182 } else if (!empty($reply)) {
183     // User is writing a new reply.
185     $parententity = $postvault->get_from_id($reply);
186     if (empty($parententity)) {
187         print_error('invalidparentpostid', 'forum');
188     }
190     $discussionentity = $discussionvault->get_from_id($parententity->get_discussion_id());
191     if (empty($discussionentity)) {
192         print_error('notpartofdiscussion', 'forum');
193     }
195     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
196     if (empty($forumentity)) {
197         print_error('invalidforumid', 'forum');
198     }
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);
208     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
209         print_error('invalidcoursemodule');
210     }
212     // Ensure lang, theme, etc. is set up properly. MDL-6926.
213     $PAGE->set_cm($cm, $course, $forum);
215     if (!$capabilitymanager->can_reply_to_post($USER, $discussionentity, $parententity)) {
216         if (!isguestuser()) {
217             if (!is_enrolled($coursecontext)) {  // User is a guest here!
218                 $SESSION->wantsurl = qualified_me();
219                 $SESSION->enrolcancel = get_local_referer(false);
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'));
223             }
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             }
229         }
230         print_error('nopostforum', 'forum');
231     }
233     // Make sure user can post here.
234     if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
235         $groupmode = $cm->groupmode;
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)) {
244                 print_error('nopostforum', 'forum');
245             }
246         }
247     }
249     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
250         print_error("activityiscurrentlyhidden");
251     }
253     if ($parententity->is_private_reply()) {
254         print_error('cannotreplytoprivatereply', 'forum');
255     }
257     // We always are going to honor the preferred format. We are creating a new post.
258     $preferredformat = editors_get_preferred_format();
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         }
266     }
268     // Load up the $post variable.
269     $post = new stdClass();
270     $post->course      = $course->id;
271     $post->forum       = $forum->id;
272     $post->discussion  = $parent->discussion;
273     $post->parent      = $parent->id;
274     $post->subject     = $subject ? $subject : $parent->subject;
275     $post->userid      = $USER->id;
276     $post->parentpostauthor = $parent->userid;
277     $post->message     = $prefilledpost;
278     $post->messageformat  = $preferredformat;
279     $post->isprivatereply = $prefilledprivatereply;
280     $canreplyprivately = $capabilitymanager->can_reply_privately_to_post($USER, $parententity);
282     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
284     $strre = get_string('re', 'forum');
285     if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
286         $post->subject = $strre.' '.$post->subject;
287     }
289     // Unsetting this will allow the correct return URL to be calculated later.
290     unset($SESSION->fromdiscussion);
292 } else if (!empty($edit)) {
293     // User is editing their own post.
295     $postentity = $postvault->get_from_id($edit);
296     if (empty($postentity)) {
297         print_error('invalidpostid', 'forum');
298     }
299     if ($postentity->has_parent()) {
300         $parententity = $postvault->get_from_id($postentity->get_parent_id());
301         $parent = $postdatamapper->to_legacy_object($parententity);
302     }
304     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
305     if (empty($discussionentity)) {
306         print_error('notpartofdiscussion', 'forum');
307     }
309     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
310     if (empty($forumentity)) {
311         print_error('invalidforumid', 'forum');
312     }
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);
322     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
323         print_error('invalidcoursemodule');
324     }
326     $PAGE->set_cm($cm, $course, $forum);
328     if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
329         if (((time() - $post->created) > $CFG->maxeditingtime) and
330             !has_capability('mod/forum:editanypost', $modcontext)) {
331             print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
332         }
333     }
334     if (($post->userid <> $USER->id) and
335         !has_capability('mod/forum:editanypost', $modcontext)) {
336         print_error('cannoteditposts', 'forum');
337     }
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;
344     if ($postentity->has_parent()) {
345         $canreplyprivately = forum_user_can_reply_privately($modcontext, $parent);
346     }
348     $post = trusttext_pre_edit($post, 'message', $modcontext);
350     // Unsetting this will allow the correct return URL to be calculated later.
351     unset($SESSION->fromdiscussion);
353 } else if (!empty($delete)) {
354     // User is deleting a post.
356     $postentity = $postvault->get_from_id($delete);
357     if (empty($postentity)) {
358         print_error('invalidpostid', 'forum');
359     }
361     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
362     if (empty($discussionentity)) {
363         print_error('notpartofdiscussion', 'forum');
364     }
366     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
367     if (empty($forumentity)) {
368         print_error('invalidforumid', 'forum');
369     }
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);
379     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
380         print_error('invalidcoursemodule');
381     }
383     require_login($course, false, $cm);
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,
390                 \core\output\notification::NOTIFY_ERROR
391             );
392     }
394     $replycount = $postvault->get_reply_count_for_post_id_in_discussion_id(
395         $USER, $postentity->get_id(), $discussionentity->get_id(), true);
397     if (!empty($confirm) && confirm_sesskey()) {
398         // User has confirmed the delete.
399         // Check user capability to delete post.
400         $timepassed = time() - $post->created;
401         if ($post->totalscore) {
402             redirect(
403                     $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
404                     get_string('couldnotdeleteratings', 'rating'),
405                     null,
406                     \core\output\notification::NOTIFY_ERROR
407                 );
408         } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
409             redirect(
410                     $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
411                     get_string('couldnotdeletereplies', 'forum'),
412                     null,
413                     \core\output\notification::NOTIFY_ERROR
414                 );
415         } else {
416             if (!$postentity->has_parent()) {
417                 // Post is a discussion topic as well, so delete discussion.
418                 if ($forum->type == 'single') {
419                     redirect(
420                             $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
421                             get_string('cannotdeletediscussioninsinglediscussion', 'forum'),
422                             null,
423                             \core\output\notification::NOTIFY_ERROR
424                         );
425                 }
426                 forum_delete_discussion($discussion, false, $course, $cm, $forum);
428                 $params = array(
429                     'objectid' => $discussion->id,
430                     'context' => $modcontext,
431                     'other' => array(
432                         'forumid' => $forum->id,
433                     )
434                 );
436                 $event = \mod_forum\event\discussion_deleted::create($params);
437                 $event->add_record_snapshot('forum_discussions', $discussion);
438                 $event->trigger();
440                 redirect(
441                     $urlfactory->get_forum_view_url_from_forum($forumentity),
442                     get_string('eventdiscussiondeleted', 'forum'),
443                     null,
444                     \core\output\notification::NOTIFY_SUCCESS
445                 );
447             } else {
448                 $deleted = forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext), $course, $cm, $forum);
450                 if (!$deleted) {
451                     redirect(
452                             $urlfactory->get_discussion_view_url_from_post($postentity),
453                             get_string('errorwhiledelete', 'forum'),
454                             null,
455                             \core\output\notification::NOTIFY_ERROR
456                         );
457                 }
459                 if ($forum->type == 'single') {
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);
463                 } else {
464                     $discussionurl = $urlfactory->get_discussion_view_url_from_discussion($discussionentity);
465                 }
467                 redirect(
468                     forum_go_back_to($discussionurl),
469                     get_string('eventpostdeleted', 'forum'),
470                     null,
471                     \core\output\notification::NOTIFY_SUCCESS
472                 );
473             }
474         }
477     } else {
478         // User just asked to delete something.
479         forum_set_return();
480         $PAGE->navbar->add(get_string('delete', 'forum'));
481         $PAGE->set_title($course->shortname);
482         $PAGE->set_heading($course->fullname);
484         if ($replycount) {
485             if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
486                 redirect(
487                         forum_go_back_to($urlfactory->get_view_post_url_from_post($postentity)),
488                         get_string('couldnotdeletereplies', 'forum'),
489                         null,
490                         \core\output\notification::NOTIFY_ERROR
491                     );
492             }
494             echo $OUTPUT->header();
495             echo $OUTPUT->heading(format_string($forum->name), 2);
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);
500             $postentities = [$postentity];
501             if (empty($post->edit)) {
502                 $postvault = $vaultfactory->get_post_vault();
503                 $replies = $postvault->get_replies_to_post(
504                         $USER,
505                         $postentity,
506                         // Note: All replies are fetched here as the user has deleteanypost.
507                         true,
508                         'created ASC'
509                     );
510                 $postentities = array_merge($postentities, $replies);
511             }
513             $rendererfactory = mod_forum\local\container::get_renderer_factory();
514             $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_NESTED, true);
515             echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
516         } else {
517             echo $OUTPUT->header();
518             echo $OUTPUT->heading(format_string($forum->name), 2);
519             echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
520                 "post.php?delete=$delete&confirm=$delete",
521                 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
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]);
526         }
528     }
529     echo $OUTPUT->footer();
530     die;
532 } else if (!empty($prune)) {
533     // Pruning.
535     $postentity = $postvault->get_from_id($prune);
536     if (empty($postentity)) {
537         print_error('invalidpostid', 'forum');
538     }
540     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
541     if (empty($discussionentity)) {
542         print_error('notpartofdiscussion', 'forum');
543     }
545     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
546     if (empty($forumentity)) {
547         print_error('invalidforumid', 'forum');
548     }
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);
558     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
559         print_error('invalidcoursemodule');
560     }
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             );
577     }
579     $PAGE->set_cm($cm);
580     $PAGE->set_context($modcontext);
582     $prunemform = new mod_forum_prune_form(null, array('prune' => $prune, 'confirm' => $prune));
584     if ($prunemform->is_cancelled()) {
585         redirect(forum_go_back_to($urlfactory->get_discussion_view_url_from_discussion($discussionentity)));
586     } else if ($fromform = $prunemform->get_data()) {
587         // User submits the data.
588         $newdiscussion = new stdClass();
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;
600         $newid = $DB->insert_record('forum_discussions', $newdiscussion);
602         $newpost = new stdClass();
603         $newpost->id      = $post->id;
604         $newpost->parent  = 0;
605         $newpost->subject = $name;
607         $DB->update_record("forum_posts", $newpost);
608         $postentity = $postvault->get_from_id($postentity->get_id());
610         forum_change_discussionid($post->id, $newid);
612         // Update last post in each discussion.
613         forum_discussion_update_last_post($discussion->id);
614         forum_discussion_update_last_post($newid);
616         // Fire events to reflect the split..
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();
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();
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();
650         redirect(
651             forum_go_back_to($urlfactory->get_discussion_view_url_from_post($postentity)),
652             get_string('discussionsplit', 'forum'),
653             null,
654             \core\output\notification::NOTIFY_SUCCESS
655         );
656     } else {
657         // Display the prune form.
658         $course = $DB->get_record('course', array('id' => $forum->course));
659         $subjectstr = format_string($post->subject, true);
660         $PAGE->navbar->add($subjectstr, new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
661         $PAGE->navbar->add(get_string("prune", "forum"));
662         $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
663         $PAGE->set_heading($course->fullname);
664         echo $OUTPUT->header();
665         echo $OUTPUT->heading(format_string($forum->name), 2);
666         echo $OUTPUT->heading(get_string('pruneheading', 'forum'), 3);
668         $prunemform->display();
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]);
676     }
678     echo $OUTPUT->footer();
679     die;
680 } else {
681     print_error('unknowaction');
685 // From now on user must be logged on properly.
687 require_login($course, false, $cm);
689 if (isguestuser()) {
690     // Just in case.
691     print_error('noguest');
694 $thresholdwarning = forum_check_throttling($forum, $cm);
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'));
708 $draftitemid = file_get_submitted_draft_itemid('attachments');
709 $postid = empty($post->id) ? null : $post->id;
710 $attachoptions = mod_forum_post_form::attachment_options($forum);
711 file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', $postid, $attachoptions);
713 // Load data into form NOW!
715 if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end.
716     $data = new stdClass();
717     $data->date = userdate($post->created);
718     if ($post->messageformat == FORMAT_HTML) {
719         $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
720             fullname($USER).'</a>';
721         $post->message .= '<p><span class="edited">('.get_string('editedby', 'forum', $data).')</span></p>';
722     } else {
723         $data->name = fullname($USER);
724         $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
725     }
726     unset($data);
729 $formheading = '';
730 if (!empty($parent)) {
731     $heading = get_string("yourreply", "forum");
732     $formheading = get_string('reply', 'forum');
733 } else {
734     if ($forum->type == 'qanda') {
735         $heading = get_string('yournewquestion', 'forum');
736     } else {
737         $heading = get_string('yournewtopic', 'forum');
738     }
741 $postid = empty($post->id) ? null : $post->id;
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);
745 $discussionid = isset($discussion) ? $discussion->id : null;
746 $discussionsubscribe = \mod_forum\subscriptions::get_user_default_subscription($forum, $coursecontext, $cm, $discussionid);
748 $mformpost->set_data(
749     array(
750         'attachments' => $draftitemid,
751         'general' => $heading,
752         'subject' => $post->subject,
753         'message' => array(
754             'text' => $currenttext,
755             'format' => !isset($post->messageformat) || !is_numeric($post->messageformat) ?
756                 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         'isprivatereply' => $post->isprivatereply ?? false
766     ) +
768     $pageparams +
770     (isset($post->format) ? array('format' => $post->format) : array()) +
772     (isset($discussion->timestart) ? array('timestart' => $discussion->timestart) : array()) +
774     (isset($discussion->timeend) ? array('timeend' => $discussion->timeend) : array()) +
776     (isset($discussion->pinned) ? array('pinned' => $discussion->pinned) : array()) +
778     (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) +
780     (isset($discussion->id) ? array('discussion' => $discussion->id) : array())
781 );
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;
788 if ($mformpost->no_submit_button_pressed()) {
789     $data = $mformpost->get_submitted_data();
791     // If a no submit button has been pressed but the default values haven't been then reset the form change.
792     if (!$dirty && isset($data->message['text']) && !empty(trim($data->message['text']))) {
793         $dirty = true;
794     }
796     if (!$dirty && isset($data->message['message']) && !empty(trim($data->message['message']))) {
797         $dirty = true;
798     }
800 $mformpost->set_initial_dirty_state($dirty);
802 if ($mformpost->is_cancelled()) {
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));
806     } else {
807         redirect($urlfactory->get_discussion_view_url_from_discussion($discussionentity));
808     }
809 } else if ($mformpost->is_submitted() && !$mformpost->no_submit_button_pressed() && $fromform = $mformpost->get_data()) {
811     if (empty($SESSION->fromurl)) {
812         $errordestination = $urlfactory->get_forum_view_url_from_forum($forumentity);
813     } else {
814         $errordestination = $SESSION->fromurl;
815     }
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);
823     // Clean message text.
824     $fromform = trusttext_pre_edit($fromform, 'message', $modcontext);
826     if ($fromform->edit) {
827         // Updating a post.
828         unset($fromform->groupid);
829         $fromform->id = $fromform->edit;
830         $message = '';
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                 );
839         }
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.
843             if (empty($fromform->groupinfo)) {
844                 $fromform->groupinfo = -1;
845             }
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                     );
854             }
856             if ($discussionentity->get_group_id() != $fromform->groupinfo) {
857                 $DB->set_field('forum_discussions', 'groupid', $fromform->groupinfo, array('firstpost' => $fromform->id));
858             }
859         }
861         // When editing first post/discussion.
862         if (!$postentity->has_parent()) {
863             if ($capabilitymanager->can_pin_discussions($USER)) {
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         }
871         $updatepost = $fromform;
872         $updatepost->forum = $forum->id;
873         if (!forum_update_post($updatepost, $mformpost)) {
874             print_error("couldnotupdate", "forum", $errordestination);
875         }
877         if ('single' == $forumentity->get_type() && !$postentity->has_parent()) {
878             // Updating first post of single discussion type -> updating forum intro.
879             $forum->intro = $updatepost->message;
880             $forum->timemodified = time();
881             $DB->update_record("forum", $forum);
882         }
884         if ($USER->id === $postentity->get_author_id()) {
885             $message .= get_string("postupdated", "forum");
886         } else {
887             $realuser = \core_user::get_user($postentity->get_author_id());
888             $message .= get_string("editedpostupdated", "forum", fullname($realuser));
889         }
891         $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
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);
896         } else {
897             $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
898         }
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         );
910         if ($USER->id !== $postentity->get_author_id()) {
911             $params['relateduserid'] = $postentity->get_author_id();
912         }
914         $event = \mod_forum\event\post_updated::create($params);
915         $event->add_record_snapshot('forum_discussions', $discussion);
916         $event->trigger();
918         redirect(
919             forum_go_back_to($discussionurl),
920             $message . $subscribemessage,
921             null,
922             \core\output\notification::NOTIFY_SUCCESS
923         );
925     } else if ($fromform->discussion) {
926         // Adding a new post to an existing discussion
927         // Before we add this we must check that the user will not exceed the blocking threshold.
928         forum_check_blocking_threshold($thresholdwarning);
930         unset($fromform->groupid);
931         $message = '';
932         $addpost = $fromform;
933         $addpost->forum = $forum->id;
934         if ($fromform->id = forum_add_new_post($addpost, $mformpost)) {
935             $postentity = $postvault->get_from_id($fromform->id);
936             $fromform->deleted = 0;
937             $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
939             if (!empty($fromform->mailnow)) {
940                 $message .= get_string("postmailnow", "forum");
941             } else {
942                 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
943                 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
944             }
946             if ($forum->type == 'single') {
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);
950             } else {
951                 $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
952             }
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();
968             // Update completion state.
969             $completion = new completion_info($course);
970             if ($completion->is_enabled($cm) &&
971                 ($forum->completionreplies || $forum->completionposts)) {
972                 $completion->update_state($cm, COMPLETION_COMPLETE);
973             }
975             redirect(
976                 forum_go_back_to($discussionurl),
977                 $message . $subscribemessage,
978                 null,
979                 \core\output\notification::NOTIFY_SUCCESS
980             );
982         } else {
983             print_error("couldnotadd", "forum", $errordestination);
984         }
985         exit;
987     } else {
988         // Adding a new discussion.
989         // The location to redirect to after successfully posting.
990         $redirectto = new moodle_url('/mod/forum/view.php', array('f' => $fromform->forum));
992         $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
994         $discussion = $fromform;
995         $discussion->name = $fromform->subject;
996         $discussion->timelocked = 0;
998         $newstopic = false;
999         if ($forum->type == 'news' && !$fromform->parent) {
1000             $newstopic = true;
1001         }
1003         if (!empty($fromform->pinned) && $capabilitymanager->can_pin_discussions($USER)) {
1004             $discussion->pinned = FORUM_DISCUSSION_PINNED;
1005         } else {
1006             $discussion->pinned = FORUM_DISCUSSION_UNPINNED;
1007         }
1009         $allowedgroups = array();
1010         $groupstopostto = array();
1012         // If we are posting a copy to all groups the user has access to.
1013         if (isset($fromform->posttomygroups)) {
1014             // Post to each of my groups.
1015             require_capability('mod/forum:canposttomygroups', $modcontext);
1017             // Fetch all of this user's groups.
1018             // Note: all groups are returned when in visible groups mode so we must manually filter.
1019             $allowedgroups = groups_get_activity_allowed_groups($cm);
1020             foreach ($allowedgroups as $groupid => $group) {
1021                 if ($capabilitymanager->can_create_discussions($USER, $groupid)) {
1022                     $groupstopostto[] = $groupid;
1023                 }
1024             }
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;
1036         }
1038         // Before we post this we must check that the user will not exceed the blocking threshold.
1039         forum_check_blocking_threshold($thresholdwarning);
1041         foreach ($groupstopostto as $group) {
1042             if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
1043                 print_error('cannotcreatediscussion', 'forum');
1044             }
1046             $discussion->groupid = $group;
1047             $message = '';
1048             if ($discussion->id = forum_add_discussion($discussion, $mformpost)) {
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();
1061                 if ($fromform->mailnow) {
1062                     $message .= get_string("postmailnow", "forum");
1063                 } else {
1064                     $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
1065                     $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
1066                 }
1068                 $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
1069             } else {
1070                 print_error("couldnotadd", "forum", $errordestination);
1071             }
1072         }
1074         // Update completion status.
1075         $completion = new completion_info($course);
1076         if ($completion->is_enabled($cm) &&
1077             ($forum->completiondiscussions || $forum->completionposts)) {
1078             $completion->update_state($cm, COMPLETION_COMPLETE);
1079         }
1081         // Redirect back to the discussion.
1082         redirect(
1083             forum_go_back_to($redirectto->out()),
1084             $message . $subscribemessage,
1085             null,
1086             \core\output\notification::NOTIFY_SUCCESS
1087         );
1088     }
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.
1095 if (!empty($discussionentity)) {
1096     $titlesubject = format_string($discussionentity->get_name(), true);
1097 } else if ('news' == $forumentity->get_type()) {
1098     $titlesubject = get_string("addanewtopic", "forum");
1099 } else {
1100     $titlesubject = get_string("addanewdiscussion", "forum");
1103 if (empty($post->edit)) {
1104     $post->edit = '';
1107 if (empty($discussion->name)) {
1108     if (empty($discussion)) {
1109         $discussion = new stdClass();
1110     }
1111     $discussion->name = $forum->name;
1114 $strdiscussionname = '';
1115 if ('single' == $forumentity->get_type()) {
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 = '';
1120 } else if (!empty($discussionentity)) {
1121     // Show the discussion name in the breadcrumbs.
1122     $strdiscussionname = format_string($discussionentity->get_name()) . ': ';
1125 $forcefocus = empty($reply) ? null : 'message';
1127 if (!empty($discussion->id)) {
1128     $PAGE->navbar->add($titlesubject, $urlfactory->get_discussion_view_url_from_discussion($discussionentity));
1131 if ($post->parent) {
1132     $PAGE->navbar->add(get_string('reply', 'forum'));
1135 if ($edit) {
1136     $PAGE->navbar->add(get_string('edit', 'forum'));
1139 $PAGE->set_title("{$course->shortname}: {$strdiscussionname}{$titlesubject}");
1140 $PAGE->set_heading($course->fullname);
1142 echo $OUTPUT->header();
1143 echo $OUTPUT->heading(format_string($forum->name), 2);
1145 // Checkup.
1146 if (!empty($parententity) && !$capabilitymanager->can_view_post($USER, $discussionentity, $parententity)) {
1147     print_error('cannotreply', 'forum');
1150 if (empty($parententity) && empty($edit) && !$capabilitymanager->can_create_discussions($USER, $groupid)) {
1151     print_error('cannotcreatediscussion', 'forum');
1154 if (!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     }
1162 // If there is a warning message and we are not editing a post we need to handle the warning.
1163 if (!empty($thresholdwarning) && !$edit) {
1164     // Here we want to throw an exception if they are no longer allowed to post.
1165     forum_check_blocking_threshold($thresholdwarning);
1168 if (!empty($parententity)) {
1169     $postentities = [$parententity];
1171     if (empty($post->edit)) {
1172         if ('qanda' != $forumentity->get_type() || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
1173             $replies = $postvault->get_replies_to_post(
1174                     $USER,
1175                     $parententity,
1176                     $capabilitymanager->can_view_any_private_reply($USER),
1177                     'created ASC'
1178                 );
1179             $postentities = array_merge($postentities, $replies);
1180         }
1181     }
1183     $rendererfactory = mod_forum\local\container::get_renderer_factory();
1184     $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_THREADED, true);
1185     echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
1186 } else {
1187     if (!empty($forum->intro)) {
1188         echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
1189     }
1192 // Call print disclosure for enabled plagiarism plugins.
1193 if (!empty($CFG->enableplagiarism)) {
1194     require_once($CFG->libdir.'/plagiarismlib.php');
1195     echo plagiarism_print_disclosure($cm->id);
1198 if (!empty($formheading)) {
1199     echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide'));
1202 if (!empty($postentity)) {
1203     $data = (object) [
1204         'tags' => core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $postentity->get_id())
1205     ];
1206     $mformpost->set_data($data);
1209 $mformpost->display();
1211 echo $OUTPUT->footer();