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