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