MDL-14589 file api improvements - converting more params to general $options array
[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
38e179a4 53 $navigation = build_navigation('', $cm);
e3f58dfb 54 print_header($course->shortname, $course->fullname, $navigation, '' , '', true, "", navmenu($course, $cm));
55
c59eb341 56 notice_yesno(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'),
93f66983 57 get_login_url(), get_referer(false));
cd8d4471 58 print_footer($course);
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 }
318 print_header();
319 notice_yesno(get_string("deletesureplural", "forum", $replycount+1),
1639731b 320 "post.php?delete=$delete&amp;confirm=$delete&amp;sesskey=".sesskey(),
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 {
331 print_header();
332 notice_yesno(get_string("deletesure", "forum", $replycount),
1639731b 333 "post.php?delete=$delete&amp;confirm=$delete&amp;sesskey=".sesskey(),
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 }
839f2456 339 print_footer($course);
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
4e445355 383 if (!$newid = $DB->insert_record('forum_discussions', $newdiscussion)) {
12e57b92 384 print_error('cannotcreatediscussion', 'forum');
cf84431b 385 }
8f0cd6ef 386
0e52f01f 387 $newpost = new object();
388 $newpost->id = $post->id;
389 $newpost->parent = 0;
bbbf2d40 390 $newpost->subject = $name;
d078ee9b 391
4e445355 392 if (!$DB->update_record("forum_posts", $newpost)) {
12e57b92 393 print_error('cannotupdatediscussion', 'forum');
d078ee9b 394 }
395
cf84431b 396 forum_change_discussionid($post->id, $newid);
8f0cd6ef 397
1da8c568 398 // update last post in each discussion
399 forum_discussion_update_last_post($discussion->id);
400 forum_discussion_update_last_post($newid);
cf84431b 401
8f0cd6ef 402 add_to_log($discussion->course, "forum", "prune post",
cf84431b 403 "discuss.php?d=$newid", "$post->id", $cm->id);
4e6a816a 404
2b63df96 405 redirect(forum_go_back_to("discuss.php?d=$newid"));
cf84431b 406
407 } else { // User just asked to prune something
408
4e445355 409 $course = $DB->get_record('course', array('id' => $forum->course));
65bcf17b 410
01e38388 411 $navlinks = array();
3b27b0fe 412 $navlinks[] = array('name' => format_string($post->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
413 $navlinks[] = array('name' => get_string("prune", "forum"), 'link' => '', 'type' => 'title');
38e179a4 414 $navigation = build_navigation($navlinks, $cm);
e3f58dfb 415 print_header_simple(format_string($discussion->name).": ".format_string($post->subject), "", $navigation, '', "", true, "", navmenu($course, $cm));
8f0cd6ef 416
cf84431b 417 print_heading(get_string('pruneheading', 'forum'));
d078ee9b 418 echo '<center>';
8f0cd6ef 419
cf84431b 420 include('prune.html');
8f0cd6ef 421
65bcf17b 422 forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
839f2456 423 echo '</center>';
cf84431b 424 }
839f2456 425 print_footer($course);
cf84431b 426 die;
501cdbd8 427 } else {
12e57b92 428 print_error('unknowaction');
501cdbd8 429
430 }
431
2b63df96 432 if (!isset($coursecontext)) {
433 // Has not yet been set by post.php.
434 $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course);
435 }
436
12fab708 437
438// from now on user must be logged on properly
439
c58efbf5 440 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
12e57b92 441 print_error('invalidcoursemodule');
c58efbf5 442 }
443 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
c13a5e71 444 require_login($course, false, $cm);
12fab708 445
446 if (isguestuser()) {
447 // just in case
448 print_error('noguest');
449 }
c58efbf5 450
30a9aff5 451 if (!isset($forum->maxattachments)) { // TODO - delete this once we add a field to the forum table
452 $forum->maxattachments = 3;
453 }
454
4cabf99f 455 require_once('post_form.php');
456
4136c239 457 $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
2b63df96 458
edc0c493 459 $draftitemid = file_get_submitted_draft_itemid('attachments');
a19a06d0 460 file_prepare_draft_area($draftitemid, $modcontext->id, 'forum_attachment', empty($post->id)?null:$post->id);
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);
e2d7687f 466 if ($post->messageformat == FORMAT_HTML) {
12fab708 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
edc0c493 497 $draftid_editor = file_get_submitted_draft_itemid('message');
a19a06d0 498 $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'forum_post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message);
12fab708 499 $mform_post->set_data(array( 'attachments'=>$draftitemid,
500 'general'=>$heading,
501 'subject'=>$post->subject,
8d96a7b4 502 'message'=>array(
503 'text'=>$currenttext,
e2d7687f 504 'format'=>empty($post->messageformat) ? FORMAT_HTML : $post->messageformat, //TODO: add some better default
8d96a7b4 505 'itemid'=>$draftid_editor
506 ),
12fab708 507 'subscribe'=>$subscribe?1:0,
508 'mailnow'=>!empty($post->mailnow),
509 'userid'=>$post->userid,
510 'parent'=>$post->parent,
511 'discussion'=>$post->discussion,
512 'course'=>$course->id) +
513 $page_params +
514
515 (isset($post->format)?array(
516 'format'=>$post->format):
517 array())+
518
519 (isset($discussion->timestart)?array(
520 'timestart'=>$discussion->timestart):
521 array())+
522
523 (isset($discussion->timeend)?array(
524 'timeend'=>$discussion->timeend):
525 array())+
526
527 (isset($post->groupid)?array(
528 'groupid'=>$post->groupid):
529 array())+
530
531 (isset($discussion->id)?
532 array('discussion'=>$discussion->id):
533 array()));
534
535 if ($fromform = $mform_post->get_data()) {
2b63df96 536
537 if (empty($SESSION->fromurl)) {
538 $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
539 } else {
540 $errordestination = $SESSION->fromurl;
541 }
542
e2d7687f 543 $fromform->itemid = $fromform->message['itemid'];
e2d7687f 544 $fromform->messageformat = $fromform->message['format'];
330895d5 545 $fromform->message = $fromform->message['text'];
546 // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
e2d7687f 547 $fromform->messagetrust = trusttext_trusted($modcontext);
2b63df96 548
549 if ($fromform->edit) { // Updating a post
89d35c49 550 unset($fromform->groupid);
2b63df96 551 $fromform->id = $fromform->edit;
552 $message = '';
553
554 //fix for bug #4314
4e445355 555 if (!$realpost = $DB->get_record('forum_posts', array('id' => $fromform->id))) {
2b63df96 556 $realpost = new object;
557 $realpost->userid = -1;
558 }
559
560
561 // if user has edit any post capability
562 // or has either startnewdiscussion or reply capability and is editting own post
563 // then he can proceed
564 // MDL-7066
565 if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
566 || has_capability('mod/forum:startdiscussion', $modcontext))) ||
567 has_capability('mod/forum:editanypost', $modcontext)) ) {
12e57b92 568 print_error('cannotupdatepost', 'forum');
2b63df96 569 }
570
dd97c328 571 $updatepost = $fromform; //realpost
572 $updatepost->forum = $forum->id;
0faf6791 573 if (!forum_update_post($updatepost, $mform_post, $message)) {
5a2a5331 574 print_error("couldnotupdate", "forum", $errordestination);
2b63df96 575 }
65bcf17b 576
0a30a59c 577 // MDL-11818
578 if (($forum->type == 'single') && ($updatepost->parent == '0')){ // updating first post of single discussion type -> updating forum intro
579 $forum->intro = $updatepost->message;
580 $forum->timemodified = time();
4e445355 581 if (!$DB->update_record("forum", $forum)) {
5a2a5331 582 print_error("couldnotupdate", "forum", $errordestination);
0a30a59c 583 }
584 }
2b63df96 585
586 $timemessage = 2;
587 if (!empty($message)) { // if we're printing stuff about the file upload
588 $timemessage = 4;
589 }
590 $message .= '<br />'.get_string("postupdated", "forum");
591
de2047e7 592 if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
2b63df96 593 $timemessage = 4;
594 }
595 if ($forum->type == 'single') {
596 // Single discussion forums are an exception. We show
597 // the forum itself since it only has one discussion
598 // thread.
599 $discussionurl = "view.php?f=$forum->id";
600 } else {
cbcd697b 601 $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
2b63df96 602 }
603 add_to_log($course->id, "forum", "update post",
604 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
605
606 redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
607
608 exit;
609
610
611 } else if ($fromform->discussion) { // Adding a new post to an existing discussion
89d35c49 612 unset($fromform->groupid);
2b63df96 613 $message = '';
e2d7687f 614 $addpost = $fromform;
2b63df96 615 $addpost->forum=$forum->id;
0faf6791 616 if ($fromform->id = forum_add_new_post($addpost, $mform_post, $message)) {
2b63df96 617
618 $timemessage = 2;
619 if (!empty($message)) { // if we're printing stuff about the file upload
620 $timemessage = 4;
621 }
65bcf17b 622
de2047e7 623 if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
2b63df96 624 $timemessage = 4;
625 }
626
c718dd57 627 if (!empty($fromform->mailnow)) {
2b63df96 628 $message .= get_string("postmailnow", "forum");
629 $timemessage = 4;
0af02710 630 } else {
902d5cc0 631 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
632 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
2b63df96 633 }
634
635 if ($forum->type == 'single') {
636 // Single discussion forums are an exception. We show
637 // the forum itself since it only has one discussion
638 // thread.
639 $discussionurl = "view.php?f=$forum->id";
640 } else {
641 $discussionurl = "discuss.php?d=$discussion->id";
642 }
643 add_to_log($course->id, "forum", "add post",
644 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
4e6a816a 645
4e781c7b 646 // Update completion state
647 $completion=new completion_info($course);
4e6a816a 648 if($completion->is_enabled($cm) &&
4e781c7b 649 ($forum->completionreplies || $forum->completionposts)) {
650 $completion->update_state($cm,COMPLETION_COMPLETE);
651 }
2b63df96 652
3b73b15d 653 redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
2b63df96 654
655 } else {
5a2a5331 656 print_error("couldnotadd", "forum", $errordestination);
2b63df96 657 }
658 exit;
659
660 } else { // Adding a new discussion
4136c239 661 if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
12e57b92 662 print_error('cannotcreatediscussion', 'forum');
4136c239 663 }
89d35c49 664 if (empty($fromform->groupid)) {
665 $fromform->groupid = -1;
666 }
667
2b63df96 668 $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
669 $discussion = $fromform;
670 $discussion->name = $fromform->subject;
671 $discussion->intro = $fromform->message;
672 $newstopic = false;
673
674 if ($forum->type == 'news' && !$fromform->parent) {
675 $newstopic = true;
676 }
11f260f4 677 $discussion->timestart = $fromform->timestart;
678 $discussion->timeend = $fromform->timeend;
2b63df96 679
680 $message = '';
0faf6791 681 if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {
2b63df96 682
683 add_to_log($course->id, "forum", "add discussion",
684 "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
685
686 $timemessage = 2;
687 if (!empty($message)) { // if we're printing stuff about the file upload
688 $timemessage = 4;
689 }
65bcf17b 690
2b63df96 691 if ($fromform->mailnow) {
692 $message .= get_string("postmailnow", "forum");
693 $timemessage = 4;
0af02710 694 } else {
902d5cc0 695 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
696 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
65bcf17b 697 }
2b63df96 698
de2047e7 699 if ($subscribemessage = forum_post_subscription($discussion, $forum)) {
2b63df96 700 $timemessage = 4;
701 }
702
4e781c7b 703 // Update completion status
704 $completion=new completion_info($course);
705 if($completion->is_enabled($cm) &&
706 ($forum->completiondiscussions || $forum->completionposts)) {
707 $completion->update_state($cm,COMPLETION_COMPLETE);
708 }
709
2b63df96 710 redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
711
712 } else {
5a2a5331 713 print_error("couldnotadd", "forum", $errordestination);
2b63df96 714 }
715
716 exit;
717 }
718 }
719
720
501cdbd8 721
8f0cd6ef 722 // To get here they need to edit a post, and the $post
501cdbd8 723 // variable will be loaded with all the particulars,
724 // so bring up the form.
725
726 // $course, $forum are defined. $discussion is for edit and reply only.
727
501cdbd8 728 if ($post->discussion) {
4e445355 729 if (! $toppost = $DB->get_record("forum_posts", array("discussion" => $post->discussion, "parent" => 0))) {
73c763b7 730 print_error('cannotfindparentpost', 'forum', '', $post->id);
501cdbd8 731 }
732 } else {
680afe2e 733 $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
21da9db8 734 get_string("addanewdiscussion", "forum");
501cdbd8 735 }
736
9c9f7d77 737 if (empty($post->edit)) {
352b8da8 738 $post->edit = '';
9c9f7d77 739 }
65bcf17b 740
f37da850 741 if (empty($discussion->name)) {
8cb091e6 742 if (empty($discussion)) {
743 $discussion = new object;
744 }
9c9f7d77 745 $discussion->name = $forum->name;
746 }
cef1ce6a 747 if ($forum->type == 'single') {
748 // There is only one discussion thread for this forum type. We should
749 // not show the discussion name (same as forum name in this case) in
750 // the breadcrumbs.
751 $strdiscussionname = '';
cef1ce6a 752 } else {
753 // Show the discussion name in the breadcrumbs.
754 $strdiscussionname = format_string($discussion->name).':';
755 }
8dec2253 756
757 $forcefocus = empty($reply) ? NULL : 'message';
758
38e179a4 759 $navlinks = array();
760 if ($post->parent) {
761 $navlinks[] = array('name' => format_string($toppost->subject, true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
65bcf17b 762 $navlinks[] = array('name' => get_string('editing', 'forum'), 'link' => '', 'type' => 'title');
38e179a4 763 } else {
764 $navlinks[] = array('name' => format_string($toppost->subject), 'link' => '', 'type' => 'title');
765 }
766 $navigation = build_navigation($navlinks, $cm);
65bcf17b 767
e3f58dfb 768 print_header("$course->shortname: $strdiscussionname ".
769 format_string($toppost->subject), $course->fullname,
770 $navigation, $mform_post->focus($forcefocus), "", true, "", navmenu($course, $cm));
65bcf17b 771
098d27d4 772// checkup
65bcf17b 773 if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
12e57b92 774 print_error('cannotreply', 'forum');
098d27d4 775 }
89d35c49 776 if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
12e57b92 777 print_error('cannotcreatediscussion', 'forum');
098d27d4 778 }
779
b304e7f7 780 if ($forum->type == 'qanda'
781 && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
782 && !empty($discussion->id)
783 && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
784 notify(get_string('qandanotify','forum'));
098d27d4 785 }
786
90f4745c 787 forum_check_throttling($forum, $cm);
a4f495bf 788
3395f2d6 789 if (!empty($parent)) {
4e445355 790 if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
12e57b92 791 print_error('notpartofdiscussion', 'forum');
65bcf17b 792 }
793
794 forum_print_post($parent, $discussion, $forum, $cm, $course, false, false, false);
f8029045 795 if (empty($post->edit)) {
0468976c 796 if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
90f4745c 797 $forumtracked = forum_tp_is_tracked($forum);
798 $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
799 forum_print_posts_threaded($course, $cm, $forum, $discussion, $parent, 0, false, false, $forumtracked, $posts);
098d27d4 800 }
f8029045 801 }
501cdbd8 802 } else {
21da9db8 803 if (!empty($forum->intro)) {
4ba2221a 804 print_box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
21da9db8 805 }
501cdbd8 806 }
501cdbd8 807
2b63df96 808 $mform_post->display();
501cdbd8 809
810 print_footer($course);
811
812
96662058 813?>