Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / mod / forum / discuss.php
blob79b98ea0cdaa4c7193ce3e6cdc350d67e4fa2511
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');
7 require_once('lib.php');
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
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');
32 require_course_login($course, true, $cm);
34 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
35 require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
37 if ($forum->type == 'news') {
38 if (!($USER->id == $discussion->userid || (($discussion->timestart == 0
39 || $discussion->timestart <= time())
40 && ($discussion->timeend == 0 || $discussion->timeend > time())))) {
41 error('Discussion ID was incorrect or no longer exists', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
45 /// move discussion if requested
46 if ($move > 0 and confirm_sesskey()) {
47 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
49 require_capability('mod/forum:movediscussions', $modcontext);
51 if ($forum->type == 'single') {
52 error('Cannot move discussion from a simple single discussion forum', $return);
55 if (!$forumto = get_record('forum', 'id', $move)) {
56 error('You can\'t move to that forum - it doesn\'t exist!', $return);
59 if (!$cmto = get_coursemodule_from_instance('forum', $forumto->id, $course->id)) {
60 error('Target forum not found in this course.', $return);
63 if (!coursemodule_visible_for_user($cmto)) {
64 error('Forum not visible', $return);
67 if (!forum_move_attachments($discussion, $forumto->id)) {
68 notify("Errors occurred while moving attachment directories - check your file permissions");
70 set_field('forum_discussions', 'forum', $forumto->id, 'id', $discussion->id);
71 set_field('forum_read', 'forumid', $forumto->id, 'discussionid', $discussion->id);
72 add_to_log($course->id, 'forum', 'move discussion', "discuss.php?d=$discussion->id", $discussion->id, $cmto->id);
74 require_once($CFG->libdir.'/rsslib.php');
75 require_once('rsslib.php');
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.
80 if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($forumto)) {
81 error('Could not purge the cached RSS feeds for the source and/or'.
82 'destination forum(s) - check your file permissionsforums', $return);
85 redirect($return.'&amp;moved=-1&amp;sesskey='.sesskey());
88 $logparameters = "d=$discussion->id";
89 if ($parent) {
90 $logparameters .= "&amp;parent=$parent";
93 add_to_log($course->id, 'forum', 'view discussion', "discuss.php?$logparameters", $discussion->id, $cm->id);
95 unset($SESSION->fromdiscussion);
97 if ($mode) {
98 set_user_preference('forum_displaymode', $mode);
101 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
103 if ($parent) {
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;
108 } else {
109 $parent = $discussion->firstpost;
112 if (! $post = forum_get_post_full($parent)) {
113 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
117 if (!forum_user_can_view_post($post, $course, $cm, $forum, $discussion)) {
118 error('You do not have permissions to view this post', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
121 if ($mark == 'read' or $mark == 'unread') {
122 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
123 if ($mark == 'read') {
124 forum_tp_add_read_record($USER->id, $postid);
125 } else {
126 // unread
127 forum_tp_delete_read_records($USER->id, $postid);
132 $searchform = forum_search_form($course);
134 $navlinks = array();
135 $navlinks[] = array('name' => format_string($discussion->name), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
136 if ($parent != $discussion->firstpost) {
137 $navlinks[] = array('name' => format_string($post->subject), 'type' => 'title');
140 $navigation = build_navigation($navlinks, $cm);
141 print_header("$course->shortname: ".format_string($discussion->name), $course->fullname,
142 $navigation, "", "", true, $searchform, navmenu($course, $cm));
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
147 /// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
149 if (isguestuser() or !isloggedin() or has_capability('moodle/legacy:guest', $modcontext, NULL, false)) {
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
153 } else {
154 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
157 /// Print the controls across the top
159 echo '<table width="100%" class="discussioncontrols"><tr><td>';
161 // groups selector not needed here
163 echo "</td><td>";
164 forum_print_mode_form($discussion->id, $displaymode);
165 echo "</td><td>";
167 if ($forum->type != 'single'
168 && has_capability('mod/forum:movediscussions', $modcontext)) {
170 // Popup menu to move discussions to other forums. The discussion in a
171 // single discussion forum can't be moved.
172 $modinfo = get_fast_modinfo($course);
173 if (isset($modinfo->instances['forum'])) {
174 if ($course->format == 'weeks') {
175 $strsection = get_string("week");
176 } else {
177 $strsection = get_string("topic");
179 $section = -1;
180 $forummenu = array();
181 foreach ($modinfo->instances['forum'] as $forumcm) {
182 if (!$forumcm->uservisible) {
183 continue;
186 if (!empty($forumcm->sectionnum) and $section != $forumcm->sectionnum) {
187 $forummenu[] = "-------------- $strsection $forumcm->sectionnum --------------";
189 $section = $forumcm->sectionnum;
190 if ($forumcm->instance != $forum->id) {
191 $url = "discuss.php?d=$discussion->id&amp;move=$forumcm->instance&amp;sesskey=".sesskey();
192 $forummenu[$url] = format_string($forumcm->name);
195 if (!empty($forummenu)) {
196 echo "<div style=\"float:right;\">";
197 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
198 get_string("movethisdiscussionto", "forum"), "", "", true);
199 echo "</div>";
203 echo "</td></tr></table>";
205 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
206 $a = new object();
207 $a->blockafter = $forum->blockafter;
208 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
209 notify(get_string('thisforumisthrottled','forum',$a));
212 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
213 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
214 notify(get_string('qandanotify','forum'));
217 if ($move == -1 and confirm_sesskey()) {
218 notify(get_string('discussionmoved', 'forum', format_string($forum->name,true)));
221 $canrate = has_capability('mod/forum:rate', $modcontext);
222 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
224 print_footer($course);