weekly release 4.0dev
[moodle.git] / mod / forum / subscribers.php
CommitLineData
cd4e6b17 1<?php
bffb106d 2
c23c9009
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 * This file is used to display and organise forum subscribers
20 *
01030f1b 21 * @package mod_forum
c23c9009
SH
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
cd4e6b17 26require_once("../../config.php");
27require_once("lib.php");
bffb106d 28
cd4e6b17 29$id = required_param('id',PARAM_INT); // forum
30$group = optional_param('group',0,PARAM_INT); // change of group
31$edit = optional_param('edit',-1,PARAM_BOOL); // Turn editing on and off
bffb106d 32
a6855934 33$url = new moodle_url('/mod/forum/subscribers.php', array('id'=>$id));
cd4e6b17 34if ($group !== 0) {
35 $url->param('group', $group);
36}
37if ($edit !== 0) {
38 $url->param('edit', $edit);
39}
40$PAGE->set_url($url);
bffb106d 41
c23c9009 42$forum = $DB->get_record('forum', array('id'=>$id), '*', MUST_EXIST);
74df2951 43$course = $DB->get_record('course', array('id'=>$forum->course), '*', MUST_EXIST);
cd4e6b17 44if (! $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
45 $cm->id = 0;
46}
bffb106d 47
cdbea7ee 48require_login($course, false, $cm);
bbbf2d40 49
bf0f06b1 50$context = context_module::instance($cm->id);
cd4e6b17 51if (!has_capability('mod/forum:viewsubscribers', $context)) {
52 print_error('nopermissiontosubscribe', 'forum');
53}
bffb106d 54
cd4e6b17 55unset($SESSION->fromdiscussion);
680afe2e 56
de277050
DP
57$params = array(
58 'context' => $context,
59 'other' => array('forumid' => $forum->id),
60);
61$event = \mod_forum\event\subscribers_viewed::create($params);
62$event->trigger();
bffb106d 63
649cf95d 64$forumoutput = $PAGE->get_renderer('mod_forum');
c23c9009
SH
65$currentgroup = groups_get_activity_group($cm);
66$options = array('forumid'=>$forum->id, 'currentgroup'=>$currentgroup, 'context'=>$context);
2789afa8
AN
67$existingselector = new mod_forum_existing_subscriber_selector('existingsubscribers', $options);
68$subscriberselector = new mod_forum_potential_subscriber_selector('potentialsubscribers', $options);
c23c9009
SH
69$subscriberselector->set_existing_subscribers($existingselector->find_users(''));
70
71if (data_submitted()) {
72 require_sesskey();
73 $subscribe = (bool)optional_param('subscribe', false, PARAM_RAW);
74 $unsubscribe = (bool)optional_param('unsubscribe', false, PARAM_RAW);
75 /** It has to be one or the other, not both or neither */
76 if (!($subscribe xor $unsubscribe)) {
77 print_error('invalidaction');
78 }
79 if ($subscribe) {
80 $users = $subscriberselector->get_selected_users();
81 foreach ($users as $user) {
59075a43 82 if (!\mod_forum\subscriptions::subscribe_user($user->id, $forum)) {
c23c9009
SH
83 print_error('cannotaddsubscriber', 'forum', '', $user->id);
84 }
85 }
86 } else if ($unsubscribe) {
87 $users = $existingselector->get_selected_users();
88 foreach ($users as $user) {
59075a43 89 if (!\mod_forum\subscriptions::unsubscribe_user($user->id, $forum)) {
c23c9009
SH
90 print_error('cannotremovesubscriber', 'forum', '', $user->id);
91 }
92 }
93 }
94 $subscriberselector->invalidate_selected_users();
95 $existingselector->invalidate_selected_users();
96 $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
97}
cd4e6b17 98
c23c9009 99$strsubscribers = get_string("subscribers", "forum");
cd4e6b17 100$PAGE->navbar->add($strsubscribers);
101$PAGE->set_title($strsubscribers);
b4c07395 102$PAGE->set_heading($COURSE->fullname);
e143918f 103if (has_capability('mod/forum:managesubscriptions', $context) && \mod_forum\subscriptions::is_forcesubscribed($forum) === false) {
cd4e6b17 104 if ($edit != -1) {
105 $USER->subscriptionsediting = $edit;
bbbf2d40 106 }
29f504de 107 $updatesubscriptionsbutton = forum_update_subscriptions_button($course->id, $id);
cd4e6b17 108} else {
29f504de 109 $updatesubscriptionsbutton = '';
cd4e6b17 110 unset($USER->subscriptionsediting);
111}
112echo $OUTPUT->header();
c23c9009 113echo $OUTPUT->heading(get_string('forum', 'forum').' '.$strsubscribers);
29f504de 114if (!empty($updatesubscriptionsbutton)) {
5db1ce53 115 echo \html_writer::div($updatesubscriptionsbutton, 'float-right');
29f504de 116}
c23c9009 117if (empty($USER->subscriptionsediting)) {
59075a43 118 $subscribers = \mod_forum\subscriptions::fetch_subscribed_users($forum, $currentgroup, $context);
e143918f
HB
119 if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
120 $subscribers = mod_forum_filter_hidden_users($cm, $context, $subscribers);
121 }
59075a43 122 echo $forumoutput->subscriber_overview($subscribers, $forum, $course);
c23c9009
SH
123} else {
124 echo $forumoutput->subscriber_selection_form($existingselector, $subscriberselector);
cd4e6b17 125}
29f504de
NK
126if (!empty($updatesubscriptionsbutton)) {
127 echo $updatesubscriptionsbutton;
128}
2789afa8 129echo $OUTPUT->footer();
e143918f
HB
130
131/**
132 * Filters a list of users for whether they can see a given activity.
133 * If the course module is hidden (closed-eye icon), then only users who have
134 * the permission to view hidden activities will appear in the output list.
135 *
136 * @todo MDL-48625 This filtering should be handled in core libraries instead.
137 *
138 * @param stdClass $cm the course module record of the activity.
139 * @param context_module $context the activity context, to save re-fetching it.
140 * @param array $users the list of users to filter.
141 * @return array the filtered list of users.
142 */
143function mod_forum_filter_hidden_users(stdClass $cm, context_module $context, array $users) {
144 if ($cm->visible) {
145 return $users;
146 } else {
147 // Filter for users that can view hidden activities.
148 $filteredusers = array();
149 $hiddenviewers = get_users_by_capability($context, 'moodle/course:viewhiddenactivities');
150 foreach ($hiddenviewers as $hiddenviewer) {
151 if (array_key_exists($hiddenviewer->id, $users)) {
152 $filteredusers[$hiddenviewer->id] = $users[$hiddenviewer->id];
153 }
154 }
155 return $filteredusers;
156 }
157}