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