MDL-62384 privacy: Modify user contexts query for auth_oauth2
[moodle.git] / mod / forum / discuss.php
blob2366e45bfa183bdf9e7088c87a0f4c3caae44947
1 <?php
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.
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/>.
18 /**
19 * Displays a post, and all the posts below it.
20 * If no post is given, displays all posts in a discussion
22 * @package mod_forum
23 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require_once('../../config.php');
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.
35 $pin = optional_param('pin', -1, PARAM_INT); // If set, pin or unpin this discussion.
37 $url = new moodle_url('/mod/forum/discuss.php', array('d'=>$d));
38 if ($parent !== 0) {
39 $url->param('parent', $parent);
41 $PAGE->set_url($url);
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);
48 require_course_login($course, true, $cm);
50 // move this down fix for MDL-6926
51 require_once($CFG->dirroot.'/mod/forum/lib.php');
53 $modcontext = context_module::instance($cm->id);
54 require_capability('mod/forum:viewdiscussion', $modcontext, NULL, true, 'noviewdiscussionspermission', 'forum');
56 if (!empty($CFG->enablerssfeeds) && !empty($CFG->forum_enablerssfeeds) && $forum->rsstype && $forum->rssarticles) {
57 require_once("$CFG->libdir/rsslib.php");
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);
63 // Move discussion if requested.
64 if ($move > 0 and confirm_sesskey()) {
65 $return = $CFG->wwwroot.'/mod/forum/discuss.php?d='.$discussion->id;
67 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
68 print_error('cannotmovetonotexist', 'forum', $return);
71 require_capability('mod/forum:movediscussions', $modcontext);
73 if ($forum->type == 'single') {
74 print_error('cannotmovefromsingleforum', 'forum', $return);
77 if (!$forumto = $DB->get_record('forum', array('id' => $move))) {
78 print_error('cannotmovetonotexist', 'forum', $return);
81 if ($forumto->type == 'single') {
82 print_error('cannotmovetosingleforum', 'forum', $return);
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);
91 $cmto = $forums[$forumto->id];
92 if (!$cmto->uservisible) {
93 print_error('cannotmovenotvisible', 'forum', $return);
96 $destinationctx = context_module::instance($cmto->id);
97 require_capability('mod/forum:startdiscussion', $destinationctx);
99 if (!forum_move_attachments($discussion, $forum->id, $forumto->id)) {
100 echo $OUTPUT->notification("Errors occurred while moving attachment directories - check your file permissions");
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
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);
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;
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));
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);
149 } else {
150 \mod_forum\subscriptions::unsubscribe_user_from_discussion($userid, $newdiscussion, $destinationctx);
154 $params = array(
155 'context' => $destinationctx,
156 'objectid' => $discussion->id,
157 'other' => array(
158 'fromforumid' => $forum->id,
159 'toforumid' => $forumto->id,
162 $event = \mod_forum\event\discussion_moved::create($params);
163 $event->add_record_snapshot('forum_discussions', $discussion);
164 $event->add_record_snapshot('forum', $forum);
165 $event->add_record_snapshot('forum', $forumto);
166 $event->trigger();
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);
173 redirect($return.'&move=-1&sesskey='.sesskey());
175 // Pin or unpin discussion if requested.
176 if ($pin !== -1 && confirm_sesskey()) {
177 require_capability('mod/forum:pindiscussions', $modcontext);
179 $params = array('context' => $modcontext, 'objectid' => $discussion->id, 'other' => array('forumid' => $forum->id));
181 switch ($pin) {
182 case FORUM_DISCUSSION_PINNED:
183 // Pin the discussion and trigger discussion pinned event.
184 forum_discussion_pin($modcontext, $forum, $discussion);
185 break;
186 case FORUM_DISCUSSION_UNPINNED:
187 // Unpin the discussion and trigger discussion unpinned event.
188 forum_discussion_unpin($modcontext, $forum, $discussion);
189 break;
190 default:
191 echo $OUTPUT->notification("Invalid value when attempting to pin/unpin discussion");
192 break;
195 redirect(new moodle_url('/mod/forum/discuss.php', array('d' => $discussion->id)));
198 // Trigger discussion viewed event.
199 forum_discussion_view($modcontext, $forum, $discussion);
201 unset($SESSION->fromdiscussion);
203 if ($mode) {
204 set_user_preference('forum_displaymode', $mode);
207 $displaymode = get_user_preferences('forum_displaymode', $CFG->forum_displaymode);
209 if ($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;
214 } else {
215 $parent = $discussion->firstpost;
218 if (! $post = forum_get_post_full($parent)) {
219 print_error("notexists", 'forum', "$CFG->wwwroot/mod/forum/view.php?f=$forum->id");
222 if (!forum_user_can_see_post($forum, $discussion, $post, null, $cm)) {
223 print_error('noviewdiscussionspermission', 'forum', "$CFG->wwwroot/mod/forum/view.php?id=$forum->id");
226 if ($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);
237 $searchform = forum_search_form($course);
239 $forumnode = $PAGE->navigation->find($cm->id, navigation_node::TYPE_ACTIVITY);
240 if (empty($forumnode)) {
241 $forumnode = $PAGE->navbar;
242 } else {
243 $forumnode->make_active();
245 $node = $forumnode->add(format_string($discussion->name), new moodle_url('/mod/forum/discuss.php', array('d'=>$discussion->id)));
246 $node->display = false;
247 if ($node && $post->id != $discussion->firstpost) {
248 $node->add(format_string($post->subject), $PAGE->url);
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');
256 echo $OUTPUT->header();
258 echo $OUTPUT->heading(format_string($forum->name), 2);
259 echo $OUTPUT->heading(format_string($discussion->name), 3, 'discussionname');
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.
263 if ((!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'
270 echo forum_get_discussion_subscription_icon_preloaders();
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
277 /// Also, if we know they should be able to reply, then explicitly set $canreply for performance reasons
279 $canreply = forum_user_can_post($forum, $discussion, $USER, $cm, $course, $modcontext);
280 if (!$canreply and $forum->type !== 'news') {
281 if (isguestuser() or !isloggedin()) {
282 $canreply = true;
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);
291 // Output the links to neighbour discussions.
292 $neighbours = forum_get_discussion_neighbours($cm, $discussion, $forum);
293 $neighbourlinks = $renderer->neighbouring_discussion_navigation($neighbours['prev'], $neighbours['next']);
294 echo $neighbourlinks;
296 /// Print the controls across the top
297 echo '<div class="discussioncontrols clearfix"><div class="controlscontainer m-b-1">';
299 if (!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';
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'));
315 // groups selector not needed here
316 echo '<div class="discussioncontrol displaymode">';
317 forum_print_mode_form($discussion->id, $displaymode);
318 echo "</div>";
320 if ($forum->type != 'single'
321 && has_capability('mod/forum:movediscussions', $modcontext)) {
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;
336 $section = $forumcm->sectionnum;
337 $sectionname = get_section_name($course, $section);
338 if (empty($forummenu[$section])) {
339 $forummenu[$section] = array($sectionname => array());
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);
348 if (!empty($forummenu)) {
349 echo '<div class="movediscussionoption">';
350 $select = new url_select($forummenu, '',
351 array('/mod/forum/discuss.php?d=' . $discussion->id => get_string("movethisdiscussionto", "forum")),
352 'forummenu', get_string('move'));
353 echo $OUTPUT->render($select);
354 echo "</div>";
357 echo "</div>";
360 if (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');
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'));
373 echo "</div></div>";
375 if (forum_discussion_is_locked($forum, $discussion)) {
376 echo $OUTPUT->notification(get_string('discussionlocked', 'forum'),
377 \core\output\notification::NOTIFY_INFO . ' discussionlocked');
380 if (!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));
387 if ($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'));
392 if ($move == -1 and confirm_sesskey()) {
393 echo $OUTPUT->notification(get_string('discussionmoved', 'forum', format_string($forum->name,true)), 'notifysuccess');
396 $canrate = has_capability('mod/forum:rate', $modcontext);
397 forum_print_discussion($course, $cm, $forum, $discussion, $post, $displaymode, $canreply, $canrate);
399 echo $neighbourlinks;
401 // Add the subscription toggle JS.
402 $PAGE->requires->yui_module('moodle-mod_forum-subscriptiontoggle', 'Y.M.mod_forum.subscriptiontoggle.init');
404 echo $OUTPUT->footer();