MDL-3714 mod_forum: Add an inbound message handler
[moodle.git] / mod / forum / post.php
1 <?php
3 // This file is part of Moodle - http://moodle.org/
4 //
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
18 /**
19  * Edit and save a new post to a discussion
20  *
21  * @package   mod_forum
22  * @copyright 1999 onwards Martin Dougiamas  {@link http://moodle.com}
23  * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24  */
26 require_once('../../config.php');
27 require_once('lib.php');
28 require_once($CFG->libdir.'/completionlib.php');
30 $reply   = optional_param('reply', 0, PARAM_INT);
31 $forum   = optional_param('forum', 0, PARAM_INT);
32 $edit    = optional_param('edit', 0, PARAM_INT);
33 $delete  = optional_param('delete', 0, PARAM_INT);
34 $prune   = optional_param('prune', 0, PARAM_INT);
35 $name    = optional_param('name', '', PARAM_CLEAN);
36 $confirm = optional_param('confirm', 0, PARAM_INT);
37 $groupid = optional_param('groupid', null, PARAM_INT);
39 $PAGE->set_url('/mod/forum/post.php', array(
40         'reply' => $reply,
41         'forum' => $forum,
42         'edit'  => $edit,
43         'delete'=> $delete,
44         'prune' => $prune,
45         'name'  => $name,
46         'confirm'=>$confirm,
47         'groupid'=>$groupid,
48         ));
49 //these page_params will be passed as hidden variables later in the form.
50 $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit);
52 $sitecontext = context_system::instance();
54 if (!isloggedin() or isguestuser()) {
56     if (!isloggedin() and !get_referer()) {
57         // No referer+not logged in - probably coming in via email  See MDL-9052
58         require_login();
59     }
61     if (!empty($forum)) {      // User is starting a new discussion in a forum
62         if (! $forum = $DB->get_record('forum', array('id' => $forum))) {
63             print_error('invalidforumid', 'forum');
64         }
65     } else if (!empty($reply)) {      // User is writing a new reply
66         if (! $parent = forum_get_post_full($reply)) {
67             print_error('invalidparentpostid', 'forum');
68         }
69         if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
70             print_error('notpartofdiscussion', 'forum');
71         }
72         if (! $forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
73             print_error('invalidforumid');
74         }
75     }
76     if (! $course = $DB->get_record('course', array('id' => $forum->course))) {
77         print_error('invalidcourseid');
78     }
80     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
81         print_error('invalidcoursemodule');
82     } else {
83         $modcontext = context_module::instance($cm->id);
84     }
86     $PAGE->set_cm($cm, $course, $forum);
87     $PAGE->set_context($modcontext);
88     $PAGE->set_title($course->shortname);
89     $PAGE->set_heading($course->fullname);
91     echo $OUTPUT->header();
92     echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), get_referer(false));
93     echo $OUTPUT->footer();
94     exit;
95 }
97 require_login(0, false);   // Script is useless unless they're logged in
99 if (!empty($forum)) {      // User is starting a new discussion in a forum
100     if (! $forum = $DB->get_record("forum", array("id" => $forum))) {
101         print_error('invalidforumid', 'forum');
102     }
103     if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
104         print_error('invalidcourseid');
105     }
106     if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
107         print_error("invalidcoursemodule");
108     }
110     $coursecontext = context_course::instance($course->id);
112     if (! forum_user_can_post_discussion($forum, $groupid, -1, $cm)) {
113         if (!isguestuser()) {
114             if (!is_enrolled($coursecontext)) {
115                 if (enrol_selfenrol_available($course->id)) {
116                     $SESSION->wantsurl = qualified_me();
117                     $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
118                     redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol'));
119                 }
120             }
121         }
122         print_error('nopostforum', 'forum');
123     }
125     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
126         print_error("activityiscurrentlyhidden");
127     }
129     if (isset($_SERVER["HTTP_REFERER"])) {
130         $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
131     } else {
132         $SESSION->fromurl = '';
133     }
136     // Load up the $post variable.
138     $post = new stdClass();
139     $post->course        = $course->id;
140     $post->forum         = $forum->id;
141     $post->discussion    = 0;           // ie discussion # not defined yet
142     $post->parent        = 0;
143     $post->subject       = '';
144     $post->userid        = $USER->id;
145     $post->message       = '';
146     $post->messageformat = editors_get_preferred_format();
147     $post->messagetrust  = 0;
149     if (isset($groupid)) {
150         $post->groupid = $groupid;
151     } else {
152         $post->groupid = groups_get_activity_group($cm);
153     }
155     // Unsetting this will allow the correct return URL to be calculated later.
156     unset($SESSION->fromdiscussion);
158 } else if (!empty($reply)) {      // User is writing a new reply
160     if (! $parent = forum_get_post_full($reply)) {
161         print_error('invalidparentpostid', 'forum');
162     }
163     if (! $discussion = $DB->get_record("forum_discussions", array("id" => $parent->discussion))) {
164         print_error('notpartofdiscussion', 'forum');
165     }
166     if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
167         print_error('invalidforumid', 'forum');
168     }
169     if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
170         print_error('invalidcourseid');
171     }
172     if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
173         print_error('invalidcoursemodule');
174     }
176     // Ensure lang, theme, etc. is set up properly. MDL-6926
177     $PAGE->set_cm($cm, $course, $forum);
179     $coursecontext = context_course::instance($course->id);
180     $modcontext    = context_module::instance($cm->id);
182     if (! forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext)) {
183         if (!isguestuser()) {
184             if (!is_enrolled($coursecontext)) {  // User is a guest here!
185                 $SESSION->wantsurl = qualified_me();
186                 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
187                 redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol'));
188             }
189         }
190         print_error('nopostforum', 'forum');
191     }
193     // Make sure user can post here
194     if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
195         $groupmode =  $cm->groupmode;
196     } else {
197         $groupmode = $course->groupmode;
198     }
199     if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
200         if ($discussion->groupid == -1) {
201             print_error('nopostforum', 'forum');
202         } else {
203             if (!groups_is_member($discussion->groupid)) {
204                 print_error('nopostforum', 'forum');
205             }
206         }
207     }
209     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
210         print_error("activityiscurrentlyhidden");
211     }
213     // Load up the $post variable.
215     $post = new stdClass();
216     $post->course      = $course->id;
217     $post->forum       = $forum->id;
218     $post->discussion  = $parent->discussion;
219     $post->parent      = $parent->id;
220     $post->subject     = $parent->subject;
221     $post->userid      = $USER->id;
222     $post->message     = '';
224     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
226     $strre = get_string('re', 'forum');
227     if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
228         $post->subject = $strre.' '.$post->subject;
229     }
231     // Unsetting this will allow the correct return URL to be calculated later.
232     unset($SESSION->fromdiscussion);
234 } else if (!empty($edit)) {  // User is editing their own post
236     if (! $post = forum_get_post_full($edit)) {
237         print_error('invalidpostid', 'forum');
238     }
239     if ($post->parent) {
240         if (! $parent = forum_get_post_full($post->parent)) {
241             print_error('invalidparentpostid', 'forum');
242         }
243     }
245     if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
246         print_error('notpartofdiscussion', 'forum');
247     }
248     if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
249         print_error('invalidforumid', 'forum');
250     }
251     if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
252         print_error('invalidcourseid');
253     }
254     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
255         print_error('invalidcoursemodule');
256     } else {
257         $modcontext = context_module::instance($cm->id);
258     }
260     $PAGE->set_cm($cm, $course, $forum);
262     if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
263         if (((time() - $post->created) > $CFG->maxeditingtime) and
264                     !has_capability('mod/forum:editanypost', $modcontext)) {
265             print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
266         }
267     }
268     if (($post->userid <> $USER->id) and
269                 !has_capability('mod/forum:editanypost', $modcontext)) {
270         print_error('cannoteditposts', 'forum');
271     }
274     // Load up the $post variable.
275     $post->edit   = $edit;
276     $post->course = $course->id;
277     $post->forum  = $forum->id;
278     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
280     $post = trusttext_pre_edit($post, 'message', $modcontext);
282     // Unsetting this will allow the correct return URL to be calculated later.
283     unset($SESSION->fromdiscussion);
285 }else if (!empty($delete)) {  // User is deleting a post
287     if (! $post = forum_get_post_full($delete)) {
288         print_error('invalidpostid', 'forum');
289     }
290     if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
291         print_error('notpartofdiscussion', 'forum');
292     }
293     if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
294         print_error('invalidforumid', 'forum');
295     }
296     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
297         print_error('invalidcoursemodule');
298     }
299     if (!$course = $DB->get_record('course', array('id' => $forum->course))) {
300         print_error('invalidcourseid');
301     }
303     require_login($course, false, $cm);
304     $modcontext = context_module::instance($cm->id);
306     if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
307                 || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
308         print_error('cannotdeletepost', 'forum');
309     }
312     $replycount = forum_count_replies($post);
314     if (!empty($confirm) && confirm_sesskey()) {    // User has confirmed the delete
315         //check user capability to delete post.
316         $timepassed = time() - $post->created;
317         if (($timepassed > $CFG->maxeditingtime) && !has_capability('mod/forum:deleteanypost', $modcontext)) {
318             print_error("cannotdeletepost", "forum",
319                       forum_go_back_to("discuss.php?d=$post->discussion"));
320         }
322         if ($post->totalscore) {
323             notice(get_string('couldnotdeleteratings', 'rating'),
324                     forum_go_back_to("discuss.php?d=$post->discussion"));
326         } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
327             print_error("couldnotdeletereplies", "forum",
328                     forum_go_back_to("discuss.php?d=$post->discussion"));
330         } else {
331             if (! $post->parent) {  // post is a discussion topic as well, so delete discussion
332                 if ($forum->type == 'single') {
333                     notice("Sorry, but you are not allowed to delete that discussion!",
334                             forum_go_back_to("discuss.php?d=$post->discussion"));
335                 }
336                 forum_delete_discussion($discussion, false, $course, $cm, $forum);
338                 $params = array(
339                     'objectid' => $discussion->id,
340                     'context' => $modcontext,
341                     'other' => array(
342                         'forumid' => $forum->id,
343                     )
344                 );
346                 $event = \mod_forum\event\discussion_deleted::create($params);
347                 $event->add_record_snapshot('forum_discussions', $discussion);
348                 $event->trigger();
350                 redirect("view.php?f=$discussion->forum");
352             } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext),
353                 $course, $cm, $forum)) {
355                 if ($forum->type == 'single') {
356                     // Single discussion forums are an exception. We show
357                     // the forum itself since it only has one discussion
358                     // thread.
359                     $discussionurl = "view.php?f=$forum->id";
360                 } else {
361                     $discussionurl = "discuss.php?d=$post->discussion";
362                 }
364                 $params = array(
365                     'context' => $modcontext,
366                     'objectid' => $post->id,
367                     'other' => array(
368                         'discussionid' => $discussion->id,
369                         'forumid' => $forum->id,
370                         'forumtype' => $forum->type,
371                     )
372                 );
374                 if ($post->userid !== $USER->id) {
375                     $params['relateduserid'] = $post->userid;
376                 }
377                 $event = \mod_forum\event\post_deleted::create($params);
378                 $event->add_record_snapshot('forum_posts', $post);
379                 $event->add_record_snapshot('forum_discussions', $discussion);
380                 $event->trigger();
382                 redirect(forum_go_back_to($discussionurl));
383             } else {
384                 print_error('errorwhiledelete', 'forum');
385             }
386         }
389     } else { // User just asked to delete something
391         forum_set_return();
392         $PAGE->navbar->add(get_string('delete', 'forum'));
393         $PAGE->set_title($course->shortname);
394         $PAGE->set_heading($course->fullname);
396         if ($replycount) {
397             if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
398                 print_error("couldnotdeletereplies", "forum",
399                       forum_go_back_to("discuss.php?d=$post->discussion"));
400             }
401             echo $OUTPUT->header();
402             echo $OUTPUT->heading(format_string($forum->name), 2);
403             echo $OUTPUT->confirm(get_string("deletesureplural", "forum", $replycount+1),
404                          "post.php?delete=$delete&confirm=$delete",
405                          $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
407             forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
409             if (empty($post->edit)) {
410                 $forumtracked = forum_tp_is_tracked($forum);
411                 $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
412                 forum_print_posts_nested($course, $cm, $forum, $discussion, $post, false, false, $forumtracked, $posts);
413             }
414         } else {
415             echo $OUTPUT->header();
416             echo $OUTPUT->heading(format_string($forum->name), 2);
417             echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
418                          "post.php?delete=$delete&confirm=$delete",
419                          $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
420             forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
421         }
423     }
424     echo $OUTPUT->footer();
425     die;
428 } else if (!empty($prune)) {  // Pruning
430     if (!$post = forum_get_post_full($prune)) {
431         print_error('invalidpostid', 'forum');
432     }
433     if (!$discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
434         print_error('notpartofdiscussion', 'forum');
435     }
436     if (!$forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
437         print_error('invalidforumid', 'forum');
438     }
439     if ($forum->type == 'single') {
440         print_error('cannotsplit', 'forum');
441     }
442     if (!$post->parent) {
443         print_error('alreadyfirstpost', 'forum');
444     }
445     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
446         print_error('invalidcoursemodule');
447     } else {
448         $modcontext = context_module::instance($cm->id);
449     }
450     if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
451         print_error('cannotsplit', 'forum');
452     }
454     if (!empty($name) && confirm_sesskey()) {    // User has confirmed the prune
456         $newdiscussion = new stdClass();
457         $newdiscussion->course       = $discussion->course;
458         $newdiscussion->forum        = $discussion->forum;
459         $newdiscussion->name         = $name;
460         $newdiscussion->firstpost    = $post->id;
461         $newdiscussion->userid       = $discussion->userid;
462         $newdiscussion->groupid      = $discussion->groupid;
463         $newdiscussion->assessed     = $discussion->assessed;
464         $newdiscussion->usermodified = $post->userid;
465         $newdiscussion->timestart    = $discussion->timestart;
466         $newdiscussion->timeend      = $discussion->timeend;
468         $newid = $DB->insert_record('forum_discussions', $newdiscussion);
470         $newpost = new stdClass();
471         $newpost->id      = $post->id;
472         $newpost->parent  = 0;
473         $newpost->subject = $name;
475         $DB->update_record("forum_posts", $newpost);
477         forum_change_discussionid($post->id, $newid);
479         // update last post in each discussion
480         forum_discussion_update_last_post($discussion->id);
481         forum_discussion_update_last_post($newid);
483         // Fire events to reflect the split..
484         $params = array(
485             'context' => $modcontext,
486             'objectid' => $discussion->id,
487             'other' => array(
488                 'forumid' => $forum->id,
489             )
490         );
491         $event = \mod_forum\event\discussion_updated::create($params);
492         $event->trigger();
494         $params = array(
495             'context' => $modcontext,
496             'objectid' => $newid,
497             'other' => array(
498                 'forumid' => $forum->id,
499             )
500         );
501         $event = \mod_forum\event\discussion_created::create($params);
502         $event->trigger();
504         $params = array(
505             'context' => $modcontext,
506             'objectid' => $post->id,
507             'other' => array(
508                 'discussionid' => $newid,
509                 'forumid' => $forum->id,
510                 'forumtype' => $forum->type,
511             )
512         );
513         $event = \mod_forum\event\post_updated::create($params);
514         $event->add_record_snapshot('forum_discussions', $discussion);
515         $event->trigger();
517         redirect(forum_go_back_to("discuss.php?d=$newid"));
519     } else { // User just asked to prune something
521         $course = $DB->get_record('course', array('id' => $forum->course));
523         $PAGE->set_cm($cm);
524         $PAGE->set_context($modcontext);
525         $PAGE->navbar->add(format_string($post->subject, true), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
526         $PAGE->navbar->add(get_string("prune", "forum"));
527         $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
528         $PAGE->set_heading($course->fullname);
529         echo $OUTPUT->header();
530         echo $OUTPUT->heading(format_string($forum->name), 2);
531         echo $OUTPUT->heading(get_string('pruneheading', 'forum'), 3);
532         echo '<center>';
534         include('prune.html');
536         forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
537         echo '</center>';
538     }
539     echo $OUTPUT->footer();
540     die;
541 } else {
542     print_error('unknowaction');
546 if (!isset($coursecontext)) {
547     // Has not yet been set by post.php.
548     $coursecontext = context_course::instance($forum->course);
552 // from now on user must be logged on properly
554 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
555     print_error('invalidcoursemodule');
557 $modcontext = context_module::instance($cm->id);
558 require_login($course, false, $cm);
560 if (isguestuser()) {
561     // just in case
562     print_error('noguest');
565 if (!isset($forum->maxattachments)) {  // TODO - delete this once we add a field to the forum table
566     $forum->maxattachments = 3;
569 $thresholdwarning = forum_check_throttling($forum, $cm);
570 $mform_post = new mod_forum_post_form('post.php', array('course' => $course,
571                                                         'cm' => $cm,
572                                                         'coursecontext' => $coursecontext,
573                                                         'modcontext' => $modcontext,
574                                                         'forum' => $forum,
575                                                         'post' => $post,
576                                                         'subscribe' => \mod_forum\subscriptions::is_subscribed($USER->id, $forum,
577                                                                 null, $cm),
578                                                         'thresholdwarning' => $thresholdwarning,
579                                                         'edit' => $edit), 'post', '', array('id' => 'mformforum'));
581 $draftitemid = file_get_submitted_draft_itemid('attachments');
582 file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id)?null:$post->id, mod_forum_post_form::attachment_options($forum));
584 //load data into form NOW!
586 if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end
587     $data = new stdClass();
588     $data->date = userdate($post->modified);
589     if ($post->messageformat == FORMAT_HTML) {
590         $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
591                        fullname($USER).'</a>';
592         $post->message .= '<p><span class="edited">('.get_string('editedby', 'forum', $data).')</span></p>';
593     } else {
594         $data->name = fullname($USER);
595         $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
596     }
597     unset($data);
600 $formheading = '';
601 if (!empty($parent)) {
602     $heading = get_string("yourreply", "forum");
603     $formheading = get_string('reply', 'forum');
604 } else {
605     if ($forum->type == 'qanda') {
606         $heading = get_string('yournewquestion', 'forum');
607     } else {
608         $heading = get_string('yournewtopic', 'forum');
609     }
612 $postid = empty($post->id) ? null : $post->id;
613 $draftid_editor = file_get_submitted_draft_itemid('message');
614 $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_forum', 'post', $postid, mod_forum_post_form::editor_options($modcontext, $postid), $post->message);
616 // Always suggest that the user be subscribed to a discussion that they're posting in unless they've already posted, in
617 // which case use their existing preference.
618 $discussionsubscribe = true;
619 if (isset($discussion) && forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
620     $discussionsubscribe = \mod_forum\subscriptions::is_subscribed($USER->id, $forum, $discussion->id, $cm);
622 $mform_post->set_data(array(        'attachments'=>$draftitemid,
623                                     'general'=>$heading,
624                                     'subject'=>$post->subject,
625                                     'message'=>array(
626                                         'text'=>$currenttext,
627                                         'format'=>empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat,
628                                         'itemid'=>$draftid_editor
629                                     ),
630                                     'discussionsubscribe' => $discussionsubscribe,
631                                     'mailnow'=>!empty($post->mailnow),
632                                     'userid'=>$post->userid,
633                                     'parent'=>$post->parent,
634                                     'discussion'=>$post->discussion,
635                                     'course'=>$course->id) +
636                                     $page_params +
638                             (isset($post->format)?array(
639                                     'format'=>$post->format):
640                                 array())+
642                             (isset($discussion->timestart)?array(
643                                     'timestart'=>$discussion->timestart):
644                                 array())+
646                             (isset($discussion->timeend)?array(
647                                     'timeend'=>$discussion->timeend):
648                                 array())+
650                             (isset($post->groupid)?array(
651                                     'groupid'=>$post->groupid):
652                                 array())+
654                             (isset($discussion->id)?
655                                     array('discussion'=>$discussion->id):
656                                     array()));
658 if ($fromform = $mform_post->get_data()) {
660     if (empty($SESSION->fromurl)) {
661         $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
662     } else {
663         $errordestination = $SESSION->fromurl;
664     }
666     $fromform->itemid        = $fromform->message['itemid'];
667     $fromform->messageformat = $fromform->message['format'];
668     $fromform->message       = $fromform->message['text'];
669     // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
670     $fromform->messagetrust  = trusttext_trusted($modcontext);
672     $contextcheck = isset($fromform->groupinfo) && has_capability('mod/forum:movediscussions', $modcontext);
674     if ($fromform->edit) {           // Updating a post
675         unset($fromform->groupid);
676         $fromform->id = $fromform->edit;
677         $message = '';
679         //fix for bug #4314
680         if (!$realpost = $DB->get_record('forum_posts', array('id' => $fromform->id))) {
681             $realpost = new stdClass();
682             $realpost->userid = -1;
683         }
686         // if user has edit any post capability
687         // or has either startnewdiscussion or reply capability and is editting own post
688         // then he can proceed
689         // MDL-7066
690         if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
691                             || has_capability('mod/forum:startdiscussion', $modcontext))) ||
692                             has_capability('mod/forum:editanypost', $modcontext)) ) {
693             print_error('cannotupdatepost', 'forum');
694         }
696         // If the user has access to all groups and they are changing the group, then update the post.
697         if ($contextcheck) {
698             if (empty($fromform->groupinfo)) {
699                 $fromform->groupinfo = -1;
700             }
701             $DB->set_field('forum_discussions' ,'groupid' , $fromform->groupinfo, array('firstpost' => $fromform->id));
702         }
704         $updatepost = $fromform; //realpost
705         $updatepost->forum = $forum->id;
706         if (!forum_update_post($updatepost, $mform_post, $message)) {
707             print_error("couldnotupdate", "forum", $errordestination);
708         }
710         // MDL-11818
711         if (($forum->type == 'single') && ($updatepost->parent == '0')){ // updating first post of single discussion type -> updating forum intro
712             $forum->intro = $updatepost->message;
713             $forum->timemodified = time();
714             $DB->update_record("forum", $forum);
715         }
717         $timemessage = 2;
718         if (!empty($message)) { // if we're printing stuff about the file upload
719             $timemessage = 4;
720         }
722         if ($realpost->userid == $USER->id) {
723             $message .= '<br />'.get_string("postupdated", "forum");
724         } else {
725             $realuser = $DB->get_record('user', array('id' => $realpost->userid));
726             $message .= '<br />'.get_string("editedpostupdated", "forum", fullname($realuser));
727         }
729         if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
730             $timemessage = 4;
731         }
732         if ($forum->type == 'single') {
733             // Single discussion forums are an exception. We show
734             // the forum itself since it only has one discussion
735             // thread.
736             $discussionurl = "view.php?f=$forum->id";
737         } else {
738             $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
739         }
741         $params = array(
742             'context' => $modcontext,
743             'objectid' => $fromform->id,
744             'other' => array(
745                 'discussionid' => $discussion->id,
746                 'forumid' => $forum->id,
747                 'forumtype' => $forum->type,
748             )
749         );
751         if ($realpost->userid !== $USER->id) {
752             $params['relateduserid'] = $realpost->userid;
753         }
755         $event = \mod_forum\event\post_updated::create($params);
756         $event->add_record_snapshot('forum_discussions', $discussion);
757         $event->trigger();
759         redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
761         exit;
764     } else if ($fromform->discussion) { // Adding a new post to an existing discussion
765         // Before we add this we must check that the user will not exceed the blocking threshold.
766         forum_check_blocking_threshold($thresholdwarning);
768         unset($fromform->groupid);
769         $message = '';
770         $addpost = $fromform;
771         $addpost->forum=$forum->id;
772         if ($fromform->id = forum_add_new_post($addpost, $mform_post, $message)) {
773             $timemessage = 2;
774             if (!empty($message)) { // if we're printing stuff about the file upload
775                 $timemessage = 4;
776             }
778             if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
779                 $timemessage = 4;
780             }
782             if (!empty($fromform->mailnow)) {
783                 $message .= get_string("postmailnow", "forum");
784                 $timemessage = 4;
785             } else {
786                 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
787                 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
788             }
790             if ($forum->type == 'single') {
791                 // Single discussion forums are an exception. We show
792                 // the forum itself since it only has one discussion
793                 // thread.
794                 $discussionurl = "view.php?f=$forum->id";
795             } else {
796                 $discussionurl = "discuss.php?d=$discussion->id";
797             }
799             $params = array(
800                 'context' => $modcontext,
801                 'objectid' => $fromform->id,
802                 'other' => array(
803                     'discussionid' => $discussion->id,
804                     'forumid' => $forum->id,
805                     'forumtype' => $forum->type,
806                 )
807             );
808             $event = \mod_forum\event\post_created::create($params);
809             $event->add_record_snapshot('forum_posts', $fromform);
810             $event->add_record_snapshot('forum_discussions', $discussion);
811             $event->trigger();
813             // Update completion state
814             $completion=new completion_info($course);
815             if($completion->is_enabled($cm) &&
816                 ($forum->completionreplies || $forum->completionposts)) {
817                 $completion->update_state($cm,COMPLETION_COMPLETE);
818             }
820             redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
822         } else {
823             print_error("couldnotadd", "forum", $errordestination);
824         }
825         exit;
827     } else { // Adding a new discussion.
828         // Before we add this we must check that the user will not exceed the blocking threshold.
829         forum_check_blocking_threshold($thresholdwarning);
831         if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
832             print_error('cannotcreatediscussion', 'forum');
833         }
834         // If the user has access all groups capability let them choose the group.
835         if ($contextcheck) {
836             $fromform->groupid = $fromform->groupinfo;
837         }
838         if (empty($fromform->groupid)) {
839             $fromform->groupid = -1;
840         }
842         $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
844         $discussion = $fromform;
845         $discussion->name    = $fromform->subject;
847         $newstopic = false;
848         if ($forum->type == 'news' && !$fromform->parent) {
849             $newstopic = true;
850         }
851         $discussion->timestart = $fromform->timestart;
852         $discussion->timeend = $fromform->timeend;
854         $message = '';
855         if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {
857             $params = array(
858                 'context' => $modcontext,
859                 'objectid' => $discussion->id,
860                 'other' => array(
861                     'forumid' => $forum->id,
862                 )
863             );
864             $event = \mod_forum\event\discussion_created::create($params);
865             $event->add_record_snapshot('forum_discussions', $discussion);
866             $event->trigger();
868             $timemessage = 2;
869             if (!empty($message)) { // if we're printing stuff about the file upload
870                 $timemessage = 4;
871             }
873             if ($fromform->mailnow) {
874                 $message .= get_string("postmailnow", "forum");
875                 $timemessage = 4;
876             } else {
877                 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
878                 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
879             }
881             if ($subscribemessage = forum_post_subscription($fromform, $forum, $discussion)) {
882                 $timemessage = 6;
883             }
885             // Update completion status
886             $completion=new completion_info($course);
887             if($completion->is_enabled($cm) &&
888                 ($forum->completiondiscussions || $forum->completionposts)) {
889                 $completion->update_state($cm,COMPLETION_COMPLETE);
890             }
892             redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
894         } else {
895             print_error("couldnotadd", "forum", $errordestination);
896         }
898         exit;
899     }
904 // To get here they need to edit a post, and the $post
905 // variable will be loaded with all the particulars,
906 // so bring up the form.
908 // $course, $forum are defined.  $discussion is for edit and reply only.
910 if ($post->discussion) {
911     if (! $toppost = $DB->get_record("forum_posts", array("discussion" => $post->discussion, "parent" => 0))) {
912         print_error('cannotfindparentpost', 'forum', '', $post->id);
913     }
914 } else {
915     $toppost = new stdClass();
916     $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
917                                                    get_string("addanewdiscussion", "forum");
920 if (empty($post->edit)) {
921     $post->edit = '';
924 if (empty($discussion->name)) {
925     if (empty($discussion)) {
926         $discussion = new stdClass();
927     }
928     $discussion->name = $forum->name;
930 if ($forum->type == 'single') {
931     // There is only one discussion thread for this forum type. We should
932     // not show the discussion name (same as forum name in this case) in
933     // the breadcrumbs.
934     $strdiscussionname = '';
935 } else {
936     // Show the discussion name in the breadcrumbs.
937     $strdiscussionname = format_string($discussion->name).':';
940 $forcefocus = empty($reply) ? NULL : 'message';
942 if (!empty($discussion->id)) {
943     $PAGE->navbar->add(format_string($toppost->subject, true), "discuss.php?d=$discussion->id");
946 if ($post->parent) {
947     $PAGE->navbar->add(get_string('reply', 'forum'));
950 if ($edit) {
951     $PAGE->navbar->add(get_string('edit', 'forum'));
954 $PAGE->set_title("$course->shortname: $strdiscussionname ".format_string($toppost->subject));
955 $PAGE->set_heading($course->fullname);
957 echo $OUTPUT->header();
958 echo $OUTPUT->heading(format_string($forum->name), 2);
960 // checkup
961 if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
962     print_error('cannotreply', 'forum');
964 if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
965     print_error('cannotcreatediscussion', 'forum');
968 if ($forum->type == 'qanda'
969             && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
970             && !empty($discussion->id)
971             && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
972     echo $OUTPUT->notification(get_string('qandanotify','forum'));
975 // If there is a warning message and we are not editing a post we need to handle the warning.
976 if (!empty($thresholdwarning) && !$edit) {
977     // Here we want to throw an exception if they are no longer allowed to post.
978     forum_check_blocking_threshold($thresholdwarning);
981 if (!empty($parent)) {
982     if (!$discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
983         print_error('notpartofdiscussion', 'forum');
984     }
986     forum_print_post($parent, $discussion, $forum, $cm, $course, false, false, false);
987     if (empty($post->edit)) {
988         if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
989             $forumtracked = forum_tp_is_tracked($forum);
990             $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
991             forum_print_posts_threaded($course, $cm, $forum, $discussion, $parent, 0, false, $forumtracked, $posts);
992         }
993     }
994 } else {
995     if (!empty($forum->intro)) {
996         echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
998         if (!empty($CFG->enableplagiarism)) {
999             require_once($CFG->libdir.'/plagiarismlib.php');
1000             echo plagiarism_print_disclosure($cm->id);
1001         }
1002     }
1005 if (!empty($formheading)) {
1006     echo $OUTPUT->heading($formheading, 2, array('class' => 'accesshide'));
1008 $mform_post->display();
1010 echo $OUTPUT->footer();