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