Tags for dev
[moodle.git] / mod / forum / post.php
CommitLineData
41905731 1<?php // $Id$
501cdbd8 2
3// Edit and save a new post to a discussion
4
16955dc6 5 require_once('../../config.php');
6 require_once('lib.php');
7
8 $reply = optional_param('reply', 0, PARAM_INT);
70e3da07 9 $forum = optional_param('forum', 0, PARAM_INT);
10 $edit = optional_param('edit', 0, PARAM_INT);
11 $delete = optional_param('delete', 0, PARAM_INT);
12 $prune = optional_param('prune',0,PARAM_INT);
13 $name = optional_param('name','',PARAM_CLEAN);
14 $confirm = optional_param('confirm',0,PARAM_INT);
bbbf2d40 15
7b08a2ea 16 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
17
18 if (has_capability('moodle/legacy:guest', $sitecontext, NULL, false)) {
8d9a0ae6 19
c59eb341 20 $wwwroot = $CFG->wwwroot.'/login/index.php';
21 if (!empty($CFG->loginhttps)) {
bbbf2d40 22 $wwwroot = str_replace('http','https', $wwwroot);
c59eb341 23 }
556963f5 24
7b08a2ea 25 if (!empty($forum)) { // User is starting a new discussion in a forum
556963f5 26 if (! $forum = get_record('forum', 'id', $forum)) {
27 error('The forum number was incorrect');
28 }
16955dc6 29 } else if (!empty($reply)) { // User is writing a new reply
556963f5 30 if (! $parent = forum_get_post_full($reply)) {
31 error('Parent post ID was incorrect');
32 }
33 if (! $discussion = get_record('forum_discussions', 'id', $parent->discussion)) {
34 error('This post is not part of a discussion!');
35 }
36 if (! $forum = get_record('forum', 'id', $discussion->forum)) {
37 error('The forum number was incorrect');
38 }
39 }
40 if (! $course = get_record('course', 'id', $forum->course)) {
41 error('The course number was incorrect');
42 }
e40a7a3c 43
556963f5 44 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
6e89ca55 45 error('Could not get the course module for the forum instance.');
bbbf2d40 46 } else {
47 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
556963f5 48 }
49
50 $strforums = get_string('modulenameplural', 'forum');
352b8da8 51 if ($course->id != SITEID) {
556963f5 52 print_header($course->shortname, $course->fullname,
53 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
e6157a73 54 <a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
cef1ce6a 55 <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a>",
56 '', '', true, "", navmenu($course, $cm));
556963f5 57 } else {
58 print_header($course->shortname, $course->fullname,
e6157a73 59 "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
cef1ce6a 60 <a href=\"view.php?f=$forum->id\">".format_string($forum->name)."</a>",
61 '', '', true, "", navmenu($course, $cm));
556963f5 62 }
c59eb341 63 notice_yesno(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'),
64 $wwwroot, $_SERVER['HTTP_REFERER']);
cd8d4471 65 print_footer($course);
c59eb341 66 exit;
501cdbd8 67 }
68
8e8d0524 69 require_login(0, false); // Script is useless unless they're logged in
48d38fad 70
36b4f985 71 if ($post = data_submitted()) {
bbbf2d40 72 if (empty($post->course)) {
73 error('No course was defined!');
f7abd64a 74 }
75
bbbf2d40 76 if (!$course = get_record('course', 'id', $post->course)) {
f7abd64a 77 error('Could not find specified course!');
78 }
96662058 79
80 if (!empty($forum)) { // Check the forum id and change it to a full object
81 if (! $forum = get_record('forum', 'id', $forum)) {
82 error('The forum number was incorrect');
83 }
84 }
e40a7a3c 85
f7abd64a 86 if (!empty($course->lang)) { // Override current language
87 $CFG->courselang = $course->lang;
0d1db48e 88 }
501cdbd8 89
3395f2d6 90 if (empty($SESSION->fromurl)) {
bbbf2d40 91 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$post->forum";
3395f2d6 92 } else {
93 $errordestination = $SESSION->fromurl;
94 }
95
f2b5d7e3 96 $post->subject = clean_param(strip_tags($post->subject, '<lang><span>'), PARAM_CLEAN); // Strip all tags except multilang
db46e49c 97
f2b5d7e3 98 //$post->message will be cleaned later before display
501cdbd8 99
36257d39 100 $post->attachment = isset($_FILES['attachment']) ? $_FILES['attachment'] : NULL;
7f6689e4 101
bbbf2d40 102 if (!$cm = get_coursemodule_from_instance("forum", $post->forum, $course->id)) { // For the logs
6e89ca55 103 error('Could not get the course module for the forum instance.');
69d79bc3 104 }
e40a7a3c 105 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
f2b5d7e3 106 trusttext_after_edit($post->message, $modcontext);
69d79bc3 107
bbbf2d40 108 if (!$post->subject or !$post->message) {
3395f2d6 109 $post->error = get_string("emptymessage", "forum");
7f6689e4 110
bbbf2d40 111 } else if ($post->edit) { // Updating a post
112 $post->id = $post->edit;
9b7bff26 113 $message = '';
afa3507c 114
bbbf2d40 115 //fix for bug #4314
0fa18d5a 116 if (!$realpost = get_record('forum_posts','id',$post->id)) {
bbbf2d40 117 $realpost = new object;
118 $realpost->userid = -1;
119 }
5f6fc25b 120
121
122 // if user has edit any post capability
123 // or has either startnewdiscussion or reply capability and is editting own post
124 // then he can proceed
125 // MDL-7066
126 if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
127 || has_capability('mod/forum:startdiscussion', $modcontext))) ||
128 has_capability('mod/forum:editanypost', $modcontext)) ) {
bbbf2d40 129 error("You can not update this post");
130 }
131
96662058 132 if ($forum->type == 'news' && !$post->parent) {
8d9a0ae6 133 $updatediscussion = new object;
bbbf2d40 134 $updatediscussion->id = $post->discussion;
fbc21e82 135 if (empty($post->timestartdisabled)) {
136 $updatediscussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
137 } else {
138 $updatediscussion->timestart = 0;
139 }
140 if (empty($post->timeenddisabled)) {
141 $updatediscussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
142 } else {
143 $updatediscussion->timeend = 0;
144 }
145 if (empty($post->timeenddisabled) && $updatediscussion->timeend <= $updatediscussion->timestart) {
146 $post->error = get_string('timestartenderror', 'forum');
147 } elseif (!update_record('forum_discussions', $updatediscussion)) {
148 error(get_string("couldnotupdate", "forum"), $errordestination);
149 }
150 }
9bdedea4 151
fbc21e82 152 if (!isset($post->error)) {
153
bbbf2d40 154 if (forum_update_post($post,$message)) {
8f0cd6ef 155
9bdedea4 156 $timemessage = 2;
157 if (!empty($message)) { // if we're printing stuff about the file upload
158 $timemessage = 4;
159 }
160 $message .= '<br />'.get_string("postupdated", "forum");
8f0cd6ef 161
bbbf2d40 162 if ($subscribemessage = forum_post_subscription($post)) {
9bdedea4 163 $timemessage = 4;
164 }
cef1ce6a 165 if ($forum->type == 'single') {
166 // Single discussion forums are an exception. We show
167 // the forum itself since it only has one discussion
168 // thread.
169 $discussionurl = "view.php?f=$forum->id";
170 } else {
171 $discussionurl = "discuss.php?d=$post->discussion#$post->id";
172 }
173 add_to_log($course->id, "forum", "update post",
174 "$discussionurl&amp;parent=$post->id", "$post->id", $cm->id);
175
176 redirect(forum_go_back_to("$discussionurl#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 177
9bdedea4 178 } else {
179 error(get_string("couldnotupdate", "forum"), $errordestination);
180 }
181 exit;
fbc21e82 182 }
0fa18d5a 183
bbbf2d40 184 } else if ($post->discussion) { // Adding a new post to an existing discussion
db290a6e 185 $message = '';
bbbf2d40 186 if ($post->id = forum_add_new_post($post,$message)) {
69d79bc3 187
0a9f61b5 188 $timemessage = 2;
db290a6e 189 if (!empty($message)) { // if we're printing stuff about the file upload
190 $timemessage = 4;
191 }
192 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
0a9f61b5 193
bbbf2d40 194 if ($subscribemessage = forum_post_subscription($post)) {
0a9f61b5 195 $timemessage = 4;
501cdbd8 196 }
197
f2b5d7e3 198 if (!empty($post->mailnow)) {
41547057 199 $message .= get_string("postmailnow", "forum");
200 $timemessage = 4;
201 }
202
cef1ce6a 203 if ($forum->type == 'single') {
204 // Single discussion forums are an exception. We show
205 // the forum itself since it only has one discussion
206 // thread.
207 $discussionurl = "view.php?f=$forum->id";
208 } else {
209 $discussionurl = "discuss.php?d=$post->discussion";
210 }
211 add_to_log($course->id, "forum", "add post",
212 "$discussionurl&amp;parent=$post->id", "$post->id", $cm->id);
213
214 redirect(forum_go_back_to("$discussionurl#$post->id"), $message.$subscribemessage, $timemessage);
0a9f61b5 215
501cdbd8 216 } else {
8f0cd6ef 217 error(get_string("couldnotadd", "forum"), $errordestination);
501cdbd8 218 }
3395f2d6 219 exit;
220
bbbf2d40 221 } else { // Adding a new discussion
222 $post->mailnow = empty($post->mailnow) ? 0 : 1;
223 $discussion = $post;
224 $discussion->name = $post->subject;
501cdbd8 225 $discussion->intro = $post->message;
fbc21e82 226 $newstopic = false;
8d9a0ae6 227
96662058 228 if ($forum->type == 'news' && !$post->parent) {
fbc21e82 229 $newstopic = true;
230 }
231 if ($newstopic && empty($post->timestartdisabled)) {
232 $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
233 } else {
234 $discussion->timestart = 0;
235 }
236 if ($newstopic && empty($post->timeenddisabled)) {
237 $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
238 } else {
239 $discussion->timeend = 0;
240 }
241 if ($newstopic && empty($post->timeenddisabled) && $discussion->timeend <= $discussion->timestart) {
242 $post->error = get_string('timestartenderror', 'forum');
243 } else {
9bdedea4 244 $message = '';
245 if ($discussion->id = forum_add_discussion($discussion,$message)) {
fbc21e82 246
9bdedea4 247 add_to_log($course->id, "forum", "add discussion",
248 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
69d79bc3 249
9bdedea4 250 $timemessage = 2;
251 if (!empty($message)) { // if we're printing stuff about the file upload
252 $timemessage = 4;
253 }
254 $message .= '<br />'.get_string("postadded", "forum", format_time($CFG->maxeditingtime));
8f0cd6ef 255
bbbf2d40 256 if ($post->mailnow) {
9bdedea4 257 $message .= get_string("postmailnow", "forum");
258 $timemessage = 4;
259 }
41547057 260
9bdedea4 261 if ($subscribemessage = forum_post_subscription($discussion)) {
262 $timemessage = 4;
263 }
0a9f61b5 264
bbbf2d40 265 redirect(forum_go_back_to("view.php?f=$post->forum"), $message.$subscribemessage, $timemessage);
0a9f61b5 266
9bdedea4 267 } else {
268 error(get_string("couldnotadd", "forum"), $errordestination);
269 }
fbc21e82 270
9bdedea4 271 exit;
fbc21e82 272 }
501cdbd8 273 }
501cdbd8 274 }
275
da077b9a 276 if ($usehtmleditor = can_use_html_editor()) {
213e8cc6 277 $defaultformat = FORMAT_HTML;
213e8cc6 278 } else {
279 $defaultformat = FORMAT_MOODLE;
280 }
501cdbd8 281
bbbf2d40 282 if (isset($post->error)) { // User is re-editing a failed posting
3395f2d6 283
284 // Set up all the required objects again, and reuse the same $post
285
286 if (! $forum = get_record("forum", "id", $post->forum)) {
287 error("The forum number was incorrect ($post->forum)");
288 }
289
290 if (! $course = get_record("course", "id", $forum->course)) {
291 error("The course number was incorrect ($forum->course)");
292 }
293
294 if (!empty($post->parent)) {
295 if (! $parent = forum_get_post_full($post->parent)) {
296 error("Parent post ID was incorrect ($post->parent)");
297 }
298 }
299
300 if (!empty($post->discussion)) {
301 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
302 error("This post is not part of a discussion! ($post->discussion)");
303 }
cc86131d 304 } else {
305 $discussion = new stdClass();
306 $newstopic = false;
307 if ($forum->type == 'news' && !$post->parent) {
308 $newstopic = true;
309 }
310 if ($newstopic && empty($post->timestartdisabled)) {
311 $discussion->timestart = make_timestamp($post->timestartyear, $post->timestartmonth, $post->timestartday);
312 } else {
313 $discussion->timestart = 0;
314 }
315 if ($newstopic && empty($post->timeenddisabled)) {
316 $discussion->timeend = make_timestamp($post->timeendyear, $post->timeendmonth, $post->timeendday);
317 } else {
318 $discussion->timeend = 0;
319 }
3395f2d6 320 }
321
352b8da8 322 $post->subject = stripslashes_safe($post->subject);
323 $post->message = stripslashes_safe($post->message);
324
bbbf2d40 325 } else if (!empty($forum)) { // User is starting a new discussion in a forum
501cdbd8 326
bbbf2d40 327 $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
501cdbd8 328
329 if (! $forum = get_record("forum", "id", $forum)) {
330 error("The forum number was incorrect ($forum)");
331 }
332 if (! $course = get_record("course", "id", $forum->course)) {
3395f2d6 333 error("The course number was incorrect ($forum->course)");
501cdbd8 334 }
e40a7a3c 335 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
336
11b0c469 337 if (! forum_user_can_post_discussion($forum)) {
a321d42b 338 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
339 $SESSION->wantsurl = $FULLME;
340 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
341 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
342 } else {
343 print_error('nopostforum', 'forum');
344 }
501cdbd8 345 }
e40a7a3c 346
80602101 347 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
8bcf97e1 348 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
80602101 349 error(get_string("activityiscurrentlyhidden"));
350 }
351 }
352
501cdbd8 353 // Load up the $post variable.
354
355 $post->course = $course->id;
356 $post->forum = $forum->id;
357 $post->discussion = 0; // ie discussion # not defined yet
358 $post->parent = 0;
359 $post->subject = "";
ebc3bd2b 360 $post->userid = $USER->id;
501cdbd8 361 $post->message = "";
213e8cc6 362 $post->format = $defaultformat;
501cdbd8 363
bbbf2d40 364 $post->groupid = get_current_group($course->id);
365 if ($post->groupid == 0) {
366 $post->groupid = -1;
367 }
368
11b0c469 369 forum_set_return();
370
bbbf2d40 371 } else if (!empty($reply)) { // User is writing a new reply
501cdbd8 372
11b0c469 373 if (! $parent = forum_get_post_full($reply)) {
29ad118c 374 error("Parent post ID was incorrect");
501cdbd8 375 }
376 if (! $discussion = get_record("forum_discussions", "id", $parent->discussion)) {
29ad118c 377 error("This post is not part of a discussion!");
501cdbd8 378 }
379 if (! $forum = get_record("forum", "id", $discussion->forum)) {
380 error("The forum number was incorrect ($discussion->forum)");
381 }
382 if (! $course = get_record("course", "id", $discussion->course)) {
383 error("The course number was incorrect ($discussion->course)");
384 }
bd4128e9 385
e40a7a3c 386 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
387
6c506ca7 388 if (! forum_user_can_post($forum)) {
bd4128e9 389 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
390 $SESSION->wantsurl = $FULLME;
391 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
392 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
393 } else {
394 print_error('nopostforum', 'forum');
395 }
6c506ca7 396 }
e40a7a3c 397
02509fe6 398 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
bbbf2d40 399 if (groupmode($course, $cm)) { // Make sure user can post here
63e20b88 400 $mygroupid = mygroupid($course->id);
fa22fd5f 401 if (!((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid)/*$mygroupid == $discussion->groupid*/))) {
02509fe6 402 error("Sorry, but you can not post in this discussion.");
403 }
404 }
0468976c 405 if (!$cm->visible and !has_capability('moodle/course:manageactivities', $coursecontext)) {
80602101 406 error(get_string("activityiscurrentlyhidden"));
407 }
02509fe6 408 }
409
501cdbd8 410 // Load up the $post variable.
411
412 $post->course = $course->id;
413 $post->forum = $forum->id;
414 $post->discussion = $parent->discussion;
415 $post->parent = $parent->id;
416 $post->subject = $parent->subject;
ebc3bd2b 417 $post->userid = $USER->id;
501cdbd8 418 $post->message = "";
213e8cc6 419 $post->format = $defaultformat;
501cdbd8 420
e9584ca3 421 $strre = get_string('re', 'forum');
422 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
423 $post->subject = $strre.' '.$post->subject;
501cdbd8 424 }
425
b22b0e61 426 unset($SESSION->fromdiscussion);
501cdbd8 427
bbbf2d40 428 } else if (!empty($edit)) { // User is editing their own post
b8be40ce 429
11b0c469 430 if (! $post = forum_get_post_full($edit)) {
501cdbd8 431 error("Post ID was incorrect");
432 }
501cdbd8 433 if ($post->parent) {
11b0c469 434 if (! $parent = forum_get_post_full($post->parent)) {
501cdbd8 435 error("Parent post ID was incorrect ($post->parent)");
436 }
437 }
438 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
439 error("This post is not part of a discussion! ($reply)");
440 }
441 if (! $forum = get_record("forum", "id", $discussion->forum)) {
442 error("The forum number was incorrect ($discussion->forum)");
443 }
444 if (! $course = get_record("course", "id", $discussion->course)) {
445 error("The course number was incorrect ($discussion->course)");
446 }
e51e3fcb 447 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
6e89ca55 448 error('Could not get the course module for the forum instance.');
449 } else {
450 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
451 }
e80ca6c6 452 if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
453 if (((time() - $post->created) > $CFG->maxeditingtime) and
454 !has_capability('mod/forum:editanypost', $modcontext)) {
455 error( get_string("maxtimehaspassed", "forum", format_time($CFG->maxeditingtime)) );
456 }
457 }
6e89ca55 458 if (($post->userid <> $USER->id) and
459 !has_capability('mod/forum:editanypost', $modcontext)) {
460 error("You can't edit other people's posts!");
461 }
501cdbd8 462
463 // Load up the $post variable.
501cdbd8 464 $post->edit = $edit;
465
466 $post->course = $course->id;
467 $post->forum = $forum->id;
468
b22b0e61 469 unset($SESSION->fromdiscussion);
501cdbd8 470
471
bbbf2d40 472 } else if (!empty($delete)) { // User is deleting a post
501cdbd8 473
11b0c469 474 if (! $post = forum_get_post_full($delete)) {
501cdbd8 475 error("Post ID was incorrect");
476 }
501cdbd8 477 if (! $discussion = get_record("forum_discussions", "id", $post->discussion)) {
478 error("This post is not part of a discussion!");
479 }
64eacd6f 480 if (! $forum = get_record("forum", "id", $discussion->forum)) {
481 error("The forum number was incorrect ($discussion->forum)");
482 }
e40a7a3c 483 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
6e89ca55 484 error('Could not get the course module for the forum instance.');
e40a7a3c 485 } else {
486 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
487 }
0468976c 488 if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
489 || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
bbbf2d40 490 error("You can't delete this post!");
64eacd6f 491 }
0d1db48e 492 if (!empty($forum->course)) {
493 if ($course = get_record('course', 'id', $forum->course)) {
494 if (!empty($course->lang)) {
495 $CFG->courselang = $course->lang;
496 }
497 }
498 }
501cdbd8 499
b82faacd 500 $replycount = forum_count_replies($post);
501
bbbf2d40 502 if (!empty($confirm)) { // User has confirmed the delete
501cdbd8 503
504 if ($post->totalscore) {
8f0cd6ef 505 notice(get_string("couldnotdeleteratings", "forum"),
11b0c469 506 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 507
0468976c 508 } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
cf38360f 509 error(get_string("couldnotdeletereplies", "forum"),
8203d211 510 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 511
512 } else {
513 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
cef1ce6a 514 if ($forum->type == 'single') {
8f0cd6ef 515 notice("Sorry, but you are not allowed to delete that discussion!",
64eacd6f 516 forum_go_back_to("discuss.php?d=$post->discussion"));
517 }
501cdbd8 518 forum_delete_discussion($discussion);
519
8f0cd6ef 520 add_to_log($discussion->course, "forum", "delete discussion",
b17333be 521 "view.php?id=$cm->id", "$forum->id", $cm->id);
69d79bc3 522
8f0cd6ef 523 redirect("view.php?f=$discussion->forum",
cf38360f 524 get_string("deleteddiscussion", "forum"), 1);
501cdbd8 525
0468976c 526 } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext))) {
cef1ce6a 527
528 if ($forum->type == 'single') {
529 // Single discussion forums are an exception. We show
530 // the forum itself since it only has one discussion
531 // thread.
532 $discussionurl = "view.php?f=$forum->id";
533 } else {
534 $discussionurl = "discuss.php?d=$post->discussion";
535 }
536
537 add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
69d79bc3 538
b82faacd 539 $feedback = $replycount ? get_string('deletedposts', 'forum') : get_string('deletedpost', 'forum');
cef1ce6a 540 redirect(forum_go_back_to($discussionurl), $feedback, 1);
501cdbd8 541 } else {
542 error("An error occurred while deleting record $post->id");
543 }
544 }
545
546
bbbf2d40 547 } else { // User just asked to delete something
501cdbd8 548
11b0c469 549 forum_set_return();
501cdbd8 550
b82faacd 551 if ($replycount) {
6e89ca55 552 if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
b82faacd 553 error(get_string("couldnotdeletereplies", "forum"),
554 forum_go_back_to("discuss.php?d=$post->discussion"));
555 }
556 print_header();
557 notice_yesno(get_string("deletesureplural", "forum", $replycount+1),
bbbf2d40 558 "post.php?delete=$delete&amp;confirm=$delete",
b82faacd 559 $_SERVER["HTTP_REFERER"]);
560
561 forum_print_post($post, $course->id, $ownpost=false, $reply=false, $link=false);
562 if (empty($post->edit)) {
eaf50aef 563 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
b82faacd 564 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
565 } else {
566 $user_read_array = array();
567 }
568 forum_print_posts_nested($post->id, $course->id, false, false, $user_read_array, $forum->id);
569 }
570 } else {
571 print_header();
572 notice_yesno(get_string("deletesure", "forum", $replycount),
bbbf2d40 573 "post.php?delete=$delete&amp;confirm=$delete",
b82faacd 574 $_SERVER["HTTP_REFERER"]);
575 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
576 }
8f0cd6ef 577
501cdbd8 578 }
839f2456 579 print_footer($course);
501cdbd8 580 die;
581
582
6e89ca55 583 } else if (!empty($prune)) { // Pruning
8f0cd6ef 584
0d5da5dd 585 if (!$post = forum_get_post_full($prune)) {
cf84431b 586 error("Post ID was incorrect");
587 }
0d5da5dd 588 if (!$discussion = get_record("forum_discussions", "id", $post->discussion)) {
cf84431b 589 error("This post is not part of a discussion!");
590 }
0d5da5dd 591 if (!$forum = get_record("forum", "id", $discussion->forum)) {
cf84431b 592 error("The forum number was incorrect ($discussion->forum)");
593 }
77dc6d0b 594 if ($forum->type == 'single') {
595 error('Discussions from this forum cannot be split');
596 }
cf84431b 597 if (!$post->parent) {
598 error('This is already the first post in the discussion');
599 }
0d5da5dd 600 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
6e89ca55 601 error('Could not get the course module for the forum instance.');
602 } else {
603 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
604 }
605 if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
606 error("You can't split discussions!");
0d5da5dd 607 }
cf84431b 608
bbbf2d40 609 if (!empty($name)) { // User has confirmed the prune
8f0cd6ef 610
cf84431b 611 $newdiscussion->course = $discussion->course;
612 $newdiscussion->forum = $discussion->forum;
bbbf2d40 613 $newdiscussion->name = $name;
cf84431b 614 $newdiscussion->firstpost = $post->id;
615 $newdiscussion->userid = $discussion->userid;
616 $newdiscussion->groupid = $discussion->groupid;
617 $newdiscussion->assessed = $discussion->assessed;
618 $newdiscussion->usermodified = $post->userid;
fbc21e82 619 $newdiscussion->timestart = $discussion->timestart;
620 $newdiscussion->timeend = $discussion->timeend;
8f0cd6ef 621
cf84431b 622 if (!$newid = insert_record('forum_discussions', $newdiscussion)) {
623 error('Could not create new discussion');
624 }
8f0cd6ef 625
13152de4 626 $newpost->id = $post->id;
627 $newpost->parent = 0;
bbbf2d40 628 $newpost->subject = $name;
d078ee9b 629
13152de4 630 if (!update_record("forum_posts", $newpost)) {
d078ee9b 631 error('Could not update the original post');
632 }
633
cf84431b 634 forum_change_discussionid($post->id, $newid);
8f0cd6ef 635
1da8c568 636 // update last post in each discussion
637 forum_discussion_update_last_post($discussion->id);
638 forum_discussion_update_last_post($newid);
cf84431b 639
8f0cd6ef 640 add_to_log($discussion->course, "forum", "prune post",
cf84431b 641 "discuss.php?d=$newid", "$post->id", $cm->id);
642
d078ee9b 643 redirect(forum_go_back_to("discuss.php?d=$newid"), get_string("prunedpost", "forum"), 1);
cf84431b 644
645 } else { // User just asked to prune something
646
647 $course = get_record('course', 'id', $forum->course);
648 $strforums = get_string("modulenameplural", "forum");
c78ac798 649 print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "",
8f0cd6ef 650 "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> ->
3849dae8 651 <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true)."</a> ->
17dc3f3c 652 <a href=\"discuss.php?d=$discussion->id\">".format_string($post->subject,true)."</a> -> ".
cf84431b 653 get_string("prune", "forum"), '', "", true, "", navmenu($course, $cm));
8f0cd6ef 654
cf84431b 655 print_heading(get_string('pruneheading', 'forum'));
d078ee9b 656 echo '<center>';
8f0cd6ef 657
cf84431b 658 include('prune.html');
8f0cd6ef 659
cf84431b 660 forum_print_post($post, $forum->course, $ownpost=false, $reply=false, $link=false);
839f2456 661 echo '</center>';
cf84431b 662 }
839f2456 663 print_footer($course);
cf84431b 664 die;
665
666
501cdbd8 667 } else {
668 error("No operation specified");
669
670 }
671
672
8f0cd6ef 673 // To get here they need to edit a post, and the $post
501cdbd8 674 // variable will be loaded with all the particulars,
675 // so bring up the form.
676
677 // $course, $forum are defined. $discussion is for edit and reply only.
678
ec81373f 679 $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id);
680
681 require_login($course->id, false, $cm);
501cdbd8 682
f2b5d7e3 683 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
dfc9ba9b 684
501cdbd8 685 if ($post->discussion) {
9fa49e22 686 if (! $toppost = get_record("forum_posts", "discussion", $post->discussion, "parent", 0)) {
501cdbd8 687 error("Could not find top parent of post $post->id");
688 }
689 } else {
680afe2e 690 $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
21da9db8 691 get_string("addanewdiscussion", "forum");
501cdbd8 692 }
693
3bbde520 694 if (empty($post->subject)) {
352b8da8 695 $formstart = 'theform.subject';
3bbde520 696 } else {
352b8da8 697 $formstart = '';
501cdbd8 698 }
699
700 if ($post->parent) {
352b8da8 701 $navtail = ' -> <a href="discuss.php?d='.$discussion->id.'">'.format_string($toppost->subject,true).'</a> -> '.
702 get_string('editing', 'forum');
501cdbd8 703 } else {
352b8da8 704 $navtail = ' -> '.format_string($toppost->subject);
501cdbd8 705 }
706
9c9f7d77 707 if (empty($post->edit)) {
352b8da8 708 $post->edit = '';
9c9f7d77 709 }
710
cf38360f 711 $strforums = get_string("modulenameplural", "forum");
712
73bb0835 713
cef1ce6a 714 $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">$strforums</a> -> <a href=\"view.php?f=$forum->id\">".format_string($forum->name,true).'</a> ';
501cdbd8 715
f37da850 716 if (empty($discussion->name)) {
8cb091e6 717 if (empty($discussion)) {
718 $discussion = new object;
719 }
9c9f7d77 720 $discussion->name = $forum->name;
721 }
cef1ce6a 722 if ($forum->type == 'single') {
723 // There is only one discussion thread for this forum type. We should
724 // not show the discussion name (same as forum name in this case) in
725 // the breadcrumbs.
726 $strdiscussionname = '';
727 $navtail = '';
728 } else {
729 // Show the discussion name in the breadcrumbs.
730 $strdiscussionname = format_string($discussion->name).':';
731 }
352b8da8 732 if ($course->id != SITEID) {
cef1ce6a 733 print_header("$course->shortname: $strdiscussionname ".
734 format_string($toppost->subject), "$course->fullname",
735 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
736 $navmiddle $navtail", $formstart, "", true, "", navmenu($course, $cm));
680afe2e 737
501cdbd8 738 } else {
cef1ce6a 739 print_header("$course->shortname: $strdiscussionname ".
740 format_string($toppost->subject), "$course->fullname",
741 "$navmiddle $navtail", "$formstart", "", true, "", navmenu($course, $cm));
501cdbd8 742
743 }
744
098d27d4 745// checkup
951e1073 746 if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post)) {
098d27d4 747 error("You cannot reply to this post");
748 }
bbbf2d40 749 if (empty($parent) && !forum_user_can_post_discussion($forum)) {
098d27d4 750 error("You cannot start a new discussion in this forum");
751 }
752
0468976c 753 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 754 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
098d27d4 755 notify(get_string('qandanotify','forum'));
756 }
757
a4f495bf 758 forum_check_throttling($forum);
759
3395f2d6 760 if (!empty($parent)) {
11b0c469 761 forum_print_post($parent, $course->id, $ownpost=false, $reply=false, $link=false);
f8029045 762 if (empty($post->edit)) {
eaf50aef 763 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
f37da850 764 $user_read_array = forum_tp_get_discussion_read_records($USER->id, $discussion->id);
765 } else {
766 $user_read_array = array();
767 }
0468976c 768 if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
098d27d4 769 forum_print_posts_threaded($parent->id, $course->id, 0, false, false, $user_read_array, $discussion->forum);
770 }
f8029045 771 }
556963f5 772 print_heading(get_string("yourreply", "forum").':');
501cdbd8 773 } else {
21da9db8 774 $forum->intro = trim($forum->intro);
775 if (!empty($forum->intro)) {
776 print_simple_box(format_text($forum->intro), 'center');
777 }
098d27d4 778 if ($forum->type == 'qanda') {
779 print_heading(get_string('yournewquestion','forum'));
780 } else {
781 print_heading(get_string('yournewtopic', 'forum'));
782 }
501cdbd8 783 }
556963f5 784 echo '<center>';
3395f2d6 785 if (!empty($post->error)) {
786 notify($post->error);
787 }
556963f5 788 echo '</center>';
501cdbd8 789
9bdedea4 790 if ($USER->id != $post->userid) { // Not the original author, so add a message to the end
791 $data->date = userdate($post->modified);
792 if ($post->format == FORMAT_HTML) {
793 $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
794 fullname($USER).'</a>';
795 $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
796 } else {
797 $data->name = fullname($USER);
798 $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
799 }
800 }
801
d30867b0 802 print_simple_box_start("center");
501cdbd8 803 require("post.html");
804 print_simple_box_end();
805
4b00b4b3 806 if ($usehtmleditor) {
76138908 807 use_html_editor("message");
4b00b4b3 808 }
809
501cdbd8 810 print_footer($course);
811
812
96662058 813?>