MDL-11517 reserved word MOD used in table alias in questions backup code
[moodle-pu.git] / mod / forum / post.php
blobacc9a4f67c584aa041ca6869bc357e4488ce75c2
1 <?php // $Id$
3 // Edit and save a new post to a discussion
5 require_once('../../config.php');
6 require_once('lib.php');
7 require_once('post_form.php');
9 $reply = optional_param('reply', 0, PARAM_INT);
10 $forum = optional_param('forum', 0, PARAM_INT);
11 $edit = optional_param('edit', 0, PARAM_INT);
12 $delete = optional_param('delete', 0, PARAM_INT);
13 $prune = optional_param('prune', 0, PARAM_INT);
14 $name = optional_param('name', '', PARAM_CLEAN);
15 $confirm = optional_param('confirm', 0, PARAM_INT);
18 //these page_params will be passed as hidden variables later in the form.
19 $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit);
21 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
23 if (has_capability('moodle/legacy:guest', $sitecontext, NULL, false)) {
25 $wwwroot = $CFG->wwwroot.'/login/index.php';
26 if (!empty($CFG->loginhttps)) {
27 $wwwroot = str_replace('http:', 'https:', $wwwroot);
30 if (!empty($forum)) { // User is starting a new discussion in a forum
31 if (! $forum = get_record('forum', 'id', $forum)) {
32 error('The forum number was incorrect');
34 } else if (!empty($reply)) { // User is writing a new reply
35 if (! $parent = forum_get_post_full($reply)) {
36 error('Parent post ID was incorrect');
38 if (! $discussion = get_record('forum_discussions', 'id', $parent->discussion)) {
39 error('This post is not part of a discussion!');
41 if (! $forum = get_record('forum', 'id', $discussion->forum)) {
42 error('The forum number was incorrect');
45 if (! $course = get_record('course', 'id', $forum->course)) {
46 error('The course number was incorrect');
49 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
50 error('Could not get the course module for the forum instance.');
51 } else {
52 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
55 $strforums = get_string('modulenameplural', 'forum');
57 if (!get_referer()) { // No referer - probably coming in via email See MDL-9052
58 require_login();
61 $navlinks = array();
62 $navlinks[] = array('name' => get_string("forums", "forum"), 'link' => "../forum/view.php?f=$forum->id", 'type' => 'activity');
63 $navlinks[] = array('name' => format_string($forum->name,true), 'link' => '../forum/index.php?id=$course->id', 'type' => 'activityinstance');
65 $navigation = build_navigation($navlinks);
68 print_header($course->shortname, $course->fullname, $navigation, '' , '', true, "", navmenu($course, $cm));
70 notice_yesno(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'),
71 $wwwroot, get_referer(false));
72 print_footer($course);
73 exit;
76 require_login(0, false); // Script is useless unless they're logged in
78 if (!empty($forum)) { // User is starting a new discussion in a forum
79 if (! $forum = get_record("forum", "id", $forum)) {
80 error("The forum number was incorrect ($forum)");
82 if (! $course = get_record("course", "id", $forum->course)) {
83 error("The course number was incorrect ($forum->course)");
85 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
86 if (! forum_user_can_post_discussion($forum)) {
87 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
88 $SESSION->wantsurl = $FULLME;
89 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
90 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
91 } else {
92 print_error('nopostforum', 'forum');
96 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
97 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
98 error(get_string("activityiscurrentlyhidden"));
102 if (isset($_SERVER["HTTP_REFERER"])) {
103 $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
104 } else {
105 $SESSION->fromurl = '';
109 // Load up the $post variable.
111 $post = new object();
112 $post->course = $course->id;
113 $post->forum = $forum->id;
114 $post->discussion = 0; // ie discussion # not defined yet
115 $post->parent = 0;
116 $post->subject = '';
117 $post->userid = $USER->id;
118 $post->message = '';
120 if ($groupmode = groups_get_activity_groupmode($cm)) {
121 $post->groupid = groups_get_activity_group($cm);
122 if (empty($post->groupid)) {
123 $post->groupid = -1; //TODO: why -1??
125 } else {
126 $post->groupid = -1; //TODO: why -1??
128 forum_set_return();
130 } else if (!empty($reply)) { // User is writing a new reply
132 if (! $parent = forum_get_post_full($reply)) {
133 error("Parent post ID was incorrect");
135 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
136 error("This post is not part of a discussion!");
138 if (! $forum = get_record("forum", "id", $discussion->forum)) {
139 error("The forum number was incorrect ($discussion->forum)");
141 if (! $course = get_record("course", "id", $discussion->course)) {
142 error("The course number was incorrect ($discussion->course)");
145 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
147 if (! forum_user_can_post($forum)) {
148 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
149 $SESSION->wantsurl = $FULLME;
150 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
151 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
152 } else {
153 print_error('nopostforum', 'forum');
157 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
158 if (groupmode($course, $cm)) { // Make sure user can post here
159 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
160 $mygroupid = mygroupid($course->id);
161 if (!((empty($mygroupid) and $discussion->groupid == -1)
162 || (groups_is_member($discussion->groupid)/*$mygroupid == $discussion->groupid*/)
163 || has_capability('moodle/site:accessallgroups', $modcontext, NULL, false) )) {
164 print_error('nopostdiscussion', 'forum');
167 if (!$cm->visible and !has_capability('moodle/course:manageactivities', $coursecontext)) {
168 error(get_string("activityiscurrentlyhidden"));
172 // Load up the $post variable.
174 $post = new object();
175 $post->course = $course->id;
176 $post->forum = $forum->id;
177 $post->discussion = $parent->discussion;
178 $post->parent = $parent->id;
179 $post->subject = $parent->subject;
180 $post->userid = $USER->id;
181 $post->message = '';
183 $strre = get_string('re', 'forum');
184 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
185 $post->subject = $strre.' '.$post->subject;
188 unset($SESSION->fromdiscussion);
190 } else if (!empty($edit)) { // User is editing their own post
192 if (! $post = forum_get_post_full($edit)) {
193 error("Post ID was incorrect");
195 if ($post->parent) {
196 if (! $parent = forum_get_post_full($post->parent)) {
197 error("Parent post ID was incorrect ($post->parent)");
201 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
202 error("This post is not part of a discussion! ($edit)");
204 if (! $forum = get_record("forum", "id", $discussion->forum)) {
205 error("The forum number was incorrect ($discussion->forum)");
207 if (! $course = get_record("course", "id", $discussion->course)) {
208 error("The course number was incorrect ($discussion->course)");
210 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
211 error('Could not get the course module for the forum instance.');
212 } else {
213 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
215 if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
216 if (((time() - $post->created) > $CFG->maxeditingtime) and
217 !has_capability('mod/forum:editanypost', $modcontext)) {
218 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
221 if (($post->userid <> $USER->id) and
222 !has_capability('mod/forum:editanypost', $modcontext)) {
223 error("You can't edit other people's posts!");
227 // Load up the $post variable.
228 $post->edit = $edit;
229 $post->course = $course->id;
230 $post->forum = $forum->id;
232 trusttext_prepare_edit($post->message, $post->format, can_use_html_editor(), $modcontext);
234 unset($SESSION->fromdiscussion);
237 }else if (!empty($delete)) { // User is deleting a post
239 if (! $post = forum_get_post_full($delete)) {
240 error("Post ID was incorrect");
242 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
243 error("This post is not part of a discussion!");
245 if (! $forum = get_record("forum", "id", $discussion->forum)) {
246 error("The forum number was incorrect ($discussion->forum)");
248 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
249 error('Could not get the course module for the forum instance.');
251 if (!$course = get_record('course', 'id', $forum->course)) {
252 error('Incorrect course');
255 require_login($course, false, $cm);
256 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
258 if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
259 || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
260 error("You can't delete this post!");
264 $replycount = forum_count_replies($post);
266 if (!empty($confirm)) { // User has confirmed the delete
268 if ($post->totalscore) {
269 notice(get_string("couldnotdeleteratings", "forum"),
270 forum_go_back_to("discuss.php?d=$post->discussion"));
272 } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
273 error(get_string("couldnotdeletereplies", "forum"),
274 forum_go_back_to("discuss.php?d=$post->discussion"));
276 } else {
277 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
278 if ($forum->type == 'single') {
279 notice("Sorry, but you are not allowed to delete that discussion!",
280 forum_go_back_to("discuss.php?d=$post->discussion"));
282 forum_delete_discussion($discussion);
284 add_to_log($discussion->course, "forum", "delete discussion",
285 "view.php?id=$cm->id", "$forum->id", $cm->id);
287 redirect("view.php?f=$discussion->forum");
289 } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext))) {
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";
300 add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
302 redirect(forum_go_back_to($discussionurl));
303 } else {
304 error("An error occurred while deleting record $post->id");
309 } else { // User just asked to delete something
311 forum_set_return();
313 if ($replycount) {
314 if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
315 error(get_string("couldnotdeletereplies", "forum"),
316 forum_go_back_to("discuss.php?d=$post->discussion"));
318 print_header();
319 notice_yesno(get_string("deletesureplural", "forum", $replycount+1),
320 "post.php?delete=$delete&amp;confirm=$delete",
321 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
323 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false);
324 if (empty($post->edit)) {
325 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
326 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
327 } else {
328 $user_read_array = array();
330 forum_print_posts_nested($post->id, $course->id, false, false, $user_read_array, $forum->id);
332 } else {
333 print_header();
334 notice_yesno(get_string("deletesure", "forum", $replycount),
335 "post.php?delete=$delete&amp;confirm=$delete",
336 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
337 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
341 print_footer($course);
342 die;
345 } else if (!empty($prune)) { // Pruning
347 if (!$post = forum_get_post_full($prune)) {
348 error("Post ID was incorrect");
350 if (!$discussion = get_record("forum_discussions", "id", $post->discussion)) {
351 error("This post is not part of a discussion!");
353 if (!$forum = get_record("forum", "id", $discussion->forum)) {
354 error("The forum number was incorrect ($discussion->forum)");
356 if ($forum->type == 'single') {
357 error('Discussions from this forum cannot be split');
359 if (!$post->parent) {
360 error('This is already the first post in the discussion');
362 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
363 error('Could not get the course module for the forum instance.');
364 } else {
365 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
367 if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
368 error("You can't split discussions!");
371 if (!empty($name)) { // User has confirmed the prune
373 $newdiscussion = new object();
374 $newdiscussion->course = $discussion->course;
375 $newdiscussion->forum = $discussion->forum;
376 $newdiscussion->name = $name;
377 $newdiscussion->firstpost = $post->id;
378 $newdiscussion->userid = $discussion->userid;
379 $newdiscussion->groupid = $discussion->groupid;
380 $newdiscussion->assessed = $discussion->assessed;
381 $newdiscussion->usermodified = $post->userid;
382 $newdiscussion->timestart = $discussion->timestart;
383 $newdiscussion->timeend = $discussion->timeend;
385 if (!$newid = insert_record('forum_discussions', $newdiscussion)) {
386 error('Could not create new discussion');
389 $newpost = new object();
390 $newpost->id = $post->id;
391 $newpost->parent = 0;
392 $newpost->subject = $name;
394 if (!update_record("forum_posts", $newpost)) {
395 error('Could not update the original post');
398 forum_change_discussionid($post->id, $newid);
400 // update last post in each discussion
401 forum_discussion_update_last_post($discussion->id);
402 forum_discussion_update_last_post($newid);
404 add_to_log($discussion->course, "forum", "prune post",
405 "discuss.php?d=$newid", "$post->id", $cm->id);
407 redirect(forum_go_back_to("discuss.php?d=$newid"));
409 } else { // User just asked to prune something
411 $course = get_record('course', 'id', $forum->course);
412 $strforums = get_string("modulenameplural", "forum");
414 $navlinks = array();
415 $navlinks[] = array('name' => $strforums, 'link' => "../forum/index.php?id=$course->id", 'type' => 'activity');
416 $navlinks[] = array('name' => $forum->name, 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
417 $navlinks[] = array('name' => format_string($post->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
418 $navlinks[] = array('name' => get_string("prune", "forum"), 'link' => '', 'type' => 'title');
420 $navigation = build_navigation($navlinks);
422 print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "", $navigation, '', "", true, "", navmenu($course, $cm));
424 print_heading(get_string('pruneheading', 'forum'));
425 echo '<center>';
427 include('prune.html');
429 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
430 echo '</center>';
432 print_footer($course);
433 die;
434 } else {
435 error("No operation specified");
439 if (!isset($coursecontext)) {
440 // Has not yet been set by post.php.
441 $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course);
444 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
445 error('Could not get the course module for the forum instance.');
447 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
449 $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
451 if ($fromform = $mform_post->get_data()) {
454 require_login($course, false, $cm);
456 if (empty($SESSION->fromurl)) {
457 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
458 } else {
459 $errordestination = $SESSION->fromurl;
462 // TODO add attachment processing
463 //$fromform->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
465 trusttext_after_edit($fromform->message, $modcontext);
467 if ($fromform->edit) { // Updating a post
468 $fromform->id = $fromform->edit;
469 $message = '';
471 //fix for bug #4314
472 if (!$realpost = get_record('forum_posts', 'id', $fromform->id)) {
473 $realpost = new object;
474 $realpost->userid = -1;
478 // if user has edit any post capability
479 // or has either startnewdiscussion or reply capability and is editting own post
480 // then he can proceed
481 // MDL-7066
482 if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
483 || has_capability('mod/forum:startdiscussion', $modcontext))) ||
484 has_capability('mod/forum:editanypost', $modcontext)) ) {
485 error("You can not update this post");
488 if ($forum->type == 'news' && !$fromform->parent) {
489 $updatediscussion = new object;
490 $updatediscussion->id = $fromform->discussion;
491 $updatediscussion->timestart = $fromform->timestart;
492 $updatediscussion->timeend = $fromform->timeend;
493 if (!update_record('forum_discussions', $updatediscussion)) {
494 error(get_string("couldnotupdate", "forum"), $errordestination);
498 $updatepost=$fromform; //realpost
499 $updatepost->forum=$forum->id;
500 if (!forum_update_post($updatepost, $message)) {
501 error(get_string("couldnotupdate", "forum"), $errordestination);
504 $timemessage = 2;
505 if (!empty($message)) { // if we're printing stuff about the file upload
506 $timemessage = 4;
508 $message .= '<br />'.get_string("postupdated", "forum");
510 if ($subscribemessage = forum_post_subscription($fromform)) {
511 $timemessage = 4;
513 if ($forum->type == 'single') {
514 // Single discussion forums are an exception. We show
515 // the forum itself since it only has one discussion
516 // thread.
517 $discussionurl = "view.php?f=$forum->id";
518 } else {
519 $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
521 add_to_log($course->id, "forum", "update post",
522 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
524 redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
526 exit;
529 } else if ($fromform->discussion) { // Adding a new post to an existing discussion
530 $message = '';
531 $addpost=$fromform;
532 $addpost->forum=$forum->id;
533 if ($fromform->id = forum_add_new_post($addpost, $message)) {
535 $timemessage = 2;
536 if (!empty($message)) { // if we're printing stuff about the file upload
537 $timemessage = 4;
540 if ($subscribemessage = forum_post_subscription($fromform)) {
541 $timemessage = 4;
544 if (!empty($fromform->mailnow)) {
545 $message .= get_string("postmailnow", "forum");
546 $timemessage = 4;
547 } else {
548 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
551 if ($forum->type == 'single') {
552 // Single discussion forums are an exception. We show
553 // the forum itself since it only has one discussion
554 // thread.
555 $discussionurl = "view.php?f=$forum->id";
556 } else {
557 $discussionurl = "discuss.php?d=$discussion->id";
559 add_to_log($course->id, "forum", "add post",
560 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
562 redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
564 } else {
565 error(get_string("couldnotadd", "forum"), $errordestination);
567 exit;
569 } else { // Adding a new discussion
570 $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
571 $discussion = $fromform;
572 $discussion->name = $fromform->subject;
573 $discussion->intro = $fromform->message;
574 $newstopic = false;
576 if ($forum->type == 'news' && !$fromform->parent) {
577 $newstopic = true;
579 $discussion->timestart = $fromform->timestart;
580 $discussion->timeend = $fromform->timeend;
582 $message = '';
583 if ($discussion->id = forum_add_discussion($discussion, $message)) {
585 add_to_log($course->id, "forum", "add discussion",
586 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
588 $timemessage = 2;
589 if (!empty($message)) { // if we're printing stuff about the file upload
590 $timemessage = 4;
593 if ($fromform->mailnow) {
594 $message .= get_string("postmailnow", "forum");
595 $timemessage = 4;
596 } else {
597 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
600 if ($subscribemessage = forum_post_subscription($discussion)) {
601 $timemessage = 4;
604 redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
606 } else {
607 error(get_string("couldnotadd", "forum"), $errordestination);
610 exit;
616 // To get here they need to edit a post, and the $post
617 // variable will be loaded with all the particulars,
618 // so bring up the form.
620 // $course, $forum are defined. $discussion is for edit and reply only.
622 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
624 require_login($course->id, false, $cm);
626 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
628 if ($post->discussion) {
629 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
630 error("Could not find top parent of post $post->id");
632 } else {
633 $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
634 get_string("addanewdiscussion", "forum");
637 $strforums = get_string("modulenameplural", "forum");
639 $navlinks = array();
640 $navlinks[] = array('name' => $strforums, 'link' => "../forum/index.php?id=$course->id", 'type' => 'activity');
641 $navlinks[] = array('name' => $forum->name, 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
644 if ($post->parent) {
645 $navlinks[] = array('name' => format_string($toppost->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'activityinstance');
646 $navlinks[] = array('name' => get_string('editing', 'forum'), 'link' => '', 'type' => 'action');
647 } else {
648 $navlinks[] = array('name' => format_string($toppost->subject), 'link' => '', 'type' => 'action');
651 if (empty($post->edit)) {
652 $post->edit = '';
656 if (empty($discussion->name)) {
657 if (empty($discussion)) {
658 $discussion = new object;
660 $discussion->name = $forum->name;
662 if ($forum->type == 'single') {
663 // There is only one discussion thread for this forum type. We should
664 // not show the discussion name (same as forum name in this case) in
665 // the breadcrumbs.
666 $strdiscussionname = '';
667 $navtail = '';
668 } else {
669 // Show the discussion name in the breadcrumbs.
670 $strdiscussionname = format_string($discussion->name).':';
673 $forcefocus = empty($reply) ? NULL : 'message';
676 $navigation = build_navigation($navlinks);
678 print_header("$course->shortname: $strdiscussionname ".
679 format_string($toppost->subject), $course->fullname,
680 $navigation, $mform_post->focus($forcefocus), "", true, "", navmenu($course, $cm));
683 // checkup
684 if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post)) {
685 error("You cannot reply to this post");
687 if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum)) {
688 error("You cannot start a new discussion in this forum");
691 if ($forum->type == 'qanda'
692 && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
693 && !empty($discussion->id)
694 && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
695 notify(get_string('qandanotify','forum'));
698 forum_check_throttling($forum);
700 if (!empty($parent)) {
701 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
702 if (empty($post->edit)) {
703 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
704 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
705 } else {
706 $user_read_array = array();
708 if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
709 forum_print_posts_threaded($parent->id, $course->id, 0, false, false, $user_read_array, $discussion->forum);
712 $heading = get_string("yourreply", "forum");
713 } else {
714 $forum->intro = trim($forum->intro);
715 if (!empty($forum->intro)) {
716 print_box(format_text($forum->intro), 'generalbox', 'intro');
718 if ($forum->type == 'qanda') {
719 $heading = get_string('yournewquestion', 'forum');
720 } else {
721 $heading = get_string('yournewtopic', 'forum');
725 if ($USER->id != $post->userid) { // Not the original author, so add a message to the end
726 $data->date = userdate($post->modified);
727 if ($post->format == FORMAT_HTML) {
728 $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
729 fullname($USER).'</a>';
730 $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
731 } else {
732 $data->name = fullname($USER);
733 $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
737 //load data into form
738 $subscribe=(isset($post->forum)&&forum_is_subscribed($USER->id, $post->forum)) ||
739 (!empty($USER->autosubscribe));
742 $mform_post->set_data(array( 'general'=>$heading,
743 'subject'=>$post->subject,
744 'message'=>$post->message,
745 'subscribe'=>$subscribe?1:0,
746 'mailnow'=>!empty($post->mailnow),
747 'userid'=>$post->userid,
748 'parent'=>$post->parent,
749 'discussion'=>$post->discussion,
750 'course'=>$course->id)+
752 $page_params+
754 (isset($post->format)?array(
755 'format'=>$post->format):
756 array())+
758 (isset($dicussion->timestart)?array(
759 'timestart'=>$discussion->timestart):
760 array())+
762 (isset($discussion->timeend)?array(
763 'timeend'=>$discussion->timeend):
764 array())+
766 (isset($post->groupid)?array(
767 'groupid'=>$post->groupid):
768 array())+
770 (isset($discussion->id)?
771 array('discussion'=>$discussion->id):
772 array()));
775 $mform_post->display();
778 print_footer($course);