MDL-11699 - Making one more common case a bit nicer for callers. Merged from MOODLE_1...
[moodle.git] / mod / forum / post.php
CommitLineData
41905731 1<?php // $Id$
501cdbd8 2
3// Edit and save a new post to a discussion
4
16955dc6 5 require_once('../../config.php');
6 require_once('lib.php');
2b63df96 7 require_once('post_form.php');
16955dc6 8
0e52f01f 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);
2b63df96 15 $confirm = optional_param('confirm', 0, PARAM_INT);
16
17
0e52f01f 18 //these page_params will be passed as hidden variables later in the form.
19 $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit);
2b63df96 20
21 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
7b08a2ea 22
23 if (has_capability('moodle/legacy:guest', $sitecontext, NULL, false)) {
8d9a0ae6 24
c59eb341 25 $wwwroot = $CFG->wwwroot.'/login/index.php';
26 if (!empty($CFG->loginhttps)) {
3a233b85 27 $wwwroot = str_replace('http:', 'https:', $wwwroot);
c59eb341 28 }
556963f5 29
7b08a2ea 30 if (!empty($forum)) { // User is starting a new discussion in a forum
556963f5 31 if (! $forum = get_record('forum', 'id', $forum)) {
32 error('The forum number was incorrect');
33 }
16955dc6 34 } else if (!empty($reply)) { // User is writing a new reply
556963f5 35 if (! $parent = forum_get_post_full($reply)) {
36 error('Parent post ID was incorrect');
37 }
38 if (! $discussion = get_record('forum_discussions', 'id', $parent->discussion)) {
39 error('This post is not part of a discussion!');
40 }
41 if (! $forum = get_record('forum', 'id', $discussion->forum)) {
42 error('The forum number was incorrect');
43 }
44 }
45 if (! $course = get_record('course', 'id', $forum->course)) {
46 error('The course number was incorrect');
47 }
2b63df96 48
556963f5 49 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
6e89ca55 50 error('Could not get the course module for the forum instance.');
bbbf2d40 51 } else {
52 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
556963f5 53 }
54
55 $strforums = get_string('modulenameplural', 'forum');
e3f58dfb 56
e1526d77 57 if (!get_referer()) { // No referer - probably coming in via email See MDL-9052
58 require_login();
59 }
e3f58dfb 60
01e38388 61 $navlinks = array();
3b27b0fe 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');
e3f58dfb 64
3b27b0fe 65 $navigation = build_navigation($navlinks);
e3f58dfb 66
67
68 print_header($course->shortname, $course->fullname, $navigation, '' , '', true, "", navmenu($course, $cm));
69
c59eb341 70 notice_yesno(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'),
c8135a35 71 $wwwroot, get_referer(false));
cd8d4471 72 print_footer($course);
c59eb341 73 exit;
501cdbd8 74 }
0e52f01f 75
8e8d0524 76 require_login(0, false); // Script is useless unless they're logged in
48d38fad 77
2b63df96 78 if (!empty($forum)) { // User is starting a new discussion in a forum
501cdbd8 79 if (! $forum = get_record("forum", "id", $forum)) {
80 error("The forum number was incorrect ($forum)");
81 }
82 if (! $course = get_record("course", "id", $forum->course)) {
3395f2d6 83 error("The course number was incorrect ($forum->course)");
501cdbd8 84 }
e40a7a3c 85 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
11b0c469 86 if (! forum_user_can_post_discussion($forum)) {
a321d42b 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');
93 }
501cdbd8 94 }
2b63df96 95
80602101 96 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
8bcf97e1 97 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
80602101 98 error(get_string("activityiscurrentlyhidden"));
99 }
100 }
101
3b27b0fe 102 if (isset($_SERVER["HTTP_REFERER"])) {
103 $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
104 } else {
105 $SESSION->fromurl = '';
106 }
2b63df96 107
108
501cdbd8 109 // Load up the $post variable.
110
0e52f01f 111 $post = new object();
112 $post->course = $course->id;
113 $post->forum = $forum->id;
501cdbd8 114 $post->discussion = 0; // ie discussion # not defined yet
0e52f01f 115 $post->parent = 0;
116 $post->subject = '';
117 $post->userid = $USER->id;
118 $post->message = '';
501cdbd8 119
13534ef7
ML
120 if ($groupmode = groups_get_activity_groupmode($cm)) {
121 $post->groupid = groups_get_activity_group($cm);
122 if (empty($post->groupid)) {
3b27b0fe 123 $post->groupid = -1; //TODO: why -1??
124 }
125 } else {
126 $post->groupid = -1; //TODO: why -1??
bbbf2d40 127 }
11b0c469 128 forum_set_return();
129
bbbf2d40 130 } else if (!empty($reply)) { // User is writing a new reply
501cdbd8 131
11b0c469 132 if (! $parent = forum_get_post_full($reply)) {
29ad118c 133 error("Parent post ID was incorrect");
501cdbd8 134 }
135 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
29ad118c 136 error("This post is not part of a discussion!");
501cdbd8 137 }
138 if (! $forum = get_record("forum", "id", $discussion->forum)) {
139 error("The forum number was incorrect ($discussion->forum)");
140 }
141 if (! $course = get_record("course", "id", $discussion->course)) {
142 error("The course number was incorrect ($discussion->course)");
143 }
8ce8fc85 144 if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
145 error("Incorrect cm");
146 }
bd4128e9 147
e40a7a3c 148 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
8ce8fc85 149 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
ea3caf69 150
6c506ca7 151 if (! forum_user_can_post($forum)) {
bd4128e9 152 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
153 $SESSION->wantsurl = $FULLME;
154 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
155 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
156 } else {
157 print_error('nopostforum', 'forum');
158 }
6c506ca7 159 }
2b63df96 160
8ce8fc85 161 if (groupmode($course, $cm)) { // Make sure user can post here
162 $mygroupid = mygroupid($course->id);
163 if (!((empty($mygroupid) and $discussion->groupid == -1)
164 || (groups_is_member($discussion->groupid)/*$mygroupid == $discussion->groupid*/)
165 || has_capability('moodle/site:accessallgroups', $modcontext, NULL, false) )) {
166 print_error('nopostdiscussion', 'forum');
80602101 167 }
02509fe6 168 }
8ce8fc85 169 if (!$cm->visible and !has_capability('moodle/course:manageactivities', $coursecontext)) {
170 error(get_string("activityiscurrentlyhidden"));
171 }
02509fe6 172
501cdbd8 173 // Load up the $post variable.
174
0e52f01f 175 $post = new object();
176 $post->course = $course->id;
177 $post->forum = $forum->id;
501cdbd8 178 $post->discussion = $parent->discussion;
0e52f01f 179 $post->parent = $parent->id;
180 $post->subject = $parent->subject;
181 $post->userid = $USER->id;
182 $post->message = '';
501cdbd8 183
e9584ca3 184 $strre = get_string('re', 'forum');
185 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
186 $post->subject = $strre.' '.$post->subject;
501cdbd8 187 }
188
b22b0e61 189 unset($SESSION->fromdiscussion);
0e52f01f 190
bbbf2d40 191 } else if (!empty($edit)) { // User is editing their own post
b8be40ce 192
11b0c469 193 if (! $post = forum_get_post_full($edit)) {
501cdbd8 194 error("Post ID was incorrect");
195 }
501cdbd8 196 if ($post->parent) {
11b0c469 197 if (! $parent = forum_get_post_full($post->parent)) {
501cdbd8 198 error("Parent post ID was incorrect ($post->parent)");
199 }
200 }
2b63df96 201
501cdbd8 202 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
2b63df96 203 error("This post is not part of a discussion! ($edit)");
501cdbd8 204 }
205 if (! $forum = get_record("forum", "id", $discussion->forum)) {
206 error("The forum number was incorrect ($discussion->forum)");
207 }
208 if (! $course = get_record("course", "id", $discussion->course)) {
209 error("The course number was incorrect ($discussion->course)");
210 }
e51e3fcb 211 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
6e89ca55 212 error('Could not get the course module for the forum instance.');
213 } else {
214 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
215 }
e80ca6c6 216 if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
217 if (((time() - $post->created) > $CFG->maxeditingtime) and
218 !has_capability('mod/forum:editanypost', $modcontext)) {
219 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
220 }
221 }
6e89ca55 222 if (($post->userid <> $USER->id) and
223 !has_capability('mod/forum:editanypost', $modcontext)) {
224 error("You can't edit other people's posts!");
225 }
501cdbd8 226
501cdbd8 227
0e52f01f 228 // Load up the $post variable.
229 $post->edit = $edit;
230 $post->course = $course->id;
501cdbd8 231 $post->forum = $forum->id;
232
0e52f01f 233 trusttext_prepare_edit($post->message, $post->format, can_use_html_editor(), $modcontext);
234
b22b0e61 235 unset($SESSION->fromdiscussion);
501cdbd8 236
237
2b63df96 238 }else if (!empty($delete)) { // User is deleting a post
501cdbd8 239
11b0c469 240 if (! $post = forum_get_post_full($delete)) {
501cdbd8 241 error("Post ID was incorrect");
242 }
501cdbd8 243 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
244 error("This post is not part of a discussion!");
245 }
64eacd6f 246 if (! $forum = get_record("forum", "id", $discussion->forum)) {
247 error("The forum number was incorrect ($discussion->forum)");
248 }
e40a7a3c 249 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
6e89ca55 250 error('Could not get the course module for the forum instance.');
e40a7a3c 251 }
dcf6d93c 252 if (!$course = get_record('course', 'id', $forum->course)) {
253 error('Incorrect course');
254 }
255
256 require_login($course, false, $cm);
257 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
258
0468976c 259 if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
260 || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
bbbf2d40 261 error("You can't delete this post!");
64eacd6f 262 }
501cdbd8 263
2b63df96 264
b82faacd 265 $replycount = forum_count_replies($post);
266
bbbf2d40 267 if (!empty($confirm)) { // User has confirmed the delete
501cdbd8 268
269 if ($post->totalscore) {
8f0cd6ef 270 notice(get_string("couldnotdeleteratings", "forum"),
11b0c469 271 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 272
0468976c 273 } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
cf38360f 274 error(get_string("couldnotdeletereplies", "forum"),
8203d211 275 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 276
277 } else {
278 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
cef1ce6a 279 if ($forum->type == 'single') {
8f0cd6ef 280 notice("Sorry, but you are not allowed to delete that discussion!",
64eacd6f 281 forum_go_back_to("discuss.php?d=$post->discussion"));
282 }
501cdbd8 283 forum_delete_discussion($discussion);
284
8f0cd6ef 285 add_to_log($discussion->course, "forum", "delete discussion",
b17333be 286 "view.php?id=$cm->id", "$forum->id", $cm->id);
69d79bc3 287
2b63df96 288 redirect("view.php?f=$discussion->forum");
501cdbd8 289
0468976c 290 } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext))) {
2b63df96 291
cef1ce6a 292 if ($forum->type == 'single') {
293 // Single discussion forums are an exception. We show
294 // the forum itself since it only has one discussion
295 // thread.
296 $discussionurl = "view.php?f=$forum->id";
297 } else {
298 $discussionurl = "discuss.php?d=$post->discussion";
299 }
2b63df96 300
cef1ce6a 301 add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
69d79bc3 302
2b63df96 303 redirect(forum_go_back_to($discussionurl));
501cdbd8 304 } else {
305 error("An error occurred while deleting record $post->id");
306 }
307 }
308
309
bbbf2d40 310 } else { // User just asked to delete something
501cdbd8 311
11b0c469 312 forum_set_return();
501cdbd8 313
b82faacd 314 if ($replycount) {
6e89ca55 315 if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
b82faacd 316 error(get_string("couldnotdeletereplies", "forum"),
317 forum_go_back_to("discuss.php?d=$post->discussion"));
318 }
319 print_header();
320 notice_yesno(get_string("deletesureplural", "forum", $replycount+1),
bbbf2d40 321 "post.php?delete=$delete&amp;confirm=$delete",
cbcd697b 322 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
b82faacd 323
324 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false);
325 if (empty($post->edit)) {
eaf50aef 326 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
b82faacd 327 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
328 } else {
329 $user_read_array = array();
330 }
331 forum_print_posts_nested($post->id, $course->id, false, false, $user_read_array, $forum->id);
332 }
333 } else {
334 print_header();
335 notice_yesno(get_string("deletesure", "forum", $replycount),
bbbf2d40 336 "post.php?delete=$delete&amp;confirm=$delete",
cbcd697b 337 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
b82faacd 338 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
339 }
8f0cd6ef 340
501cdbd8 341 }
839f2456 342 print_footer($course);
501cdbd8 343 die;
344
345
6e89ca55 346 } else if (!empty($prune)) { // Pruning
8f0cd6ef 347
0d5da5dd 348 if (!$post = forum_get_post_full($prune)) {
cf84431b 349 error("Post ID was incorrect");
350 }
0d5da5dd 351 if (!$discussion = get_record("forum_discussions", "id", $post->discussion)) {
cf84431b 352 error("This post is not part of a discussion!");
353 }
0d5da5dd 354 if (!$forum = get_record("forum", "id", $discussion->forum)) {
cf84431b 355 error("The forum number was incorrect ($discussion->forum)");
356 }
77dc6d0b 357 if ($forum->type == 'single') {
358 error('Discussions from this forum cannot be split');
359 }
cf84431b 360 if (!$post->parent) {
361 error('This is already the first post in the discussion');
362 }
0d5da5dd 363 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
6e89ca55 364 error('Could not get the course module for the forum instance.');
365 } else {
366 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
367 }
368 if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
369 error("You can't split discussions!");
0d5da5dd 370 }
cf84431b 371
bbbf2d40 372 if (!empty($name)) { // User has confirmed the prune
8f0cd6ef 373
0e52f01f 374 $newdiscussion = new object();
375 $newdiscussion->course = $discussion->course;
376 $newdiscussion->forum = $discussion->forum;
377 $newdiscussion->name = $name;
378 $newdiscussion->firstpost = $post->id;
379 $newdiscussion->userid = $discussion->userid;
380 $newdiscussion->groupid = $discussion->groupid;
381 $newdiscussion->assessed = $discussion->assessed;
cf84431b 382 $newdiscussion->usermodified = $post->userid;
0e52f01f 383 $newdiscussion->timestart = $discussion->timestart;
384 $newdiscussion->timeend = $discussion->timeend;
8f0cd6ef 385
cf84431b 386 if (!$newid = insert_record('forum_discussions', $newdiscussion)) {
387 error('Could not create new discussion');
388 }
8f0cd6ef 389
0e52f01f 390 $newpost = new object();
391 $newpost->id = $post->id;
392 $newpost->parent = 0;
bbbf2d40 393 $newpost->subject = $name;
d078ee9b 394
13152de4 395 if (!update_record("forum_posts", $newpost)) {
d078ee9b 396 error('Could not update the original post');
397 }
398
cf84431b 399 forum_change_discussionid($post->id, $newid);
8f0cd6ef 400
1da8c568 401 // update last post in each discussion
402 forum_discussion_update_last_post($discussion->id);
403 forum_discussion_update_last_post($newid);
cf84431b 404
8f0cd6ef 405 add_to_log($discussion->course, "forum", "prune post",
cf84431b 406 "discuss.php?d=$newid", "$post->id", $cm->id);
407
2b63df96 408 redirect(forum_go_back_to("discuss.php?d=$newid"));
cf84431b 409
410 } else { // User just asked to prune something
411
412 $course = get_record('course', 'id', $forum->course);
413 $strforums = get_string("modulenameplural", "forum");
e3f58dfb 414
01e38388 415 $navlinks = array();
3b27b0fe 416 $navlinks[] = array('name' => $strforums, 'link' => "../forum/index.php?id=$course->id", 'type' => 'activity');
417 $navlinks[] = array('name' => $forum->name, 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
418 $navlinks[] = array('name' => format_string($post->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
419 $navlinks[] = array('name' => get_string("prune", "forum"), 'link' => '', 'type' => 'title');
e3f58dfb 420
3b27b0fe 421 $navigation = build_navigation($navlinks);
e3f58dfb 422
423 print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "", $navigation, '', "", true, "", navmenu($course, $cm));
8f0cd6ef 424
cf84431b 425 print_heading(get_string('pruneheading', 'forum'));
d078ee9b 426 echo '<center>';
8f0cd6ef 427
cf84431b 428 include('prune.html');
8f0cd6ef 429
cf84431b 430 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
839f2456 431 echo '</center>';
cf84431b 432 }
839f2456 433 print_footer($course);
cf84431b 434 die;
501cdbd8 435 } else {
436 error("No operation specified");
437
438 }
439
2b63df96 440 if (!isset($coursecontext)) {
441 // Has not yet been set by post.php.
442 $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course);
443 }
444
c58efbf5 445 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
446 error('Could not get the course module for the forum instance.');
447 }
448 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
449
1d284fbd 450 $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
2b63df96 451
beac4717 452 if ($fromform = $mform_post->get_data()) {
2b63df96 453
454
dcf6d93c 455 require_login($course, false, $cm);
2b63df96 456
457 if (empty($SESSION->fromurl)) {
458 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
459 } else {
460 $errordestination = $SESSION->fromurl;
461 }
462
463 // TODO add attachment processing
464 //$fromform->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
465
2b63df96 466 trusttext_after_edit($fromform->message, $modcontext);
467
468 if ($fromform->edit) { // Updating a post
469 $fromform->id = $fromform->edit;
470 $message = '';
471
472 //fix for bug #4314
473 if (!$realpost = get_record('forum_posts', 'id', $fromform->id)) {
474 $realpost = new object;
475 $realpost->userid = -1;
476 }
477
478
479 // if user has edit any post capability
480 // or has either startnewdiscussion or reply capability and is editting own post
481 // then he can proceed
482 // MDL-7066
483 if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
484 || has_capability('mod/forum:startdiscussion', $modcontext))) ||
485 has_capability('mod/forum:editanypost', $modcontext)) ) {
486 error("You can not update this post");
487 }
488
489 if ($forum->type == 'news' && !$fromform->parent) {
490 $updatediscussion = new object;
491 $updatediscussion->id = $fromform->discussion;
11f260f4 492 $updatediscussion->timestart = $fromform->timestart;
493 $updatediscussion->timeend = $fromform->timeend;
2b63df96 494 if (!update_record('forum_discussions', $updatediscussion)) {
495 error(get_string("couldnotupdate", "forum"), $errordestination);
496 }
497 }
498
499 $updatepost=$fromform; //realpost
500 $updatepost->forum=$forum->id;
501 if (!forum_update_post($updatepost, $message)) {
502 error(get_string("couldnotupdate", "forum"), $errordestination);
503 }
504
505 $timemessage = 2;
506 if (!empty($message)) { // if we're printing stuff about the file upload
507 $timemessage = 4;
508 }
509 $message .= '<br />'.get_string("postupdated", "forum");
510
511 if ($subscribemessage = forum_post_subscription($fromform)) {
512 $timemessage = 4;
513 }
514 if ($forum->type == 'single') {
515 // Single discussion forums are an exception. We show
516 // the forum itself since it only has one discussion
517 // thread.
518 $discussionurl = "view.php?f=$forum->id";
519 } else {
cbcd697b 520 $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
2b63df96 521 }
522 add_to_log($course->id, "forum", "update post",
523 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
524
525 redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
526
527 exit;
528
529
530 } else if ($fromform->discussion) { // Adding a new post to an existing discussion
531 $message = '';
532 $addpost=$fromform;
533 $addpost->forum=$forum->id;
534 if ($fromform->id = forum_add_new_post($addpost, $message)) {
535
536 $timemessage = 2;
537 if (!empty($message)) { // if we're printing stuff about the file upload
538 $timemessage = 4;
539 }
0af02710 540
2b63df96 541 if ($subscribemessage = forum_post_subscription($fromform)) {
542 $timemessage = 4;
543 }
544
c718dd57 545 if (!empty($fromform->mailnow)) {
2b63df96 546 $message .= get_string("postmailnow", "forum");
547 $timemessage = 4;
0af02710 548 } else {
549 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
2b63df96 550 }
551
552 if ($forum->type == 'single') {
553 // Single discussion forums are an exception. We show
554 // the forum itself since it only has one discussion
555 // thread.
556 $discussionurl = "view.php?f=$forum->id";
557 } else {
558 $discussionurl = "discuss.php?d=$discussion->id";
559 }
560 add_to_log($course->id, "forum", "add post",
561 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
562
3b73b15d 563 redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
2b63df96 564
565 } else {
566 error(get_string("couldnotadd", "forum"), $errordestination);
567 }
568 exit;
569
570 } else { // Adding a new discussion
571 $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
572 $discussion = $fromform;
573 $discussion->name = $fromform->subject;
574 $discussion->intro = $fromform->message;
575 $newstopic = false;
576
577 if ($forum->type == 'news' && !$fromform->parent) {
578 $newstopic = true;
579 }
11f260f4 580 $discussion->timestart = $fromform->timestart;
581 $discussion->timeend = $fromform->timeend;
2b63df96 582
583 $message = '';
584 if ($discussion->id = forum_add_discussion($discussion, $message)) {
585
586 add_to_log($course->id, "forum", "add discussion",
587 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
588
589 $timemessage = 2;
590 if (!empty($message)) { // if we're printing stuff about the file upload
591 $timemessage = 4;
592 }
0af02710 593
2b63df96 594 if ($fromform->mailnow) {
595 $message .= get_string("postmailnow", "forum");
596 $timemessage = 4;
0af02710 597 } else {
598 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
599 }
2b63df96 600
601 if ($subscribemessage = forum_post_subscription($discussion)) {
602 $timemessage = 4;
603 }
604
605 redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
606
607 } else {
608 error(get_string("couldnotadd", "forum"), $errordestination);
609 }
610
611 exit;
612 }
613 }
614
615
501cdbd8 616
8f0cd6ef 617 // To get here they need to edit a post, and the $post
501cdbd8 618 // variable will be loaded with all the particulars,
619 // so bring up the form.
620
621 // $course, $forum are defined. $discussion is for edit and reply only.
622
ec81373f 623 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
624
625 require_login($course->id, false, $cm);
501cdbd8 626
f2b5d7e3 627 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
dfc9ba9b 628
501cdbd8 629 if ($post->discussion) {
9fa49e22 630 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
501cdbd8 631 error("Could not find top parent of post $post->id");
632 }
633 } else {
680afe2e 634 $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
21da9db8 635 get_string("addanewdiscussion", "forum");
501cdbd8 636 }
637
e3f58dfb 638 $strforums = get_string("modulenameplural", "forum");
639
01e38388 640 $navlinks = array();
3b27b0fe 641 $navlinks[] = array('name' => $strforums, 'link' => "../forum/index.php?id=$course->id", 'type' => 'activity');
642 $navlinks[] = array('name' => $forum->name, 'link' => "view.php?f=$forum->id", 'type' => 'activityinstance');
2b63df96 643
501cdbd8 644
645 if ($post->parent) {
3b27b0fe 646 $navlinks[] = array('name' => format_string($toppost->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'activityinstance');
647 $navlinks[] = array('name' => get_string('editing', 'forum'), 'link' => '', 'type' => 'action');
e73457e4 648 } else {
3b27b0fe 649 $navlinks[] = array('name' => format_string($toppost->subject), 'link' => '', 'type' => 'action');
501cdbd8 650 }
651
9c9f7d77 652 if (empty($post->edit)) {
352b8da8 653 $post->edit = '';
9c9f7d77 654 }
655
e3f58dfb 656
f37da850 657 if (empty($discussion->name)) {
8cb091e6 658 if (empty($discussion)) {
659 $discussion = new object;
660 }
9c9f7d77 661 $discussion->name = $forum->name;
662 }
cef1ce6a 663 if ($forum->type == 'single') {
664 // There is only one discussion thread for this forum type. We should
665 // not show the discussion name (same as forum name in this case) in
666 // the breadcrumbs.
667 $strdiscussionname = '';
668 $navtail = '';
669 } else {
670 // Show the discussion name in the breadcrumbs.
671 $strdiscussionname = format_string($discussion->name).':';
672 }
8dec2253 673
674 $forcefocus = empty($reply) ? NULL : 'message';
675
680afe2e 676
3b27b0fe 677 $navigation = build_navigation($navlinks);
e3f58dfb 678
679 print_header("$course->shortname: $strdiscussionname ".
680 format_string($toppost->subject), $course->fullname,
681 $navigation, $mform_post->focus($forcefocus), "", true, "", navmenu($course, $cm));
501cdbd8 682
e3f58dfb 683
098d27d4 684// checkup
951e1073 685 if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post)) {
098d27d4 686 error("You cannot reply to this post");
687 }
57f876b7 688 if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum)) {
098d27d4 689 error("You cannot start a new discussion in this forum");
690 }
691
b304e7f7 692 if ($forum->type == 'qanda'
693 && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
694 && !empty($discussion->id)
695 && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
696 notify(get_string('qandanotify','forum'));
098d27d4 697 }
698
a4f495bf 699 forum_check_throttling($forum);
700
3395f2d6 701 if (!empty($parent)) {
11b0c469 702 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
f8029045 703 if (empty($post->edit)) {
eaf50aef 704 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
f37da850 705 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
706 } else {
707 $user_read_array = array();
708 }
0468976c 709 if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
098d27d4 710 forum_print_posts_threaded($parent->id, $course->id, 0, false, false, $user_read_array, $discussion->forum);
711 }
f8029045 712 }
0e52f01f 713 $heading = get_string("yourreply", "forum");
501cdbd8 714 } else {
21da9db8 715 $forum->intro = trim($forum->intro);
716 if (!empty($forum->intro)) {
c1afef80 717 print_box(format_text($forum->intro), 'generalbox', 'intro');
21da9db8 718 }
098d27d4 719 if ($forum->type == 'qanda') {
0e52f01f 720 $heading = get_string('yournewquestion', 'forum');
098d27d4 721 } else {
0e52f01f 722 $heading = get_string('yournewtopic', 'forum');
098d27d4 723 }
501cdbd8 724 }
501cdbd8 725
9bdedea4 726 if ($USER->id != $post->userid) { // Not the original author, so add a message to the end
727 $data->date = userdate($post->modified);
728 if ($post->format == FORMAT_HTML) {
729 $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
730 fullname($USER).'</a>';
731 $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
732 } else {
733 $data->name = fullname($USER);
734 $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
735 }
736 }
737
2b63df96 738 //load data into form
739 $subscribe=(isset($post->forum)&&forum_is_subscribed($USER->id, $post->forum)) ||
740 (!empty($USER->autosubscribe));
741
742
beac4717 743 $mform_post->set_data(array( 'general'=>$heading,
bb40325e 744 'subject'=>$post->subject,
2b63df96 745 'message'=>$post->message,
746 'subscribe'=>$subscribe?1:0,
747 'mailnow'=>!empty($post->mailnow),
748 'userid'=>$post->userid,
749 'parent'=>$post->parent,
750 'discussion'=>$post->discussion,
751 'course'=>$course->id)+
752
753 $page_params+
754
755 (isset($post->format)?array(
756 'format'=>$post->format):
757 array())+
758
2840c216 759 (isset($dicussion->timestart)?array(
760 'timestart'=>$discussion->timestart):
761 array())+
762
763 (isset($discussion->timeend)?array(
764 'timeend'=>$discussion->timeend):
765 array())+
766
2b63df96 767 (isset($post->groupid)?array(
768 'groupid'=>$post->groupid):
769 array())+
770
2b63df96 771 (isset($discussion->id)?
772 array('discussion'=>$discussion->id):
773 array()));
774
2b63df96 775
776 $mform_post->display();
501cdbd8 777
4b00b4b3 778
501cdbd8 779 print_footer($course);
780
781
96662058 782?>