MDL-10234
[moodle-linuxchix.git] / mod / forum / discuss.php
blob82863dd16c0bb4fe4a791b5e7e70c11fedb6eaeb
1 <?php // $Id$
3 // Displays a post, and all the posts below it.
4 // If no post is given, displays all posts in a discussion
6 require_once("../../config.php");
8 $d = required_param('d', PARAM_INT); // Discussion ID
9 $parent = optional_param('parent', 0, PARAM_INT); // If set, then display this post and all children.
10 $mode = optional_param('mode', 0, PARAM_INT); // If set, changes the layout of the thread
11 $move = optional_param('move', 0, PARAM_INT); // If set, moves this discussion to another forum
12 $fromforum = optional_param('fromforum', 0, PARAM_INT); // Needs to be set when we want to move a discussion.
13 $mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated.
14 $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
16 if (!$discussion = get_record("forum_discussions", "id", $d)) {
17 error("Discussion ID was incorrect or no longer exists");
20 if (!$course = get_record("course", "id", $discussion->course)) {
21 error("Course ID is incorrect - discussion is faulty");
24 if (!$forum = get_record("forum", "id", $discussion->forum)) {
25 notify("Bad forum ID stored in this discussion");
28 if (!$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
29 error('Course Module ID was incorrect');
31 // move this down fix for MDL-6926
32 require_once("lib.php");
33 require_course_login($course, true, $cm);
35 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
36 $canviewdiscussion = has_capability('mod/forum:viewdiscussion', $modcontext);
38 if ($forum->type == "news") {
39 if (!($USER->id == $discussion->userid || (($discussion->timestart == 0
40 || $discussion->timestart <= time())
41 && ($discussion->timeend == 0 || $discussion->timeend > time())))) {
42 error('Discussion ID was incorrect or no longer exists', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
47 if (!empty($move)) {
49 if (!$sourceforum = get_record('forum', 'id', $fromforum)) {
50 error('Cannot find which forum this discussion is being moved from');
52 if ($sourceforum->type == 'single') {
53 error('Cannot move discussion from a simple single discussion forum');
56 require_capability('mod/forum:movediscussions', $modcontext);
58 if ($forum = get_record("forum", "id", $move)) {
59 if (!forum_move_attachments($discussion, $move)) {
60 notify("Errors occurred while moving attachment directories - check your file permissions");
62 set_field("forum_discussions", "forum", $forum->id, "id", $discussion->id);
63 $discussion->forum = $forum->id;
64 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
65 add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id",
66 $cm->id);
67 } else {
68 add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id");
70 $discussionmoved = true;
72 require_once('rsslib.php');
73 require_once($CFG->libdir.'/rsslib.php');
75 // Delete the RSS files for the 2 forums because we want to force
76 // the regeneration of the feeds since the discussions have been
77 // moved.
78 if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($sourceforum)) {
79 notify('Could not purge the cached RSS feeds for the source and/or'.
80 'destination forum(s) - check your file permissionsforums');
82 } else {
83 error('You can\'t move to that forum - it doesn\'t exist!');
87 $logparameters = "d=$discussion->id";
88 if ($parent) {
89 $logparameters .= "&amp;parent=$parent";
92 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
93 add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id", $cm->id);
94 } else {
95 add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id");
98 unset($SESSION->fromdiscussion);
100 $crumbs[] = array('name' => get_string("forums", "forum"), 'link' => "../forum/index.php?id=$course->id", 'type' => 'activity');
101 $crumbs[] = array('name' => format_string($forum->name,true), 'link' => "../forum/view.php?f=$forum->id", 'type' => 'activityinstance');
102 $crumbs[] = array('name' => format_string($discussion->name,true), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
105 if ($mode) {
106 set_user_preference('forum_displaymode', $mode);
109 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
111 if ($parent) {
112 if (abs($displaymode) == 1) { // If flat AND parent, then force nested display this time
113 $displaymode = 3;
115 } else {
116 $parent = $discussion->firstpost;
119 if (!forum_user_can_view_post($parent, $course, $cm, $forum, $discussion)) {
120 error('You do not have permissions to view this post', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
123 if (! $post = forum_get_post_full($parent)) {
124 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
127 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum) &&
128 $CFG->forum_usermarksread) {
129 if ($mark == 'read') {
130 forum_tp_add_read_record($USER->id, $postid, $discussion->id, $forum->id);
131 } else if ($mark == 'unread') {
132 forum_tp_delete_read_records($USER->id, $postid);
137 $searchform = forum_search_form($course);
139 $navigation = build_navigation($crumbs);
140 print_header("$course->shortname: ".format_string($discussion->name), $course->fullname,
141 $navigation, "", "", true, $searchform, navmenu($course, $cm));
144 /// Check to see if groups are being used in this forum
145 /// If so, make sure the current person is allowed to see this discussion
146 /// Also, if we know they should be able to reply, then explicitly set $canreply
148 if ($forum->type == 'news') {
149 $capname = 'mod/forum:replynews';
150 } else {
151 $capname = 'mod/forum:replypost';
154 $groupmode = groupmode($course, $cm);
155 if ($canreply = has_capability($capname, $modcontext)) {
157 if ($groupmode && !has_capability('moodle/site:accessallgroups', $modcontext)) {
158 // Groups must be kept separate
159 //change this to ismember
160 $mygroupid = mygroupid($course->id); //only useful if 0, otherwise it's an array now
161 if ($groupmode == SEPARATEGROUPS) {
162 require_login();
164 if ((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid) || $mygroupid == $discussion->groupid)) {
165 // $canreply = true;
166 } elseif ($discussion->groupid == -1) {
167 $canreply = false;
168 } else {
169 print_heading("Sorry, you can't see this discussion because you are not in this group");
170 print_footer($course);
171 die;
174 } else if ($groupmode == VISIBLEGROUPS) {
175 $canreply = ( (empty($mygroupid) && $discussion->groupid == -1) ||
176 (ismember($discussion->groupid) || $mygroupid == $discussion->groupid) );
179 } else { // allow guests to see the link
180 $coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);
181 if (has_capability('moodle/legacy:guest', $coursecontext, NULL, false)) { // User is a guest here!
182 $canreply = true;
186 /// Print the controls across the top
188 echo '<table width="100%" class="discussioncontrols"><tr><td>';
190 // groups selector not needed here
192 echo "</td><td>";
193 forum_print_mode_form($discussion->id, $displaymode);
194 echo "</td><td>";
196 if ($forum->type != 'single'
197 && has_capability('mod/forum:movediscussions', $modcontext)) {
199 // Popup menu to move discussions to other forums. The discussion in a
200 // single discussion forum can't be moved.
201 if ($forums = get_all_instances_in_course("forum", $course)) {
202 if ($course->format == 'weeks') {
203 $strsection = get_string("week");
204 } else {
205 $strsection = get_string("topic");
207 $section = -1;
208 foreach ($forums as $courseforum) {
209 if (!empty($courseforum->section) and $section != $courseforum->section) {
210 $forummenu[] = "-------------- $strsection $courseforum->section --------------";
212 $section = $courseforum->section;
213 if ($courseforum->id != $forum->id) {
214 $url = "discuss.php?d=$discussion->id&amp;fromforum=$discussion->forum&amp;move=$courseforum->id";
215 $forummenu[$url] = format_string($courseforum->name,true);
218 if (!empty($forummenu)) {
219 echo "<div style=\"float:right;\">";
220 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
221 get_string("movethisdiscussionto", "forum"), "", "", true);
222 echo "</div>";
226 echo "</td></tr></table>";
228 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
229 $a->blockafter = $forum->blockafter;
230 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
231 notify(get_string('thisforumisthrottled','forum',$a));
234 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
235 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
236 notify(get_string('qandanotify','forum'));
239 if (isset($discussionmoved)) {
240 notify(get_string("discussionmoved", "forum", format_string($forum->name,true)));
244 /// Print the actual discussion
245 if (!$canviewdiscussion) {
246 notice(get_string('noviewdiscussionspermission', 'forum'));
247 } else {
248 $canrate = has_capability('mod/forum:rate', $modcontext);
249 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
252 print_footer($course);