Merge branch 'MDL-64715-master-self-starred-pr-ryan' of git://github.com/sarjona...
[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);
38 $PAGE->set_url('/mod/forum/post.php', array(
39     'reply' => $reply,
40     'forum' => $forum,
41     'edit'  => $edit,
42     'delete' => $delete,
43     'prune' => $prune,
44     'name'  => $name,
45     'confirm' => $confirm,
46     'groupid' => $groupid,
47 ));
48 // These page_params will be passed as hidden variables later in the form.
49 $pageparams = array('reply' => $reply, 'forum' => $forum, 'edit' => $edit);
51 $sitecontext = context_system::instance();
53 $entityfactory = mod_forum\local\container::get_entity_factory();
54 $vaultfactory = mod_forum\local\container::get_vault_factory();
55 $managerfactory = mod_forum\local\container::get_manager_factory();
56 $legacydatamapperfactory = mod_forum\local\container::get_legacy_data_mapper_factory();
57 $urlfactory = mod_forum\local\container::get_url_factory();
59 $forumvault = $vaultfactory->get_forum_vault();
60 $forumdatamapper = $legacydatamapperfactory->get_forum_data_mapper();
62 $discussionvault = $vaultfactory->get_discussion_vault();
63 $discussiondatamapper = $legacydatamapperfactory->get_discussion_data_mapper();
65 $postvault = $vaultfactory->get_post_vault();
66 $postdatamapper = $legacydatamapperfactory->get_post_data_mapper();
68 if (!isloggedin() or isguestuser()) {
69     if (!isloggedin() and !get_local_referer()) {
70         // No referer+not logged in - probably coming in via email  See MDL-9052.
71         require_login();
72     }
74     if (!empty($forum)) {
75         // User is starting a new discussion in a forum.
76         $forumentity = $forumvault->get_from_id($forum);
77         if (empty($forumentity)) {
78             print_error('invalidforumid', 'forum');
79         }
80     } else if (!empty($reply)) {
81         // User is writing a new reply.
82         $forumentity = $forumvault->get_from_post_id($reply);
83         if (empty($forumentity)) {
84             print_error('invalidparentpostid', 'forum');
85         }
86     }
88     $forum = $forumdatamapper->to_legacy_object($forumentity);
89     $modcontext = $forumentity->get_context();
90     $course = $forumentity->get_course_record();
91     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
92         print_error("invalidcoursemodule");
93     }
95     $PAGE->set_cm($cm, $course, $forum);
96     $PAGE->set_context($modcontext);
97     $PAGE->set_title($course->shortname);
98     $PAGE->set_heading($course->fullname);
99     $referer = get_local_referer(false);
101     echo $OUTPUT->header();
102     echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), $referer);
103     echo $OUTPUT->footer();
104     exit;
107 require_login(0, false);   // Script is useless unless they're logged in.
109 $canreplyprivately = false;
111 if (!empty($forum)) {
112     // User is starting a new discussion in a forum.
113     $forumentity = $forumvault->get_from_id($forum);
114     if (empty($forumentity)) {
115         print_error('invalidforumid', 'forum');
116     }
118     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
119     $forum = $forumdatamapper->to_legacy_object($forumentity);
120     $course = $forumentity->get_course_record();
121     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
122         print_error("invalidcoursemodule");
123     }
125     // Retrieve the contexts.
126     $modcontext = $forumentity->get_context();
127     $coursecontext = context_course::instance($course->id);
129     if ($forumentity->is_in_group_mode() && null === $groupid) {
130         $groupid = groups_get_activity_group($cm);
131     }
133     if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
134         if (!isguestuser()) {
135             if (!is_enrolled($coursecontext)) {
136                 if (enrol_selfenrol_available($course->id)) {
137                     $SESSION->wantsurl = qualified_me();
138                     $SESSION->enrolcancel = get_local_referer(false);
139                     redirect(new moodle_url('/enrol/index.php', array('id' => $course->id,
140                         'returnurl' => '/mod/forum/view.php?f=' . $forum->id)),
141                         get_string('youneedtoenrol'));
142                 }
143             }
144         }
145         print_error('nopostforum', 'forum');
146     }
148     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
149         redirect(
150                 $urlfactory->get_course_url_from_forum($forumentity),
151                 get_string('activityiscurrentlyhidden'),
152                 null,
153                 \core\output\notice::NOTIFY_ERROR
154             );
155     }
157     $SESSION->fromurl = get_local_referer(false);
159     // Load up the $post variable.
161     $post = new stdClass();
162     $post->course        = $course->id;
163     $post->forum         = $forum->id;
164     $post->discussion    = 0;           // Ie discussion # not defined yet.
165     $post->parent        = 0;
166     $post->subject       = '';
167     $post->userid        = $USER->id;
168     $post->message       = '';
169     $post->messageformat = editors_get_preferred_format();
170     $post->messagetrust  = 0;
171     $post->groupid = $groupid;
173     // Unsetting this will allow the correct return URL to be calculated later.
174     unset($SESSION->fromdiscussion);
176 } else if (!empty($reply)) {
177     // User is writing a new reply.
179     $parententity = $postvault->get_from_id($reply);
180     if (empty($parententity)) {
181         print_error('invalidparentpostid', 'forum');
182     }
184     $discussionentity = $discussionvault->get_from_id($parententity->get_discussion_id());
185     if (empty($discussionentity)) {
186         print_error('notpartofdiscussion', 'forum');
187     }
189     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
190     if (empty($forumentity)) {
191         print_error('invalidforumid', 'forum');
192     }
194     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
195     $parent = $postdatamapper->to_legacy_object($parententity);
196     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
197     $forum = $forumdatamapper->to_legacy_object($forumentity);
198     $course = $forumentity->get_course_record();
199     $modcontext = $forumentity->get_context();
200     $coursecontext = context_course::instance($course->id);
202     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
203         print_error('invalidcoursemodule');
204     }
206     // Ensure lang, theme, etc. is set up properly. MDL-6926.
207     $PAGE->set_cm($cm, $course, $forum);
209     if (!$capabilitymanager->can_reply_to_post($USER, $discussionentity, $parententity)) {
210         if (!isguestuser()) {
211             if (!is_enrolled($coursecontext)) {  // User is a guest here!
212                 $SESSION->wantsurl = qualified_me();
213                 $SESSION->enrolcancel = get_local_referer(false);
214                 redirect(new moodle_url('/enrol/index.php', array('id' => $course->id,
215                     'returnurl' => '/mod/forum/view.php?f=' . $forum->id)),
216                     get_string('youneedtoenrol'));
217             }
218         }
219         print_error('nopostforum', 'forum');
220     }
222     // Make sure user can post here.
223     if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
224         $groupmode = $cm->groupmode;
225     } else {
226         $groupmode = $course->groupmode;
227     }
228     if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
229         if ($discussion->groupid == -1) {
230             print_error('nopostforum', 'forum');
231         } else {
232             if (!groups_is_member($discussion->groupid)) {
233                 print_error('nopostforum', 'forum');
234             }
235         }
236     }
238     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $modcontext)) {
239         print_error("activityiscurrentlyhidden");
240     }
242     if ($parententity->is_private_reply()) {
243         print_error('cannotreplytoprivatereply', 'forum');
244     }
246     // Load up the $post variable.
247     $post = new stdClass();
248     $post->course      = $course->id;
249     $post->forum       = $forum->id;
250     $post->discussion  = $parent->discussion;
251     $post->parent      = $parent->id;
252     $post->subject     = $parent->subject;
253     $post->userid      = $USER->id;
254     $post->message     = '';
255     $post->parentpostauthor = $parent->userid;
256     $canreplyprivately = $capabilitymanager->can_reply_privately_to_post($USER, $parententity);
258     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
260     $strre = get_string('re', 'forum');
261     if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
262         $post->subject = $strre.' '.$post->subject;
263     }
265     // Unsetting this will allow the correct return URL to be calculated later.
266     unset($SESSION->fromdiscussion);
268 } else if (!empty($edit)) {
269     // User is editing their own post.
271     $postentity = $postvault->get_from_id($edit);
272     if (empty($postentity)) {
273         print_error('invalidpostid', 'forum');
274     }
275     if ($postentity->has_parent()) {
276         $parententity = $postvault->get_from_id($postentity->get_parent_id());
277         $parent = $postdatamapper->to_legacy_object($parententity);
278     }
280     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
281     if (empty($discussionentity)) {
282         print_error('notpartofdiscussion', 'forum');
283     }
285     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
286     if (empty($forumentity)) {
287         print_error('invalidforumid', 'forum');
288     }
290     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
291     $post = $postdatamapper->to_legacy_object($postentity);
292     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
293     $forum = $forumdatamapper->to_legacy_object($forumentity);
294     $course = $forumentity->get_course_record();
295     $modcontext = $forumentity->get_context();
296     $coursecontext = context_course::instance($course->id);
298     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
299         print_error('invalidcoursemodule');
300     }
302     $PAGE->set_cm($cm, $course, $forum);
304     if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
305         if (((time() - $post->created) > $CFG->maxeditingtime) and
306             !has_capability('mod/forum:editanypost', $modcontext)) {
307             print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
308         }
309     }
310     if (($post->userid <> $USER->id) and
311         !has_capability('mod/forum:editanypost', $modcontext)) {
312         print_error('cannoteditposts', 'forum');
313     }
315     // Load up the $post variable.
316     $post->edit   = $edit;
317     $post->course = $course->id;
318     $post->forum  = $forum->id;
319     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
320     if ($postentity->has_parent()) {
321         $canreplyprivately = forum_user_can_reply_privately($modcontext, $parent);
322     }
324     $post = trusttext_pre_edit($post, 'message', $modcontext);
326     // Unsetting this will allow the correct return URL to be calculated later.
327     unset($SESSION->fromdiscussion);
329 } else if (!empty($delete)) {
330     // User is deleting a post.
332     $postentity = $postvault->get_from_id($delete);
333     if (empty($postentity)) {
334         print_error('invalidpostid', 'forum');
335     }
337     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
338     if (empty($discussionentity)) {
339         print_error('notpartofdiscussion', 'forum');
340     }
342     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
343     if (empty($forumentity)) {
344         print_error('invalidforumid', 'forum');
345     }
347     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
348     $post = $postdatamapper->to_legacy_object($postentity);
349     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
350     $forum = $forumdatamapper->to_legacy_object($forumentity);
351     $course = $forumentity->get_course_record();
352     $modcontext = $forumentity->get_context();
353     $coursecontext = context_course::instance($course->id);
355     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
356         print_error('invalidcoursemodule');
357     }
359     require_login($course, false, $cm);
361     if (!$capabilitymanager->can_delete_post($USER, $discussionentity, $postentity)) {
362         redirect(
363                 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
364                 get_string('cannotdeletepost', 'forum'),
365                 null,
366                 \core\output\notice::NOTIFY_ERROR
367             );
368     }
370     $replycount = $postvault->get_reply_count_for_post_id_in_discussion_id(
371         $USER, $postentity->get_id(), $discussionentity->get_id(), true);
373     if (!empty($confirm) && confirm_sesskey()) {
374         // User has confirmed the delete.
375         // Check user capability to delete post.
376         $timepassed = time() - $post->created;
377         if ($post->totalscore) {
378             redirect(
379                     $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
380                     get_string('couldnotdeleteratings', 'rating'),
381                     null,
382                     \core\output\notice::NOTIFY_ERROR
383                 );
384         } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
385             redirect(
386                     $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
387                     get_string('couldnotdeletereplies', 'rating'),
388                     null,
389                     \core\output\notice::NOTIFY_ERROR
390                 );
391         } else {
392             if (!$postentity->has_parent()) {
393                 // Post is a discussion topic as well, so delete discussion.
394                 if ($forum->type == 'single') {
395                     redirect(
396                             $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
397                             get_string('cannotdeletediscussioninsinglediscussion', 'rating'),
398                             null,
399                             \core\output\notice::NOTIFY_ERROR
400                         );
401                 }
402                 forum_delete_discussion($discussion, false, $course, $cm, $forum);
404                 $params = array(
405                     'objectid' => $discussion->id,
406                     'context' => $modcontext,
407                     'other' => array(
408                         'forumid' => $forum->id,
409                     )
410                 );
412                 $event = \mod_forum\event\discussion_deleted::create($params);
413                 $event->add_record_snapshot('forum_discussions', $discussion);
414                 $event->trigger();
416                 redirect(
417                     $urlfactory->get_forum_view_url_from_forum($forumentity),
418                     get_string('eventdiscussiondeleted', 'forum'),
419                     null,
420                     \core\output\notification::NOTIFY_SUCCESS
421                 );
423             } else {
424                 $deleted = forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext), $course, $cm, $forum);
426                 if (!$deleted) {
427                     redirect(
428                             $urlfactory->get_discussion_view_url_from_post($postentity),
429                             get_string('errorwhiledelete', 'forum'),
430                             null,
431                             \core\output\notice::NOTIFY_ERROR
432                         );
433                 }
435                 if ($forum->type == 'single') {
436                     // Single discussion forums are an exception.
437                     // We show the forum itself since it only has one discussion thread.
438                     $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
439                 } else {
440                     $discussionurl = $urlfactory->get_discussion_view_url_from_discussion($discussionentity);
441                 }
443                 redirect(
444                     forum_go_back_to($discussionurl),
445                     get_string('eventpostdeleted', 'forum'),
446                     null,
447                     \core\output\notification::NOTIFY_SUCCESS
448                 );
449             }
450         }
453     } else {
454         // User just asked to delete something.
455         forum_set_return();
456         $PAGE->navbar->add(get_string('delete', 'forum'));
457         $PAGE->set_title($course->shortname);
458         $PAGE->set_heading($course->fullname);
460         if ($replycount) {
461             if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
462                 redirect(
463                         forum_go_back_to($urlfactory->get_view_post_url_from_post($postentity)),
464                         get_string('couldnotdeletereplies', 'rating'),
465                         null,
466                         \core\output\notice::NOTIFY_ERROR
467                     );
468             }
470             echo $OUTPUT->header();
471             echo $OUTPUT->heading(format_string($forum->name), 2);
472             echo $OUTPUT->confirm(get_string("deletesureplural", "forum", $replycount + 1),
473                 "post.php?delete=$delete&confirm=$delete",
474                 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
476             $postentities = [$postentity];
477             if (empty($post->edit)) {
478                 $postvault = $vaultfactory->get_post_vault();
479                 $replies = $postvault->get_replies_to_post(
480                         $USER,
481                         $postentity,
482                         // Note: All replies are fetched here as the user has deleteanypost.
483                         true,
484                         'created ASC'
485                     );
486                 $postentities = array_merge($postentities, $replies);
487             }
489             $rendererfactory = mod_forum\local\container::get_renderer_factory();
490             $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_NESTED, true);
491             echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
492         } else {
493             echo $OUTPUT->header();
494             echo $OUTPUT->heading(format_string($forum->name), 2);
495             echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
496                 "post.php?delete=$delete&confirm=$delete",
497                 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
499             $rendererfactory = mod_forum\local\container::get_renderer_factory();
500             $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
501             echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
502         }
504     }
505     echo $OUTPUT->footer();
506     die;
508 } else if (!empty($prune)) {
509     // Pruning.
511     $postentity = $postvault->get_from_id($prune);
512     if (empty($postentity)) {
513         print_error('invalidpostid', 'forum');
514     }
516     $discussionentity = $discussionvault->get_from_id($postentity->get_discussion_id());
517     if (empty($discussionentity)) {
518         print_error('notpartofdiscussion', 'forum');
519     }
521     $forumentity = $forumvault->get_from_id($discussionentity->get_forum_id());
522     if (empty($forumentity)) {
523         print_error('invalidforumid', 'forum');
524     }
526     $capabilitymanager = $managerfactory->get_capability_manager($forumentity);
527     $post = $postdatamapper->to_legacy_object($postentity);
528     $discussion = $discussiondatamapper->to_legacy_object($discussionentity);
529     $forum = $forumdatamapper->to_legacy_object($forumentity);
530     $course = $forumentity->get_course_record();
531     $modcontext = $forumentity->get_context();
532     $coursecontext = context_course::instance($course->id);
534     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
535         print_error('invalidcoursemodule');
536     }
538     if (!$postentity->has_parent()) {
539         redirect(
540                 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
541                 get_string('alreadyfirstpost', 'forum'),
542                 null,
543                 \core\output\notification::NOTIFY_ERROR
544             );
545     }
546     if (!$capabilitymanager->can_split_post($USER, $discussionentity, $postentity)) {
547         redirect(
548                 $urlfactory->get_discussion_view_url_from_discussion($discussionentity),
549                 get_string('cannotsplit', 'forum'),
550                 null,
551                 \core\output\notification::NOTIFY_ERROR
552             );
553     }
555     $PAGE->set_cm($cm);
556     $PAGE->set_context($modcontext);
558     $prunemform = new mod_forum_prune_form(null, array('prune' => $prune, 'confirm' => $prune));
560     if ($prunemform->is_cancelled()) {
561         redirect(forum_go_back_to($urlfactory->get_discussion_view_url_from_discussion($discussionentity)));
562     } else if ($fromform = $prunemform->get_data()) {
563         // User submits the data.
564         $newdiscussion = new stdClass();
565         $newdiscussion->course       = $discussion->course;
566         $newdiscussion->forum        = $discussion->forum;
567         $newdiscussion->name         = $name;
568         $newdiscussion->firstpost    = $post->id;
569         $newdiscussion->userid       = $discussion->userid;
570         $newdiscussion->groupid      = $discussion->groupid;
571         $newdiscussion->assessed     = $discussion->assessed;
572         $newdiscussion->usermodified = $post->userid;
573         $newdiscussion->timestart    = $discussion->timestart;
574         $newdiscussion->timeend      = $discussion->timeend;
576         $newid = $DB->insert_record('forum_discussions', $newdiscussion);
578         $newpost = new stdClass();
579         $newpost->id      = $post->id;
580         $newpost->parent  = 0;
581         $newpost->subject = $name;
583         $DB->update_record("forum_posts", $newpost);
584         $postentity = $postvault->get_from_id($postentity->get_id());
586         forum_change_discussionid($post->id, $newid);
588         // Update last post in each discussion.
589         forum_discussion_update_last_post($discussion->id);
590         forum_discussion_update_last_post($newid);
592         // Fire events to reflect the split..
593         $params = array(
594             'context' => $modcontext,
595             'objectid' => $discussion->id,
596             'other' => array(
597                 'forumid' => $forum->id,
598             )
599         );
600         $event = \mod_forum\event\discussion_updated::create($params);
601         $event->trigger();
603         $params = array(
604             'context' => $modcontext,
605             'objectid' => $newid,
606             'other' => array(
607                 'forumid' => $forum->id,
608             )
609         );
610         $event = \mod_forum\event\discussion_created::create($params);
611         $event->trigger();
613         $params = array(
614             'context' => $modcontext,
615             'objectid' => $post->id,
616             'other' => array(
617                 'discussionid' => $newid,
618                 'forumid' => $forum->id,
619                 'forumtype' => $forum->type,
620             )
621         );
622         $event = \mod_forum\event\post_updated::create($params);
623         $event->add_record_snapshot('forum_discussions', $discussion);
624         $event->trigger();
626         redirect(
627             forum_go_back_to($urlfactory->get_discussion_view_url_from_post($postentity)),
628             get_string('discussionsplit', 'forum'),
629             null,
630             \core\output\notification::NOTIFY_SUCCESS
631         );
632     } else {
633         // Display the prune form.
634         $course = $DB->get_record('course', array('id' => $forum->course));
635         $subjectstr = format_string($post->subject, true);
636         $PAGE->navbar->add($subjectstr, new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
637         $PAGE->navbar->add(get_string("prune", "forum"));
638         $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
639         $PAGE->set_heading($course->fullname);
640         echo $OUTPUT->header();
641         echo $OUTPUT->heading(format_string($forum->name), 2);
642         echo $OUTPUT->heading(get_string('pruneheading', 'forum'), 3);
644         $prunemform->display();
646         $postentity = $entityfactory->get_post_from_stdclass($post);
647         $discussionentity = $entityfactory->get_discussion_from_stdclass($discussion);
648         $forumentity = $entityfactory->get_forum_from_stdclass($forum, $modcontext, $cm, $course);
649         $rendererfactory = mod_forum\local\container::get_renderer_factory();
650         $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(null, true);
651         echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], [$postentity]);
652     }
654     echo $OUTPUT->footer();
655     die;
656 } else {
657     print_error('unknowaction');
661 // From now on user must be logged on properly.
663 require_login($course, false, $cm);
665 if (isguestuser()) {
666     // Just in case.
667     print_error('noguest');
670 $thresholdwarning = forum_check_throttling($forum, $cm);
671 $mformpost = new mod_forum_post_form('post.php', [
672         'course' => $course,
673         'cm' => $cm,
674         'coursecontext' => $coursecontext,
675         'modcontext' => $modcontext,
676         'forum' => $forum,
677         'post' => $post,
678         'subscribe' => \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm),
679         'thresholdwarning' => $thresholdwarning,
680         'edit' => $edit,
681         'canreplyprivately' => $canreplyprivately,
682     ], 'post', '', array('id' => 'mformforum'));
684 $draftitemid = file_get_submitted_draft_itemid('attachments');
685 $postid = empty($post->id) ? null : $post->id;
686 $attachoptions = mod_forum_post_form::attachment_options($forum);
687 file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', $postid, $attachoptions);
689 // Load data into form NOW!
691 if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end.
692     $data = new stdClass();
693     $data->date = userdate($post->created);
694     if ($post->messageformat == FORMAT_HTML) {
695         $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
696             fullname($USER).'</a>';
697         $post->message .= '<p><span class="edited">('.get_string('editedby', 'forum', $data).')</span></p>';
698     } else {
699         $data->name = fullname($USER);
700         $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
701     }
702     unset($data);
705 $formheading = '';
706 if (!empty($parent)) {
707     $heading = get_string("yourreply", "forum");
708     $formheading = get_string('reply', 'forum');
709 } else {
710     if ($forum->type == 'qanda') {
711         $heading = get_string('yournewquestion', 'forum');
712     } else {
713         $heading = get_string('yournewtopic', 'forum');
714     }
717 $postid = empty($post->id) ? null : $post->id;
718 $draftideditor = file_get_submitted_draft_itemid('message');
719 $editoropts = mod_forum_post_form::editor_options($modcontext, $postid);
720 $currenttext = file_prepare_draft_area($draftideditor, $modcontext->id, 'mod_forum', 'post', $postid, $editoropts, $post->message);
722 $manageactivities = has_capability('moodle/course:manageactivities', $coursecontext);
723 if (\mod_forum\subscriptions::subscription_disabled($forum) && !$manageactivities) {
724     // User does not have permission to subscribe to this discussion at all.
725     $discussionsubscribe = false;
726 } else if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
727     // User does not have permission to unsubscribe from this discussion at all.
728     $discussionsubscribe = true;
729 } else {
730     if (isset($discussion) && \mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id, $cm)) {
731         // User is subscribed to the discussion - continue the subscription.
732         $discussionsubscribe = true;
733     } else if (!isset($discussion) && \mod_forum\subscriptions::is_subscribed($USER->id, $forum, null, $cm)) {
734         // Starting a new discussion, and the user is subscribed to the forum - subscribe to the discussion.
735         $discussionsubscribe = true;
736     } else {
737         // User is not subscribed to either forum or discussion. Follow user preference.
738         $discussionsubscribe = $USER->autosubscribe;
739     }
742 $mformpost->set_data(
743     array(
744         'attachments' => $draftitemid,
745         'general' => $heading,
746         'subject' => $post->subject,
747         'message' => array(
748             'text' => $currenttext,
749             'format' => empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat,
750             'itemid' => $draftideditor
751         ),
752         'discussionsubscribe' => $discussionsubscribe,
753         'mailnow' => !empty($post->mailnow),
754         'userid' => $post->userid,
755         'parent' => $post->parent,
756         'discussion' => $post->discussion,
757         'course' => $course->id
758     ) +
760     $pageparams +
762     (isset($post->format) ? array('format' => $post->format) : array()) +
764     (isset($discussion->timestart) ? array('timestart' => $discussion->timestart) : array()) +
766     (isset($discussion->timeend) ? array('timeend' => $discussion->timeend) : array()) +
768     (isset($discussion->pinned) ? array('pinned' => $discussion->pinned) : array()) +
770     (isset($post->groupid) ? array('groupid' => $post->groupid) : array()) +
772     (isset($discussion->id) ? array('discussion' => $discussion->id) : array())
773 );
775 if ($mformpost->is_cancelled()) {
776     if (!isset($discussion->id) || $forum->type === 'single') {
777         // Single forums don't have a discussion page.
778         redirect($urlfactory->get_forum_view_url_from_forum($forumentity));
779     } else {
780         redirect($urlfactory->get_discussion_view_url_from_discussion($discussionentity));
781     }
782 } else if ($fromform = $mformpost->get_data()) {
784     if (empty($SESSION->fromurl)) {
785         $errordestination = $urlfactory->get_forum_view_url_from_forum($forumentity);
786     } else {
787         $errordestination = $SESSION->fromurl;
788     }
790     $fromform->itemid        = $fromform->message['itemid'];
791     $fromform->messageformat = $fromform->message['format'];
792     $fromform->message       = $fromform->message['text'];
793     // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
794     $fromform->messagetrust  = trusttext_trusted($modcontext);
796     // Clean message text.
797     $fromform = trusttext_pre_edit($fromform, 'message', $modcontext);
799     if ($fromform->edit) {
800         // Updating a post.
801         unset($fromform->groupid);
802         $fromform->id = $fromform->edit;
803         $message = '';
805         if (!$capabilitymanager->can_edit_post($USER, $discussionentity, $postentity)) {
806             redirect(
807                     $urlfactory->get_view_post_url_from_post($postentity),
808                     get_string('cannotupdatepost', 'forum'),
809                     null,
810                     \core\output\notification::ERROR
811                 );
812         }
814         if (isset($fromform->groupinfo) && $capabilitymanager->can_move_discussions($USER)) {
815             // If the user has access to all groups and they are changing the group, then update the post.
816             if (empty($fromform->groupinfo)) {
817                 $fromform->groupinfo = -1;
818             }
820             if (!$capabilitymanager->can_create_discussions($USER, $fromform->groupinfo)) {
821                 redirect(
822                         $urlfactory->get_view_post_url_from_post($postentity),
823                         get_string('cannotupdatepost', 'forum'),
824                         null,
825                         \core\output\notification::ERROR
826                     );
827             }
829             if ($discussionentity->get_group_id() != $fromform->groupinfo) {
830                 $DB->set_field('forum_discussions', 'groupid', $fromform->groupinfo, array('firstpost' => $fromform->id));
831             }
832         }
834         // When editing first post/discussion.
835         if ($postentity->has_parent()) {
836             if ($capabilitymanager->can_pin_discussions($USER)) {
837                 // Can change pinned if we have capability.
838                 $fromform->pinned = !empty($fromform->pinned) ? FORUM_DISCUSSION_PINNED : FORUM_DISCUSSION_UNPINNED;
839             } else {
840                 // We don't have the capability to change so keep to previous value.
841                 unset($fromform->pinned);
842             }
843         }
844         $updatepost = $fromform;
845         $updatepost->forum = $forum->id;
846         if (!forum_update_post($updatepost, $mformpost)) {
847             print_error("couldnotupdate", "forum", $errordestination);
848         }
850         if ('single' == $forumentity->get_type() && !$postentity->has_parent()) {
851             // Updating first post of single discussion type -> updating forum intro.
852             $forum->intro = $updatepost->message;
853             $forum->timemodified = time();
854             $DB->update_record("forum", $forum);
855         }
857         if ($USER->id === $postentity->get_author_id()) {
858             $message .= get_string("postupdated", "forum");
859         } else {
860             $realuser = \core_user::get_user($postentity->get_author_id());
861             $message .= get_string("editedpostupdated", "forum", fullname($realuser));
862         }
864         $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
865         if ('single' == $forumentity->get_type()) {
866             // Single discussion forums are an exception.
867             // We show the forum itself since it only has one discussion thread.
868             $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
869         } else {
870             $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
871         }
873         $params = array(
874             'context' => $modcontext,
875             'objectid' => $fromform->id,
876             'other' => array(
877                 'discussionid' => $discussion->id,
878                 'forumid' => $forum->id,
879                 'forumtype' => $forum->type,
880             )
881         );
883         if ($USER->id !== $postentity->get_author_id()) {
884             $params['relateduserid'] = $postentity->get_author_id();
885         }
887         $event = \mod_forum\event\post_updated::create($params);
888         $event->add_record_snapshot('forum_discussions', $discussion);
889         $event->trigger();
891         redirect(
892             forum_go_back_to($discussionurl),
893             $message . $subscribemessage,
894             null,
895             \core\output\notification::NOTIFY_SUCCESS
896         );
898     } else if ($fromform->discussion) {
899         // Adding a new post to an existing discussion
900         // Before we add this we must check that the user will not exceed the blocking threshold.
901         forum_check_blocking_threshold($thresholdwarning);
903         unset($fromform->groupid);
904         $message = '';
905         $addpost = $fromform;
906         $addpost->forum = $forum->id;
907         if ($fromform->id = forum_add_new_post($addpost, $mformpost)) {
908             $postentity = $postvault->get_from_id($fromform->id);
909             $fromform->deleted = 0;
910             $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
912             if (!empty($fromform->mailnow)) {
913                 $message .= get_string("postmailnow", "forum");
914             } else {
915                 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
916                 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
917             }
919             if ($forum->type == 'single') {
920                 // Single discussion forums are an exception.
921                 // We show the forum itself since it only has one discussion thread.
922                 $discussionurl = $urlfactory->get_forum_view_url_from_forum($forumentity);
923             } else {
924                 $discussionurl = $urlfactory->get_view_post_url_from_post($postentity);
925             }
927             $params = array(
928                 'context' => $modcontext,
929                 'objectid' => $fromform->id,
930                 'other' => array(
931                     'discussionid' => $discussion->id,
932                     'forumid' => $forum->id,
933                     'forumtype' => $forum->type,
934                 )
935             );
936             $event = \mod_forum\event\post_created::create($params);
937             $event->add_record_snapshot('forum_posts', $fromform);
938             $event->add_record_snapshot('forum_discussions', $discussion);
939             $event->trigger();
941             // Update completion state.
942             $completion = new completion_info($course);
943             if ($completion->is_enabled($cm) &&
944                 ($forum->completionreplies || $forum->completionposts)) {
945                 $completion->update_state($cm, COMPLETION_COMPLETE);
946             }
948             redirect(
949                 forum_go_back_to($discussionurl),
950                 $message . $subscribemessage,
951                 null,
952                 \core\output\notification::NOTIFY_SUCCESS
953             );
955         } else {
956             print_error("couldnotadd", "forum", $errordestination);
957         }
958         exit;
960     } else {
961         // Adding a new discussion.
962         // The location to redirect to after successfully posting.
963         $redirectto = new moodle_url('/mod/forum/view.php', array('f' => $fromform->forum));
965         $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
967         $discussion = $fromform;
968         $discussion->name = $fromform->subject;
970         $newstopic = false;
971         if ($forum->type == 'news' && !$fromform->parent) {
972             $newstopic = true;
973         }
974         $discussion->timestart = $fromform->timestart;
975         $discussion->timeend = $fromform->timeend;
976         $discussion->pinned = FORUM_DISCUSSION_UNPINNED;
978         if (!empty($fromform->pinned) && $capabilitymanager->can_pin_discussions($USER)) {
979             $discussion->pinned = FORUM_DISCUSSION_PINNED;
980         }
982         $allowedgroups = array();
983         $groupstopostto = array();
985         // If we are posting a copy to all groups the user has access to.
986         if (isset($fromform->posttomygroups)) {
987             // Post to each of my groups.
988             require_capability('mod/forum:canposttomygroups', $modcontext);
990             // Fetch all of this user's groups.
991             // Note: all groups are returned when in visible groups mode so we must manually filter.
992             $allowedgroups = groups_get_activity_allowed_groups($cm);
993             foreach ($allowedgroups as $groupid => $group) {
994                 if ($capabilitymanager->can_create_discussions($USER, $groupid)) {
995                     $groupstopostto[] = $groupid;
996                 }
997             }
998         } else if (isset($fromform->groupinfo)) {
999             // Use the value provided in the dropdown group selection.
1000             $groupstopostto[] = $fromform->groupinfo;
1001             $redirectto->param('group', $fromform->groupinfo);
1002         } else if (isset($fromform->groupid) && !empty($fromform->groupid)) {
1003             // Use the value provided in the hidden form element instead.
1004             $groupstopostto[] = $fromform->groupid;
1005             $redirectto->param('group', $fromform->groupid);
1006         } else {
1007             // Use the value for all participants instead.
1008             $groupstopostto[] = -1;
1009         }
1011         // Before we post this we must check that the user will not exceed the blocking threshold.
1012         forum_check_blocking_threshold($thresholdwarning);
1014         foreach ($groupstopostto as $group) {
1015             if (!$capabilitymanager->can_create_discussions($USER, $groupid)) {
1016                 print_error('cannotcreatediscussion', 'forum');
1017             }
1019             $discussion->groupid = $group;
1020             $message = '';
1021             if ($discussion->id = forum_add_discussion($discussion, $mformpost)) {
1023                 $params = array(
1024                     'context' => $modcontext,
1025                     'objectid' => $discussion->id,
1026                     'other' => array(
1027                         'forumid' => $forum->id,
1028                     )
1029                 );
1030                 $event = \mod_forum\event\discussion_created::create($params);
1031                 $event->add_record_snapshot('forum_discussions', $discussion);
1032                 $event->trigger();
1034                 if ($fromform->mailnow) {
1035                     $message .= get_string("postmailnow", "forum");
1036                 } else {
1037                     $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
1038                     $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
1039                 }
1041                 $subscribemessage = forum_post_subscription($fromform, $forum, $discussion);
1042             } else {
1043                 print_error("couldnotadd", "forum", $errordestination);
1044             }
1045         }
1047         // Update completion status.
1048         $completion = new completion_info($course);
1049         if ($completion->is_enabled($cm) &&
1050             ($forum->completiondiscussions || $forum->completionposts)) {
1051             $completion->update_state($cm, COMPLETION_COMPLETE);
1052         }
1054         // Redirect back to the discussion.
1055         redirect(
1056             forum_go_back_to($redirectto->out()),
1057             $message . $subscribemessage,
1058             null,
1059             \core\output\notification::NOTIFY_SUCCESS
1060         );
1061     }
1065 // This section is only shown after all checks are in place, and the forumentity and any relevant discussion and post
1066 // entity are available.
1068 if (!empty($discussionentity)) {
1069     $titlesubject = format_string($discussionentity->get_name(), true);
1070 } else if ('news' == $forumentity->get_type()) {
1071     $titlesubject = get_string("addanewtopic", "forum");
1072 } else {
1073     $titlesubject = get_string("addanewdiscussion", "forum");
1076 if (empty($post->edit)) {
1077     $post->edit = '';
1080 if (empty($discussion->name)) {
1081     if (empty($discussion)) {
1082         $discussion = new stdClass();
1083     }
1084     $discussion->name = $forum->name;
1087 $strdiscussionname = '';
1088 if ('single' == $forumentity->get_type()) {
1089     // There is only one discussion thread for this forum type. We should
1090     // not show the discussion name (same as forum name in this case) in
1091     // the breadcrumbs.
1092     $strdiscussionname = '';
1093 } else if (!empty($discussionentity)) {
1094     // Show the discussion name in the breadcrumbs.
1095     $strdiscussionname = format_string($discussionentity->get_name()) . ': ';
1098 $forcefocus = empty($reply) ? null : 'message';
1100 if (!empty($discussion->id)) {
1101     $PAGE->navbar->add($titlesubject, $urlfactory->get_discussion_view_url_from_discussion($discussionentity));
1104 if ($post->parent) {
1105     $PAGE->navbar->add(get_string('reply', 'forum'));
1108 if ($edit) {
1109     $PAGE->navbar->add(get_string('edit', 'forum'));
1112 $PAGE->set_title("{$course->shortname}: {$strdiscussionname}{$titlesubject}");
1113 $PAGE->set_heading($course->fullname);
1115 echo $OUTPUT->header();
1116 echo $OUTPUT->heading(format_string($forum->name), 2);
1118 // Checkup.
1119 if (!empty($parententity) && !$capabilitymanager->can_view_post($USER, $discussionentity, $parententity)) {
1120     print_error('cannotreply', 'forum');
1123 if (empty($parententity) && empty($edit) && !$capabilitymanager->can_create_discussions($USER, $groupid)) {
1124     print_error('cannotcreatediscussion', 'forum');
1127 if (!empty($discussionentity) && 'qanda' == $forumentity->get_type()) {
1128     $displaywarning = $capabilitymanager->must_post_before_viewing_discussion($USER, $discussionentity);
1129     $displaywarning = $displaywarning && !forum_user_has_posted($forumentity->get_id(), $discussionentity->get_id(), $USER->id);
1130     if ($displaywarning) {
1131         echo $OUTPUT->notification(get_string('qandanotify', 'forum'));
1132     }
1135 // If there is a warning message and we are not editing a post we need to handle the warning.
1136 if (!empty($thresholdwarning) && !$edit) {
1137     // Here we want to throw an exception if they are no longer allowed to post.
1138     forum_check_blocking_threshold($thresholdwarning);
1141 if (!empty($parententity)) {
1142     $postentities = [$parententity];
1144     if (empty($post->edit)) {
1145         if ('qanda' != $forumentity->get_type() || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
1146             $replies = $postvault->get_replies_to_post(
1147                     $USER,
1148                     $parententity,
1149                     $capabilitymanager->can_view_any_private_reply($USER),
1150                     'created ASC'
1151                 );
1152             $postentities = array_merge($postentities, $replies);
1153         }
1154     }
1156     $rendererfactory = mod_forum\local\container::get_renderer_factory();
1157     $postsrenderer = $rendererfactory->get_single_discussion_posts_renderer(FORUM_MODE_THREADED, true);
1158     echo $postsrenderer->render($USER, [$forumentity], [$discussionentity], $postentities);
1159 } else {
1160     if (!empty($forum->intro)) {
1161         echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
1162     }
1165 // Call print disclosure for enabled plagiarism plugins.
1166 if (!empty($CFG->enableplagiarism)) {
1167     require_once($CFG->libdir.'/plagiarismlib.php');
1168     echo plagiarism_print_disclosure($cm->id);
1171 if (!empty($formheading)) {
1172     echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide'));
1175 if (!empty($postentity)) {
1176     $data = (object) [
1177         'tags' => core_tag_tag::get_item_tags_array('mod_forum', 'forum_posts', $postentity->get_id())
1178     ];
1179     $mformpost->set_data($data);
1182 $mformpost->display();
1184 echo $OUTPUT->footer();