Merging fix for bug 1957 from MOODLE_14_STABLE.
[moodle.git] / mod / forum / post.php
CommitLineData
41905731 1<?php // $Id$
501cdbd8 2
3// Edit and save a new post to a discussion
4
5
b0e3a925 6 require_once("../../config.php");
7 require_once("lib.php");
501cdbd8 8
9 if (isguest()) {
607809b3 10 error(get_string("noguestpost", "forum"), $_SERVER["HTTP_REFERER"]);
501cdbd8 11 }
12
8e8d0524 13 require_login(0, false); // Script is useless unless they're logged in
48d38fad 14
36b4f985 15 if ($post = data_submitted()) {
0d1db48e 16 if (!empty($post->course)) {
17 if ($course = get_record('course', 'id', $post->course)) {
18 if (!empty($course->lang)) {
19 $CFG->courselang = $course->lang;
20 }
21 }
22 }
501cdbd8 23
3395f2d6 24 if (empty($SESSION->fromurl)) {
25 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$post->forum";
26 } else {
27 $errordestination = $SESSION->fromurl;
28 }
29
83ec9098 30 $post->subject = strip_tags($post->subject, '<lang>'); // Strip all tags except lang
db46e49c 31 $post->subject = break_up_long_words($post->subject);
32
3b7d3db5 33 //$post->message = clean_text($post->message, $post->format); // Clean up any bad tags
501cdbd8 34
36257d39 35 $post->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
7f6689e4 36
69d79bc3 37 if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $post->course)) { // For the logs
38 $cm->id = 0;
39 }
40
3395f2d6 41 if (!$post->subject or !$post->message) {
42 $post->error = get_string("emptymessage", "forum");
7f6689e4 43
3395f2d6 44 } else if ($post->edit) { // Updating a post
501cdbd8 45 $post->id = $post->edit;
7f6689e4 46 if (forum_update_post($post)) {
69d79bc3 47
48 add_to_log($post->course, "forum", "update post",
839f2456 49 "discuss.php?d=$post->discussion&amp;parent=$post->id", "$post->id", $cm->id);
69d79bc3 50
0a9f61b5 51 $message = get_string("postupdated", "forum");
52 $timemessage = 1;
53
54 if ($subscribemessage = forum_post_subscription($post)) {
55 $timemessage = 2;
56 }
b22b0e61 57 redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 58
501cdbd8 59 } else {
3395f2d6 60 error(get_string("couldnotupdate", "forum"), $errordestination);
501cdbd8 61 }
3395f2d6 62 exit;
7f6689e4 63
501cdbd8 64 } else if ($post->discussion) { // Adding a new post to an existing discussion
11b0c469 65 if ($post->id = forum_add_new_post($post)) {
69d79bc3 66
67 add_to_log($post->course, "forum", "add post",
839f2456 68 "discuss.php?d=$post->discussion&amp;parent=$post->id", "$post->id", $cm->id);
69d79bc3 69
0a9f61b5 70 $message = get_string("postadded", "forum", format_time($CFG->maxeditingtime));
71 $timemessage = 2;
72
73 if ($subscribemessage = forum_post_subscription($post)) {
74 $timemessage = 4;
501cdbd8 75 }
76
b22b0e61 77 redirect(forum_go_back_to("discuss.php?d=$post->discussion#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 78
501cdbd8 79 } else {
3395f2d6 80 error(get_string("couldnotadd", "forum"), $errordestination);
501cdbd8 81 }
3395f2d6 82 exit;
83
501cdbd8 84 } else { // Adding a new discussion
85 $discussion = $post;
86 $discussion->name = $post->subject;
87 $discussion->intro = $post->message;
88 if ($discussion->id = forum_add_discussion($discussion)) {
69d79bc3 89
90 add_to_log($post->course, "forum", "add discussion",
91 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
92
0a9f61b5 93 $message = get_string("postadded", "forum", format_time($CFG->maxeditingtime));
94 $timemessage = 2;
95
96 if ($subscribemessage = forum_post_subscription($discussion)) {
97 $timemessage = 4;
98 }
99
100 redirect(forum_go_back_to("view.php?f=$post->forum"), $message.$subscribemessage, $timemessage);
101
501cdbd8 102 } else {
3395f2d6 103 error(get_string("couldnotadd", "forum"), $errordestination);
501cdbd8 104 }
3395f2d6 105 exit;
501cdbd8 106 }
501cdbd8 107 }
108
213e8cc6 109 if ($usehtmleditor = can_use_richtext_editor()) {
110 $defaultformat = FORMAT_HTML;
213e8cc6 111 } else {
112 $defaultformat = FORMAT_MOODLE;
113 }
501cdbd8 114
115
3395f2d6 116 if (isset($post->error)) { // User is re-editing a failed posting
117
118 // Set up all the required objects again, and reuse the same $post
119
120 if (! $forum = get_record("forum", "id", $post->forum)) {
121 error("The forum number was incorrect ($post->forum)");
122 }
123
124 if (! $course = get_record("course", "id", $forum->course)) {
125 error("The course number was incorrect ($forum->course)");
126 }
127
128 if (!empty($post->parent)) {
129 if (! $parent = forum_get_post_full($post->parent)) {
130 error("Parent post ID was incorrect ($post->parent)");
131 }
132 }
133
134 if (!empty($post->discussion)) {
135 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
136 error("This post is not part of a discussion! ($post->discussion)");
137 }
138 }
139
140 } else if (isset($forum)) { // User is starting a new discussion in a forum
501cdbd8 141
607809b3 142 $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
501cdbd8 143
144 if (! $forum = get_record("forum", "id", $forum)) {
145 error("The forum number was incorrect ($forum)");
146 }
147 if (! $course = get_record("course", "id", $forum->course)) {
3395f2d6 148 error("The course number was incorrect ($forum->course)");
501cdbd8 149 }
150
11b0c469 151 if (! forum_user_can_post_discussion($forum)) {
501cdbd8 152 error("Sorry, but you can not post a new discussion in this forum.");
153 }
154
80602101 155 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
156 if (!$cm->visible and !isteacher($course->id)) {
157 error(get_string("activityiscurrentlyhidden"));
158 }
159 }
160
501cdbd8 161 // Load up the $post variable.
162
163 $post->course = $course->id;
164 $post->forum = $forum->id;
165 $post->discussion = 0; // ie discussion # not defined yet
166 $post->parent = 0;
167 $post->subject = "";
ebc3bd2b 168 $post->userid = $USER->id;
501cdbd8 169 $post->message = "";
213e8cc6 170 $post->format = $defaultformat;
501cdbd8 171
2862b309 172 $post->groupid = get_current_group($course->id);
173 if (isteacheredit($course->id) and $post->groupid == 0) {
174 $post->groupid = -1;
175 }
176
11b0c469 177 forum_set_return();
178
501cdbd8 179 } else if (isset($reply)) { // User is writing a new reply
180
11b0c469 181 if (! $parent = forum_get_post_full($reply)) {
29ad118c 182 error("Parent post ID was incorrect");
501cdbd8 183 }
184 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
29ad118c 185 error("This post is not part of a discussion!");
501cdbd8 186 }
187 if (! $forum = get_record("forum", "id", $discussion->forum)) {
188 error("The forum number was incorrect ($discussion->forum)");
189 }
190 if (! $course = get_record("course", "id", $discussion->course)) {
191 error("The course number was incorrect ($discussion->course)");
192 }
6c506ca7 193
194 if (! forum_user_can_post($forum)) {
195 error("Sorry, but you can not post in this forum.");
196 }
02509fe6 197
198 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
199 if (groupmode($course, $cm) and !isteacheredit($course->id)) { // Make sure user can post here
200 if (mygroupid($course->id) != $discussion->groupid) {
201 error("Sorry, but you can not post in this discussion.");
202 }
203 }
80602101 204 if (!$cm->visible and !isteacher($course->id)) {
205 error(get_string("activityiscurrentlyhidden"));
206 }
02509fe6 207 }
208
501cdbd8 209 // Load up the $post variable.
210
211 $post->course = $course->id;
212 $post->forum = $forum->id;
213 $post->discussion = $parent->discussion;
214 $post->parent = $parent->id;
215 $post->subject = $parent->subject;
ebc3bd2b 216 $post->userid = $USER->id;
501cdbd8 217 $post->message = "";
213e8cc6 218 $post->format = $defaultformat;
501cdbd8 219
e9584ca3 220 $strre = get_string('re', 'forum');
221 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
222 $post->subject = $strre.' '.$post->subject;
501cdbd8 223 }
224
b22b0e61 225 unset($SESSION->fromdiscussion);
501cdbd8 226
227 } else if (isset($edit)) { // User is editing their own post
228
b8be40ce 229 $adminedit = (isadmin() and !empty($CFG->admineditalways));
230
11b0c469 231 if (! $post = forum_get_post_full($edit)) {
501cdbd8 232 error("Post ID was incorrect");
233 }
b8be40ce 234 if (($post->userid <> $USER->id) and !$adminedit) {
501cdbd8 235 error("You can't edit other people's posts!");
236 }
b8be40ce 237 if (((time() - $post->created) > $CFG->maxeditingtime) and !$adminedit) {
cf38360f 238 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
501cdbd8 239 }
240 if ($post->parent) {
11b0c469 241 if (! $parent = forum_get_post_full($post->parent)) {
501cdbd8 242 error("Parent post ID was incorrect ($post->parent)");
243 }
244 }
245 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
246 error("This post is not part of a discussion! ($reply)");
247 }
248 if (! $forum = get_record("forum", "id", $discussion->forum)) {
249 error("The forum number was incorrect ($discussion->forum)");
250 }
251 if (! $course = get_record("course", "id", $discussion->course)) {
252 error("The course number was incorrect ($discussion->course)");
253 }
254
255 // Load up the $post variable.
256
257 $post->edit = $edit;
258
259 $post->course = $course->id;
260 $post->forum = $forum->id;
261
b22b0e61 262 unset($SESSION->fromdiscussion);
501cdbd8 263
264
265 } else if (isset($delete)) { // User is deleting a post
266
11b0c469 267 if (! $post = forum_get_post_full($delete)) {
501cdbd8 268 error("Post ID was incorrect");
269 }
501cdbd8 270 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
271 error("This post is not part of a discussion!");
272 }
64eacd6f 273 if (! $forum = get_record("forum", "id", $discussion->forum)) {
274 error("The forum number was incorrect ($discussion->forum)");
275 }
ebc3bd2b 276 if (($post->userid <> $USER->id) and !isteacher($forum->course)) {
64eacd6f 277 error("You can't delete other people's posts!");
278 }
0d1db48e 279 if (!empty($forum->course)) {
280 if ($course = get_record('course', 'id', $forum->course)) {
281 if (!empty($course->lang)) {
282 $CFG->courselang = $course->lang;
283 }
284 }
285 }
501cdbd8 286
287 if (isset($confirm)) { // User has confirmed the delete
288
289 if ($post->totalscore) {
cf38360f 290 notice(get_string("couldnotdeleteratings", "forum"),
11b0c469 291 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 292
293 } else if (record_exists("forum_posts", "parent", $delete)) {
cf38360f 294 error(get_string("couldnotdeletereplies", "forum"),
11b0c469 295 forum_go_back_to("discuss.php?id=$post->discussion"));
501cdbd8 296
297 } else {
69d79bc3 298 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
299 $cm->id = 0;
300 }
501cdbd8 301 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
64eacd6f 302 if ($forum->type == "single") {
303 notice("Sorry, but you are not allowed to delete that discussion!",
304 forum_go_back_to("discuss.php?d=$post->discussion"));
305 }
501cdbd8 306 forum_delete_discussion($discussion);
307
69d79bc3 308 add_to_log($discussion->course, "forum", "delete discussion",
b17333be 309 "view.php?id=$cm->id", "$forum->id", $cm->id);
69d79bc3 310
501cdbd8 311 redirect("view.php?f=$discussion->forum",
cf38360f 312 get_string("deleteddiscussion", "forum"), 1);
501cdbd8 313
7f6689e4 314 } else if (forum_delete_post($post)) {
501cdbd8 315
69d79bc3 316 add_to_log($discussion->course, "forum", "delete post",
317 "discuss.php?d=$post->discussion", "$post->id", $cm->id);
318
11b0c469 319 redirect(forum_go_back_to("discuss.php?d=$post->discussion"),
cf38360f 320 get_string("deletedpost", "forum"), 1);
501cdbd8 321 } else {
322 error("An error occurred while deleting record $post->id");
323 }
324 }
325
326
327 } else { // User just asked to delete something
328
11b0c469 329 forum_set_return();
501cdbd8 330
331 print_header();
cf38360f 332 notice_yesno(get_string("deletesure", "forum"),
839f2456 333 "post.php?delete=$delete&amp;confirm=$delete",
607809b3 334 $_SERVER["HTTP_REFERER"]);
501cdbd8 335
41905731 336 echo "<center><hr />";
8aed46c7 337 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
839f2456 338 echo "</center>";
501cdbd8 339 }
839f2456 340 print_footer($course);
501cdbd8 341 die;
342
343
cf84431b 344 } else if (isset($prune)) { // Teacher is pruning
345
346 if (! $post = forum_get_post_full($prune)) {
347 error("Post ID was incorrect");
348 }
349 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
350 error("This post is not part of a discussion!");
351 }
352 if (! $forum = get_record("forum", "id", $discussion->forum)) {
353 error("The forum number was incorrect ($discussion->forum)");
354 }
355 if (!isteacher($forum->course)) {
4d35d88b 356 error("You can't split discussions!");
cf84431b 357 }
358 if (!$post->parent) {
359 error('This is already the first post in the discussion');
360 }
361
362 if (isset($_REQUEST['name'])) { // User has confirmed the prune
363
364 $newdiscussion->course = $discussion->course;
365 $newdiscussion->forum = $discussion->forum;
366 $newdiscussion->name = $name;
367 $newdiscussion->firstpost = $post->id;
368 $newdiscussion->userid = $discussion->userid;
369 $newdiscussion->groupid = $discussion->groupid;
370 $newdiscussion->assessed = $discussion->assessed;
371 $newdiscussion->usermodified = $post->userid;
372
373 if (!$newid = insert_record('forum_discussions', $newdiscussion)) {
374 error('Could not create new discussion');
375 }
376
13152de4 377 $newpost->id = $post->id;
378 $newpost->parent = 0;
379 $newpost->subject = $name;
d078ee9b 380
13152de4 381 if (!update_record("forum_posts", $newpost)) {
d078ee9b 382 error('Could not update the original post');
383 }
384
cf84431b 385 forum_change_discussionid($post->id, $newid);
386
387 // set timemodified to time of last post in each discussion
388 $lastpost = get_record_sql("SELECT MAX(modified) AS time
389 FROM {$CFG->prefix}forum_posts
390 WHERE discussion = '$discussion->id'");
391 set_field('forum_discussions', 'timemodified', $lastpost->time, 'id', $discussion->id);
392 $lastpost = get_record_sql("SELECT MAX(modified) AS time
393 FROM {$CFG->prefix}forum_posts
394 WHERE discussion = '$newid'");
395 set_field('forum_discussions', 'timemodified', $lastpost->time, 'id', $newid);
396
397
398 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
399 $cm->id = 0;
400 }
401 add_to_log($discussion->course, "forum", "prune post",
402 "discuss.php?d=$newid", "$post->id", $cm->id);
403
d078ee9b 404 redirect(forum_go_back_to("discuss.php?d=$newid"), get_string("prunedpost", "forum"), 1);
cf84431b 405
406 } else { // User just asked to prune something
407
408 $course = get_record('course', 'id', $forum->course);
409 $strforums = get_string("modulenameplural", "forum");
f950af3c 410 print_header_simple("$discussion->name: $post->subject", "",
411 "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
d078ee9b 412 <a href=\"view.php?f=$forum->id\">$forum->name</a> ->
413 <a href=\"discuss.php?d=$discussion->id\">$post->subject</a> -> ".
cf84431b 414 get_string("prune", "forum"), '', "", true, "", navmenu($course, $cm));
415
416 print_heading(get_string('pruneheading', 'forum'));
d078ee9b 417 echo '<center>';
cf84431b 418
419 include('prune.html');
420
421 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
839f2456 422 echo '</center>';
cf84431b 423 }
839f2456 424 print_footer($course);
cf84431b 425 die;
426
427
501cdbd8 428 } else {
429 error("No operation specified");
430
431 }
432
433
434 // To get here they need to edit a post, and the $post
435 // variable will be loaded with all the particulars,
436 // so bring up the form.
437
438 // $course, $forum are defined. $discussion is for edit and reply only.
439
440 require_login($course->id);
441
dfc9ba9b 442
501cdbd8 443 if ($post->discussion) {
9fa49e22 444 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
501cdbd8 445 error("Could not find top parent of post $post->id");
446 }
447 } else {
cf38360f 448 $toppost->subject = get_string("yournewtopic", "forum");
501cdbd8 449 }
450
3bbde520 451 if (empty($post->subject)) {
0ae5e5ea 452 $formstart = "theform.subject";
3bbde520 453 } else {
454 $formstart = "";
501cdbd8 455 }
456
457 if ($post->parent) {
41905731 458 $navtail = "<a href=\"discuss.php?d=$discussion->id\">$toppost->subject</a> -> ".get_string("editing", "forum");
501cdbd8 459 } else {
460 $navtail = "$toppost->subject";
461 }
462
9c9f7d77 463 if (empty($post->edit)) {
464 $post->edit = "";
465 }
466
cf38360f 467 $strforums = get_string("modulenameplural", "forum");
468
73bb0835 469
41905731 470 $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> <a href=\"view.php?f=$forum->id\">$forum->name</a>";
501cdbd8 471
dfc9ba9b 472 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
473
9c9f7d77 474 if (empty($discussion->name)) {
475 $discussion->name = $forum->name;
476 }
477
501cdbd8 478 if ($course->category) {
479 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
41905731 480 "<a href=../../course/view.php?id=$course->id>$course->shortname</a> ->
3bbde520 481 $navmiddle -> $navtail", $formstart, "", true, "", navmenu($course, $cm));
501cdbd8 482 } else {
483 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
9c9f7d77 484 "$navmiddle -> $navtail", "$formstart", "", true, "", navmenu($course, $cm));
501cdbd8 485
486 }
487
3395f2d6 488 if (!empty($parent)) {
11b0c469 489 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
f8029045 490 if (empty($post->edit)) {
491 forum_print_posts_threaded($parent->id, $course, 0, false, false);
492 }
2e82fd38 493 echo "<center>";
41905731 494 echo "<h2>".get_string("yourreply", "forum").":</h2>";
501cdbd8 495 } else {
2e82fd38 496 echo "<center>";
41905731 497 echo "<h2>".get_string("yournewtopic", "forum")."</h2>";
501cdbd8 498 }
3395f2d6 499 if (!empty($post->error)) {
500 notify($post->error);
501 }
4b00b4b3 502 echo "</center>";
501cdbd8 503
504 print_simple_box_start("center", "", "$THEME->cellheading");
505 require("post.html");
506 print_simple_box_end();
507
4b00b4b3 508 if ($usehtmleditor) {
76138908 509 use_html_editor("message");
4b00b4b3 510 }
511
501cdbd8 512 print_footer($course);
513
514
515?>