mod-folder MDL-19799 Added set_url calls, replaced deprecated functions
[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
0e52f01f 8 $reply = optional_param('reply', 0, PARAM_INT);
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);
2b63df96 14 $confirm = optional_param('confirm', 0, PARAM_INT);
89d35c49 15 $groupid = optional_param('groupid', null, PARAM_INT);
2b63df96 16
d3558659 17 $PAGE->set_url('/mod/forum/post.php', array(
18 'reply' => $reply,
19 'forum' => $forum,
20 'edit' => $edit,
21 'delete'=> $delete,
22 'prune' => $prune,
23 'name' => $name,
24 'confirm'=>$confirm,
25 'groupid'=>$groupid,
26 ));
0e52f01f 27 //these page_params will be passed as hidden variables later in the form.
28 $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit);
2b63df96 29
30 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
7b08a2ea 31
32 if (has_capability('moodle/legacy:guest', $sitecontext, NULL, false)) {
8d9a0ae6 33
7b08a2ea 34 if (!empty($forum)) { // User is starting a new discussion in a forum
4e445355 35 if (! $forum = $DB->get_record('forum', array('id' => $forum))) {
12e57b92 36 print_error('invalidforumid', 'forum');
556963f5 37 }
16955dc6 38 } else if (!empty($reply)) { // User is writing a new reply
556963f5 39 if (! $parent = forum_get_post_full($reply)) {
12e57b92 40 print_error('invalidparentpostid', 'forum');
556963f5 41 }
4e445355 42 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
12e57b92 43 print_error('notpartofdiscussion', 'forum');
556963f5 44 }
4e445355 45 if (! $forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
12e57b92 46 print_error('invalidforumid');
556963f5 47 }
48 }
4e445355 49 if (! $course = $DB->get_record('course', array('id' => $forum->course))) {
12e57b92 50 print_error('invalidcourseid');
556963f5 51 }
2b63df96 52
556963f5 53 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
12e57b92 54 print_error('invalidcoursemodule');
bbbf2d40 55 } else {
56 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
556963f5 57 }
58
e1526d77 59 if (!get_referer()) { // No referer - probably coming in via email See MDL-9052
60 require_login();
61 }
65bcf17b 62
15ca5e5e 63 $PAGE->set_title($course->shortname);
64 $PAGE->set_heading($course->fullname);
d3558659 65
15ca5e5e 66 echo $OUTPUT->header();
59e28d8f 67 echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), get_referer(false));
396fb912 68 echo $OUTPUT->footer();
c59eb341 69 exit;
501cdbd8 70 }
0e52f01f 71
8e8d0524 72 require_login(0, false); // Script is useless unless they're logged in
48d38fad 73
2b63df96 74 if (!empty($forum)) { // User is starting a new discussion in a forum
4e445355 75 if (! $forum = $DB->get_record("forum", array("id" => $forum))) {
12e57b92 76 print_error('invalidforumid', 'forum');
501cdbd8 77 }
4e445355 78 if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
12e57b92 79 print_error('invalidcourseid');
501cdbd8 80 }
65bcf17b 81 if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
12e57b92 82 print_error("invalidcoursemodule");
65bcf17b 83 }
84
e40a7a3c 85 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
65bcf17b 86
89d35c49 87 if (! forum_user_can_post_discussion($forum, $groupid, -1, $cm)) {
a321d42b 88 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
89 $SESSION->wantsurl = $FULLME;
90 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
91 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
92 } else {
93 print_error('nopostforum', 'forum');
94 }
501cdbd8 95 }
2b63df96 96
65bcf17b 97 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
5a2a5331 98 print_error("activityiscurrentlyhidden");
80602101 99 }
100
3b27b0fe 101 if (isset($_SERVER["HTTP_REFERER"])) {
102 $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
103 } else {
104 $SESSION->fromurl = '';
105 }
2b63df96 106
107
501cdbd8 108 // Load up the $post variable.
109
0e52f01f 110 $post = new object();
e2d7687f 111 $post->course = $course->id;
112 $post->forum = $forum->id;
113 $post->discussion = 0; // ie discussion # not defined yet
114 $post->parent = 0;
115 $post->subject = '';
116 $post->userid = $USER->id;
117 $post->message = '';
118 $post->messageformat = FORMAT_HTML; // TODO: better default
702dc57b 119 $post->messagetrust = 0;
501cdbd8 120
89d35c49 121 if (isset($groupid)) {
122 $post->groupid = $groupid;
3b27b0fe 123 } else {
89d35c49 124 $post->groupid = groups_get_activity_group($cm);
bbbf2d40 125 }
89d35c49 126
11b0c469 127 forum_set_return();
128
bbbf2d40 129 } else if (!empty($reply)) { // User is writing a new reply
501cdbd8 130
11b0c469 131 if (! $parent = forum_get_post_full($reply)) {
12e57b92 132 print_error('invalidparentpostid', 'forum');
501cdbd8 133 }
4e445355 134 if (! $discussion = $DB->get_record("forum_discussions", array("id" => $parent->discussion))) {
12e57b92 135 print_error('notpartofdiscussion', 'forum');
501cdbd8 136 }
4e445355 137 if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
12e57b92 138 print_error('invalidforumid', 'forum');
501cdbd8 139 }
4e445355 140 if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
12e57b92 141 print_error('invalidcourseid');
501cdbd8 142 }
8ce8fc85 143 if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
12e57b92 144 print_error('invalidcoursemodule');
8ce8fc85 145 }
bd4128e9 146
c13a5e71 147 // Ensure lang, theme, etc. is set up properly. MDL-6926
148 $PAGE->set_course($course);
4cabf99f 149
e40a7a3c 150 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
8ce8fc85 151 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
ea3caf69 152
8b79a625 153 if (! forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext)) {
bd4128e9 154 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
155 $SESSION->wantsurl = $FULLME;
156 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
157 redirect($CFG->wwwroot.'/course/enrol.php?id='.$course->id, get_string('youneedtoenrol'));
158 } else {
159 print_error('nopostforum', 'forum');
160 }
6c506ca7 161 }
2b63df96 162
89d35c49 163 // Make sure user can post here
164 if (groupmode($course, $cm) == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
4136c239 165 if ($discussion->groupid == -1) {
89d35c49 166 print_error('nopostforum', 'forum');
4136c239 167 } else {
168 if (!groups_is_member($discussion->groupid)) {
169 print_error('nopostforum', 'forum');
170 }
80602101 171 }
02509fe6 172 }
4136c239 173
1e7ba87e 174 if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
5a2a5331 175 print_error("activityiscurrentlyhidden");
8ce8fc85 176 }
02509fe6 177
501cdbd8 178 // Load up the $post variable.
179
0e52f01f 180 $post = new object();
181 $post->course = $course->id;
182 $post->forum = $forum->id;
501cdbd8 183 $post->discussion = $parent->discussion;
0e52f01f 184 $post->parent = $parent->id;
185 $post->subject = $parent->subject;
186 $post->userid = $USER->id;
187 $post->message = '';
501cdbd8 188
89d35c49 189 $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
4136c239 190
e9584ca3 191 $strre = get_string('re', 'forum');
192 if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
193 $post->subject = $strre.' '.$post->subject;
501cdbd8 194 }
195
b22b0e61 196 unset($SESSION->fromdiscussion);
0e52f01f 197
bbbf2d40 198 } else if (!empty($edit)) { // User is editing their own post
b8be40ce 199
11b0c469 200 if (! $post = forum_get_post_full($edit)) {
12e57b92 201 print_error('invalidpostid', 'forum');
501cdbd8 202 }
501cdbd8 203 if ($post->parent) {
11b0c469 204 if (! $parent = forum_get_post_full($post->parent)) {
12e57b92 205 print_error('invalidparentpostid', 'forum');
501cdbd8 206 }
207 }
2b63df96 208
4e445355 209 if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
12e57b92 210 print_error('notpartofdiscussion', 'forum');
501cdbd8 211 }
4e445355 212 if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
12e57b92 213 print_error('invalidforumid', 'forum');
501cdbd8 214 }
4e445355 215 if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
12e57b92 216 print_error('invalidcourseid');
501cdbd8 217 }
e51e3fcb 218 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
12e57b92 219 print_error('invalidcoursemodule');
6e89ca55 220 } else {
221 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
222 }
e80ca6c6 223 if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
224 if (((time() - $post->created) > $CFG->maxeditingtime) and
225 !has_capability('mod/forum:editanypost', $modcontext)) {
12e57b92 226 print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
e80ca6c6 227 }
228 }
6e89ca55 229 if (($post->userid <> $USER->id) and
230 !has_capability('mod/forum:editanypost', $modcontext)) {
12e57b92 231 print_error('cannoteditposts', 'forum');
6e89ca55 232 }
501cdbd8 233
501cdbd8 234
0e52f01f 235 // Load up the $post variable.
236 $post->edit = $edit;
237 $post->course = $course->id;
501cdbd8 238 $post->forum = $forum->id;
89d35c49 239 $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
501cdbd8 240
e2d7687f 241 $post = trusttext_pre_edit($post, 'message', $modcontext);
0e52f01f 242
b22b0e61 243 unset($SESSION->fromdiscussion);
501cdbd8 244
245
2b63df96 246 }else if (!empty($delete)) { // User is deleting a post
501cdbd8 247
11b0c469 248 if (! $post = forum_get_post_full($delete)) {
12e57b92 249 print_error('invalidpostid', 'forum');
501cdbd8 250 }
4e445355 251 if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
12e57b92 252 print_error('notpartofdiscussion', 'forum');
501cdbd8 253 }
4e445355 254 if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
12e57b92 255 print_error('invalidforumid', 'forum');
64eacd6f 256 }
e40a7a3c 257 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
12e57b92 258 print_error('invalidcoursemodule');
e40a7a3c 259 }
4e445355 260 if (!$course = $DB->get_record('course', array('id' => $forum->course))) {
12e57b92 261 print_error('invalidcourseid');
dcf6d93c 262 }
263
264 require_login($course, false, $cm);
265 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
266
0468976c 267 if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
268 || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
12e57b92 269 print_error('cannotdeletepost', 'forum');
64eacd6f 270 }
501cdbd8 271
2b63df96 272
b82faacd 273 $replycount = forum_count_replies($post);
274
1639731b 275 if (!empty($confirm) && confirm_sesskey()) { // User has confirmed the delete
501cdbd8 276
277 if ($post->totalscore) {
8f0cd6ef 278 notice(get_string("couldnotdeleteratings", "forum"),
11b0c469 279 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 280
0468976c 281 } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
5a2a5331 282 print_error("couldnotdeletereplies", "forum",
8203d211 283 forum_go_back_to("discuss.php?d=$post->discussion"));
501cdbd8 284
285 } else {
286 if (! $post->parent) { // post is a discussion topic as well, so delete discussion
cef1ce6a 287 if ($forum->type == 'single') {
8f0cd6ef 288 notice("Sorry, but you are not allowed to delete that discussion!",
64eacd6f 289 forum_go_back_to("discuss.php?d=$post->discussion"));
290 }
4e6a816a 291 forum_delete_discussion($discussion, false, $course, $cm, $forum);
501cdbd8 292
8f0cd6ef 293 add_to_log($discussion->course, "forum", "delete discussion",
b17333be 294 "view.php?id=$cm->id", "$forum->id", $cm->id);
4e6a816a 295
2b63df96 296 redirect("view.php?f=$discussion->forum");
501cdbd8 297
4e781c7b 298 } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext),
299 $course, $cm, $forum)) {
2b63df96 300
cef1ce6a 301 if ($forum->type == 'single') {
302 // Single discussion forums are an exception. We show
303 // the forum itself since it only has one discussion
304 // thread.
305 $discussionurl = "view.php?f=$forum->id";
306 } else {
307 $discussionurl = "discuss.php?d=$post->discussion";
308 }
2b63df96 309
cef1ce6a 310 add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
69d79bc3 311
2b63df96 312 redirect(forum_go_back_to($discussionurl));
501cdbd8 313 } else {
12e57b92 314 print_error('errorwhiledelete', 'forum');
501cdbd8 315 }
316 }
317
318
bbbf2d40 319 } else { // User just asked to delete something
501cdbd8 320
11b0c469 321 forum_set_return();
501cdbd8 322
b82faacd 323 if ($replycount) {
6e89ca55 324 if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
5a2a5331 325 print_error("couldnotdeletereplies", "forum",
b82faacd 326 forum_go_back_to("discuss.php?d=$post->discussion"));
327 }
15ca5e5e 328 echo $OUTPUT->header();
59e28d8f 329 echo $OUTPUT->confirm(get_string("deletesureplural", "forum", $replycount+1),
330 "post.php?delete=$delete&confirm=$delete",
cbcd697b 331 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
b82faacd 332
65bcf17b 333 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
90f4745c 334
b82faacd 335 if (empty($post->edit)) {
90f4745c 336 $forumtracked = forum_tp_is_tracked($forum);
337 $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
338 forum_print_posts_nested($course, $cm, $forum, $discussion, $post, false, false, $forumtracked, $posts);
b82faacd 339 }
340 } else {
15ca5e5e 341 echo $OUTPUT->header();
59e28d8f 342 echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
343 "post.php?delete=$delete&confirm=$delete",
cbcd697b 344 $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
65bcf17b 345 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
b82faacd 346 }
8f0cd6ef 347
501cdbd8 348 }
396fb912 349 echo $OUTPUT->footer();
501cdbd8 350 die;
351
352
6e89ca55 353 } else if (!empty($prune)) { // Pruning
8f0cd6ef 354
0d5da5dd 355 if (!$post = forum_get_post_full($prune)) {
12e57b92 356 print_error('invalidpostid', 'forum');
cf84431b 357 }
4e445355 358 if (!$discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
12e57b92 359 print_error('notpartofdiscussion', 'forum');
cf84431b 360 }
4e445355 361 if (!$forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
12e57b92 362 print_error('invalidforumid', 'forum');
cf84431b 363 }
77dc6d0b 364 if ($forum->type == 'single') {
12e57b92 365 print_error('cannotsplit', 'forum');
77dc6d0b 366 }
cf84431b 367 if (!$post->parent) {
12e57b92 368 print_error('alreadyfirstpost', 'forum');
cf84431b 369 }
0d5da5dd 370 if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
12e57b92 371 print_error('invalidcoursemodule');
6e89ca55 372 } else {
373 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
374 }
375 if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
12e57b92 376 print_error('cannotsplit', 'forum');
0d5da5dd 377 }
cf84431b 378
d44d02a2 379 if (!empty($name) && confirm_sesskey()) { // User has confirmed the prune
8f0cd6ef 380
0e52f01f 381 $newdiscussion = new object();
382 $newdiscussion->course = $discussion->course;
383 $newdiscussion->forum = $discussion->forum;
384 $newdiscussion->name = $name;
385 $newdiscussion->firstpost = $post->id;
386 $newdiscussion->userid = $discussion->userid;
387 $newdiscussion->groupid = $discussion->groupid;
388 $newdiscussion->assessed = $discussion->assessed;
cf84431b 389 $newdiscussion->usermodified = $post->userid;
0e52f01f 390 $newdiscussion->timestart = $discussion->timestart;
391 $newdiscussion->timeend = $discussion->timeend;
8f0cd6ef 392
a8f3a651 393 $newid = $DB->insert_record('forum_discussions', $newdiscussion);
8f0cd6ef 394
0e52f01f 395 $newpost = new object();
396 $newpost->id = $post->id;
397 $newpost->parent = 0;
bbbf2d40 398 $newpost->subject = $name;
d078ee9b 399
a8d6ef8c 400 $DB->update_record("forum_posts", $newpost);
d078ee9b 401
cf84431b 402 forum_change_discussionid($post->id, $newid);
8f0cd6ef 403
1da8c568 404 // update last post in each discussion
405 forum_discussion_update_last_post($discussion->id);
406 forum_discussion_update_last_post($newid);
cf84431b 407
8f0cd6ef 408 add_to_log($discussion->course, "forum", "prune post",
cf84431b 409 "discuss.php?d=$newid", "$post->id", $cm->id);
4e6a816a 410
2b63df96 411 redirect(forum_go_back_to("discuss.php?d=$newid"));
cf84431b 412
413 } else { // User just asked to prune something
414
4e445355 415 $course = $DB->get_record('course', array('id' => $forum->course));
65bcf17b 416
15ca5e5e 417 $PAGE->navbar->add(format_string($post->subject, true), new moodle_url($CFG->wwwroot.'/mod/forum/discuss.php', array('d'=>$discussion->id)));
418 $PAGE->navbar->add(get_string("prune", "forum"));
419 $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
420 echo $OUTPUT->header();
f43f1834 421 echo $OUTPUT->heading(get_string('pruneheading', 'forum'));
d078ee9b 422 echo '<center>';
8f0cd6ef 423
cf84431b 424 include('prune.html');
8f0cd6ef 425
65bcf17b 426 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
839f2456 427 echo '</center>';
cf84431b 428 }
396fb912 429 echo $OUTPUT->footer();
cf84431b 430 die;
501cdbd8 431 } else {
12e57b92 432 print_error('unknowaction');
501cdbd8 433
434 }
435
2b63df96 436 if (!isset($coursecontext)) {
437 // Has not yet been set by post.php.
438 $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course);
439 }
440
12fab708 441
442// from now on user must be logged on properly
443
c58efbf5 444 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
12e57b92 445 print_error('invalidcoursemodule');
c58efbf5 446 }
447 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
c13a5e71 448 require_login($course, false, $cm);
12fab708 449
450 if (isguestuser()) {
451 // just in case
452 print_error('noguest');
453 }
c58efbf5 454
30a9aff5 455 if (!isset($forum->maxattachments)) { // TODO - delete this once we add a field to the forum table
456 $forum->maxattachments = 3;
457 }
458
4cabf99f 459 require_once('post_form.php');
460
4136c239 461 $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
2b63df96 462
edc0c493 463 $draftitemid = file_get_submitted_draft_itemid('attachments');
a19a06d0 464 file_prepare_draft_area($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id);
2b63df96 465
12fab708 466 //load data into form NOW!
467
468 if ($USER->id != $post->userid) { // Not the original author, so add a message to the end
469 $data->date = userdate($post->modified);
e2d7687f 470 if ($post->messageformat == FORMAT_HTML) {
12fab708 471 $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
472 fullname($USER).'</a>';
473 $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
474 } else {
475 $data->name = fullname($USER);
476 $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
477 }
478 }
479
480 if (!empty($parent)) {
481 $heading = get_string("yourreply", "forum");
482 } else {
483 if ($forum->type == 'qanda') {
484 $heading = get_string('yournewquestion', 'forum');
485 } else {
486 $heading = get_string('yournewtopic', 'forum');
487 }
488 }
489
490 if (forum_is_subscribed($USER->id, $forum->id)) {
491 $subscribe = true;
492
493 } else if (forum_user_has_posted($forum->id, 0, $USER->id)) {
494 $subscribe = false;
495
496 } else {
497 // user not posted yet - use subscription default specified in profile
498 $subscribe = !empty($USER->autosubscribe);
499 }
500
edc0c493 501 $draftid_editor = file_get_submitted_draft_itemid('message');
a19a06d0 502 $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'forum_post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message);
12fab708 503 $mform_post->set_data(array( 'attachments'=>$draftitemid,
504 'general'=>$heading,
505 'subject'=>$post->subject,
8d96a7b4 506 'message'=>array(
507 'text'=>$currenttext,
e2d7687f 508 'format'=>empty($post->messageformat) ? FORMAT_HTML : $post->messageformat, //TODO: add some better default
8d96a7b4 509 'itemid'=>$draftid_editor
510 ),
12fab708 511 'subscribe'=>$subscribe?1:0,
512 'mailnow'=>!empty($post->mailnow),
513 'userid'=>$post->userid,
514 'parent'=>$post->parent,
515 'discussion'=>$post->discussion,
516 'course'=>$course->id) +
517 $page_params +
518
519 (isset($post->format)?array(
520 'format'=>$post->format):
521 array())+
522
523 (isset($discussion->timestart)?array(
524 'timestart'=>$discussion->timestart):
525 array())+
526
527 (isset($discussion->timeend)?array(
528 'timeend'=>$discussion->timeend):
529 array())+
530
531 (isset($post->groupid)?array(
532 'groupid'=>$post->groupid):
533 array())+
534
535 (isset($discussion->id)?
536 array('discussion'=>$discussion->id):
537 array()));
538
539 if ($fromform = $mform_post->get_data()) {
2b63df96 540
541 if (empty($SESSION->fromurl)) {
542 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
543 } else {
544 $errordestination = $SESSION->fromurl;
545 }
546
e2d7687f 547 $fromform->itemid = $fromform->message['itemid'];
e2d7687f 548 $fromform->messageformat = $fromform->message['format'];
330895d5 549 $fromform->message = $fromform->message['text'];
550 // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
e2d7687f 551 $fromform->messagetrust = trusttext_trusted($modcontext);
2b63df96 552
553 if ($fromform->edit) { // Updating a post
89d35c49 554 unset($fromform->groupid);
2b63df96 555 $fromform->id = $fromform->edit;
556 $message = '';
557
558 //fix for bug #4314
4e445355 559 if (!$realpost = $DB->get_record('forum_posts', array('id' => $fromform->id))) {
2b63df96 560 $realpost = new object;
561 $realpost->userid = -1;
562 }
563
564
565 // if user has edit any post capability
566 // or has either startnewdiscussion or reply capability and is editting own post
567 // then he can proceed
568 // MDL-7066
569 if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
570 || has_capability('mod/forum:startdiscussion', $modcontext))) ||
571 has_capability('mod/forum:editanypost', $modcontext)) ) {
12e57b92 572 print_error('cannotupdatepost', 'forum');
2b63df96 573 }
574
dd97c328 575 $updatepost = $fromform; //realpost
576 $updatepost->forum = $forum->id;
0faf6791 577 if (!forum_update_post($updatepost, $mform_post, $message)) {
5a2a5331 578 print_error("couldnotupdate", "forum", $errordestination);
2b63df96 579 }
65bcf17b 580
0a30a59c 581 // MDL-11818
582 if (($forum->type == 'single') && ($updatepost->parent == '0')){ // updating first post of single discussion type -> updating forum intro
583 $forum->intro = $updatepost->message;
584 $forum->timemodified = time();
9d749339 585 $DB->update_record("forum", $forum);
0a30a59c 586 }
2b63df96 587
588 $timemessage = 2;
589 if (!empty($message)) { // if we're printing stuff about the file upload
590 $timemessage = 4;
591 }
592 $message .= '<br />'.get_string("postupdated", "forum");
593
de2047e7 594 if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
2b63df96 595 $timemessage = 4;
596 }
597 if ($forum->type == 'single') {
598 // Single discussion forums are an exception. We show
599 // the forum itself since it only has one discussion
600 // thread.
601 $discussionurl = "view.php?f=$forum->id";
602 } else {
cbcd697b 603 $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
2b63df96 604 }
605 add_to_log($course->id, "forum", "update post",
606 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
607
608 redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
609
610 exit;
611
612
613 } else if ($fromform->discussion) { // Adding a new post to an existing discussion
89d35c49 614 unset($fromform->groupid);
2b63df96 615 $message = '';
e2d7687f 616 $addpost = $fromform;
2b63df96 617 $addpost->forum=$forum->id;
0faf6791 618 if ($fromform->id = forum_add_new_post($addpost, $mform_post, $message)) {
2b63df96 619
620 $timemessage = 2;
621 if (!empty($message)) { // if we're printing stuff about the file upload
622 $timemessage = 4;
623 }
65bcf17b 624
de2047e7 625 if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
2b63df96 626 $timemessage = 4;
627 }
628
c718dd57 629 if (!empty($fromform->mailnow)) {
2b63df96 630 $message .= get_string("postmailnow", "forum");
631 $timemessage = 4;
0af02710 632 } else {
902d5cc0 633 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
634 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
2b63df96 635 }
636
637 if ($forum->type == 'single') {
638 // Single discussion forums are an exception. We show
639 // the forum itself since it only has one discussion
640 // thread.
641 $discussionurl = "view.php?f=$forum->id";
642 } else {
643 $discussionurl = "discuss.php?d=$discussion->id";
644 }
645 add_to_log($course->id, "forum", "add post",
646 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
4e6a816a 647
4e781c7b 648 // Update completion state
649 $completion=new completion_info($course);
4e6a816a 650 if($completion->is_enabled($cm) &&
4e781c7b 651 ($forum->completionreplies || $forum->completionposts)) {
652 $completion->update_state($cm,COMPLETION_COMPLETE);
653 }
2b63df96 654
3b73b15d 655 redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
2b63df96 656
657 } else {
5a2a5331 658 print_error("couldnotadd", "forum", $errordestination);
2b63df96 659 }
660 exit;
661
662 } else { // Adding a new discussion
4136c239 663 if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
12e57b92 664 print_error('cannotcreatediscussion', 'forum');
4136c239 665 }
89d35c49 666 if (empty($fromform->groupid)) {
667 $fromform->groupid = -1;
668 }
669
2b63df96 670 $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
671 $discussion = $fromform;
672 $discussion->name = $fromform->subject;
673 $discussion->intro = $fromform->message;
674 $newstopic = false;
675
676 if ($forum->type == 'news' && !$fromform->parent) {
677 $newstopic = true;
678 }
11f260f4 679 $discussion->timestart = $fromform->timestart;
680 $discussion->timeend = $fromform->timeend;
2b63df96 681
682 $message = '';
0faf6791 683 if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {
2b63df96 684
685 add_to_log($course->id, "forum", "add discussion",
686 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
687
688 $timemessage = 2;
689 if (!empty($message)) { // if we're printing stuff about the file upload
690 $timemessage = 4;
691 }
65bcf17b 692
2b63df96 693 if ($fromform->mailnow) {
694 $message .= get_string("postmailnow", "forum");
695 $timemessage = 4;
0af02710 696 } else {
902d5cc0 697 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
698 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
65bcf17b 699 }
2b63df96 700
de2047e7 701 if ($subscribemessage = forum_post_subscription($discussion, $forum)) {
2b63df96 702 $timemessage = 4;
703 }
704
4e781c7b 705 // Update completion status
706 $completion=new completion_info($course);
707 if($completion->is_enabled($cm) &&
708 ($forum->completiondiscussions || $forum->completionposts)) {
709 $completion->update_state($cm,COMPLETION_COMPLETE);
710 }
711
2b63df96 712 redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
713
714 } else {
5a2a5331 715 print_error("couldnotadd", "forum", $errordestination);
2b63df96 716 }
717
718 exit;
719 }
720 }
721
722
501cdbd8 723
8f0cd6ef 724 // To get here they need to edit a post, and the $post
501cdbd8 725 // variable will be loaded with all the particulars,
726 // so bring up the form.
727
728 // $course, $forum are defined. $discussion is for edit and reply only.
729
501cdbd8 730 if ($post->discussion) {
4e445355 731 if (! $toppost = $DB->get_record("forum_posts", array("discussion" => $post->discussion, "parent" => 0))) {
73c763b7 732 print_error('cannotfindparentpost', 'forum', '', $post->id);
501cdbd8 733 }
734 } else {
680afe2e 735 $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
21da9db8 736 get_string("addanewdiscussion", "forum");
501cdbd8 737 }
738
9c9f7d77 739 if (empty($post->edit)) {
352b8da8 740 $post->edit = '';
9c9f7d77 741 }
65bcf17b 742
f37da850 743 if (empty($discussion->name)) {
8cb091e6 744 if (empty($discussion)) {
745 $discussion = new object;
746 }
9c9f7d77 747 $discussion->name = $forum->name;
748 }
cef1ce6a 749 if ($forum->type == 'single') {
750 // There is only one discussion thread for this forum type. We should
751 // not show the discussion name (same as forum name in this case) in
752 // the breadcrumbs.
753 $strdiscussionname = '';
cef1ce6a 754 } else {
755 // Show the discussion name in the breadcrumbs.
756 $strdiscussionname = format_string($discussion->name).':';
757 }
8dec2253 758
759 $forcefocus = empty($reply) ? NULL : 'message';
760
d3558659 761 if (!empty($discussion->id)) {
762 $PAGE->navbar->add(format_string($toppost->subject, true), "discuss.php?d=$discussion->id");
763 }
15ca5e5e 764
38e179a4 765 if ($post->parent) {
15ca5e5e 766 $PAGE->navbar->add(get_string('editing', 'forum'));
38e179a4 767 }
65bcf17b 768
15ca5e5e 769 $PAGE->set_title("$course->shortname: $strdiscussionname ".format_string($toppost->subject));
770 $PAGE->set_heading($course->fullname);
771 $PAGE->set_focuscontrol($mform_post->focus($forcefocus));
772
773 echo $OUTPUT->header();
65bcf17b 774
098d27d4 775// checkup
65bcf17b 776 if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
12e57b92 777 print_error('cannotreply', 'forum');
098d27d4 778 }
89d35c49 779 if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
12e57b92 780 print_error('cannotcreatediscussion', 'forum');
098d27d4 781 }
782
b304e7f7 783 if ($forum->type == 'qanda'
784 && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
785 && !empty($discussion->id)
786 && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
9146b979 787 echo $OUTPUT->notification(get_string('qandanotify','forum'));
098d27d4 788 }
789
90f4745c 790 forum_check_throttling($forum, $cm);
a4f495bf 791
3395f2d6 792 if (!empty($parent)) {
4e445355 793 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
12e57b92 794 print_error('notpartofdiscussion', 'forum');
65bcf17b 795 }
796
797 forum_print_post($parent, $discussion, $forum, $cm, $course, false, false, false);
f8029045 798 if (empty($post->edit)) {
0468976c 799 if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
90f4745c 800 $forumtracked = forum_tp_is_tracked($forum);
801 $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
802 forum_print_posts_threaded($course, $cm, $forum, $discussion, $parent, 0, false, false, $forumtracked, $posts);
098d27d4 803 }
f8029045 804 }
501cdbd8 805 } else {
21da9db8 806 if (!empty($forum->intro)) {
698c90d0 807 echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
21da9db8 808 }
501cdbd8 809 }
501cdbd8 810
2b63df96 811 $mform_post->display();
501cdbd8 812
396fb912 813 echo $OUTPUT->footer();
501cdbd8 814
815
96662058 816?>