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