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