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