MDL-64820 forum: add subscription toggling to discussion list
[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.
5f219cf1 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}
87b007b4
CF
175// Pin or unpin discussion if requested.
176if ($pin !== -1 && confirm_sesskey()) {
177 require_capability('mod/forum:pindiscussions', $modcontext);
178
179 $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('forumid' => $forum->id));
180
181 switch ($pin) {
182 case FORUM_DISCUSSION_PINNED:
5f219cf1
BK
183 // Pin the discussion and trigger discussion pinned event.
184 forum_discussion_pin($modcontext, $forum, $discussion);
87b007b4
CF
185 break;
186 case FORUM_DISCUSSION_UNPINNED:
5f219cf1
BK
187 // Unpin the discussion and trigger discussion unpinned event.
188 forum_discussion_unpin($modcontext, $forum, $discussion);
87b007b4
CF
189 break;
190 default:
5f219cf1 191 echo $OUTPUT->notification("Invalid value when attempting to pin/unpin discussion");
87b007b4
CF
192 break;
193 }
194
195 redirect(new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
196}
197
1546987b
JL
198// Trigger discussion viewed event.
199forum_discussion_view($modcontext, $forum, $discussion);
501cdbd8 200
5f9997ae 201unset($SESSION->fromdiscussion);
501cdbd8 202
5f9997ae
AN
203if ($mode) {
204 set_user_preference('forum_displaymode', $mode);
205}
f37da850 206
5f9997ae 207$displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
39790bd8 208
5f9997ae
AN
209if ($parent) {
210 // If flat AND parent, then force nested display this time
211 if ($displaymode == FORUM_MODE_FLATOLDEST or $displaymode == FORUM_MODE_FLATNEWEST) {
212 $displaymode = FORUM_MODE_NESTED;
01d0aceb 213 }
5f9997ae
AN
214} else {
215 $parent = $discussion->firstpost;
216}
217
218if (! $post = forum_get_post_full($parent)) {
219 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
220}
221
3e95e09b 222if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm, false)) {
5f9997ae
AN
223 print_error('noviewdiscussionspermission', 'forum', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
224}
225
226if ($mark == 'read' or $mark == 'unread') {
227 if ($CFG->forum_usermarksread && forum_tp_can_track_forums($forum) && forum_tp_is_tracked($forum)) {
228 if ($mark == 'read') {
229 forum_tp_add_read_record($USER->id, $postid);
230 } else {
231 // unread
232 forum_tp_delete_read_records($USER->id, $postid);
ebfb73db
AN
233 }
234 }
5f9997ae
AN
235}
236
237$searchform = forum_search_form($course);
238
239$forumnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
240if (empty($forumnode)) {
241 $forumnode = $PAGE->navbar;
242} else {
243 $forumnode->make_active();
244}
245$node = $forumnode->add(format_string($discussion->name), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
246$node->display = false;
247if ($node && $post->id != $discussion->firstpost) {
248 $node->add(format_string($post->subject), $PAGE->url);
249}
250
251$PAGE->set_title("$course->shortname: ".format_string($discussion->name));
252$PAGE->set_heading($course->fullname);
253$PAGE->set_button($searchform);
254$renderer = $PAGE->get_renderer('mod_forum');
255
256echo $OUTPUT->header();
257
258echo $OUTPUT->heading(format_string($forum->name), 2);
259echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname');
260
261// is_guest should be used here as this also checks whether the user is a guest in the current course.
262// Guests and visitors cannot subscribe - only enrolled users.
263if ((!is_guest($modcontext, $USER) && isloggedin()) && has_capability('mod/forum:viewdiscussion', $modcontext)) {
264 // Discussion subscription.
265 if (\mod_forum\subscriptions::is_subscribable($forum)) {
266 echo html_writer::div(
267 forum_get_discussion_subscription_icon($forum, $post->discussion, null, true),
268 'discussionsubscription'
269 );
270 echo forum_get_discussion_subscription_icon_preloaders();
271 }
272}
ebfb73db 273
c6d691dc 274
9197e147 275/// Check to see if groups are being used in this forum
276/// If so, make sure the current person is allowed to see this discussion
8b79a625 277/// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
c6d691dc 278
5f9997ae
AN
279$canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
280if (!$canreply and $forum->type !== 'news') {
281 if (isguestuser() or !isloggedin()) {
282 $canreply = true;
283 }
284 if (!is_enrolled($modcontext) and !is_viewing($modcontext)) {
285 // allow guests and not-logged-in to see the link - they are prompted to log in after clicking the link
286 // normal users with temporary guest access see this link too, they are asked to enrol instead
287 $canreply = enrol_selfenrol_available($course->id);
9197e147 288 }
5f9997ae 289}
9197e147 290
5f9997ae 291// Output the links to neighbour discussions.
9e381efd 292$neighbours = forum_get_discussion_neighbours($cm, $discussion, $forum);
5f9997ae
AN
293$neighbourlinks = $renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
294echo $neighbourlinks;
d7862878 295
c6d691dc 296/// Print the controls across the top
7d41ccf0 297echo '<div class="discussioncontrols clearfix"><div class="controlscontainer m-b-1">';
5f9997ae
AN
298
299if (!empty($CFG->enableportfolios) && has_capability('mod/forum:exportdiscussion', $modcontext)) {
300 require_once($CFG->libdir.'/portfoliolib.php');
301 $button = new portfolio_add_button();
302 $button->set_callback_options('forum_portfolio_caller', array('discussionid' => $discussion->id), 'mod_forum');
303 $button = $button->to_html(PORTFOLIO_ADD_FULL_FORM, get_string('exportdiscussion', 'mod_forum'));
304 $buttonextraclass = '';
305 if (empty($button)) {
306 // no portfolio plugin available.
307 $button = '&nbsp;';
308 $buttonextraclass = ' noavailable';
10ae55f9 309 }
5f9997ae
AN
310 echo html_writer::tag('div', $button, array('class' => 'discussioncontrol exporttoportfolio'.$buttonextraclass));
311} else {
312 echo html_writer::tag('div', '&nbsp;', array('class'=>'discussioncontrol nullcontrol'));
313}
314
315// groups selector not needed here
316echo '<div class="discussioncontrol displaymode">';
317forum_print_mode_form($discussion->id, $displaymode);
318echo "</div>";
319
320if ($forum->type != 'single'
321 && has_capability('mod/forum:movediscussions', $modcontext)) {
322
323 echo '<div class="discussioncontrol movediscussion">';
324 // Popup menu to move discussions to other forums. The discussion in a
325 // single discussion forum can't be moved.
326 $modinfo = get_fast_modinfo($course);
327 if (isset($modinfo->instances['forum'])) {
328 $forummenu = array();
329 // Check forum types and eliminate simple discussions.
330 $forumcheck = $DB->get_records('forum', array('course' => $course->id),'', 'id, type');
331 foreach ($modinfo->instances['forum'] as $forumcm) {
332 if (!$forumcm->uservisible || !has_capability('mod/forum:startdiscussion',
333 context_module::instance($forumcm->id))) {
334 continue;
335 }
336 $section = $forumcm->sectionnum;
337 $sectionname = get_section_name($course, $section);
338 if (empty($forummenu[$section])) {
339 $forummenu[$section] = array($sectionname => array());
1fc49f00 340 }
5f9997ae
AN
341 $forumidcompare = $forumcm->instance != $forum->id;
342 $forumtypecheck = $forumcheck[$forumcm->instance]->type !== 'single';
343 if ($forumidcompare and $forumtypecheck) {
344 $url = "/mod/forum/discuss.php?d=$discussion->id&move=$forumcm->instance&sesskey=".sesskey();
345 $forummenu[$section][$sectionname][$url] = format_string($forumcm->name);
1fc49f00 346 }
347 }
5f9997ae
AN
348 if (!empty($forummenu)) {
349 echo '<div class="movediscussionoption">';
350 $select = new url_select($forummenu, '',
616dd1b5 351 array('/mod/forum/discuss.php?d=' . $discussion->id => get_string("movethisdiscussionto", "forum")),
d7f95392 352 'forummenu', get_string('move'));
5f9997ae
AN
353 echo $OUTPUT->render($select);
354 echo "</div>";
355 }
1fc49f00 356 }
12a24e00 357 echo "</div>";
5f9997ae 358}
87b007b4
CF
359
360if (has_capability('mod/forum:pindiscussions', $modcontext)) {
361 if ($discussion->pinned == FORUM_DISCUSSION_PINNED) {
362 $pinlink = FORUM_DISCUSSION_UNPINNED;
363 $pintext = get_string('discussionunpin', 'forum');
364 } else {
365 $pinlink = FORUM_DISCUSSION_PINNED;
366 $pintext = get_string('discussionpin', 'forum');
367 }
368 $button = new single_button(new moodle_url('discuss.php', array('pin' => $pinlink, 'd' => $discussion->id)), $pintext, 'post');
369 echo html_writer::tag('div', $OUTPUT->render($button), array('class' => 'discussioncontrol pindiscussion'));
370}
371
5f219cf1
BK
372
373echo "</div></div>";
1fc49f00 374
0f3bbfd4 375if (forum_discussion_is_locked($forum, $discussion)) {
8f725c12
MG
376 echo $OUTPUT->notification(get_string('discussionlocked', 'forum'),
377 \core\output\notification::NOTIFY_INFO . ' discussionlocked');
0f3bbfd4
AN
378}
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)) {
5f219cf1 389 echo $OUTPUT->notification(get_string('qandanotify', 'forum'));
5f9997ae 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();