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