MDL-14589 file api improvements - converting more params to general $options array
[moodle.git] / mod / forum / post.php
1 <?php // $Id$
3 //  Edit and save a new post to a discussion
5     require_once('../../config.php');
6     require_once('lib.php');
8     $reply   = optional_param('reply', 0, PARAM_INT);
9     $forum   = optional_param('forum', 0, PARAM_INT);
10     $edit    = optional_param('edit', 0, PARAM_INT);
11     $delete  = optional_param('delete', 0, PARAM_INT);
12     $prune   = optional_param('prune', 0, PARAM_INT);
13     $name    = optional_param('name', '', PARAM_CLEAN);
14     $confirm = optional_param('confirm', 0, PARAM_INT);
15     $groupid = optional_param('groupid', null, PARAM_INT);
17     //these page_params will be passed as hidden variables later in the form.
18     $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit);
20     $sitecontext = get_context_instance(CONTEXT_SYSTEM);
22     if (has_capability('moodle/legacy:guest', $sitecontext, NULL, false)) {
24         if (!empty($forum)) {      // User is starting a new discussion in a forum
25             if (! $forum = $DB->get_record('forum', array('id' => $forum))) {
26                 print_error('invalidforumid', 'forum');
27             }
28         } else if (!empty($reply)) {      // User is writing a new reply
29             if (! $parent = forum_get_post_full($reply)) {
30                 print_error('invalidparentpostid', 'forum');
31             }
32             if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
33                 print_error('notpartofdiscussion', 'forum');
34             }
35             if (! $forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
36                 print_error('invalidforumid');
37             }
38         }
39         if (! $course = $DB->get_record('course', array('id' => $forum->course))) {
40             print_error('invalidcourseid');
41         }
43         if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
44             print_error('invalidcoursemodule');
45         } else {
46             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
47         }
49         if (!get_referer()) {   // No referer - probably coming in via email  See MDL-9052
50             require_login();
51         }
53         $navigation = build_navigation('', $cm);
54         print_header($course->shortname, $course->fullname, $navigation, '' , '', true, "", navmenu($course, $cm));
56         notice_yesno(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'),
57                      get_login_url(), get_referer(false));
58         print_footer($course);
59         exit;
60     }
62     require_login(0, false);   // Script is useless unless they're logged in
64     if (!empty($forum)) {      // User is starting a new discussion in a forum
65         if (! $forum = $DB->get_record("forum", array("id" => $forum))) {
66             print_error('invalidforumid', 'forum');
67         }
68         if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
69             print_error('invalidcourseid');
70         }
71         if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
72             print_error("invalidcoursemodule");
73         }
75         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
77         if (! forum_user_can_post_discussion($forum, $groupid, -1, $cm)) {
78             if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) {  // User is a guest here!
79                 $SESSION->wantsurl = $FULLME;
80                 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
81                 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
82             } else {
83                 print_error('nopostforum', 'forum');
84             }
85         }
87         if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
88             print_error("activityiscurrentlyhidden");
89         }
91         if (isset($_SERVER["HTTP_REFERER"])) {
92             $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
93         } else {
94             $SESSION->fromurl = '';
95         }
98         // Load up the $post variable.
100         $post = new object();
101         $post->course        = $course->id;
102         $post->forum         = $forum->id;
103         $post->discussion    = 0;           // ie discussion # not defined yet
104         $post->parent        = 0;
105         $post->subject       = '';
106         $post->userid        = $USER->id;
107         $post->message       = '';
108         $post->messageformat = FORMAT_HTML; // TODO: better default
109         $post->messagetrust  = 0;
111         if (isset($groupid)) {
112             $post->groupid = $groupid;
113         } else {
114             $post->groupid = groups_get_activity_group($cm);
115         }
117         forum_set_return();
119     } else if (!empty($reply)) {      // User is writing a new reply
121         if (! $parent = forum_get_post_full($reply)) {
122             print_error('invalidparentpostid', 'forum');
123         }
124         if (! $discussion = $DB->get_record("forum_discussions", array("id" => $parent->discussion))) {
125             print_error('notpartofdiscussion', 'forum');
126         }
127         if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
128             print_error('invalidforumid', 'forum');
129         }
130         if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
131             print_error('invalidcourseid');
132         }
133         if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
134             print_error('invalidcoursemodule');
135         }
137         // Ensure lang, theme, etc. is set up properly. MDL-6926
138         $PAGE->set_course($course);
140         $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
141         $modcontext    = get_context_instance(CONTEXT_MODULE, $cm->id);
143         if (! forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext)) {
144             if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) {  // User is a guest here!
145                 $SESSION->wantsurl = $FULLME;
146                 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
147                 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
148             } else {
149                 print_error('nopostforum', 'forum');
150             }
151         }
153         // Make sure user can post here
154         if (groupmode($course, $cm) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
155             if ($discussion->groupid == -1) {
156                 print_error('nopostforum', 'forum');
157             } else {
158                 if (!groups_is_member($discussion->groupid)) {
159                     print_error('nopostforum', 'forum');
160                 }
161             }
162         }
164         if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
165             print_error("activityiscurrentlyhidden");
166         }
168         // Load up the $post variable.
170         $post = new object();
171         $post->course      = $course->id;
172         $post->forum       = $forum->id;
173         $post->discussion  = $parent->discussion;
174         $post->parent      = $parent->id;
175         $post->subject     = $parent->subject;
176         $post->userid      = $USER->id;
177         $post->message     = '';
179         $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
181         $strre = get_string('re', 'forum');
182         if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
183             $post->subject = $strre.' '.$post->subject;
184         }
186         unset($SESSION->fromdiscussion);
188     } else if (!empty($edit)) {  // User is editing their own post
190         if (! $post = forum_get_post_full($edit)) {
191             print_error('invalidpostid', 'forum');
192         }
193         if ($post->parent) {
194             if (! $parent = forum_get_post_full($post->parent)) {
195                 print_error('invalidparentpostid', 'forum');
196             }
197         }
199         if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
200             print_error('notpartofdiscussion', 'forum');
201         }
202         if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
203             print_error('invalidforumid', 'forum');
204         }
205         if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
206             print_error('invalidcourseid');
207         }
208         if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
209             print_error('invalidcoursemodule');
210         } else {
211             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
212         }
213         if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
214             if (((time() - $post->created) > $CFG->maxeditingtime) and
215                         !has_capability('mod/forum:editanypost', $modcontext)) {
216                 print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
217             }
218         }
219         if (($post->userid <> $USER->id) and
220                     !has_capability('mod/forum:editanypost', $modcontext)) {
221             print_error('cannoteditposts', 'forum');
222         }
225         // Load up the $post variable.
226         $post->edit   = $edit;
227         $post->course = $course->id;
228         $post->forum  = $forum->id;
229         $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
231         $post = trusttext_pre_edit($post, 'message', $modcontext);
233         unset($SESSION->fromdiscussion);
236     }else if (!empty($delete)) {  // User is deleting a post
238         if (! $post = forum_get_post_full($delete)) {
239             print_error('invalidpostid', 'forum');
240         }
241         if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
242             print_error('notpartofdiscussion', 'forum');
243         }
244         if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
245             print_error('invalidforumid', 'forum');
246         }
247         if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
248             print_error('invalidcoursemodule');
249         }
250         if (!$course = $DB->get_record('course', array('id' => $forum->course))) {
251             print_error('invalidcourseid');
252         }
254         require_login($course, false, $cm);
255         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
257         if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
258                     || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
259             print_error('cannotdeletepost', 'forum');
260         }
263         $replycount = forum_count_replies($post);
265         if (!empty($confirm) && confirm_sesskey()) {    // User has confirmed the delete
267             if ($post->totalscore) {
268                 notice(get_string("couldnotdeleteratings", "forum"),
269                         forum_go_back_to("discuss.php?d=$post->discussion"));
271             } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
272                 print_error("couldnotdeletereplies", "forum",
273                         forum_go_back_to("discuss.php?d=$post->discussion"));
275             } else {
276                 if (! $post->parent) {  // post is a discussion topic as well, so delete discussion
277                     if ($forum->type == 'single') {
278                         notice("Sorry, but you are not allowed to delete that discussion!",
279                                 forum_go_back_to("discuss.php?d=$post->discussion"));
280                     }
281                     forum_delete_discussion($discussion, false, $course, $cm, $forum);
283                     add_to_log($discussion->course, "forum", "delete discussion",
284                                "view.php?id=$cm->id", "$forum->id", $cm->id);
286                     redirect("view.php?f=$discussion->forum");
288                 } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext),
289                     $course, $cm, $forum)) {
291                     if ($forum->type == 'single') {
292                         // Single discussion forums are an exception. We show
293                         // the forum itself since it only has one discussion
294                         // thread.
295                         $discussionurl = "view.php?f=$forum->id";
296                     } else {
297                         $discussionurl = "discuss.php?d=$post->discussion";
298                     }
300                     add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
302                     redirect(forum_go_back_to($discussionurl));
303                 } else {
304                     print_error('errorwhiledelete', 'forum');
305                 }
306             }
309         } else { // User just asked to delete something
311             forum_set_return();
313             if ($replycount) {
314                 if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
315                     print_error("couldnotdeletereplies", "forum",
316                           forum_go_back_to("discuss.php?d=$post->discussion"));
317                 }
318                 print_header();
319                 notice_yesno(get_string("deletesureplural", "forum", $replycount+1),
320                              "post.php?delete=$delete&amp;confirm=$delete&amp;sesskey=".sesskey(),
321                              $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
323                 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
325                 if (empty($post->edit)) {
326                     $forumtracked = forum_tp_is_tracked($forum);
327                     $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
328                     forum_print_posts_nested($course, $cm, $forum, $discussion, $post, false, false, $forumtracked, $posts);
329                 }
330             } else {
331                 print_header();
332                 notice_yesno(get_string("deletesure", "forum", $replycount),
333                              "post.php?delete=$delete&amp;confirm=$delete&amp;sesskey=".sesskey(),
334                              $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
335                 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
336             }
338         }
339         print_footer($course);
340         die;
343     } else if (!empty($prune)) {  // Pruning
345         if (!$post = forum_get_post_full($prune)) {
346             print_error('invalidpostid', 'forum');
347         }
348         if (!$discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
349             print_error('notpartofdiscussion', 'forum');
350         }
351         if (!$forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
352             print_error('invalidforumid', 'forum');
353         }
354         if ($forum->type == 'single') {
355             print_error('cannotsplit', 'forum');
356         }
357         if (!$post->parent) {
358             print_error('alreadyfirstpost', 'forum');
359         }
360         if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
361             print_error('invalidcoursemodule');
362         } else {
363             $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
364         }
365         if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
366             print_error('cannotsplit', 'forum');
367         }
369         if (!empty($name) && confirm_sesskey()) {    // User has confirmed the prune
371             $newdiscussion = new object();
372             $newdiscussion->course       = $discussion->course;
373             $newdiscussion->forum        = $discussion->forum;
374             $newdiscussion->name         = $name;
375             $newdiscussion->firstpost    = $post->id;
376             $newdiscussion->userid       = $discussion->userid;
377             $newdiscussion->groupid      = $discussion->groupid;
378             $newdiscussion->assessed     = $discussion->assessed;
379             $newdiscussion->usermodified = $post->userid;
380             $newdiscussion->timestart    = $discussion->timestart;
381             $newdiscussion->timeend      = $discussion->timeend;
383             if (!$newid = $DB->insert_record('forum_discussions', $newdiscussion)) {
384                 print_error('cannotcreatediscussion', 'forum');
385             }
387             $newpost = new object();
388             $newpost->id      = $post->id;
389             $newpost->parent  = 0;
390             $newpost->subject = $name;
392             if (!$DB->update_record("forum_posts", $newpost)) {
393                 print_error('cannotupdatediscussion', 'forum');
394             }
396             forum_change_discussionid($post->id, $newid);
398             // update last post in each discussion
399             forum_discussion_update_last_post($discussion->id);
400             forum_discussion_update_last_post($newid);
402             add_to_log($discussion->course, "forum", "prune post",
403                            "discuss.php?d=$newid", "$post->id", $cm->id);
405             redirect(forum_go_back_to("discuss.php?d=$newid"));
407         } else { // User just asked to prune something
409             $course = $DB->get_record('course', array('id' => $forum->course));
411             $navlinks = array();
412             $navlinks[] = array('name' => format_string($post->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
413             $navlinks[] = array('name' => get_string("prune", "forum"), 'link' => '', 'type' => 'title');
414             $navigation = build_navigation($navlinks, $cm);
415             print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "", $navigation, '', "", true, "", navmenu($course, $cm));
417             print_heading(get_string('pruneheading', 'forum'));
418             echo '<center>';
420             include('prune.html');
422             forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
423             echo '</center>';
424         }
425         print_footer($course);
426         die;
427     } else {
428         print_error('unknowaction');
430     }
432     if (!isset($coursecontext)) {
433         // Has not yet been set by post.php.
434         $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course);
435     }
438 // from now on user must be logged on properly
440     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
441         print_error('invalidcoursemodule');
442     }
443     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
444     require_login($course, false, $cm);
446     if (isguestuser()) {
447         // just in case
448         print_error('noguest');
449     }
451     if (!isset($forum->maxattachments)) {  // TODO - delete this once we add a field to the forum table
452         $forum->maxattachments = 3;
453     }
455     require_once('post_form.php');
457     $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
459     $draftitemid = file_get_submitted_draft_itemid('attachments');
460     file_prepare_draft_area($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id);
462     //load data into form NOW!
464     if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end
465         $data->date = userdate($post->modified);
466         if ($post->messageformat == FORMAT_HTML) {
467             $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
468                            fullname($USER).'</a>';
469             $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
470         } else {
471             $data->name = fullname($USER);
472             $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
473         }
474     }
476     if (!empty($parent)) {
477         $heading = get_string("yourreply", "forum");
478     } else {
479         if ($forum->type == 'qanda') {
480             $heading = get_string('yournewquestion', 'forum');
481         } else {
482             $heading = get_string('yournewtopic', 'forum');
483         }
484     }
486     if (forum_is_subscribed($USER->id, $forum->id)) {
487         $subscribe = true;
489     } else if (forum_user_has_posted($forum->id, 0, $USER->id)) {
490         $subscribe = false;
492     } else {
493         // user not posted yet - use subscription default specified in profile
494         $subscribe = !empty($USER->autosubscribe);
495     }
497     $draftid_editor = file_get_submitted_draft_itemid('message');
498     $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'forum_post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message);
499     $mform_post->set_data(array(        'attachments'=>$draftitemid,
500                                         'general'=>$heading,
501                                         'subject'=>$post->subject,
502                                         'message'=>array(
503                                             'text'=>$currenttext,
504                                             'format'=>empty($post->messageformat) ? FORMAT_HTML : $post->messageformat, //TODO: add some better default
505                                             'itemid'=>$draftid_editor
506                                         ),
507                                         'subscribe'=>$subscribe?1:0,
508                                         'mailnow'=>!empty($post->mailnow),
509                                         'userid'=>$post->userid,
510                                         'parent'=>$post->parent,
511                                         'discussion'=>$post->discussion,
512                                         'course'=>$course->id) +
513                                         $page_params +
515                                 (isset($post->format)?array(
516                                         'format'=>$post->format):
517                                     array())+
519                                 (isset($discussion->timestart)?array(
520                                         'timestart'=>$discussion->timestart):
521                                     array())+
523                                 (isset($discussion->timeend)?array(
524                                         'timeend'=>$discussion->timeend):
525                                     array())+
527                                 (isset($post->groupid)?array(
528                                         'groupid'=>$post->groupid):
529                                     array())+
531                                 (isset($discussion->id)?
532                                         array('discussion'=>$discussion->id):
533                                         array()));
535     if ($fromform = $mform_post->get_data()) {
537         if (empty($SESSION->fromurl)) {
538             $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
539         } else {
540             $errordestination = $SESSION->fromurl;
541         }
543         $fromform->itemid        = $fromform->message['itemid'];
544         $fromform->messageformat = $fromform->message['format'];
545         $fromform->message       = $fromform->message['text'];
546         // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
547         $fromform->messagetrust  = trusttext_trusted($modcontext);
549         if ($fromform->edit) {           // Updating a post
550             unset($fromform->groupid);
551             $fromform->id = $fromform->edit;
552             $message = '';
554             //fix for bug #4314
555             if (!$realpost = $DB->get_record('forum_posts', array('id' => $fromform->id))) {
556                 $realpost = new object;
557                 $realpost->userid = -1;
558             }
561             // if user has edit any post capability
562             // or has either startnewdiscussion or reply capability and is editting own post
563             // then he can proceed
564             // MDL-7066
565             if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
566                                 || has_capability('mod/forum:startdiscussion', $modcontext))) ||
567                                 has_capability('mod/forum:editanypost', $modcontext)) ) {
568                 print_error('cannotupdatepost', 'forum');
569             }
571             $updatepost = $fromform; //realpost
572             $updatepost->forum = $forum->id;
573             if (!forum_update_post($updatepost, $mform_post, $message)) {
574                 print_error("couldnotupdate", "forum", $errordestination);
575             }
577             // MDL-11818
578             if (($forum->type == 'single') && ($updatepost->parent == '0')){ // updating first post of single discussion type -> updating forum intro
579                 $forum->intro = $updatepost->message;
580                 $forum->timemodified = time();
581                 if (!$DB->update_record("forum", $forum)) {
582                     print_error("couldnotupdate", "forum", $errordestination);
583                 }
584             }
586             $timemessage = 2;
587             if (!empty($message)) { // if we're printing stuff about the file upload
588                 $timemessage = 4;
589             }
590             $message .= '<br />'.get_string("postupdated", "forum");
592             if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
593                 $timemessage = 4;
594             }
595             if ($forum->type == 'single') {
596                 // Single discussion forums are an exception. We show
597                 // the forum itself since it only has one discussion
598                 // thread.
599                 $discussionurl = "view.php?f=$forum->id";
600             } else {
601                 $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
602             }
603             add_to_log($course->id, "forum", "update post",
604                     "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
606             redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
608             exit;
611         } else if ($fromform->discussion) { // Adding a new post to an existing discussion
612             unset($fromform->groupid);
613             $message = '';
614             $addpost = $fromform;
615             $addpost->forum=$forum->id;
616             if ($fromform->id = forum_add_new_post($addpost, $mform_post, $message)) {
618                 $timemessage = 2;
619                 if (!empty($message)) { // if we're printing stuff about the file upload
620                     $timemessage = 4;
621                 }
623                 if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
624                     $timemessage = 4;
625                 }
627                 if (!empty($fromform->mailnow)) {
628                     $message .= get_string("postmailnow", "forum");
629                     $timemessage = 4;
630                 } else {
631                     $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
632                     $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
633                 }
635                 if ($forum->type == 'single') {
636                     // Single discussion forums are an exception. We show
637                     // the forum itself since it only has one discussion
638                     // thread.
639                     $discussionurl = "view.php?f=$forum->id";
640                 } else {
641                     $discussionurl = "discuss.php?d=$discussion->id";
642                 }
643                 add_to_log($course->id, "forum", "add post",
644                           "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
646                 // Update completion state
647                 $completion=new completion_info($course);
648                 if($completion->is_enabled($cm) &&
649                     ($forum->completionreplies || $forum->completionposts)) {
650                     $completion->update_state($cm,COMPLETION_COMPLETE);
651                 }
653                 redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
655             } else {
656                 print_error("couldnotadd", "forum", $errordestination);
657             }
658             exit;
660         } else {                     // Adding a new discussion
661             if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
662                 print_error('cannotcreatediscussion', 'forum');
663             }
664             if (empty($fromform->groupid)) {
665                 $fromform->groupid = -1;
666             }
668             $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
669             $discussion = $fromform;
670             $discussion->name  = $fromform->subject;
671             $discussion->intro = $fromform->message;
672             $newstopic = false;
674             if ($forum->type == 'news' && !$fromform->parent) {
675                 $newstopic = true;
676             }
677             $discussion->timestart = $fromform->timestart;
678             $discussion->timeend = $fromform->timeend;
680             $message = '';
681             if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {
683                 add_to_log($course->id, "forum", "add discussion",
684                         "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
686                 $timemessage = 2;
687                 if (!empty($message)) { // if we're printing stuff about the file upload
688                     $timemessage = 4;
689                 }
691                 if ($fromform->mailnow) {
692                     $message .= get_string("postmailnow", "forum");
693                     $timemessage = 4;
694                 } else {
695                     $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
696                     $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
697                 }
699                 if ($subscribemessage = forum_post_subscription($discussion, $forum)) {
700                     $timemessage = 4;
701                 }
703                 // Update completion status
704                 $completion=new completion_info($course);
705                 if($completion->is_enabled($cm) &&
706                     ($forum->completiondiscussions || $forum->completionposts)) {
707                     $completion->update_state($cm,COMPLETION_COMPLETE);
708                 }
710                 redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
712             } else {
713                 print_error("couldnotadd", "forum", $errordestination);
714             }
716             exit;
717         }
718     }
722     // To get here they need to edit a post, and the $post
723     // variable will be loaded with all the particulars,
724     // so bring up the form.
726     // $course, $forum are defined.  $discussion is for edit and reply only.
728     if ($post->discussion) {
729         if (! $toppost = $DB->get_record("forum_posts", array("discussion" => $post->discussion, "parent" => 0))) {
730             print_error('cannotfindparentpost', 'forum', '', $post->id);
731         }
732     } else {
733         $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
734                                                        get_string("addanewdiscussion", "forum");
735     }
737     if (empty($post->edit)) {
738         $post->edit = '';
739     }
741     if (empty($discussion->name)) {
742         if (empty($discussion)) {
743             $discussion = new object;
744         }
745         $discussion->name = $forum->name;
746     }
747     if ($forum->type == 'single') {
748         // There is only one discussion thread for this forum type. We should
749         // not show the discussion name (same as forum name in this case) in
750         // the breadcrumbs.
751         $strdiscussionname = '';
752     } else {
753         // Show the discussion name in the breadcrumbs.
754         $strdiscussionname = format_string($discussion->name).':';
755     }
757     $forcefocus = empty($reply) ? NULL : 'message';
759     $navlinks = array();
760     if ($post->parent) {
761         $navlinks[] = array('name' => format_string($toppost->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
762         $navlinks[] = array('name' => get_string('editing', 'forum'), 'link' => '', 'type' => 'title');
763     } else {
764         $navlinks[] = array('name' => format_string($toppost->subject), 'link' => '', 'type' => 'title');
765     }
766     $navigation = build_navigation($navlinks, $cm);
768     print_header("$course->shortname: $strdiscussionname ".
769                   format_string($toppost->subject), $course->fullname,
770                   $navigation, $mform_post->focus($forcefocus), "", true, "", navmenu($course, $cm));
772 // checkup
773     if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
774         print_error('cannotreply', 'forum');
775     }
776     if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
777         print_error('cannotcreatediscussion', 'forum');
778     }
780     if ($forum->type == 'qanda'
781                 && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
782                 && !empty($discussion->id)
783                 && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
784         notify(get_string('qandanotify','forum'));
785     }
787     forum_check_throttling($forum, $cm);
789     if (!empty($parent)) {
790         if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
791             print_error('notpartofdiscussion', 'forum');
792         }
794         forum_print_post($parent, $discussion, $forum, $cm, $course, false, false, false);
795         if (empty($post->edit)) {
796             if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
797                 $forumtracked = forum_tp_is_tracked($forum);
798                 $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
799                 forum_print_posts_threaded($course, $cm, $forum, $discussion, $parent, 0, false, false, $forumtracked, $posts);
800             }
801         }
802     } else {
803         if (!empty($forum->intro)) {
804             print_box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
805         }
806     }
808     $mform_post->display();
810     print_footer($course);
813 ?>