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