MDL-15760 - fix teeny notice-causing bug in box.net portfolio plugin
[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
4e445355 16 if (!$discussion = $DB->get_record('forum_discussions', array('id' => $d))) {
90f4745c 17 error("Discussion ID was incorrect or no longer exists");
501cdbd8 18 }
19
4e445355 20 if (!$course = $DB->get_record('course', array('id' => $discussion->course))) {
90f4745c 21 error("Course ID is incorrect - discussion is faulty");
501cdbd8 22 }
23
4e445355 24 if (!$forum = $DB->get_record('forum', array('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
4e445355 55 if (!$forumto = $DB->get_record('forum', array('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
0faf6791 67 if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
65bcf17b 68 notify("Errors occurred while moving attachment directories - check your file permissions");
69 }
4e445355 70 $DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
71 $DB->set_field('forum_read', 'forumid', $forumto->id, array('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
8b79a625 147/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
c6d691dc 148
7214101b 149 if (isguestuser() or !isloggedin() or has_capability('moodle/legacy:guest', $modcontext, NULL, false)) {
525041df 150 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
151 $canreply = ($forum->type != 'news'); // no reply in news forums
152
8b79a625 153 } else {
154 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
9197e147 155 }
156
c6d691dc 157/// Print the controls across the top
158
adf0ff69 159 if (has_capability('mod/forum:exportdiscussion', $modcontext)) {
10ae55f9 160
161 $p = array(
162 'discussionid' => $discussion->id,
163 );
c9e74546 164 $portfolio = portfolio_add_button('forum_portfolio_caller', $p, '/mod/forum/lib.php', null, null, true);
10ae55f9 165 }
a1857045 166 echo '<table width="100%" class="discussioncontrols"><tr><td>';
c6d691dc 167
87b0b499 168 // groups selector not needed here
c6d691dc 169
c7a049a3 170 echo "</td><td>";
ec9c0d44 171 forum_print_mode_form($discussion->id, $displaymode);
c7a049a3 172 echo "</td><td>";
cef1ce6a 173
14bea70c 174 if (isset($portfolio)) {
10ae55f9 175 echo $portfolio . '</td><td>';
176 }
177
cef1ce6a 178 if ($forum->type != 'single'
179 && has_capability('mod/forum:movediscussions', $modcontext)) {
65bcf17b 180
cef1ce6a 181 // Popup menu to move discussions to other forums. The discussion in a
182 // single discussion forum can't be moved.
65bcf17b 183 $modinfo = get_fast_modinfo($course);
184 if (isset($modinfo->instances['forum'])) {
fcc69042 185 if ($course->format == 'weeks') {
186 $strsection = get_string("week");
187 } else {
188 $strsection = get_string("topic");
189 }
190 $section = -1;
65bcf17b 191 $forummenu = array();
192 foreach ($modinfo->instances['forum'] as $forumcm) {
193 if (!$forumcm->uservisible) {
194 continue;
fcc69042 195 }
65bcf17b 196
197 if (!empty($forumcm->sectionnum) and $section != $forumcm->sectionnum) {
198 $forummenu[] = "-------------- $strsection $forumcm->sectionnum --------------";
199 }
200 $section = $forumcm->sectionnum;
201 if ($forumcm->instance != $forum->id) {
202 $url = "discuss.php?d=$discussion->id&amp;move=$forumcm->instance&amp;sesskey=".sesskey();
203 $forummenu[$url] = format_string($forumcm->name);
1fc49f00 204 }
205 }
206 if (!empty($forummenu)) {
41a22032 207 echo "<div style=\"float:right;\">";
8f0cd6ef 208 echo popup_form("$CFG->wwwroot/mod/forum/", $forummenu, "forummenu", "",
1fc49f00 209 get_string("movethisdiscussionto", "forum"), "", "", true);
210 echo "</div>";
211 }
212 }
213 }
02ebf404 214 echo "</td></tr></table>";
1fc49f00 215
a4f495bf 216 if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
65bcf17b 217 $a = new object();
218 $a->blockafter = $forum->blockafter;
a4f495bf 219 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
220 notify(get_string('thisforumisthrottled','forum',$a));
221 }
222
0468976c 223 if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
bbbf2d40 224 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
098d27d4 225 notify(get_string('qandanotify','forum'));
226 }
227
65bcf17b 228 if ($move == -1 and confirm_sesskey()) {
229 notify(get_string('discussionmoved', 'forum', format_string($forum->name,true)));
8de14dc7 230 }
231
65bcf17b 232 $canrate = has_capability('mod/forum:rate', $modcontext);
233 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 234
501cdbd8 235 print_footer($course);
65bcf17b 236
501cdbd8 237
50e07a49 238?>