MDL-62653 behat: Ensure that tasks run properly from behat
[moodle.git] / mod / forum / subscribers.php
blob16e403df628d729a3e57436df660b4ba8957eb55
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 * This file is used to display and organise forum subscribers
21 * @package mod_forum
22 * @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 require_once("../../config.php");
27 require_once("lib.php");
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
33 $url = new moodle_url('/mod/forum/subscribers.php', array('id'=>$id));
34 if ($group !== 0) {
35 $url->param('group', $group);
37 if ($edit !== 0) {
38 $url->param('edit', $edit);
40 $PAGE->set_url($url);
42 $forum = $DB->get_record('forum', array('id'=>$id), '*', MUST_EXIST);
43 $course = $DB->get_record('course', array('id'=>$forum->course), '*', MUST_EXIST);
44 if (! $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
45 $cm->id = 0;
48 require_login($course, false, $cm);
50 $context = context_module::instance($cm->id);
51 if (!has_capability('mod/forum:viewsubscribers', $context)) {
52 print_error('nopermissiontosubscribe', 'forum');
55 unset($SESSION->fromdiscussion);
57 $params = array(
58 'context' => $context,
59 'other' => array('forumid' => $forum->id),
61 $event = \mod_forum\event\subscribers_viewed::create($params);
62 $event->trigger();
64 $forumoutput = $PAGE->get_renderer('mod_forum');
65 $currentgroup = groups_get_activity_group($cm);
66 $options = array('forumid'=>$forum->id, 'currentgroup'=>$currentgroup, 'context'=>$context);
67 $existingselector = new mod_forum_existing_subscriber_selector('existingsubscribers', $options);
68 $subscriberselector = new mod_forum_potential_subscriber_selector('potentialsubscribers', $options);
69 $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
71 if (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');
79 if ($subscribe) {
80 $users = $subscriberselector->get_selected_users();
81 foreach ($users as $user) {
82 if (!\mod_forum\subscriptions::subscribe_user($user->id, $forum)) {
83 print_error('cannotaddsubscriber', 'forum', '', $user->id);
86 } else if ($unsubscribe) {
87 $users = $existingselector->get_selected_users();
88 foreach ($users as $user) {
89 if (!\mod_forum\subscriptions::unsubscribe_user($user->id, $forum)) {
90 print_error('cannotremovesubscriber', 'forum', '', $user->id);
94 $subscriberselector->invalidate_selected_users();
95 $existingselector->invalidate_selected_users();
96 $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
99 $strsubscribers = get_string("subscribers", "forum");
100 $PAGE->navbar->add($strsubscribers);
101 $PAGE->set_title($strsubscribers);
102 $PAGE->set_heading($COURSE->fullname);
103 if (has_capability('mod/forum:managesubscriptions', $context) && \mod_forum\subscriptions::is_forcesubscribed($forum) === false) {
104 if ($edit != -1) {
105 $USER->subscriptionsediting = $edit;
107 $updatesubscriptionsbutton = forum_update_subscriptions_button($course->id, $id);
108 } else {
109 $updatesubscriptionsbutton = '';
110 unset($USER->subscriptionsediting);
112 echo $OUTPUT->header();
113 echo $OUTPUT->heading(get_string('forum', 'forum').' '.$strsubscribers);
114 if (!empty($updatesubscriptionsbutton)) {
115 echo \html_writer::div($updatesubscriptionsbutton, 'pull-right');
117 if (empty($USER->subscriptionsediting)) {
118 $subscribers = \mod_forum\subscriptions::fetch_subscribed_users($forum, $currentgroup, $context);
119 if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
120 $subscribers = mod_forum_filter_hidden_users($cm, $context, $subscribers);
122 echo $forumoutput->subscriber_overview($subscribers, $forum, $course);
123 } else {
124 echo $forumoutput->subscriber_selection_form($existingselector, $subscriberselector);
126 if (!empty($updatesubscriptionsbutton)) {
127 echo $updatesubscriptionsbutton;
129 echo $OUTPUT->footer();
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.
136 * @todo MDL-48625 This filtering should be handled in core libraries instead.
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.
143 function 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];
155 return $filteredusers;