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