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