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