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