MDL-372 forum: support for pinned discussion topics
[moodle.git] / mod / forum / discuss.php
CommitLineData
1adbd2c3 1<?php
501cdbd8 2
8f685009
SH
3// This file is part of Moodle - http://moodle.org/
4//
5// Moodle is free software: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License as published by
7// the Free Software Foundation, either version 3 of the License, or
8// (at your option) any later version.
9//
10// Moodle is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17
18/**
19 * Displays a post, and all the posts below it.
20 * If no post is given, displays all posts in a discussion
21 *
01030f1b 22 * @package mod_forum
8f685009
SH
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
501cdbd8 26
5f9997ae 27require_once('../../config.php');
d3558659 28
5f9997ae
AN
29$d = required_param('d', PARAM_INT); // Discussion ID
30$parent = optional_param('parent', 0, PARAM_INT); // If set, then display this post and all children.
31$mode = optional_param('mode', 0, PARAM_INT); // If set, changes the layout of the thread
32$move = optional_param('move', 0, PARAM_INT); // If set, moves this discussion to another forum
33$mark = optional_param('mark', '', PARAM_ALPHA); // Used for tracking read posts if user initiated.
34$postid = optional_param('postid', 0, PARAM_INT); // Used for tracking read posts if user initiated.
87b007b4 35$pin = optional_param('pin', -1, PARAM_INT); // If set, pin or unpin this discussion.
68258534 36
5f9997ae
AN
37$url = new moodle_url('/mod/forum/discuss.php', array('d'=>$d));
38if ($parent !== 0) {
39 $url->param('parent', $parent);
40}
41$PAGE->set_url($url);
50e07a49 42
5f9997ae
AN
43$discussion = $DB->get_record('forum_discussions', array('id' => $d), '*', MUST_EXIST);
44$course = $DB->get_record('course', array('id' => $discussion->course), '*', MUST_EXIST);
45$forum = $DB->get_record('forum', array('id' => $discussion->forum), '*', MUST_EXIST);
46$cm = get_coursemodule_from_instance('forum', $forum->id, $course->id, false, MUST_EXIST);
4cabf99f 47
5f9997ae 48require_course_login($course, true, $cm);
65bcf17b 49
5f9997ae
AN
50// move this down fix for MDL-6926
51require_once($CFG->dirroot.'/mod/forum/lib.php');
9e86f2e7 52
5f9997ae
AN
53$modcontext = context_module::instance($cm->id);
54require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
9e86f2e7 55
5f9997ae
AN
56if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->rsstype && $forum->rssarticles) {
57 require_once("$CFG->libdir/rsslib.php");
65bcf17b 58
5f9997ae
AN
59 $rsstitle = format_string($course->shortname, true, array('context' => context_course::instance($course->id))) . ': ' . format_string($forum->name);
60 rss_add_http_header($modcontext, 'mod_forum', $forum, $rsstitle);
61}
03574650 62
5f9997ae
AN
63// Move discussion if requested.
64if ($move > 0 and confirm_sesskey()) {
65 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
1fc49f00 66
5f9997ae
AN
67 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
68 print_error('cannotmovetonotexist', 'forum', $return);
69 }
65bcf17b 70
5f9997ae 71 require_capability('mod/forum:movediscussions', $modcontext);
7c900d5d 72
5f9997ae
AN
73 if ($forum->type == 'single') {
74 print_error('cannotmovefromsingleforum', 'forum', $return);
75 }
5fad08b4 76
5f9997ae
AN
77 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
78 print_error('cannotmovetonotexist', 'forum', $return);
79 }
65bcf17b 80
5f9997ae
AN
81 if ($forumto->type == 'single') {
82 print_error('cannotmovetosingleforum', 'forum', $return);
83 }
ee8d825f 84
5f9997ae
AN
85 // Get target forum cm and check it is visible to current user.
86 $modinfo = get_fast_modinfo($course);
87 $forums = $modinfo->get_instances_of('forum');
88 if (!array_key_exists($forumto->id, $forums)) {
89 print_error('cannotmovetonotfound', 'forum', $return);
90 }
91 $cmto = $forums[$forumto->id];
92 if (!$cmto->uservisible) {
93 print_error('cannotmovenotvisible', 'forum', $return);
94 }
03574650 95
5f9997ae
AN
96 $destinationctx = context_module::instance($cmto->id);
97 require_capability('mod/forum:startdiscussion', $destinationctx);
98
99 if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
100 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
101 }
102 // For each subscribed user in this forum and discussion, copy over per-discussion subscriptions if required.
103 $discussiongroup = $discussion->groupid == -1 ? 0 : $discussion->groupid;
104 $potentialsubscribers = \mod_forum\subscriptions::fetch_subscribed_users(
105 $forum,
106 $discussiongroup,
107 $modcontext,
108 'u.id',
109 true
110 );
111
112 // Pre-seed the subscribed_discussion caches.
113 // Firstly for the forum being moved to.
114 \mod_forum\subscriptions::fill_subscription_cache($forumto->id);
115 // And also for the discussion being moved.
116 \mod_forum\subscriptions::fill_subscription_cache($forum->id);
117 $subscriptionchanges = array();
118 $subscriptiontime = time();
119 foreach ($potentialsubscribers as $subuser) {
120 $userid = $subuser->id;
121 $targetsubscription = \mod_forum\subscriptions::is_subscribed($userid, $forumto, null, $cmto);
122 $discussionsubscribed = \mod_forum\subscriptions::is_subscribed($userid, $forum, $discussion->id);
123 $forumsubscribed = \mod_forum\subscriptions::is_subscribed($userid, $forum);
124
125 if ($forumsubscribed && !$discussionsubscribed && $targetsubscription) {
126 // The user has opted out of this discussion and the move would cause them to receive notifications again.
127 // Ensure they are unsubscribed from the discussion still.
128 $subscriptionchanges[$userid] = \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED;
129 } else if (!$forumsubscribed && $discussionsubscribed && !$targetsubscription) {
130 // The user has opted into this discussion and would otherwise not receive the subscription after the move.
131 // Ensure they are subscribed to the discussion still.
132 $subscriptionchanges[$userid] = $subscriptiontime;
03574650 133 }
5f9997ae 134 }
03574650 135
5f9997ae
AN
136 $DB->set_field('forum_discussions', 'forum', $forumto->id, array('id' => $discussion->id));
137 $DB->set_field('forum_read', 'forumid', $forumto->id, array('discussionid' => $discussion->id));
138
139 // Delete the existing per-discussion subscriptions and replace them with the newly calculated ones.
140 $DB->delete_records('forum_discussion_subs', array('discussion' => $discussion->id));
141 $newdiscussion = clone $discussion;
142 $newdiscussion->forum = $forumto->id;
143 foreach ($subscriptionchanges as $userid => $preference) {
144 if ($preference != \mod_forum\subscriptions::FORUM_DISCUSSION_UNSUBSCRIBED) {
145 // Users must have viewdiscussion to a discussion.
146 if (has_capability('mod/forum:viewdiscussion', $destinationctx, $userid)) {
147 \mod_forum\subscriptions::subscribe_user_to_discussion($userid, $newdiscussion, $destinationctx);
03574650 148 }
5f9997ae
AN
149 } else {
150 \mod_forum\subscriptions::unsubscribe_user_from_discussion($userid, $newdiscussion, $destinationctx);
03574650 151 }
1fc49f00 152 }
153
71595d00 154 $params = array(
5f9997ae 155 'context' => $destinationctx,
71595d00 156 'objectid' => $discussion->id,
5f9997ae
AN
157 'other' => array(
158 'fromforumid' => $forum->id,
159 'toforumid' => $forumto->id,
160 )
71595d00 161 );
5f9997ae 162 $event = \mod_forum\event\discussion_moved::create($params);
71595d00
DP
163 $event->add_record_snapshot('forum_discussions', $discussion);
164 $event->add_record_snapshot('forum', $forum);
5f9997ae 165 $event->add_record_snapshot('forum', $forumto);
71595d00 166 $event->trigger();
501cdbd8 167
5f9997ae
AN
168 // Delete the RSS files for the 2 forums to force regeneration of the feeds
169 require_once($CFG->dirroot.'/mod/forum/rsslib.php');
170 forum_rss_delete_file($forum);
171 forum_rss_delete_file($forumto);
501cdbd8 172
cb658a4d 173 redirect($return.'&move=-1&sesskey='.sesskey());
5f9997ae 174}
501cdbd8 175
87b007b4
CF
176// Pin or unpin discussion if requested.
177if ($pin !== -1 && confirm_sesskey()) {
178 require_capability('mod/forum:pindiscussions', $modcontext);
179
180 $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('forumid' => $forum->id));
181
182 switch ($pin) {
183 case FORUM_DISCUSSION_PINNED:
184 $DB->set_field('forum_discussions', 'pinned', $pin, array('id' => $discussion->id));
185 $event = \mod_forum\event\discussion_pinned::create($params);
186 $event->add_record_snapshot('forum_discussions', $discussion);
187 $event->trigger();
188 break;
189 case FORUM_DISCUSSION_UNPINNED:
190 $DB->set_field('forum_discussions', 'pinned', $pin, array('id' => $discussion->id));
191 $event = \mod_forum\event\discussion_unpinned::create($params);
192 $event->add_record_snapshot('forum_discussions', $discussion);
193 $event->trigger();
194 break;
195 default:
196 echo $OUTPUT->notfication("Invalid value when attempting to pin/unpin discussion");
197 break;
198 }
199
200 redirect(new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
201}
202
1546987b
JL
203// Trigger discussion viewed event.
204forum_discussion_view($modcontext, $forum, $discussion);
501cdbd8 205
5f9997ae 206unset($SESSION->fromdiscussion);
501cdbd8 207
5f9997ae
AN
208if ($mode) {
209 set_user_preference('forum_displaymode', $mode);
210}
f37da850 211
5f9997ae 212$displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
39790bd8 213
5f9997ae
AN
214if ($parent) {
215 // If flat AND parent, then force nested display this time
216 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
217 $displaymode = FORUM_MODE_NESTED;
01d0aceb 218 }
5f9997ae
AN
219} else {
220 $parent = $discussion->firstpost;
221}
222
223if (! $post = forum_get_post_full($parent)) {
224 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
225}
226
227if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
228 print_error('noviewdiscussionspermission', 'forum', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
229}
230
231if ($mark == 'read' or $mark == 'unread') {
232 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
233 if ($mark == 'read') {
234 forum_tp_add_read_record($USER->id, $postid);
235 } else {
236 // unread
237 forum_tp_delete_read_records($USER->id, $postid);
ebfb73db
AN
238 }
239 }
5f9997ae
AN
240}
241
242$searchform = forum_search_form($course);
243
244$forumnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
245if (empty($forumnode)) {
246 $forumnode = $PAGE->navbar;
247} else {
248 $forumnode->make_active();
249}
250$node = $forumnode->add(format_string($discussion->name), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
251$node->display = false;
252if ($node && $post->id != $discussion->firstpost) {
253 $node->add(format_string($post->subject), $PAGE->url);
254}
255
256$PAGE->set_title("$course->shortname: ".format_string($discussion->name));
257$PAGE->set_heading($course->fullname);
258$PAGE->set_button($searchform);
259$renderer = $PAGE->get_renderer('mod_forum');
260
261echo $OUTPUT->header();
262
263echo $OUTPUT->heading(format_string($forum->name), 2);
264echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname');
265
266// is_guest should be used here as this also checks whether the user is a guest in the current course.
267// Guests and visitors cannot subscribe - only enrolled users.
268if ((!is_guest($modcontext, $USER) && isloggedin()) && has_capability('mod/forum:viewdiscussion', $modcontext)) {
269 // Discussion subscription.
270 if (\mod_forum\subscriptions::is_subscribable($forum)) {
271 echo html_writer::div(
272 forum_get_discussion_subscription_icon($forum, $post->discussion, null, true),
273 'discussionsubscription'
274 );
275 echo forum_get_discussion_subscription_icon_preloaders();
276 }
277}
ebfb73db 278
c6d691dc 279
9197e147 280/// Check to see if groups are being used in this forum
281/// If so, make sure the current person is allowed to see this discussion
8b79a625 282/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
c6d691dc 283
5f9997ae
AN
284$canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
285if (!$canreply and $forum->type !== 'news') {
286 if (isguestuser() or !isloggedin()) {
287 $canreply = true;
288 }
289 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
290 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
291 // normal users with temporary guest access see this link too, they are asked to enrol instead
292 $canreply = enrol_selfenrol_available($course->id);
9197e147 293 }
5f9997ae 294}
9197e147 295
5f9997ae 296// Output the links to neighbour discussions.
9e381efd 297$neighbours = forum_get_discussion_neighbours($cm, $discussion, $forum);
5f9997ae
AN
298$neighbourlinks = $renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
299echo $neighbourlinks;
d7862878 300
c6d691dc 301/// Print the controls across the top
5f9997ae
AN
302echo '<div class="discussioncontrols clearfix">';
303
304if (!empty($CFG->enableportfolios) && has_capability('mod/forum:exportdiscussion', $modcontext)) {
305 require_once($CFG->libdir.'/portfoliolib.php');
306 $button = new portfolio_add_button();
307 $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), 'mod_forum');
308 $button = $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportdiscussion', 'mod_forum'));
309 $buttonextraclass = '';
310 if (empty($button)) {
311 // no portfolio plugin available.
312 $button = '&nbsp;';
313 $buttonextraclass = ' noavailable';
10ae55f9 314 }
5f9997ae
AN
315 echo html_writer::tag('div', $button, array('class' => 'discussioncontrol exporttoportfolio'.$buttonextraclass));
316} else {
317 echo html_writer::tag('div', '&nbsp;', array('class'=>'discussioncontrol nullcontrol'));
318}
319
320// groups selector not needed here
321echo '<div class="discussioncontrol displaymode">';
322forum_print_mode_form($discussion->id, $displaymode);
323echo "</div>";
324
325if ($forum->type != 'single'
326 && has_capability('mod/forum:movediscussions', $modcontext)) {
327
328 echo '<div class="discussioncontrol movediscussion">';
329 // Popup menu to move discussions to other forums. The discussion in a
330 // single discussion forum can't be moved.
331 $modinfo = get_fast_modinfo($course);
332 if (isset($modinfo->instances['forum'])) {
333 $forummenu = array();
334 // Check forum types and eliminate simple discussions.
335 $forumcheck = $DB->get_records('forum', array('course' => $course->id),'', 'id, type');
336 foreach ($modinfo->instances['forum'] as $forumcm) {
337 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
338 context_module::instance($forumcm->id))) {
339 continue;
340 }
341 $section = $forumcm->sectionnum;
342 $sectionname = get_section_name($course, $section);
343 if (empty($forummenu[$section])) {
344 $forummenu[$section] = array($sectionname => array());
1fc49f00 345 }
5f9997ae
AN
346 $forumidcompare = $forumcm->instance != $forum->id;
347 $forumtypecheck = $forumcheck[$forumcm->instance]->type !== 'single';
348 if ($forumidcompare and $forumtypecheck) {
349 $url = "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
350 $forummenu[$section][$sectionname][$url] = format_string($forumcm->name);
1fc49f00 351 }
352 }
5f9997ae
AN
353 if (!empty($forummenu)) {
354 echo '<div class="movediscussionoption">';
355 $select = new url_select($forummenu, '',
616dd1b5 356 array('/mod/forum/discuss.php?d=' . $discussion->id => get_string("movethisdiscussionto", "forum")),
d7f95392 357 'forummenu', get_string('move'));
5f9997ae
AN
358 echo $OUTPUT->render($select);
359 echo "</div>";
360 }
1fc49f00 361 }
12a24e00 362 echo "</div>";
5f9997ae 363}
87b007b4
CF
364
365if (has_capability('mod/forum:pindiscussions', $modcontext)) {
366 if ($discussion->pinned == FORUM_DISCUSSION_PINNED) {
367 $pinlink = FORUM_DISCUSSION_UNPINNED;
368 $pintext = get_string('discussionunpin', 'forum');
369 } else {
370 $pinlink = FORUM_DISCUSSION_PINNED;
371 $pintext = get_string('discussionpin', 'forum');
372 }
373 $button = new single_button(new moodle_url('discuss.php', array('pin' => $pinlink, 'd' => $discussion->id)), $pintext, 'post');
374 echo html_writer::tag('div', $OUTPUT->render($button), array('class' => 'discussioncontrol pindiscussion'));
375}
376
5f9997ae
AN
377echo '<div class="clearfloat">&nbsp;</div>';
378echo "</div>";
1fc49f00 379
5f9997ae
AN
380if (!empty($forum->blockafter) && !empty($forum->blockperiod)) {
381 $a = new stdClass();
382 $a->blockafter = $forum->blockafter;
383 $a->blockperiod = get_string('secondstotime'.$forum->blockperiod);
384 echo $OUTPUT->notification(get_string('thisforumisthrottled','forum',$a));
385}
a4f495bf 386
5f9997ae
AN
387if ($forum->type == 'qanda' && !has_capability('mod/forum:viewqandawithoutposting', $modcontext) &&
388 !forum_user_has_posted($forum->id,$discussion->id,$USER->id)) {
389 echo $OUTPUT->notification(get_string('qandanotify','forum'));
390}
098d27d4 391
5f9997ae 392if ($move == -1 and confirm_sesskey()) {
0880780e 393 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', format_string($forum->name,true)), 'notifysuccess');
5f9997ae 394}
8de14dc7 395
5f9997ae
AN
396$canrate = has_capability('mod/forum:rate', $modcontext);
397forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
c6d691dc 398
5f9997ae 399echo $neighbourlinks;
8744529f 400
5f9997ae
AN
401// Add the subscription toggle JS.
402$PAGE->requires->yui_module('moodle-mod_forum-subscriptiontoggle', 'Y.M.mod_forum.subscriptiontoggle.init');
1adbd2c3 403
5f9997ae 404echo $OUTPUT->footer();