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