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