Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/mod/forum/post.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 require_once('../../config.php');
00027 require_once('lib.php');
00028 require_once($CFG->libdir.'/completionlib.php');
00029 
00030 $reply   = optional_param('reply', 0, PARAM_INT);
00031 $forum   = optional_param('forum', 0, PARAM_INT);
00032 $edit    = optional_param('edit', 0, PARAM_INT);
00033 $delete  = optional_param('delete', 0, PARAM_INT);
00034 $prune   = optional_param('prune', 0, PARAM_INT);
00035 $name    = optional_param('name', '', PARAM_CLEAN);
00036 $confirm = optional_param('confirm', 0, PARAM_INT);
00037 $groupid = optional_param('groupid', null, PARAM_INT);
00038 
00039 $PAGE->set_url('/mod/forum/post.php', array(
00040         'reply' => $reply,
00041         'forum' => $forum,
00042         'edit'  => $edit,
00043         'delete'=> $delete,
00044         'prune' => $prune,
00045         'name'  => $name,
00046         'confirm'=>$confirm,
00047         'groupid'=>$groupid,
00048         ));
00049 //these page_params will be passed as hidden variables later in the form.
00050 $page_params = array('reply'=>$reply, 'forum'=>$forum, 'edit'=>$edit);
00051 
00052 $sitecontext = get_context_instance(CONTEXT_SYSTEM);
00053 
00054 if (!isloggedin() or isguestuser()) {
00055 
00056     if (!isloggedin() and !get_referer()) {
00057         // No referer+not logged in - probably coming in via email  See MDL-9052
00058         require_login();
00059     }
00060 
00061     if (!empty($forum)) {      // User is starting a new discussion in a forum
00062         if (! $forum = $DB->get_record('forum', array('id' => $forum))) {
00063             print_error('invalidforumid', 'forum');
00064         }
00065     } else if (!empty($reply)) {      // User is writing a new reply
00066         if (! $parent = forum_get_post_full($reply)) {
00067             print_error('invalidparentpostid', 'forum');
00068         }
00069         if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
00070             print_error('notpartofdiscussion', 'forum');
00071         }
00072         if (! $forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
00073             print_error('invalidforumid');
00074         }
00075     }
00076     if (! $course = $DB->get_record('course', array('id' => $forum->course))) {
00077         print_error('invalidcourseid');
00078     }
00079 
00080     if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
00081         print_error('invalidcoursemodule');
00082     } else {
00083         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00084     }
00085 
00086     $PAGE->set_cm($cm, $course, $forum);
00087     $PAGE->set_context($modcontext);
00088     $PAGE->set_title($course->shortname);
00089     $PAGE->set_heading($course->fullname);
00090 
00091     echo $OUTPUT->header();
00092     echo $OUTPUT->confirm(get_string('noguestpost', 'forum').'<br /><br />'.get_string('liketologin'), get_login_url(), get_referer(false));
00093     echo $OUTPUT->footer();
00094     exit;
00095 }
00096 
00097 require_login(0, false);   // Script is useless unless they're logged in
00098 
00099 if (!empty($forum)) {      // User is starting a new discussion in a forum
00100     if (! $forum = $DB->get_record("forum", array("id" => $forum))) {
00101         print_error('invalidforumid', 'forum');
00102     }
00103     if (! $course = $DB->get_record("course", array("id" => $forum->course))) {
00104         print_error('invalidcourseid');
00105     }
00106     if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
00107         print_error("invalidcoursemodule");
00108     }
00109 
00110     $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00111 
00112     if (! forum_user_can_post_discussion($forum, $groupid, -1, $cm)) {
00113         if (!isguestuser()) {
00114             if (!is_enrolled($coursecontext)) {
00115                 if (enrol_selfenrol_available($course->id)) {
00116                     $SESSION->wantsurl = $FULLME;
00117                     $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
00118                     redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol'));
00119                 }
00120             }
00121         }
00122         print_error('nopostforum', 'forum');
00123     }
00124 
00125     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
00126         print_error("activityiscurrentlyhidden");
00127     }
00128 
00129     if (isset($_SERVER["HTTP_REFERER"])) {
00130         $SESSION->fromurl = $_SERVER["HTTP_REFERER"];
00131     } else {
00132         $SESSION->fromurl = '';
00133     }
00134 
00135 
00136     // Load up the $post variable.
00137 
00138     $post = new stdClass();
00139     $post->course        = $course->id;
00140     $post->forum         = $forum->id;
00141     $post->discussion    = 0;           // ie discussion # not defined yet
00142     $post->parent        = 0;
00143     $post->subject       = '';
00144     $post->userid        = $USER->id;
00145     $post->message       = '';
00146     $post->messageformat = editors_get_preferred_format();
00147     $post->messagetrust  = 0;
00148 
00149     if (isset($groupid)) {
00150         $post->groupid = $groupid;
00151     } else {
00152         $post->groupid = groups_get_activity_group($cm);
00153     }
00154 
00155     forum_set_return();
00156 
00157 } else if (!empty($reply)) {      // User is writing a new reply
00158 
00159     if (! $parent = forum_get_post_full($reply)) {
00160         print_error('invalidparentpostid', 'forum');
00161     }
00162     if (! $discussion = $DB->get_record("forum_discussions", array("id" => $parent->discussion))) {
00163         print_error('notpartofdiscussion', 'forum');
00164     }
00165     if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
00166         print_error('invalidforumid', 'forum');
00167     }
00168     if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
00169         print_error('invalidcourseid');
00170     }
00171     if (! $cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
00172         print_error('invalidcoursemodule');
00173     }
00174 
00175     // Ensure lang, theme, etc. is set up properly. MDL-6926
00176     $PAGE->set_cm($cm, $course, $forum);
00177 
00178     $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
00179     $modcontext    = get_context_instance(CONTEXT_MODULE, $cm->id);
00180 
00181     if (! forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext)) {
00182         if (!isguestuser()) {
00183             if (!is_enrolled($coursecontext)) {  // User is a guest here!
00184                 $SESSION->wantsurl = $FULLME;
00185                 $SESSION->enrolcancel = $_SERVER['HTTP_REFERER'];
00186                 redirect($CFG->wwwroot.'/enrol/index.php?id='.$course->id, get_string('youneedtoenrol'));
00187             }
00188         }
00189         print_error('nopostforum', 'forum');
00190     }
00191 
00192     // Make sure user can post here
00193     if (isset($cm->groupmode) && empty($course->groupmodeforce)) {
00194         $groupmode =  $cm->groupmode;
00195     } else {
00196         $groupmode = $course->groupmode;
00197     }
00198     if ($groupmode == SEPARATEGROUPS and !has_capability('moodle/site:accessallgroups', $modcontext)) {
00199         if ($discussion->groupid == -1) {
00200             print_error('nopostforum', 'forum');
00201         } else {
00202             if (!groups_is_member($discussion->groupid)) {
00203                 print_error('nopostforum', 'forum');
00204             }
00205         }
00206     }
00207 
00208     if (!$cm->visible and !has_capability('moodle/course:viewhiddenactivities', $coursecontext)) {
00209         print_error("activityiscurrentlyhidden");
00210     }
00211 
00212     // Load up the $post variable.
00213 
00214     $post = new stdClass();
00215     $post->course      = $course->id;
00216     $post->forum       = $forum->id;
00217     $post->discussion  = $parent->discussion;
00218     $post->parent      = $parent->id;
00219     $post->subject     = $parent->subject;
00220     $post->userid      = $USER->id;
00221     $post->message     = '';
00222 
00223     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
00224 
00225     $strre = get_string('re', 'forum');
00226     if (!(substr($post->subject, 0, strlen($strre)) == $strre)) {
00227         $post->subject = $strre.' '.$post->subject;
00228     }
00229 
00230     unset($SESSION->fromdiscussion);
00231 
00232 } else if (!empty($edit)) {  // User is editing their own post
00233 
00234     if (! $post = forum_get_post_full($edit)) {
00235         print_error('invalidpostid', 'forum');
00236     }
00237     if ($post->parent) {
00238         if (! $parent = forum_get_post_full($post->parent)) {
00239             print_error('invalidparentpostid', 'forum');
00240         }
00241     }
00242 
00243     if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
00244         print_error('notpartofdiscussion', 'forum');
00245     }
00246     if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
00247         print_error('invalidforumid', 'forum');
00248     }
00249     if (! $course = $DB->get_record("course", array("id" => $discussion->course))) {
00250         print_error('invalidcourseid');
00251     }
00252     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
00253         print_error('invalidcoursemodule');
00254     } else {
00255         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00256     }
00257 
00258     $PAGE->set_cm($cm, $course, $forum);
00259 
00260     if (!($forum->type == 'news' && !$post->parent && $discussion->timestart > time())) {
00261         if (((time() - $post->created) > $CFG->maxeditingtime) and
00262                     !has_capability('mod/forum:editanypost', $modcontext)) {
00263             print_error('maxtimehaspassed', 'forum', '', format_time($CFG->maxeditingtime));
00264         }
00265     }
00266     if (($post->userid <> $USER->id) and
00267                 !has_capability('mod/forum:editanypost', $modcontext)) {
00268         print_error('cannoteditposts', 'forum');
00269     }
00270 
00271 
00272     // Load up the $post variable.
00273     $post->edit   = $edit;
00274     $post->course = $course->id;
00275     $post->forum  = $forum->id;
00276     $post->groupid = ($discussion->groupid == -1) ? 0 : $discussion->groupid;
00277 
00278     $post = trusttext_pre_edit($post, 'message', $modcontext);
00279 
00280     unset($SESSION->fromdiscussion);
00281 
00282 
00283 }else if (!empty($delete)) {  // User is deleting a post
00284 
00285     if (! $post = forum_get_post_full($delete)) {
00286         print_error('invalidpostid', 'forum');
00287     }
00288     if (! $discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
00289         print_error('notpartofdiscussion', 'forum');
00290     }
00291     if (! $forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
00292         print_error('invalidforumid', 'forum');
00293     }
00294     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) {
00295         print_error('invalidcoursemodule');
00296     }
00297     if (!$course = $DB->get_record('course', array('id' => $forum->course))) {
00298         print_error('invalidcourseid');
00299     }
00300 
00301     require_login($course, false, $cm);
00302     $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00303 
00304     if ( !(($post->userid == $USER->id && has_capability('mod/forum:deleteownpost', $modcontext))
00305                 || has_capability('mod/forum:deleteanypost', $modcontext)) ) {
00306         print_error('cannotdeletepost', 'forum');
00307     }
00308 
00309 
00310     $replycount = forum_count_replies($post);
00311 
00312     if (!empty($confirm) && confirm_sesskey()) {    // User has confirmed the delete
00313         //check user capability to delete post.
00314         $timepassed = time() - $post->created;
00315         if (($timepassed > $CFG->maxeditingtime) && !has_capability('mod/forum:deleteanypost', $modcontext)) {
00316             print_error("cannotdeletepost", "forum",
00317                       forum_go_back_to("discuss.php?d=$post->discussion"));
00318         }
00319 
00320         if ($post->totalscore) {
00321             notice(get_string('couldnotdeleteratings', 'rating'),
00322                     forum_go_back_to("discuss.php?d=$post->discussion"));
00323 
00324         } else if ($replycount && !has_capability('mod/forum:deleteanypost', $modcontext)) {
00325             print_error("couldnotdeletereplies", "forum",
00326                     forum_go_back_to("discuss.php?d=$post->discussion"));
00327 
00328         } else {
00329             if (! $post->parent) {  // post is a discussion topic as well, so delete discussion
00330                 if ($forum->type == 'single') {
00331                     notice("Sorry, but you are not allowed to delete that discussion!",
00332                             forum_go_back_to("discuss.php?d=$post->discussion"));
00333                 }
00334                 forum_delete_discussion($discussion, false, $course, $cm, $forum);
00335 
00336                 add_to_log($discussion->course, "forum", "delete discussion",
00337                            "view.php?id=$cm->id", "$forum->id", $cm->id);
00338 
00339                 redirect("view.php?f=$discussion->forum");
00340 
00341             } else if (forum_delete_post($post, has_capability('mod/forum:deleteanypost', $modcontext),
00342                 $course, $cm, $forum)) {
00343 
00344                 if ($forum->type == 'single') {
00345                     // Single discussion forums are an exception. We show
00346                     // the forum itself since it only has one discussion
00347                     // thread.
00348                     $discussionurl = "view.php?f=$forum->id";
00349                 } else {
00350                     $discussionurl = "discuss.php?d=$post->discussion";
00351                 }
00352 
00353                 add_to_log($discussion->course, "forum", "delete post", $discussionurl, "$post->id", $cm->id);
00354 
00355                 redirect(forum_go_back_to($discussionurl));
00356             } else {
00357                 print_error('errorwhiledelete', 'forum');
00358             }
00359         }
00360 
00361 
00362     } else { // User just asked to delete something
00363 
00364         forum_set_return();
00365         $PAGE->navbar->add(get_string('delete', 'forum'));
00366         $PAGE->set_title($course->shortname);
00367         $PAGE->set_heading($course->fullname);
00368 
00369         if ($replycount) {
00370             if (!has_capability('mod/forum:deleteanypost', $modcontext)) {
00371                 print_error("couldnotdeletereplies", "forum",
00372                       forum_go_back_to("discuss.php?d=$post->discussion"));
00373             }
00374             echo $OUTPUT->header();
00375             echo $OUTPUT->confirm(get_string("deletesureplural", "forum", $replycount+1),
00376                          "post.php?delete=$delete&confirm=$delete",
00377                          $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
00378 
00379             forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
00380 
00381             if (empty($post->edit)) {
00382                 $forumtracked = forum_tp_is_tracked($forum);
00383                 $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
00384                 forum_print_posts_nested($course, $cm, $forum, $discussion, $post, false, false, $forumtracked, $posts);
00385             }
00386         } else {
00387             echo $OUTPUT->header();
00388             echo $OUTPUT->confirm(get_string("deletesure", "forum", $replycount),
00389                          "post.php?delete=$delete&confirm=$delete",
00390                          $CFG->wwwroot.'/mod/forum/discuss.php?d='.$post->discussion.'#p'.$post->id);
00391             forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
00392         }
00393 
00394     }
00395     echo $OUTPUT->footer();
00396     die;
00397 
00398 
00399 } else if (!empty($prune)) {  // Pruning
00400 
00401     if (!$post = forum_get_post_full($prune)) {
00402         print_error('invalidpostid', 'forum');
00403     }
00404     if (!$discussion = $DB->get_record("forum_discussions", array("id" => $post->discussion))) {
00405         print_error('notpartofdiscussion', 'forum');
00406     }
00407     if (!$forum = $DB->get_record("forum", array("id" => $discussion->forum))) {
00408         print_error('invalidforumid', 'forum');
00409     }
00410     if ($forum->type == 'single') {
00411         print_error('cannotsplit', 'forum');
00412     }
00413     if (!$post->parent) {
00414         print_error('alreadyfirstpost', 'forum');
00415     }
00416     if (!$cm = get_coursemodule_from_instance("forum", $forum->id, $forum->course)) { // For the logs
00417         print_error('invalidcoursemodule');
00418     } else {
00419         $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00420     }
00421     if (!has_capability('mod/forum:splitdiscussions', $modcontext)) {
00422         print_error('cannotsplit', 'forum');
00423     }
00424 
00425     if (!empty($name) && confirm_sesskey()) {    // User has confirmed the prune
00426 
00427         $newdiscussion = new stdClass();
00428         $newdiscussion->course       = $discussion->course;
00429         $newdiscussion->forum        = $discussion->forum;
00430         $newdiscussion->name         = $name;
00431         $newdiscussion->firstpost    = $post->id;
00432         $newdiscussion->userid       = $discussion->userid;
00433         $newdiscussion->groupid      = $discussion->groupid;
00434         $newdiscussion->assessed     = $discussion->assessed;
00435         $newdiscussion->usermodified = $post->userid;
00436         $newdiscussion->timestart    = $discussion->timestart;
00437         $newdiscussion->timeend      = $discussion->timeend;
00438 
00439         $newid = $DB->insert_record('forum_discussions', $newdiscussion);
00440 
00441         $newpost = new stdClass();
00442         $newpost->id      = $post->id;
00443         $newpost->parent  = 0;
00444         $newpost->subject = $name;
00445 
00446         $DB->update_record("forum_posts", $newpost);
00447 
00448         forum_change_discussionid($post->id, $newid);
00449 
00450         // update last post in each discussion
00451         forum_discussion_update_last_post($discussion->id);
00452         forum_discussion_update_last_post($newid);
00453 
00454         add_to_log($discussion->course, "forum", "prune post",
00455                        "discuss.php?d=$newid", "$post->id", $cm->id);
00456 
00457         redirect(forum_go_back_to("discuss.php?d=$newid"));
00458 
00459     } else { // User just asked to prune something
00460 
00461         $course = $DB->get_record('course', array('id' => $forum->course));
00462 
00463         $PAGE->set_cm($cm);
00464         $PAGE->set_context($modcontext);
00465         $PAGE->navbar->add(format_string($post->subject, true), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
00466         $PAGE->navbar->add(get_string("prune", "forum"));
00467         $PAGE->set_title(format_string($discussion->name).": ".format_string($post->subject));
00468         $PAGE->set_heading($course->fullname);
00469         echo $OUTPUT->header();
00470         echo $OUTPUT->heading(get_string('pruneheading', 'forum'));
00471         echo '<center>';
00472 
00473         include('prune.html');
00474 
00475         forum_print_post($post, $discussion, $forum, $cm, $course, false, false, false);
00476         echo '</center>';
00477     }
00478     echo $OUTPUT->footer();
00479     die;
00480 } else {
00481     print_error('unknowaction');
00482 
00483 }
00484 
00485 if (!isset($coursecontext)) {
00486     // Has not yet been set by post.php.
00487     $coursecontext = get_context_instance(CONTEXT_COURSE, $forum->course);
00488 }
00489 
00490 
00491 // from now on user must be logged on properly
00492 
00493 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) { // For the logs
00494     print_error('invalidcoursemodule');
00495 }
00496 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
00497 require_login($course, false, $cm);
00498 
00499 if (isguestuser()) {
00500     // just in case
00501     print_error('noguest');
00502 }
00503 
00504 if (!isset($forum->maxattachments)) {  // TODO - delete this once we add a field to the forum table
00505     $forum->maxattachments = 3;
00506 }
00507 
00508 require_once('post_form.php');
00509 
00510 $mform_post = new mod_forum_post_form('post.php', array('course'=>$course, 'cm'=>$cm, 'coursecontext'=>$coursecontext, 'modcontext'=>$modcontext, 'forum'=>$forum, 'post'=>$post));
00511 
00512 $draftitemid = file_get_submitted_draft_itemid('attachments');
00513 file_prepare_draft_area($draftitemid, $modcontext->id, 'mod_forum', 'attachment', empty($post->id)?null:$post->id);
00514 
00515 //load data into form NOW!
00516 
00517 if ($USER->id != $post->userid) {   // Not the original author, so add a message to the end
00518     $data->date = userdate($post->modified);
00519     if ($post->messageformat == FORMAT_HTML) {
00520         $data->name = '<a href="'.$CFG->wwwroot.'/user/view.php?id='.$USER->id.'&course='.$post->course.'">'.
00521                        fullname($USER).'</a>';
00522         $post->message .= '<p>(<span class="edited">'.get_string('editedby', 'forum', $data).'</span>)</p>';
00523     } else {
00524         $data->name = fullname($USER);
00525         $post->message .= "\n\n(".get_string('editedby', 'forum', $data).')';
00526     }
00527 }
00528 
00529 if (!empty($parent)) {
00530     $heading = get_string("yourreply", "forum");
00531 } else {
00532     if ($forum->type == 'qanda') {
00533         $heading = get_string('yournewquestion', 'forum');
00534     } else {
00535         $heading = get_string('yournewtopic', 'forum');
00536     }
00537 }
00538 
00539 if (forum_is_subscribed($USER->id, $forum->id)) {
00540     $subscribe = true;
00541 
00542 } else if (forum_user_has_posted($forum->id, 0, $USER->id)) {
00543     $subscribe = false;
00544 
00545 } else {
00546     // user not posted yet - use subscription default specified in profile
00547     $subscribe = !empty($USER->autosubscribe);
00548 }
00549 
00550 $draftid_editor = file_get_submitted_draft_itemid('message');
00551 $currenttext = file_prepare_draft_area($draftid_editor, $modcontext->id, 'mod_forum', 'post', empty($post->id) ? null : $post->id, array('subdirs'=>true), $post->message);
00552 $mform_post->set_data(array(        'attachments'=>$draftitemid,
00553                                     'general'=>$heading,
00554                                     'subject'=>$post->subject,
00555                                     'message'=>array(
00556                                         'text'=>$currenttext,
00557                                         'format'=>empty($post->messageformat) ? editors_get_preferred_format() : $post->messageformat,
00558                                         'itemid'=>$draftid_editor
00559                                     ),
00560                                     'subscribe'=>$subscribe?1:0,
00561                                     'mailnow'=>!empty($post->mailnow),
00562                                     'userid'=>$post->userid,
00563                                     'parent'=>$post->parent,
00564                                     'discussion'=>$post->discussion,
00565                                     'course'=>$course->id) +
00566                                     $page_params +
00567 
00568                             (isset($post->format)?array(
00569                                     'format'=>$post->format):
00570                                 array())+
00571 
00572                             (isset($discussion->timestart)?array(
00573                                     'timestart'=>$discussion->timestart):
00574                                 array())+
00575 
00576                             (isset($discussion->timeend)?array(
00577                                     'timeend'=>$discussion->timeend):
00578                                 array())+
00579 
00580                             (isset($post->groupid)?array(
00581                                     'groupid'=>$post->groupid):
00582                                 array())+
00583 
00584                             (isset($discussion->id)?
00585                                     array('discussion'=>$discussion->id):
00586                                     array()));
00587 
00588 if ($fromform = $mform_post->get_data()) {
00589 
00590     if (empty($SESSION->fromurl)) {
00591         $errordestination = "$CFG->wwwroot/mod/forum/view.php?f=$forum->id";
00592     } else {
00593         $errordestination = $SESSION->fromurl;
00594     }
00595 
00596     $fromform->itemid        = $fromform->message['itemid'];
00597     $fromform->messageformat = $fromform->message['format'];
00598     $fromform->message       = $fromform->message['text'];
00599     // WARNING: the $fromform->message array has been overwritten, do not use it anymore!
00600     $fromform->messagetrust  = trusttext_trusted($modcontext);
00601 
00602     $contextcheck = isset($fromform->groupinfo) && has_capability('mod/forum:movediscussions', $modcontext);
00603 
00604     if ($fromform->edit) {           // Updating a post
00605         unset($fromform->groupid);
00606         $fromform->id = $fromform->edit;
00607         $message = '';
00608 
00609         //fix for bug #4314
00610         if (!$realpost = $DB->get_record('forum_posts', array('id' => $fromform->id))) {
00611             $realpost = new stdClass();
00612             $realpost->userid = -1;
00613         }
00614 
00615 
00616         // if user has edit any post capability
00617         // or has either startnewdiscussion or reply capability and is editting own post
00618         // then he can proceed
00619         // MDL-7066
00620         if ( !(($realpost->userid == $USER->id && (has_capability('mod/forum:replypost', $modcontext)
00621                             || has_capability('mod/forum:startdiscussion', $modcontext))) ||
00622                             has_capability('mod/forum:editanypost', $modcontext)) ) {
00623             print_error('cannotupdatepost', 'forum');
00624         }
00625 
00626         // If the user has access to all groups and they are changing the group, then update the post.
00627         if ($contextcheck) {
00628             $DB->set_field('forum_discussions' ,'groupid' , $fromform->groupinfo, array('firstpost' => $fromform->id));
00629         }
00630 
00631         $updatepost = $fromform; //realpost
00632         $updatepost->forum = $forum->id;
00633         if (!forum_update_post($updatepost, $mform_post, $message)) {
00634             print_error("couldnotupdate", "forum", $errordestination);
00635         }
00636 
00637         // MDL-11818
00638         if (($forum->type == 'single') && ($updatepost->parent == '0')){ // updating first post of single discussion type -> updating forum intro
00639             $forum->intro = $updatepost->message;
00640             $forum->timemodified = time();
00641             $DB->update_record("forum", $forum);
00642         }
00643 
00644         $timemessage = 2;
00645         if (!empty($message)) { // if we're printing stuff about the file upload
00646             $timemessage = 4;
00647         }
00648         $message .= '<br />'.get_string("postupdated", "forum");
00649 
00650         if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
00651             $timemessage = 4;
00652         }
00653         if ($forum->type == 'single') {
00654             // Single discussion forums are an exception. We show
00655             // the forum itself since it only has one discussion
00656             // thread.
00657             $discussionurl = "view.php?f=$forum->id";
00658         } else {
00659             $discussionurl = "discuss.php?d=$discussion->id#p$fromform->id";
00660         }
00661         add_to_log($course->id, "forum", "update post",
00662                 "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
00663 
00664         redirect(forum_go_back_to("$discussionurl"), $message.$subscribemessage, $timemessage);
00665 
00666         exit;
00667 
00668 
00669     } else if ($fromform->discussion) { // Adding a new post to an existing discussion
00670         unset($fromform->groupid);
00671         $message = '';
00672         $addpost = $fromform;
00673         $addpost->forum=$forum->id;
00674         if ($fromform->id = forum_add_new_post($addpost, $mform_post, $message)) {
00675 
00676             $timemessage = 2;
00677             if (!empty($message)) { // if we're printing stuff about the file upload
00678                 $timemessage = 4;
00679             }
00680 
00681             if ($subscribemessage = forum_post_subscription($fromform, $forum)) {
00682                 $timemessage = 4;
00683             }
00684 
00685             if (!empty($fromform->mailnow)) {
00686                 $message .= get_string("postmailnow", "forum");
00687                 $timemessage = 4;
00688             } else {
00689                 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
00690                 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
00691             }
00692 
00693             if ($forum->type == 'single') {
00694                 // Single discussion forums are an exception. We show
00695                 // the forum itself since it only has one discussion
00696                 // thread.
00697                 $discussionurl = "view.php?f=$forum->id";
00698             } else {
00699                 $discussionurl = "discuss.php?d=$discussion->id";
00700             }
00701             add_to_log($course->id, "forum", "add post",
00702                       "$discussionurl&amp;parent=$fromform->id", "$fromform->id", $cm->id);
00703 
00704             // Update completion state
00705             $completion=new completion_info($course);
00706             if($completion->is_enabled($cm) &&
00707                 ($forum->completionreplies || $forum->completionposts)) {
00708                 $completion->update_state($cm,COMPLETION_COMPLETE);
00709             }
00710 
00711             redirect(forum_go_back_to("$discussionurl#p$fromform->id"), $message.$subscribemessage, $timemessage);
00712 
00713         } else {
00714             print_error("couldnotadd", "forum", $errordestination);
00715         }
00716         exit;
00717 
00718     } else {                     // Adding a new discussion
00719         if (!forum_user_can_post_discussion($forum, $fromform->groupid, -1, $cm, $modcontext)) {
00720             print_error('cannotcreatediscussion', 'forum');
00721         }
00722         // If the user has access all groups capability let them choose the group.
00723         if ($contextcheck) {
00724             $fromform->groupid = $fromform->groupinfo;
00725         }
00726         if (empty($fromform->groupid)) {
00727             $fromform->groupid = -1;
00728         }
00729 
00730         $fromform->mailnow = empty($fromform->mailnow) ? 0 : 1;
00731 
00732         $discussion = $fromform;
00733         $discussion->name    = $fromform->subject;
00734 
00735         $newstopic = false;
00736         if ($forum->type == 'news' && !$fromform->parent) {
00737             $newstopic = true;
00738         }
00739         $discussion->timestart = $fromform->timestart;
00740         $discussion->timeend = $fromform->timeend;
00741 
00742         $message = '';
00743         if ($discussion->id = forum_add_discussion($discussion, $mform_post, $message)) {
00744 
00745             add_to_log($course->id, "forum", "add discussion",
00746                     "discuss.php?d=$discussion->id", "$discussion->id", $cm->id);
00747 
00748             $timemessage = 2;
00749             if (!empty($message)) { // if we're printing stuff about the file upload
00750                 $timemessage = 4;
00751             }
00752 
00753             if ($fromform->mailnow) {
00754                 $message .= get_string("postmailnow", "forum");
00755                 $timemessage = 4;
00756             } else {
00757                 $message .= '<p>'.get_string("postaddedsuccess", "forum") . '</p>';
00758                 $message .= '<p>'.get_string("postaddedtimeleft", "forum", format_time($CFG->maxeditingtime)) . '</p>';
00759             }
00760 
00761             if ($subscribemessage = forum_post_subscription($discussion, $forum)) {
00762                 $timemessage = 4;
00763             }
00764 
00765             // Update completion status
00766             $completion=new completion_info($course);
00767             if($completion->is_enabled($cm) &&
00768                 ($forum->completiondiscussions || $forum->completionposts)) {
00769                 $completion->update_state($cm,COMPLETION_COMPLETE);
00770             }
00771 
00772             redirect(forum_go_back_to("view.php?f=$fromform->forum"), $message.$subscribemessage, $timemessage);
00773 
00774         } else {
00775             print_error("couldnotadd", "forum", $errordestination);
00776         }
00777 
00778         exit;
00779     }
00780 }
00781 
00782 
00783 
00784 // To get here they need to edit a post, and the $post
00785 // variable will be loaded with all the particulars,
00786 // so bring up the form.
00787 
00788 // $course, $forum are defined.  $discussion is for edit and reply only.
00789 
00790 if ($post->discussion) {
00791     if (! $toppost = $DB->get_record("forum_posts", array("discussion" => $post->discussion, "parent" => 0))) {
00792         print_error('cannotfindparentpost', 'forum', '', $post->id);
00793     }
00794 } else {
00795     $toppost = new stdClass();
00796     $toppost->subject = ($forum->type == "news") ? get_string("addanewtopic", "forum") :
00797                                                    get_string("addanewdiscussion", "forum");
00798 }
00799 
00800 if (empty($post->edit)) {
00801     $post->edit = '';
00802 }
00803 
00804 if (empty($discussion->name)) {
00805     if (empty($discussion)) {
00806         $discussion = new stdClass();
00807     }
00808     $discussion->name = $forum->name;
00809 }
00810 if ($forum->type == 'single') {
00811     // There is only one discussion thread for this forum type. We should
00812     // not show the discussion name (same as forum name in this case) in
00813     // the breadcrumbs.
00814     $strdiscussionname = '';
00815 } else {
00816     // Show the discussion name in the breadcrumbs.
00817     $strdiscussionname = format_string($discussion->name).':';
00818 }
00819 
00820 $forcefocus = empty($reply) ? NULL : 'message';
00821 
00822 if (!empty($discussion->id)) {
00823     $PAGE->navbar->add(format_string($toppost->subject, true), "discuss.php?d=$discussion->id");
00824 }
00825 
00826 if ($post->parent) {
00827     $PAGE->navbar->add(get_string('reply', 'forum'));
00828 }
00829 
00830 if ($edit) {
00831     $PAGE->navbar->add(get_string('edit', 'forum'));
00832 }
00833 
00834 $PAGE->set_title("$course->shortname: $strdiscussionname ".format_string($toppost->subject));
00835 $PAGE->set_heading($course->fullname);
00836 
00837 echo $OUTPUT->header();
00838 
00839 // checkup
00840 if (!empty($parent) && !forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
00841     print_error('cannotreply', 'forum');
00842 }
00843 if (empty($parent) && empty($edit) && !forum_user_can_post_discussion($forum, $groupid, -1, $cm, $modcontext)) {
00844     print_error('cannotcreatediscussion', 'forum');
00845 }
00846 
00847 if ($forum->type == 'qanda'
00848             && !has_capability('mod/forum:viewqandawithoutposting', $modcontext)
00849             && !empty($discussion->id)
00850             && !forum_user_has_posted($forum->id, $discussion->id, $USER->id)) {
00851     echo $OUTPUT->notification(get_string('qandanotify','forum'));
00852 }
00853 
00854 forum_check_throttling($forum, $cm);
00855 
00856 if (!empty($parent)) {
00857     if (! $discussion = $DB->get_record('forum_discussions', array('id' => $parent->discussion))) {
00858         print_error('notpartofdiscussion', 'forum');
00859     }
00860 
00861     forum_print_post($parent, $discussion, $forum, $cm, $course, false, false, false);
00862     if (empty($post->edit)) {
00863         if ($forum->type != 'qanda' || forum_user_can_see_discussion($forum, $discussion, $modcontext)) {
00864             $forumtracked = forum_tp_is_tracked($forum);
00865             $posts = forum_get_all_discussion_posts($discussion->id, "created ASC", $forumtracked);
00866             forum_print_posts_threaded($course, $cm, $forum, $discussion, $parent, 0, false, $forumtracked, $posts);
00867         }
00868     }
00869 } else {
00870     if (!empty($forum->intro)) {
00871         echo $OUTPUT->box(format_module_intro('forum', $forum, $cm->id), 'generalbox', 'intro');
00872     }
00873 }
00874 
00875 $mform_post->display();
00876 
00877 echo $OUTPUT->footer();
00878 
 All Data Structures Namespaces Files Functions Variables Enumerations