Fixed the primary key problem in the mysql.
[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",
49 "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id);
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",
68 "discuss.php?d=$post->discussion&parent=$post->id", "$post->id", $cm->id);
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"),
501cdbd8 333 "post.php?delete=$delete&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);
501cdbd8 338 }
339
340 die;
341
342
cf84431b 343 } else if (isset($prune)) { // Teacher is pruning
344
345 if (! $post = forum_get_post_full($prune)) {
346 error("Post ID was incorrect");
347 }
348 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
349 error("This post is not part of a discussion!");
350 }
351 if (! $forum = get_record("forum", "id", $discussion->forum)) {
352 error("The forum number was incorrect ($discussion->forum)");
353 }
354 if (!isteacher($forum->course)) {
4d35d88b 355 error("You can't split discussions!");
cf84431b 356 }
357 if (!$post->parent) {
358 error('This is already the first post in the discussion');
359 }
360
361 if (isset($_REQUEST['name'])) { // User has confirmed the prune
362
363 $newdiscussion->course = $discussion->course;
364 $newdiscussion->forum = $discussion->forum;
365 $newdiscussion->name = $name;
366 $newdiscussion->firstpost = $post->id;
367 $newdiscussion->userid = $discussion->userid;
368 $newdiscussion->groupid = $discussion->groupid;
369 $newdiscussion->assessed = $discussion->assessed;
370 $newdiscussion->usermodified = $post->userid;
371
372 if (!$newid = insert_record('forum_discussions', $newdiscussion)) {
373 error('Could not create new discussion');
374 }
375
13152de4 376 $newpost->id = $post->id;
377 $newpost->parent = 0;
378 $newpost->subject = $name;
d078ee9b 379
13152de4 380 if (!update_record("forum_posts", $newpost)) {
d078ee9b 381 error('Could not update the original post');
382 }
383
cf84431b 384 forum_change_discussionid($post->id, $newid);
385
386 // set timemodified to time of last post in each discussion
387 $lastpost = get_record_sql("SELECT MAX(modified) AS time
388 FROM {$CFG->prefix}forum_posts
389 WHERE discussion = '$discussion->id'");
390 set_field('forum_discussions', 'timemodified', $lastpost->time, 'id', $discussion->id);
391 $lastpost = get_record_sql("SELECT MAX(modified) AS time
392 FROM {$CFG->prefix}forum_posts
393 WHERE discussion = '$newid'");
394 set_field('forum_discussions', 'timemodified', $lastpost->time, 'id', $newid);
395
396
397 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
398 $cm->id = 0;
399 }
400 add_to_log($discussion->course, "forum", "prune post",
401 "discuss.php?d=$newid", "$post->id", $cm->id);
402
d078ee9b 403 redirect(forum_go_back_to("discuss.php?d=$newid"), get_string("prunedpost", "forum"), 1);
cf84431b 404
405 } else { // User just asked to prune something
406
407 $course = get_record('course', 'id', $forum->course);
408 $strforums = get_string("modulenameplural", "forum");
f950af3c 409 print_header_simple("$discussion->name: $post->subject", "",
410 "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
d078ee9b 411 <a href=\"view.php?f=$forum->id\">$forum->name</a> ->
412 <a href=\"discuss.php?d=$discussion->id\">$post->subject</a> -> ".
cf84431b 413 get_string("prune", "forum"), '', "", true, "", navmenu($course, $cm));
414
415 print_heading(get_string('pruneheading', 'forum'));
d078ee9b 416 echo '<center>';
cf84431b 417
418 include('prune.html');
419
420 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
421 }
422
423 die;
424
425
501cdbd8 426 } else {
427 error("No operation specified");
428
429 }
430
431
432 // To get here they need to edit a post, and the $post
433 // variable will be loaded with all the particulars,
434 // so bring up the form.
435
436 // $course, $forum are defined. $discussion is for edit and reply only.
437
438 require_login($course->id);
439
dfc9ba9b 440
501cdbd8 441 if ($post->discussion) {
9fa49e22 442 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
501cdbd8 443 error("Could not find top parent of post $post->id");
444 }
445 } else {
cf38360f 446 $toppost->subject = get_string("yournewtopic", "forum");
501cdbd8 447 }
448
3bbde520 449 if (empty($post->subject)) {
0ae5e5ea 450 $formstart = "theform.subject";
3bbde520 451 } else {
452 $formstart = "";
501cdbd8 453 }
454
455 if ($post->parent) {
41905731 456 $navtail = "<a href=\"discuss.php?d=$discussion->id\">$toppost->subject</a> -> ".get_string("editing", "forum");
501cdbd8 457 } else {
458 $navtail = "$toppost->subject";
459 }
460
9c9f7d77 461 if (empty($post->edit)) {
462 $post->edit = "";
463 }
464
cf38360f 465 $strforums = get_string("modulenameplural", "forum");
466
73bb0835 467
41905731 468 $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> <a href=\"view.php?f=$forum->id\">$forum->name</a>";
501cdbd8 469
dfc9ba9b 470 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
471
9c9f7d77 472 if (empty($discussion->name)) {
473 $discussion->name = $forum->name;
474 }
475
501cdbd8 476 if ($course->category) {
477 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
41905731 478 "<a href=../../course/view.php?id=$course->id>$course->shortname</a> ->
3bbde520 479 $navmiddle -> $navtail", $formstart, "", true, "", navmenu($course, $cm));
501cdbd8 480 } else {
481 print_header("$course->shortname: $discussion->name: $toppost->subject", "$course->fullname",
9c9f7d77 482 "$navmiddle -> $navtail", "$formstart", "", true, "", navmenu($course, $cm));
501cdbd8 483
484 }
485
3395f2d6 486 if (!empty($parent)) {
11b0c469 487 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
f8029045 488 if (empty($post->edit)) {
489 forum_print_posts_threaded($parent->id, $course, 0, false, false);
490 }
2e82fd38 491 echo "<center>";
41905731 492 echo "<h2>".get_string("yourreply", "forum").":</h2>";
501cdbd8 493 } else {
2e82fd38 494 echo "<center>";
41905731 495 echo "<h2>".get_string("yournewtopic", "forum")."</h2>";
501cdbd8 496 }
3395f2d6 497 if (!empty($post->error)) {
498 notify($post->error);
499 }
4b00b4b3 500 echo "</center>";
501cdbd8 501
502 print_simple_box_start("center", "", "$THEME->cellheading");
503 require("post.html");
504 print_simple_box_end();
505
4b00b4b3 506 if ($usehtmleditor) {
76138908 507 use_html_editor("message");
4b00b4b3 508 }
509
501cdbd8 510 print_footer($course);
511
512
513?>