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