Updated the HEAD build version to 20081212
[moodle.git] / mod / forum / discuss.php
CommitLineData
41905731 1<?php // $Id$
501cdbd8 2
3// Displays a post, and all the posts below it.
4// If no post is given, displays all posts in a discussion
5
65bcf17b 6 require_once('../../config.php');
7 require_once('lib.php');
8
83da3d28 9 $d = required_param('d', PARAM_INT); // Discussion ID
10 $parent = optional_param('parent', 0, PARAM_INT); // If set, then display this post and all children.
11 $mode = optional_param('mode', 0, PARAM_INT); // If set, changes the layout of the thread
12 $move = optional_param('move', 0, PARAM_INT); // If set, moves this discussion to another forum
87b0b499 13 $mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated.
83da3d28 14 $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
501cdbd8 15
4e445355 16 if (!$discussion = $DB->get_record('forum_discussions', array('id' => $d))) {
90f4745c 17 error("Discussion ID was incorrect or no longer exists");
501cdbd8 18 }
19
4e445355 20 if (!$course = $DB->get_record('course', array('id' => $discussion->course))) {
90f4745c 21 error("Course ID is incorrect - discussion is faulty");
501cdbd8 22 }
23
4e445355 24 if (!$forum = $DB->get_record('forum', array('id' => $discussion->forum))) {
68258534 25 notify("Bad forum ID stored in this discussion");
26 }
27
0fa18d5a 28 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
90f4745c 29 error('Course Module ID was incorrect');
bbbf2d40 30 }
65bcf17b 31
bf553d9e 32 require_course_login($course, true, $cm);
50e07a49 33
9af1611a 34 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
65bcf17b 35 require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
36
37 if ($forum->type == 'news') {
49a0ba94 38 if (!($USER->id == $discussion->userid || (($discussion->timestart == 0
39 || $discussion->timestart <= time())
fbc21e82 40 && ($discussion->timeend == 0 || $discussion->timeend > time())))) {
90f4745c 41 error('Discussion ID was incorrect or no longer exists', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
fbc21e82 42 }
68ddf8bc 43 }
44
65bcf17b 45/// move discussion if requested
46 if ($move > 0 and confirm_sesskey()) {
a5f77de6 47 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
65bcf17b 48
49 require_capability('mod/forum:movediscussions', $modcontext);
1fc49f00 50
65bcf17b 51 if ($forum->type == 'single') {
90f4745c 52 error('Cannot move discussion from a simple single discussion forum', $return);
83da3d28 53 }
65bcf17b 54
4e445355 55 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
90f4745c 56 error('You can\'t move to that forum - it doesn\'t exist!', $return);
83da3d28 57 }
7c900d5d 58
65bcf17b 59 if (!$cmto = get_coursemodule_from_instance('forum', $forumto->id, $course->id)) {
90f4745c 60 error('Target forum not found in this course.', $return);
65bcf17b 61 }
62
63 if (!coursemodule_visible_for_user($cmto)) {
90f4745c 64 error('Forum not visible', $return);
1fc49f00 65 }
65bcf17b 66
0faf6791 67 if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
65bcf17b 68 notify("Errors occurred while moving attachment directories - check your file permissions");
69 }
4e445355 70 $DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
71 $DB->set_field('forum_read', 'forumid', $forumto->id, array('discussionid' => $discussion->id));
65bcf17b 72 add_to_log($course->id, 'forum', 'move discussion', "discuss.php?d=$discussion->id", $discussion->id, $cmto->id);
73
74 require_once($CFG->libdir.'/rsslib.php');
75 require_once('rsslib.php');
76
77 // Delete the RSS files for the 2 forums because we want to force
78 // the regeneration of the feeds since the discussions have been
79 // moved.
b9973acc 80 if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($forumto)) {
90f4745c 81 error('Could not purge the cached RSS feeds for the source and/or'.
82 'destination forum(s) - check your file permissionsforums', $return);
65bcf17b 83 }
84
85 redirect($return.'&amp;moved=-1&amp;sesskey='.sesskey());
1fc49f00 86 }
87
1fc49f00 88 $logparameters = "d=$discussion->id";
89 if ($parent) {
839f2456 90 $logparameters .= "&amp;parent=$parent";
501cdbd8 91 }
69d79bc3 92
65bcf17b 93 add_to_log($course->id, 'forum', 'view discussion', "discuss.php?$logparameters", $discussion->id, $cm->id);
501cdbd8 94
95 unset($SESSION->fromdiscussion);
96
279826e2 97 if ($mode) {
f2d042c4 98 set_user_preference('forum_displaymode', $mode);
279826e2 99 }
501cdbd8 100
acb50c1b 101 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 102
e92ea3d8 103 if ($parent) {
65bcf17b 104 // If flat AND parent, then force nested display this time
105 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
106 $displaymode = FORUM_MODE_NESTED;
e92ea3d8 107 }
108 } else {
501cdbd8 109 $parent = $discussion->firstpost;
501cdbd8 110 }
111
11b0c469 112 if (! $post = forum_get_post_full($parent)) {
90f4745c 113 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
501cdbd8 114 }
115
c15b86dc 116
65bcf17b 117 if (!forum_user_can_view_post($post, $course, $cm, $forum, $discussion)) {
90f4745c 118 error('You do not have permissions to view this post', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
f37da850 119 }
120
65bcf17b 121 if ($mark == 'read' or $mark == 'unread') {
90f4745c 122 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
65bcf17b 123 if ($mark == 'read') {
90f4745c 124 forum_tp_add_read_record($USER->id, $postid);
65bcf17b 125 } else {
126 // unread
127 forum_tp_delete_read_records($USER->id, $postid);
128 }
129 }
130 }
cef1ce6a 131
6f1cc8d6 132 $searchform = forum_search_form($course);
87a462ea 133
134 $navlinks = array();
65bcf17b 135 $navlinks[] = array('name' => format_string($discussion->name), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
87a462ea 136 if ($parent != $discussion->firstpost) {
65bcf17b 137 $navlinks[] = array('name' => format_string($post->subject), 'type' => 'title');
87a462ea 138 }
65bcf17b 139
140 $navigation = build_navigation($navlinks, $cm);
e3f58dfb 141 print_header("$course->shortname: ".format_string($discussion->name), $course->fullname,
142 $navigation, "", "", true, $searchform, navmenu($course, $cm));
65bcf17b 143
c6d691dc 144
9197e147 145/// Check to see if groups are being used in this forum
146/// If so, make sure the current person is allowed to see this discussion
8b79a625 147/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
c6d691dc 148
7214101b 149 if (isguestuser() or !isloggedin() or has_capability('moodle/legacy:guest', $modcontext, NULL, false)) {
525041df 150 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
151 $canreply = ($forum->type != 'news'); // no reply in news forums
152
8b79a625 153 } else {
154 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
9197e147 155 }
156
c6d691dc 157/// Print the controls across the top
158
a1857045 159 echo '<table width="100%" class="discussioncontrols"><tr><td>';
c6d691dc 160
87b0b499 161 // groups selector not needed here
c6d691dc 162
c7a049a3 163 echo "</td><td>";
ec9c0d44 164 forum_print_mode_form($discussion->id, $displaymode);
c7a049a3 165 echo "</td><td>";
cef1ce6a 166
0d06b6fd 167 if (has_capability('mod/forum:exportdiscussion', $modcontext)) {
168 $button = new portfolio_add_button();
169 $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), '/mod/forum/lib.php');
170 $button->render();
171 echo '</td><td>';
10ae55f9 172 }
173
cef1ce6a 174 if ($forum->type != 'single'
175 && has_capability('mod/forum:movediscussions', $modcontext)) {
65bcf17b 176
cef1ce6a 177 // Popup menu to move discussions to other forums. The discussion in a
178 // single discussion forum can't be moved.
65bcf17b 179 $modinfo = get_fast_modinfo($course);
180 if (isset($modinfo->instances['forum'])) {
fcc69042 181 if ($course->format == 'weeks') {
182 $strsection = get_string("week");
183 } else {
184 $strsection = get_string("topic");
185 }
186 $section = -1;
65bcf17b 187 $forummenu = array();
188 foreach ($modinfo->instances['forum'] as $forumcm) {
189 if (!$forumcm->uservisible) {
190 continue;
fcc69042 191 }
65bcf17b 192
193 if (!empty($forumcm->sectionnum) and $section != $forumcm->sectionnum) {
194 $forummenu[] = "-------------- $strsection $forumcm->sectionnum --------------";
195 }
196 $section = $forumcm->sectionnum;
197 if ($forumcm->instance != $forum->id) {
198 $url = "discuss.php?d=$discussion->id&amp;move=$forumcm->instance&amp;sesskey=".sesskey();
199 $forummenu[$url] = format_string($forumcm->name);
1fc49f00 200 }
201 }
202 if (!empty($forummenu)) {
41a22032 203 echo "<div style=\"float:right;\">";
8f0cd6ef 204 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
1fc49f00 205 get_string("movethisdiscussionto", "forum"), "", "", true);
206 echo "</div>";
207 }
208 }
209 }
02ebf404 210 echo "</td></tr></table>";
1fc49f00 211
a4f495bf 212 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
65bcf17b 213 $a = new object();
214 $a->blockafter = $forum->blockafter;
a4f495bf 215 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
216 notify(get_string('thisforumisthrottled','forum',$a));
217 }
218
0468976c 219 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 220 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
098d27d4 221 notify(get_string('qandanotify','forum'));
222 }
223
65bcf17b 224 if ($move == -1 and confirm_sesskey()) {
225 notify(get_string('discussionmoved', 'forum', format_string($forum->name,true)));
8de14dc7 226 }
227
65bcf17b 228 $canrate = has_capability('mod/forum:rate', $modcontext);
229 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 230
501cdbd8 231 print_footer($course);
65bcf17b 232
501cdbd8 233
50e07a49 234?>