Added moodle/site:accessallgroups capability.
[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");
7 require_once("lib.php");
501cdbd8 8
c992ff85 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', 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
16 if (! $discussion = get_record("forum_discussions", "id", $d)) {
94361e02 17 error("Discussion ID was incorrect or no longer exists");
501cdbd8 18 }
19
20 if (! $course = get_record("course", "id", $discussion->course)) {
21 error("Course ID is incorrect - discussion is faulty");
22 }
23
68258534 24 if (! $forum = get_record("forum", "id", $discussion->forum)) {
25 notify("Bad forum ID stored in this discussion");
26 }
27
bbbf2d40 28 if (! $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
29 error('Course Module ID was incorrect');
30 }
31
32 $canviewdiscussion = false;
68258534 33
bbbf2d40 34 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
35 if (has_capability('mod/forum:viewdiscussion', $context->id)) {
36 $canviewdiscussion = true;
37 }
68258534 38
bbbf2d40 39
40 if ($forum->type == "news") {
41 if (!($canviewdiscussion || $USER->id == $discussion->userid
fbc21e82 42 || (($discussion->timestart == 0 || $discussion->timestart <= time())
43 && ($discussion->timeend == 0 || $discussion->timeend > time())))) {
44 error('Discussion ID was incorrect or no longer exists', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
45 }
46
68258534 47 } else {
48 if (! $cm = get_coursemodule_from_instance("forum", $discussion->forum, $course->id)) {
49 error("Course Module ID was incorrect");
50 }
51 require_course_login($course, false, $cm);
68ddf8bc 52 }
53
1fc49f00 54
8f0cd6ef 55 if (!empty($move)) {
bbbf2d40 56 if (has_capability('mod/forum:movediscussions', $context->id)) {
57 error("You do not have the permission to move this discussion!");
1fc49f00 58 }
59 if ($forum = get_record("forum", "id", $move)) {
cc2b7ea5 60 if (!forum_move_attachments($discussion, $move)) {
61 notify("Errors occurred while moving attachment directories - check your file permissions");
62 }
bbbf2d40 63 set_field("forum_discussions", "forum", $forum->id, "id", $discussion->id);
1fc49f00 64 $discussion->forum = $forum->id;
69d79bc3 65 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
66 add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id",
67 $cm->id);
68 } else {
69 add_to_log($course->id, "forum", "move discussion", "discuss.php?d=$discussion->id", "$discussion->id");
70 }
8de14dc7 71 $discussionmoved = true;
1fc49f00 72 } else {
73 error("You can't move to that forum - it doesn't exist!");
74 }
75 }
76
1fc49f00 77 $logparameters = "d=$discussion->id";
78 if ($parent) {
839f2456 79 $logparameters .= "&amp;parent=$parent";
501cdbd8 80 }
69d79bc3 81
82 if ($cm = get_coursemodule_from_instance("forum", $forum->id, $course->id)) {
83 add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id", $cm->id);
84 } else {
85 add_to_log($course->id, "forum", "view discussion", "discuss.php?$logparameters", "$discussion->id");
86 }
501cdbd8 87
88 unset($SESSION->fromdiscussion);
89
279826e2 90 if ($mode) {
acb50c1b 91 if (isguest()) {
92 $USER->preference['forum_displaymode'] = $mode; // don't save it in database
93 } else {
94 set_user_preference('forum_displaymode', $mode);
95 }
279826e2 96 }
501cdbd8 97
acb50c1b 98 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 99
e92ea3d8 100 if ($parent) {
279826e2 101 if (abs($displaymode) == 1) { // If flat AND parent, then force nested display this time
e92ea3d8 102 $displaymode = 3;
103 }
104 } else {
501cdbd8 105 $parent = $discussion->firstpost;
c78ac798 106 $navtail = format_string($discussion->name);
501cdbd8 107 }
073286f0 108
ff1dc046 109 if (!forum_user_can_view_post($parent, $course, $cm, $forum, $discussion)) {
110 error('You do not have permissions to view this post', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
073286f0 111 }
501cdbd8 112
11b0c469 113 if (! $post = forum_get_post_full($parent)) {
501cdbd8 114 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
115 }
116
eaf50aef 117 if (forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum) &&
118 $CFG->forum_usermarksread) {
f37da850 119 if ($mark == 'read') {
120 forum_tp_add_read_record($USER->id, $postid, $discussion->id, $forum->id);
121 } else if ($mark == 'unread') {
122 forum_tp_delete_read_records($USER->id, $postid);
123 }
124 }
125
61e96406 126 if (empty($navtail)) {
c78ac798 127 $navtail = "<a href=\"discuss.php?d=$discussion->id\">".format_string($discussion->name,true)."</a> -> ".format_string($post->subject);
501cdbd8 128 }
129
3849dae8 130 $navmiddle = "<a href=\"../forum/index.php?id=$course->id\">".get_string("forums", "forum")."</a> -> <a href=\"../forum/view.php?f=$forum->id\">".format_string($forum->name,true)."</a>";
501cdbd8 131
6f1cc8d6 132 $searchform = forum_search_form($course);
97485d07 133
501cdbd8 134 if ($course->category) {
c78ac798 135 print_header("$course->shortname: ".format_string($discussion->name), "$course->fullname",
72b4e283 136 "<a href=\"../../course/view.php?id=$course->id\">$course->shortname</a> ->
8f0cd6ef 137 $navmiddle -> $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 138 } else {
c78ac798 139 print_header("$course->shortname: ".format_string($discussion->name), "$course->fullname",
8f0cd6ef 140 "$navmiddle -> $navtail", "", "", true, $searchform, navmenu($course, $cm));
501cdbd8 141 }
142
c6d691dc 143
9197e147 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
c6d691dc 146/// Also, if we know they should be able to reply, then explicitly set $canreply
147
148 $canreply = NULL; /// No override one way or the other
9197e147 149
52b201ee 150 if ($forum->type == "teacher") {
151 $groupmode = NOGROUPS;
152 } else {
153 $groupmode = groupmode($course, $cm);
154 }
9197e147 155
d0087b2f 156
157
bbbf2d40 158 if ($groupmode and !has_capability('mod/forum:viewdiscussionsfromallgroups', $context->id)) { // Groups must be kept separate
fa22fd5f 159 //change this to ismember
bbbf2d40 160 $mygroupid = mygroupid($course->id); //only useful if 0, otherwise it's an array now
c6d691dc 161 if ($groupmode == SEPARATEGROUPS) {
162 require_login();
163
fa22fd5f 164 if ((empty($mygroupid) and $discussion->groupid == -1) || (ismember($discussion->groupid) || $mygroupid == $discussion->groupid)) {
c6d691dc 165 $canreply = true;
2862b309 166 } elseif ($discussion->groupid == -1) {
167 $canreply = false;
c6d691dc 168 } else {
169 print_heading("Sorry, you can't see this discussion because you are not in this group");
cd8d4471 170 print_footer($course);
c6d691dc 171 die;
172 }
173
174 } else if ($groupmode == VISIBLEGROUPS) {
bbbf2d40 175 $canreply = ( (empty($mygroupid) && $discussion->groupid == -1) ||
176 (ismember($discussion->groupid) || $mygroupid == $discussion->groupid) &&
177 has_capability('mod/forum:replypost', $context->id) );
9197e147 178 }
179 }
180
d0087b2f 181
9197e147 182
c6d691dc 183/// Print the controls across the top
184
d5bbc556 185 echo '<table width="100%"><tr><td width="33%">';
c6d691dc 186
bbbf2d40 187 if ($groupmode == VISIBLEGROUPS or ($groupmode and has_capability('mod/forum:viewdiscussionsfromallgroups', $context->id))) {
d5bbc556 188 if ($groups = get_records_menu('groups', 'courseid', $course->id, 'name ASC', 'id,name')) {
839f2456 189 print_group_menu($groups, $groupmode, $discussion->groupid, "view.php?id=$cm->id&amp;group=");
c6d691dc 190 }
191 }
192
193 echo "</td><td width=\"33%\">";
ec9c0d44 194 forum_print_mode_form($discussion->id, $displaymode);
c6d691dc 195
02ebf404 196 echo "</td><td width=\"33%\">";
bbbf2d40 197 if (has_capability('mod/forum:movediscussions', $context->id)) { // Popup menu to move discussions to other forums
cccb016a 198 if ($forums = get_all_instances_in_course("forum", $course)) {
fcc69042 199 if ($course->format == 'weeks') {
200 $strsection = get_string("week");
201 } else {
202 $strsection = get_string("topic");
203 }
204 $section = -1;
1fc49f00 205 foreach ($forums as $courseforum) {
fcc69042 206 if (!empty($courseforum->section) and $section != $courseforum->section) {
207 $forummenu[] = "-------------- $strsection $courseforum->section --------------";
208 }
209 $section = $courseforum->section;
1fc49f00 210 if ($courseforum->id != $forum->id) {
839f2456 211 $url = "discuss.php?d=$discussion->id&amp;move=$courseforum->id";
3849dae8 212 $forummenu[$url] = format_string($courseforum->name,true);
1fc49f00 213 }
214 }
215 if (!empty($forummenu)) {
216 echo "<div align=\"right\">";
8f0cd6ef 217 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
1fc49f00 218 get_string("movethisdiscussionto", "forum"), "", "", true);
219 echo "</div>";
220 }
221 }
222 }
02ebf404 223 echo "</td></tr></table>";
1fc49f00 224
a4f495bf 225 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
226 $a->blockafter = $forum->blockafter;
227 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
228 notify(get_string('thisforumisthrottled','forum',$a));
229 }
230
bbbf2d40 231 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $context->id) &&
232 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
098d27d4 233 notify(get_string('qandanotify','forum'));
234 }
235
8de14dc7 236 if (isset($discussionmoved)) {
3849dae8 237 notify(get_string("discussionmoved", "forum", format_string($forum->name,true)));
8de14dc7 238 }
239
c6d691dc 240/// Print the actual discussion
bbbf2d40 241 $canrate = has_capability('mod/forum:rate', $context->id);
242 forum_print_discussion($course, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 243
501cdbd8 244 print_footer($course);
245
bbbf2d40 246?>