merged fix for MDL-8864, users with no replypost capabilitiy can see reply link
[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
b0e3a925 6 require_once("../../config.php");
f0ef40c5 7
83da3d28 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', 0, PARAM_INT); // Used for tracking read posts if user initiated.
14 $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
501cdbd8 15
0fa18d5a 16 if (!$discussion = get_record("forum_discussions", "id", $d)) {
94361e02 17 error("Discussion ID was incorrect or no longer exists");
501cdbd8 18 }
19
0fa18d5a 20 if (!$course = get_record("course", "id", $discussion->course)) {
501cdbd8 21 error("Course ID is incorrect - discussion is faulty");
22 }
23
0fa18d5a 24 if (!$forum = get_record("forum", "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)) {
bbbf2d40 29 error('Course Module ID was incorrect');
30 }
f0ef40c5 31 // move this down fix for MDL-6926
32 require_once("lib.php");
bf553d9e 33 require_course_login($course, true, $cm);
50e07a49 34
9af1611a 35 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
0468976c 36 $canviewdiscussion = has_capability('mod/forum:viewdiscussion', $modcontext);
0fa18d5a 37
bbbf2d40 38 if ($forum->type == "news") {
49a0ba94 39 if (!($USER->id == $discussion->userid || (($discussion->timestart == 0
40 || $discussion->timestart <= time())
fbc21e82 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");
43 }
68ddf8bc 44 }
45
1fc49f00 46
8f0cd6ef 47 if (!empty($move)) {
83da3d28 48
49 if (!$sourceforum = get_record('forum', 'id', $fromforum)) {
50 error('Cannot find which forum this discussion is being moved from');
51 }
52 if ($sourceforum->type == 'single') {
53 error('Cannot move discussion from a simple single discussion forum');
54 }
55
7c900d5d 56 require_capability('mod/forum:movediscussions', $modcontext);
57
1fc49f00 58 if ($forum = get_record("forum", "id", $move)) {
cc2b7ea5 59 if (!forum_move_attachments($discussion, $move)) {
60 notify("Errors occurred while moving attachment directories - check your file permissions");
61 }
bbbf2d40 62 set_field("forum_discussions", "forum", $forum->id, "id", $discussion->id);
1fc49f00 63 $discussion->forum = $forum->id;
69d79bc3 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");
69 }
8de14dc7 70 $discussionmoved = true;
83da3d28 71
5a39def9 72 require_once('rsslib.php');
73 require_once($CFG->libdir.'/rsslib.php');
74
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.
83da3d28 78 if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($sourceforum)) {
5a39def9 79 notify('Could not purge the cached RSS feeds for the source and/or'.
80 'destination forum(s) - check your file permissionsforums');
81 }
1fc49f00 82 } else {
499c6214 83 error('You can\'t move to that forum - it doesn\'t exist!');
1fc49f00 84 }
85 }
86
1fc49f00 87 $logparameters = "d=$discussion->id";
88 if ($parent) {
839f2456 89 $logparameters .= "&amp;parent=$parent";
501cdbd8 90 }
69d79bc3 91
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");
96 }
501cdbd8 97
98 unset($SESSION->fromdiscussion);
99
279826e2 100 if ($mode) {
f2d042c4 101 set_user_preference('forum_displaymode', $mode);
279826e2 102 }
501cdbd8 103
acb50c1b 104 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 105
e92ea3d8 106 if ($parent) {
279826e2 107 if (abs($displaymode) == 1) { // If flat AND parent, then force nested display this time
e92ea3d8 108 $displaymode = 3;
109 }
102a5809 110 $navtail = '';
e92ea3d8 111 } else {
501cdbd8 112 $parent = $discussion->firstpost;
825d73aa 113 $navtail = '-> '.format_string($discussion->name);
501cdbd8 114 }
073286f0 115
ff1dc046 116 if (!forum_user_can_view_post($parent, $course, $cm, $forum, $discussion)) {
117 error('You do not have permissions to view this post', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
073286f0 118 }
501cdbd8 119
11b0c469 120 if (! $post = forum_get_post_full($parent)) {
501cdbd8 121 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
122 }
123
eaf50aef 124 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum) &&
125 $CFG->forum_usermarksread) {
f37da850 126 if ($mark == 'read') {
127 forum_tp_add_read_record($USER->id, $postid, $discussion->id, $forum->id);
128 } else if ($mark == 'unread') {
129 forum_tp_delete_read_records($USER->id, $postid);
130 }
131 }
132
cef1ce6a 133
61e96406 134 if (empty($navtail)) {
cef1ce6a 135 $navtail = "-> <a href=\"discuss.php?d=$discussion->id\">".
136 format_string($discussion->name,true)."</a> -> ".
137 format_string($post->subject);
501cdbd8 138 }
cef1ce6a 139 if ($forum->type == 'single') {
140 $navforum = '';
141 } else {
142 $navforum = "<a href=\"../forum/view.php?f=$forum->id\">".
143 format_string($forum->name,true)."</a> ";
144 }
145 $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">".
146 get_string("forums", "forum").'</a> -> '.$navforum;
501cdbd8 147
6f1cc8d6 148 $searchform = forum_search_form($course);
97485d07 149
1936c10e 150 if ($course->id != SITEID) {
6ba65fa0 151 print_header("$course->shortname: ".format_string($discussion->name), $course->fullname,
cef1ce6a 152 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
153 $navmiddle $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 154 } else {
6ba65fa0 155 print_header("$course->shortname: ".format_string($discussion->name), $course->fullname,
cef1ce6a 156 "$navmiddle $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 157 }
158
c6d691dc 159
9197e147 160/// Check to see if groups are being used in this forum
161/// If so, make sure the current person is allowed to see this discussion
c6d691dc 162/// Also, if we know they should be able to reply, then explicitly set $canreply
163
9276d742 164 if ($forum->type == 'news') {
165 $capname = 'mod/forum:replynews';
166 } else {
167 $capname = 'mod/forum:replypost';
168 }
45c38eef 169
9276d742 170 $groupmode = groupmode($course, $cm);
171 if ($canreply = has_capability($capname, $modcontext)) {
d0087b2f 172
9276d742 173
174 if ($groupmode && !has_capability('moodle/site:accessallgroups', $modcontext)) {
175 // Groups must be kept separate
176 //change this to ismember
177 $mygroupid = mygroupid($course->id); //only useful if 0, otherwise it's an array now
178 if ($groupmode == SEPARATEGROUPS) {
179 require_login();
180
181 if ((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid) || $mygroupid == $discussion->groupid)) {
182 // $canreply = true;
183 } elseif ($discussion->groupid == -1) {
184 $canreply = false;
185 } else {
186 print_heading("Sorry, you can't see this discussion because you are not in this group");
187 print_footer($course);
188 die;
189 }
190
191 } else if ($groupmode == VISIBLEGROUPS) {
192 $canreply = ( (empty($mygroupid) && $discussion->groupid == -1) ||
7b08a2ea 193 (ismember($discussion->groupid) || $mygroupid == $discussion->groupid) );
f5bf7ea8 194 }
195 }
9197e147 196 }
197
c6d691dc 198/// Print the controls across the top
199
a1857045 200 echo '<table width="100%" class="discussioncontrols"><tr><td>';
c6d691dc 201
0468976c 202 if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('moodle/site:accessallgroups', $modcontext))) {
f3f7610c 203 if ($groups = groups_get_groups_names($course->id)) { //TODO:
839f2456 204 print_group_menu($groups, $groupmode, $discussion->groupid, "view.php?id=$cm->id&amp;group=");
c6d691dc 205 }
206 }
207
c7a049a3 208 echo "</td><td>";
ec9c0d44 209 forum_print_mode_form($discussion->id, $displaymode);
c7a049a3 210 echo "</td><td>";
cef1ce6a 211
212 if ($forum->type != 'single'
213 && has_capability('mod/forum:movediscussions', $modcontext)) {
214
215 // Popup menu to move discussions to other forums. The discussion in a
216 // single discussion forum can't be moved.
cccb016a 217 if ($forums = get_all_instances_in_course("forum", $course)) {
fcc69042 218 if ($course->format == 'weeks') {
219 $strsection = get_string("week");
220 } else {
221 $strsection = get_string("topic");
222 }
223 $section = -1;
1fc49f00 224 foreach ($forums as $courseforum) {
fcc69042 225 if (!empty($courseforum->section) and $section != $courseforum->section) {
226 $forummenu[] = "-------------- $strsection $courseforum->section --------------";
227 }
228 $section = $courseforum->section;
1fc49f00 229 if ($courseforum->id != $forum->id) {
83da3d28 230 $url = "discuss.php?d=$discussion->id&amp;fromforum=$discussion->forum&amp;move=$courseforum->id";
3849dae8 231 $forummenu[$url] = format_string($courseforum->name,true);
1fc49f00 232 }
233 }
234 if (!empty($forummenu)) {
41a22032 235 echo "<div style=\"float:right;\">";
8f0cd6ef 236 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
1fc49f00 237 get_string("movethisdiscussionto", "forum"), "", "", true);
238 echo "</div>";
239 }
240 }
241 }
02ebf404 242 echo "</td></tr></table>";
1fc49f00 243
a4f495bf 244 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
245 $a->blockafter = $forum->blockafter;
246 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
247 notify(get_string('thisforumisthrottled','forum',$a));
248 }
249
0468976c 250 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 251 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
098d27d4 252 notify(get_string('qandanotify','forum'));
253 }
254
8de14dc7 255 if (isset($discussionmoved)) {
3849dae8 256 notify(get_string("discussionmoved", "forum", format_string($forum->name,true)));
8de14dc7 257 }
258
c6d691dc 259
49a0ba94 260/// Print the actual discussion
261 if (!$canviewdiscussion) {
262 notice(get_string('noviewdiscussionspermission', 'forum'));
263 } else {
264 $canrate = has_capability('mod/forum:rate', $modcontext);
265 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
266 }
267
501cdbd8 268 print_footer($course);
49a0ba94 269
501cdbd8 270
50e07a49 271?>