News items on home page should be sorted by date of first post
[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');
7
8 $reply = optional_param('reply', 0, PARAM_INT);
70e3da07 9 $forum = optional_param('forum', 0, PARAM_INT);
10 $edit = optional_param('edit', 0, PARAM_INT);
11 $delete = optional_param('delete', 0, PARAM_INT);
12 $prune = optional_param('prune',0,PARAM_INT);
13 $name = optional_param('name','',PARAM_CLEAN);
14 $confirm = optional_param('confirm',0,PARAM_INT);
501cdbd8 15
16 if (isguest()) {
c59eb341 17 $wwwroot = $CFG->wwwroot.'/login/index.php';
18 if (!empty($CFG->loginhttps)) {
19 $wwwroot = str_replace('http','https', $wwwroot);
20 }
556963f5 21
22 if (isset($forum)) { // User is starting a new discussion in a forum
23 if (! $forum = get_record('forum', 'id', $forum)) {
24 error('The forum number was incorrect');
25 }
16955dc6 26 } else if (!empty($reply)) { // User is writing a new reply
556963f5 27 if (! $parent = forum_get_post_full($reply)) {
28 error('Parent post ID was incorrect');
29 }
30 if (! $discussion = get_record('forum_discussions', 'id', $parent->discussion)) {
31 error('This post is not part of a discussion!');
32 }
33 if (! $forum = get_record('forum', 'id', $discussion->forum)) {
34 error('The forum number was incorrect');
35 }
36 }
37 if (! $course = get_record('course', 'id', $forum->course)) {
38 error('The course number was incorrect');
39 }
40 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
41 $cm->id = 0;
42 }
43
44 $strforums = get_string('modulenameplural', 'forum');
45 if ($course->category) {
46 print_header($course->shortname, $course->fullname,
47 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
e6157a73 48 <a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
3849dae8 49 <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>", '', '', true, "", navmenu($course, $cm));
556963f5 50 } else {
51 print_header($course->shortname, $course->fullname,
e6157a73 52 "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
3849dae8 53 <a href=\"view.php?f=$forum->id\">".format_string($forum->name)."</a>", '', '', true, "", navmenu($course, $cm));
556963f5 54 }
c59eb341 55 notice_yesno(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'),
56 $wwwroot, $_SERVER['HTTP_REFERER']);
cd8d4471 57 print_footer($course);
c59eb341 58 exit;
501cdbd8 59 }
60
8e8d0524 61 require_login(0, false); // Script is useless unless they're logged in
48d38fad 62
36b4f985 63 if ($post = data_submitted()) {
f7abd64a 64 if (empty($post->course)) {
65 error('No course was defined!');
66 }
67
68 if (!$course = get_record('course', 'id', $post->course)) {
69 error('Could not find specified course!');
70 }
71
72 if (!empty($course->lang)) { // Override current language
73 $CFG->courselang = $course->lang;
0d1db48e 74 }
501cdbd8 75
3395f2d6 76 if (empty($SESSION->fromurl)) {
77 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$post->forum";
78 } else {
79 $errordestination = $SESSION->fromurl;
80 }
81
50d83d8f 82 $post->subject = strip_tags($post->subject, '<lang><span>'); // Strip all tags except lang
db46e49c 83
3b7d3db5 84 //$post->message = clean_text($post->message, $post->format); // Clean up any bad tags
501cdbd8 85
36257d39 86 $post->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
7f6689e4 87
f7abd64a 88 if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $course->id)) { // For the logs
69d79bc3 89 $cm->id = 0;
90 }
91
3395f2d6 92 if (!$post->subject or !$post->message) {
93 $post->error = get_string("emptymessage", "forum");
7f6689e4 94
3395f2d6 95 } else if ($post->edit) { // Updating a post
501cdbd8 96 $post->id = $post->edit;
db290a6e 97 $message = '';
afa3507c 98
99 //fix for bug #4314
100 if (!$realpost = get_record('forum_posts','id',$post->id)){
101 $realpost = new object;
102 $realpost->userid = -1;
103 }
104
105 if ($realpost->userid <> $USER->id && !isadmin()){
106 error ("you can not update this post");
107 }
108
fbc21e82 109 if (get_field('forum', 'type', 'id', $forum) == 'news' && !$post->parent) {
110 $updatediscussion->id = $post->discussion;
111 if (empty($post->timestartdisabled)) {
112 $updatediscussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
113 } else {
114 $updatediscussion->timestart = 0;
115 }
116 if (empty($post->timeenddisabled)) {
117 $updatediscussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
118 } else {
119 $updatediscussion->timeend = 0;
120 }
121 if (empty($post->timeenddisabled) && $updatediscussion->timeend <= $updatediscussion->timestart) {
122 $post->error = get_string('timestartenderror', 'forum');
123 } elseif (!update_record('forum_discussions', $updatediscussion)) {
124 error(get_string("couldnotupdate", "forum"), $errordestination);
125 }
126 }
127 if (!isset($post->error)) {
128
db290a6e 129 if (forum_update_post($post,$message)) {
8f0cd6ef 130
f7abd64a 131 add_to_log($course->id, "forum", "update post",
839f2456 132 "discuss.php?d=$post->discussion&amp;parent=$post->id", "$post->id", $cm->id);
69d79bc3 133
db290a6e 134 $timemessage = 2;
135 if (!empty($message)) { // if we're printing stuff about the file upload
136 $timemessage = 4;
137 }
138 $message .= '<br />'.get_string("postupdated", "forum");
8f0cd6ef 139
0a9f61b5 140 if ($subscribemessage = forum_post_subscription($post)) {
db290a6e 141 $timemessage = 4;
0a9f61b5 142 }
b22b0e61 143 redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 144
501cdbd8 145 } else {
8f0cd6ef 146 error(get_string("couldnotupdate", "forum"), $errordestination);
501cdbd8 147 }
3395f2d6 148 exit;
7f6689e4 149
fbc21e82 150 }
501cdbd8 151 } else if ($post->discussion) { // Adding a new post to an existing discussion
db290a6e 152 $message = '';
153 if ($post->id = forum_add_new_post($post,$message)) {
69d79bc3 154
f7abd64a 155 add_to_log($course->id, "forum", "add post",
839f2456 156 "discuss.php?d=$post->discussion&amp;parent=$post->id", "$post->id", $cm->id);
69d79bc3 157
0a9f61b5 158 $timemessage = 2;
db290a6e 159 if (!empty($message)) { // if we're printing stuff about the file upload
160 $timemessage = 4;
161 }
162 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
0a9f61b5 163
164 if ($subscribemessage = forum_post_subscription($post)) {
165 $timemessage = 4;
501cdbd8 166 }
167
41547057 168 if ($post->mailnow) {
169 $message .= get_string("postmailnow", "forum");
170 $timemessage = 4;
171 }
172
b22b0e61 173 redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 174
501cdbd8 175 } else {
8f0cd6ef 176 error(get_string("couldnotadd", "forum"), $errordestination);
501cdbd8 177 }
3395f2d6 178 exit;
179
501cdbd8 180 } else { // Adding a new discussion
41547057 181 $post->mailnow = empty($post->mailnow) ? 0 : 1;
501cdbd8 182 $discussion = $post;
183 $discussion->name = $post->subject;
184 $discussion->intro = $post->message;
fbc21e82 185 $newstopic = false;
186 if (get_field('forum', 'type', 'id', $forum) == 'news' && !$post->parent) {
187 $newstopic = true;
188 }
189 if ($newstopic && empty($post->timestartdisabled)) {
190 $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
191 } else {
192 $discussion->timestart = 0;
193 }
194 if ($newstopic && empty($post->timeenddisabled)) {
195 $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
196 } else {
197 $discussion->timeend = 0;
198 }
199 if ($newstopic && empty($post->timeenddisabled) && $discussion->timeend <= $discussion->timestart) {
200 $post->error = get_string('timestartenderror', 'forum');
201 } else {
202
db290a6e 203 $message = '';
204 if ($discussion->id = forum_add_discussion($discussion,$message)) {
69d79bc3 205
680afe2e 206 add_to_log($course->id, "forum", "add discussion",
69d79bc3 207 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
208
0a9f61b5 209 $timemessage = 2;
db290a6e 210 if (!empty($message)) { // if we're printing stuff about the file upload
211 $timemessage = 4;
212 }
213 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
8f0cd6ef 214
41547057 215 if ($post->mailnow) {
216 $message .= get_string("postmailnow", "forum");
217 $timemessage = 4;
218 }
219
0a9f61b5 220 if ($subscribemessage = forum_post_subscription($discussion)) {
221 $timemessage = 4;
222 }
223
224 redirect(forum_go_back_to("view.php?f=$post->forum"), $message.$subscribemessage, $timemessage);
225
501cdbd8 226 } else {
8f0cd6ef 227 error(get_string("couldnotadd", "forum"), $errordestination);
501cdbd8 228 }
fbc21e82 229
3395f2d6 230 exit;
fbc21e82 231 }
501cdbd8 232 }
501cdbd8 233 }
234
da077b9a 235 if ($usehtmleditor = can_use_html_editor()) {
213e8cc6 236 $defaultformat = FORMAT_HTML;
213e8cc6 237 } else {
238 $defaultformat = FORMAT_MOODLE;
239 }
501cdbd8 240
3395f2d6 241 if (isset($post->error)) { // User is re-editing a failed posting
242
243 // Set up all the required objects again, and reuse the same $post
244
245 if (! $forum = get_record("forum", "id", $post->forum)) {
246 error("The forum number was incorrect ($post->forum)");
247 }
248
249 if (! $course = get_record("course", "id", $forum->course)) {
250 error("The course number was incorrect ($forum->course)");
251 }
252
253 if (!empty($post->parent)) {
254 if (! $parent = forum_get_post_full($post->parent)) {
255 error("Parent post ID was incorrect ($post->parent)");
256 }
257 }
258
259 if (!empty($post->discussion)) {
260 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
261 error("This post is not part of a discussion! ($post->discussion)");
262 }
cc86131d 263 } else {
264 $discussion = new stdClass();
265 $newstopic = false;
266 if ($forum->type == 'news' && !$post->parent) {
267 $newstopic = true;
268 }
269 if ($newstopic && empty($post->timestartdisabled)) {
270 $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
271 } else {
272 $discussion->timestart = 0;
273 }
274 if ($newstopic && empty($post->timeenddisabled)) {
275 $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
276 } else {
277 $discussion->timeend = 0;
278 }
3395f2d6 279 }
280
70e3da07 281 } else if (!empty($forum)) { // User is starting a new discussion in a forum
501cdbd8 282
607809b3 283 $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
501cdbd8 284
285 if (! $forum = get_record("forum", "id", $forum)) {
286 error("The forum number was incorrect ($forum)");
287 }
288 if (! $course = get_record("course", "id", $forum->course)) {
3395f2d6 289 error("The course number was incorrect ($forum->course)");
501cdbd8 290 }
291
11b0c469 292 if (! forum_user_can_post_discussion($forum)) {
501cdbd8 293 error("Sorry, but you can not post a new discussion in this forum.");
294 }
295
80602101 296 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
297 if (!$cm->visible and !isteacher($course->id)) {
298 error(get_string("activityiscurrentlyhidden"));
299 }
300 }
301
501cdbd8 302 // Load up the $post variable.
303
304 $post->course = $course->id;
305 $post->forum = $forum->id;
306 $post->discussion = 0; // ie discussion # not defined yet
307 $post->parent = 0;
308 $post->subject = "";
ebc3bd2b 309 $post->userid = $USER->id;
501cdbd8 310 $post->message = "";
213e8cc6 311 $post->format = $defaultformat;
501cdbd8 312
2862b309 313 $post->groupid = get_current_group($course->id);
314 if (isteacheredit($course->id) and $post->groupid == 0) {
315 $post->groupid = -1;
316 }
317
11b0c469 318 forum_set_return();
319
16955dc6 320 } else if (!empty($reply)) { // User is writing a new reply
501cdbd8 321
11b0c469 322 if (! $parent = forum_get_post_full($reply)) {
29ad118c 323 error("Parent post ID was incorrect");
501cdbd8 324 }
325 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
29ad118c 326 error("This post is not part of a discussion!");
501cdbd8 327 }
328 if (! $forum = get_record("forum", "id", $discussion->forum)) {
329 error("The forum number was incorrect ($discussion->forum)");
330 }
331 if (! $course = get_record("course", "id", $discussion->course)) {
332 error("The course number was incorrect ($discussion->course)");
333 }
6c506ca7 334
335 if (! forum_user_can_post($forum)) {
336 error("Sorry, but you can not post in this forum.");
337 }
02509fe6 338
339 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
340 if (groupmode($course, $cm) and !isteacheredit($course->id)) { // Make sure user can post here
63e20b88 341 $mygroupid = mygroupid($course->id);
fa22fd5f 342 if (!((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid)/*$mygroupid == $discussion->groupid*/))) {
02509fe6 343 error("Sorry, but you can not post in this discussion.");
344 }
345 }
80602101 346 if (!$cm->visible and !isteacher($course->id)) {
347 error(get_string("activityiscurrentlyhidden"));
348 }
02509fe6 349 }
350
501cdbd8 351 // Load up the $post variable.
352
353 $post->course = $course->id;
354 $post->forum = $forum->id;
355 $post->discussion = $parent->discussion;
356 $post->parent = $parent->id;
357 $post->subject = $parent->subject;
ebc3bd2b 358 $post->userid = $USER->id;
501cdbd8 359 $post->message = "";
213e8cc6 360 $post->format = $defaultformat;
501cdbd8 361
e9584ca3 362 $strre = get_string('re', 'forum');
363 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
364 $post->subject = $strre.' '.$post->subject;
501cdbd8 365 }
366
b22b0e61 367 unset($SESSION->fromdiscussion);
501cdbd8 368
70e3da07 369 } else if (!empty($edit)) { // User is editing their own post
501cdbd8 370
b8be40ce 371 $adminedit = (isadmin() and !empty($CFG->admineditalways));
372
11b0c469 373 if (! $post = forum_get_post_full($edit)) {
501cdbd8 374 error("Post ID was incorrect");
375 }
b8be40ce 376 if (($post->userid <> $USER->id) and !$adminedit) {
501cdbd8 377 error("You can't edit other people's posts!");
378 }
501cdbd8 379 if ($post->parent) {
11b0c469 380 if (! $parent = forum_get_post_full($post->parent)) {
501cdbd8 381 error("Parent post ID was incorrect ($post->parent)");
382 }
383 }
384 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
385 error("This post is not part of a discussion! ($reply)");
386 }
387 if (! $forum = get_record("forum", "id", $discussion->forum)) {
388 error("The forum number was incorrect ($discussion->forum)");
389 }
fbc21e82 390 if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
391 if (((time() - $post->created) > $CFG->maxeditingtime) and !$adminedit) {
392 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
393 }
394 }
501cdbd8 395 if (! $course = get_record("course", "id", $discussion->course)) {
396 error("The course number was incorrect ($discussion->course)");
397 }
398
399 // Load up the $post variable.
400
401 $post->edit = $edit;
402
403 $post->course = $course->id;
404 $post->forum = $forum->id;
405
b22b0e61 406 unset($SESSION->fromdiscussion);
501cdbd8 407
408
70e3da07 409 } else if (!empty($delete)) { // User is deleting a post
501cdbd8 410
11b0c469 411 if (! $post = forum_get_post_full($delete)) {
501cdbd8 412 error("Post ID was incorrect");
413 }
501cdbd8 414 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
415 error("This post is not part of a discussion!");
416 }
64eacd6f 417 if (! $forum = get_record("forum", "id", $discussion->forum)) {
418 error("The forum number was incorrect ($discussion->forum)");
419 }
ebc3bd2b 420 if (($post->userid <> $USER->id) and !isteacher($forum->course)) {
64eacd6f 421 error("You can't delete other people's posts!");
422 }
0d1db48e 423 if (!empty($forum->course)) {
424 if ($course = get_record('course', 'id', $forum->course)) {
425 if (!empty($course->lang)) {
426 $CFG->courselang = $course->lang;
427 }
428 }
429 }
501cdbd8 430
b82faacd 431 $replycount = forum_count_replies($post);
432
70e3da07 433 if (!empty($confirm)) { // User has confirmed the delete
501cdbd8 434
435 if ($post->totalscore) {
8f0cd6ef 436 notice(get_string("couldnotdeleteratings", "forum"),
11b0c469 437 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 438
b82faacd 439 } else if ($replycount && !isteacher($course->id)) {
cf38360f 440 error(get_string("couldnotdeletereplies", "forum"),
8203d211 441 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 442
443 } else {
69d79bc3 444 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
445 $cm->id = 0;
446 }
501cdbd8 447 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
64eacd6f 448 if ($forum->type == "single") {
8f0cd6ef 449 notice("Sorry, but you are not allowed to delete that discussion!",
64eacd6f 450 forum_go_back_to("discuss.php?d=$post->discussion"));
451 }
501cdbd8 452 forum_delete_discussion($discussion);
453
8f0cd6ef 454 add_to_log($discussion->course, "forum", "delete discussion",
b17333be 455 "view.php?id=$cm->id", "$forum->id", $cm->id);
69d79bc3 456
8f0cd6ef 457 redirect("view.php?f=$discussion->forum",
cf38360f 458 get_string("deleteddiscussion", "forum"), 1);
501cdbd8 459
b82faacd 460 } else if (forum_delete_post($post, isteacher($course->id))) {
501cdbd8 461
8f0cd6ef 462 add_to_log($discussion->course, "forum", "delete post",
69d79bc3 463 "discuss.php?d=$post->discussion", "$post->id", $cm->id);
464
b82faacd 465 $feedback = $replycount ? get_string('deletedposts', 'forum') : get_string('deletedpost', 'forum');
466 redirect(forum_go_back_to("discuss.php?d=$post->discussion"), $feedback, 1);
501cdbd8 467 } else {
468 error("An error occurred while deleting record $post->id");
469 }
470 }
471
472
473 } else { // User just asked to delete something
474
11b0c469 475 forum_set_return();
501cdbd8 476
b82faacd 477 if ($replycount) {
478 if (!isteacher($course->id)) {
479 error(get_string("couldnotdeletereplies", "forum"),
480 forum_go_back_to("discuss.php?d=$post->discussion"));
481 }
482 print_header();
483 notice_yesno(get_string("deletesureplural", "forum", $replycount+1),
484 "post.php?delete=$delete&amp;confirm=$delete",
485 $_SERVER["HTTP_REFERER"]);
486
487 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false);
488 if (empty($post->edit)) {
eaf50aef 489 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
b82faacd 490 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
491 } else {
492 $user_read_array = array();
493 }
494 forum_print_posts_nested($post->id, $course->id, false, false, $user_read_array, $forum->id);
495 }
496 } else {
497 print_header();
498 notice_yesno(get_string("deletesure", "forum", $replycount),
499 "post.php?delete=$delete&amp;confirm=$delete",
500 $_SERVER["HTTP_REFERER"]);
501 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
502 }
8f0cd6ef 503
501cdbd8 504 }
839f2456 505 print_footer($course);
501cdbd8 506 die;
507
508
70e3da07 509 } else if (!empty($prune)) { // Teacher is pruning
8f0cd6ef 510
0d5da5dd 511 if (!$post = forum_get_post_full($prune)) {
cf84431b 512 error("Post ID was incorrect");
513 }
0d5da5dd 514 if (!$discussion = get_record("forum_discussions", "id", $post->discussion)) {
cf84431b 515 error("This post is not part of a discussion!");
516 }
0d5da5dd 517 if (!$forum = get_record("forum", "id", $discussion->forum)) {
cf84431b 518 error("The forum number was incorrect ($discussion->forum)");
519 }
520 if (!isteacher($forum->course)) {
4d35d88b 521 error("You can't split discussions!");
cf84431b 522 }
523 if (!$post->parent) {
524 error('This is already the first post in the discussion');
525 }
0d5da5dd 526 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
527 $cm->id = 0;
528 }
cf84431b 529
70e3da07 530 if (!empty($name)) { // User has confirmed the prune
8f0cd6ef 531
cf84431b 532 $newdiscussion->course = $discussion->course;
533 $newdiscussion->forum = $discussion->forum;
534 $newdiscussion->name = $name;
535 $newdiscussion->firstpost = $post->id;
536 $newdiscussion->userid = $discussion->userid;
537 $newdiscussion->groupid = $discussion->groupid;
538 $newdiscussion->assessed = $discussion->assessed;
539 $newdiscussion->usermodified = $post->userid;
fbc21e82 540 $newdiscussion->timestart = $discussion->timestart;
541 $newdiscussion->timeend = $discussion->timeend;
8f0cd6ef 542
cf84431b 543 if (!$newid = insert_record('forum_discussions', $newdiscussion)) {
544 error('Could not create new discussion');
545 }
8f0cd6ef 546
13152de4 547 $newpost->id = $post->id;
548 $newpost->parent = 0;
549 $newpost->subject = $name;
d078ee9b 550
13152de4 551 if (!update_record("forum_posts", $newpost)) {
d078ee9b 552 error('Could not update the original post');
553 }
554
cf84431b 555 forum_change_discussionid($post->id, $newid);
8f0cd6ef 556
1da8c568 557 // update last post in each discussion
558 forum_discussion_update_last_post($discussion->id);
559 forum_discussion_update_last_post($newid);
cf84431b 560
8f0cd6ef 561 add_to_log($discussion->course, "forum", "prune post",
cf84431b 562 "discuss.php?d=$newid", "$post->id", $cm->id);
563
d078ee9b 564 redirect(forum_go_back_to("discuss.php?d=$newid"), get_string("prunedpost", "forum"), 1);
cf84431b 565
566 } else { // User just asked to prune something
567
568 $course = get_record('course', 'id', $forum->course);
569 $strforums = get_string("modulenameplural", "forum");
c78ac798 570 print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "",
8f0cd6ef 571 "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
3849dae8 572 <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a> ->
17dc3f3c 573 <a href=\"discuss.php?d=$discussion->id\">".format_string($post->subject,true)."</a> -> ".
cf84431b 574 get_string("prune", "forum"), '', "", true, "", navmenu($course, $cm));
8f0cd6ef 575
cf84431b 576 print_heading(get_string('pruneheading', 'forum'));
d078ee9b 577 echo '<center>';
8f0cd6ef 578
cf84431b 579 include('prune.html');
8f0cd6ef 580
cf84431b 581 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
839f2456 582 echo '</center>';
cf84431b 583 }
839f2456 584 print_footer($course);
cf84431b 585 die;
586
587
501cdbd8 588 } else {
589 error("No operation specified");
590
591 }
592
593
8f0cd6ef 594 // To get here they need to edit a post, and the $post
501cdbd8 595 // variable will be loaded with all the particulars,
596 // so bring up the form.
597
598 // $course, $forum are defined. $discussion is for edit and reply only.
599
ec81373f 600 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
601
602 require_login($course->id, false, $cm);
501cdbd8 603
dfc9ba9b 604
501cdbd8 605 if ($post->discussion) {
9fa49e22 606 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
501cdbd8 607 error("Could not find top parent of post $post->id");
608 }
609 } else {
680afe2e 610 $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
21da9db8 611 get_string("addanewdiscussion", "forum");
501cdbd8 612 }
613
3bbde520 614 if (empty($post->subject)) {
0ae5e5ea 615 $formstart = "theform.subject";
3bbde520 616 } else {
617 $formstart = "";
501cdbd8 618 }
619
620 if ($post->parent) {
17dc3f3c 621 $navtail = "<a href=\"discuss.php?d=$discussion->id\">".format_string($toppost->subject,true)."</a> -> ".get_string("editing", "forum");
501cdbd8 622 } else {
17dc3f3c 623 $navtail = format_string($toppost->subject);
501cdbd8 624 }
625
9c9f7d77 626 if (empty($post->edit)) {
627 $post->edit = "";
628 }
629
cf38360f 630 $strforums = get_string("modulenameplural", "forum");
631
73bb0835 632
3849dae8 633 $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 634
f37da850 635 if (empty($discussion->name)) {
8cb091e6 636 if (empty($discussion)) {
637 $discussion = new object;
638 }
9c9f7d77 639 $discussion->name = $forum->name;
640 }
641
501cdbd8 642 if ($course->category) {
c78ac798 643 print_header("$course->shortname: ".format_string($discussion->name).": ".format_string($toppost->subject), "$course->fullname",
325505f4 644 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
3bbde520 645 $navmiddle -> $navtail", $formstart, "", true, "", navmenu($course, $cm));
680afe2e 646
501cdbd8 647 } else {
c78ac798 648 print_header("$course->shortname: ".format_string($discussion->name).": ".format_string($toppost->subject), "$course->fullname",
9c9f7d77 649 "$navmiddle -> $navtail", "$formstart", "", true, "", navmenu($course, $cm));
501cdbd8 650
651 }
652
098d27d4 653// checkup
654 if (!empty($parent) && !forum_user_can_see_post($forum,$discussion,$post)) {
655 error("You cannot reply to this post");
656 }
657 if (empty($parent) && !forum_user_can_post_discussion($forum)) {
658 error("You cannot start a new discussion in this forum");
659 }
660
661 if ($forum->type == 'qanda' && !isteacher($forum->course) && !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
662 notify(get_string('qandanotify','forum'));
663 }
664
a4f495bf 665 forum_check_throttling($forum);
666
3395f2d6 667 if (!empty($parent)) {
11b0c469 668 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
f8029045 669 if (empty($post->edit)) {
eaf50aef 670 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
f37da850 671 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
672 } else {
673 $user_read_array = array();
674 }
098d27d4 675 if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum,$discussion)) {
676 forum_print_posts_threaded($parent->id, $course->id, 0, false, false, $user_read_array, $discussion->forum);
677 }
f8029045 678 }
556963f5 679 print_heading(get_string("yourreply", "forum").':');
501cdbd8 680 } else {
21da9db8 681 $forum->intro = trim($forum->intro);
682 if (!empty($forum->intro)) {
683 print_simple_box(format_text($forum->intro), 'center');
684 }
098d27d4 685 if ($forum->type == 'qanda') {
686 print_heading(get_string('yournewquestion','forum'));
687 } else {
688 print_heading(get_string('yournewtopic', 'forum'));
689 }
501cdbd8 690 }
556963f5 691 echo '<center>';
3395f2d6 692 if (!empty($post->error)) {
693 notify($post->error);
694 }
556963f5 695 echo '</center>';
501cdbd8 696
d30867b0 697 print_simple_box_start("center");
501cdbd8 698 require("post.html");
699 print_simple_box_end();
700
4b00b4b3 701 if ($usehtmleditor) {
76138908 702 use_html_editor("message");
4b00b4b3 703 }
704
501cdbd8 705 print_footer($course);
706
707
708?>