MDL-14428 fixed tag cron warning; merged from MOODLE_19_STABLE
[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
65bcf17b 6 require_once('../../config.php');
7 require_once('lib.php');
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
87b0b499 13 $mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated.
83da3d28 14 $postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
501cdbd8 15
65bcf17b 16 if (!$discussion = get_record('forum_discussions', 'id', $d)) {
90f4745c 17 error("Discussion ID was incorrect or no longer exists");
501cdbd8 18 }
19
65bcf17b 20 if (!$course = get_record('course', 'id', $discussion->course)) {
90f4745c 21 error("Course ID is incorrect - discussion is faulty");
501cdbd8 22 }
23
65bcf17b 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)) {
90f4745c 29 error('Course Module ID was incorrect');
bbbf2d40 30 }
65bcf17b 31
bf553d9e 32 require_course_login($course, true, $cm);
50e07a49 33
9af1611a 34 $modcontext = get_context_instance(CONTEXT_MODULE, $cm->id);
65bcf17b 35 require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
36
37 if ($forum->type == 'news') {
49a0ba94 38 if (!($USER->id == $discussion->userid || (($discussion->timestart == 0
39 || $discussion->timestart <= time())
fbc21e82 40 && ($discussion->timeend == 0 || $discussion->timeend > time())))) {
90f4745c 41 error('Discussion ID was incorrect or no longer exists', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
fbc21e82 42 }
68ddf8bc 43 }
44
65bcf17b 45/// move discussion if requested
46 if ($move > 0 and confirm_sesskey()) {
a5f77de6 47 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
65bcf17b 48
49 require_capability('mod/forum:movediscussions', $modcontext);
1fc49f00 50
65bcf17b 51 if ($forum->type == 'single') {
90f4745c 52 error('Cannot move discussion from a simple single discussion forum', $return);
83da3d28 53 }
65bcf17b 54
a5f77de6 55 if (!$forumto = get_record('forum', 'id', $move)) {
90f4745c 56 error('You can\'t move to that forum - it doesn\'t exist!', $return);
83da3d28 57 }
7c900d5d 58
65bcf17b 59 if (!$cmto = get_coursemodule_from_instance('forum', $forumto->id, $course->id)) {
90f4745c 60 error('Target forum not found in this course.', $return);
65bcf17b 61 }
62
63 if (!coursemodule_visible_for_user($cmto)) {
90f4745c 64 error('Forum not visible', $return);
1fc49f00 65 }
65bcf17b 66
b9973acc 67 if (!forum_move_attachments($discussion, $forumto->id)) {
65bcf17b 68 notify("Errors occurred while moving attachment directories - check your file permissions");
69 }
70 set_field('forum_discussions', 'forum', $forumto->id, 'id', $discussion->id);
90f4745c 71 set_field('forum_read', 'forumid', $forumto->id, 'discussionid', $discussion->id);
65bcf17b 72 add_to_log($course->id, 'forum', 'move discussion', "discuss.php?d=$discussion->id", $discussion->id, $cmto->id);
73
74 require_once($CFG->libdir.'/rsslib.php');
75 require_once('rsslib.php');
76
77 // Delete the RSS files for the 2 forums because we want to force
78 // the regeneration of the feeds since the discussions have been
79 // moved.
b9973acc 80 if (!forum_rss_delete_file($forum) || !forum_rss_delete_file($forumto)) {
90f4745c 81 error('Could not purge the cached RSS feeds for the source and/or'.
82 'destination forum(s) - check your file permissionsforums', $return);
65bcf17b 83 }
84
85 redirect($return.'&amp;moved=-1&amp;sesskey='.sesskey());
1fc49f00 86 }
87
1fc49f00 88 $logparameters = "d=$discussion->id";
89 if ($parent) {
839f2456 90 $logparameters .= "&amp;parent=$parent";
501cdbd8 91 }
69d79bc3 92
65bcf17b 93 add_to_log($course->id, 'forum', 'view discussion', "discuss.php?$logparameters", $discussion->id, $cm->id);
501cdbd8 94
95 unset($SESSION->fromdiscussion);
96
279826e2 97 if ($mode) {
f2d042c4 98 set_user_preference('forum_displaymode', $mode);
279826e2 99 }
501cdbd8 100
acb50c1b 101 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
501cdbd8 102
e92ea3d8 103 if ($parent) {
65bcf17b 104 // If flat AND parent, then force nested display this time
105 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
106 $displaymode = FORUM_MODE_NESTED;
e92ea3d8 107 }
108 } else {
501cdbd8 109 $parent = $discussion->firstpost;
501cdbd8 110 }
111
11b0c469 112 if (! $post = forum_get_post_full($parent)) {
90f4745c 113 error("Discussion no longer exists", "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
501cdbd8 114 }
115
c15b86dc 116
65bcf17b 117 if (!forum_user_can_view_post($post, $course, $cm, $forum, $discussion)) {
90f4745c 118 error('You do not have permissions to view this post', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
f37da850 119 }
120
65bcf17b 121 if ($mark == 'read' or $mark == 'unread') {
90f4745c 122 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
65bcf17b 123 if ($mark == 'read') {
90f4745c 124 forum_tp_add_read_record($USER->id, $postid);
65bcf17b 125 } else {
126 // unread
127 forum_tp_delete_read_records($USER->id, $postid);
128 }
129 }
130 }
cef1ce6a 131
6f1cc8d6 132 $searchform = forum_search_form($course);
87a462ea 133
134 $navlinks = array();
65bcf17b 135 $navlinks[] = array('name' => format_string($discussion->name), 'link' => "discuss.php?d=$discussion->id", 'type' => 'title');
87a462ea 136 if ($parent != $discussion->firstpost) {
65bcf17b 137 $navlinks[] = array('name' => format_string($post->subject), 'type' => 'title');
87a462ea 138 }
65bcf17b 139
140 $navigation = build_navigation($navlinks, $cm);
e3f58dfb 141 print_header("$course->shortname: ".format_string($discussion->name), $course->fullname,
142 $navigation, "", "", true, $searchform, navmenu($course, $cm));
65bcf17b 143
c6d691dc 144
9197e147 145/// Check to see if groups are being used in this forum
146/// If so, make sure the current person is allowed to see this discussion
c6d691dc 147/// Also, if we know they should be able to reply, then explicitly set $canreply
148
9276d742 149 if ($forum->type == 'news') {
150 $capname = 'mod/forum:replynews';
151 } else {
152 $capname = 'mod/forum:replypost';
153 }
65bcf17b 154
c57da119 155 $canreply = false;
7214101b 156 if (isguestuser() or !isloggedin() or has_capability('moodle/legacy:guest', $modcontext, NULL, false)) {
525041df 157 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
158 $canreply = ($forum->type != 'news'); // no reply in news forums
159
160 } else if (has_capability($capname, $modcontext)) {
c57da119 161 $groupmode = groups_get_activity_groupmode($cm);
162 if ($groupmode) {
163 if (has_capability('moodle/site:accessallgroups', $modcontext)) {
164 $canreply = true;
165 } else {
166 if ($groupmode == SEPARATEGROUPS) {
167 require_login();
168 if ($discussion->groupid == -1) {
169 // can not reply to discussions for "All participants" in separate mode without accessallgroups cap
170 } else if (groups_is_member($discussion->groupid)) {
171 $canreply = true;
172 } else {
173 // this should not happen
174 print_heading("Sorry, you can't see this discussion because you are not in this group");
175 print_footer($course);
176 die;
177 }
65bcf17b 178
c57da119 179 } else if ($groupmode == VISIBLEGROUPS) {
180 if ($discussion->groupid == -1 or groups_is_member($discussion->groupid)) {
181 $canreply = true;
182 }
9276d742 183 }
65bcf17b 184 }
c57da119 185 } else {
186 $canreply = true;
f5bf7ea8 187 }
9197e147 188 }
189
c6d691dc 190/// Print the controls across the top
191
a1857045 192 echo '<table width="100%" class="discussioncontrols"><tr><td>';
c6d691dc 193
87b0b499 194 // groups selector not needed here
c6d691dc 195
c7a049a3 196 echo "</td><td>";
ec9c0d44 197 forum_print_mode_form($discussion->id, $displaymode);
c7a049a3 198 echo "</td><td>";
cef1ce6a 199
200 if ($forum->type != 'single'
201 && has_capability('mod/forum:movediscussions', $modcontext)) {
65bcf17b 202
cef1ce6a 203 // Popup menu to move discussions to other forums. The discussion in a
204 // single discussion forum can't be moved.
65bcf17b 205 $modinfo = get_fast_modinfo($course);
206 if (isset($modinfo->instances['forum'])) {
fcc69042 207 if ($course->format == 'weeks') {
208 $strsection = get_string("week");
209 } else {
210 $strsection = get_string("topic");
211 }
212 $section = -1;
65bcf17b 213 $forummenu = array();
214 foreach ($modinfo->instances['forum'] as $forumcm) {
215 if (!$forumcm->uservisible) {
216 continue;
fcc69042 217 }
65bcf17b 218
219 if (!empty($forumcm->sectionnum) and $section != $forumcm->sectionnum) {
220 $forummenu[] = "-------------- $strsection $forumcm->sectionnum --------------";
221 }
222 $section = $forumcm->sectionnum;
223 if ($forumcm->instance != $forum->id) {
224 $url = "discuss.php?d=$discussion->id&amp;move=$forumcm->instance&amp;sesskey=".sesskey();
225 $forummenu[$url] = format_string($forumcm->name);
1fc49f00 226 }
227 }
228 if (!empty($forummenu)) {
41a22032 229 echo "<div style=\"float:right;\">";
8f0cd6ef 230 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
1fc49f00 231 get_string("movethisdiscussionto", "forum"), "", "", true);
232 echo "</div>";
233 }
234 }
235 }
02ebf404 236 echo "</td></tr></table>";
1fc49f00 237
a4f495bf 238 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
65bcf17b 239 $a = new object();
240 $a->blockafter = $forum->blockafter;
a4f495bf 241 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
242 notify(get_string('thisforumisthrottled','forum',$a));
243 }
244
0468976c 245 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 246 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
098d27d4 247 notify(get_string('qandanotify','forum'));
248 }
249
65bcf17b 250 if ($move == -1 and confirm_sesskey()) {
251 notify(get_string('discussionmoved', 'forum', format_string($forum->name,true)));
8de14dc7 252 }
253
65bcf17b 254 $canrate = has_capability('mod/forum:rate', $modcontext);
255 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 256
501cdbd8 257 print_footer($course);
65bcf17b 258
501cdbd8 259
50e07a49 260?>