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