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