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