MDL-30975 Docs Documenting report/livelogs
[moodle.git] / mod / forum / subscribers.php
blob086ed3afebb0630bdfca505fdcd2883c423f6954
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->id, false, $cm);
50 $context = get_context_instance(CONTEXT_MODULE, $cm->id);
51 if (!has_capability('mod/forum:viewsubscribers', $context)) {
52 print_error('nopermissiontosubscribe', 'forum');
55 unset($SESSION->fromdiscussion);
57 add_to_log($course->id, "forum", "view subscribers", "subscribers.php?id=$forum->id", $forum->id, $cm->id);
59 $forumoutput = $PAGE->get_renderer('mod_forum');
60 $currentgroup = groups_get_activity_group($cm);
61 $options = array('forumid'=>$forum->id, 'currentgroup'=>$currentgroup, 'context'=>$context);
62 $existingselector = new forum_existing_subscriber_selector('existingsubscribers', $options);
63 $subscriberselector = new forum_potential_subscriber_selector('potentialsubscribers', $options);
64 $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
66 if (data_submitted()) {
67 require_sesskey();
68 $subscribe = (bool)optional_param('subscribe', false, PARAM_RAW);
69 $unsubscribe = (bool)optional_param('unsubscribe', false, PARAM_RAW);
70 /** It has to be one or the other, not both or neither */
71 if (!($subscribe xor $unsubscribe)) {
72 print_error('invalidaction');
74 if ($subscribe) {
75 $users = $subscriberselector->get_selected_users();
76 foreach ($users as $user) {
77 if (!forum_subscribe($user->id, $id)) {
78 print_error('cannotaddsubscriber', 'forum', '', $user->id);
81 } else if ($unsubscribe) {
82 $users = $existingselector->get_selected_users();
83 foreach ($users as $user) {
84 if (!forum_unsubscribe($user->id, $id)) {
85 print_error('cannotremovesubscriber', 'forum', '', $user->id);
89 $subscriberselector->invalidate_selected_users();
90 $existingselector->invalidate_selected_users();
91 $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
94 $strsubscribers = get_string("subscribers", "forum");
95 $PAGE->navbar->add($strsubscribers);
96 $PAGE->set_title($strsubscribers);
97 $PAGE->set_heading($COURSE->fullname);
98 if (has_capability('mod/forum:managesubscriptions', $context)) {
99 $PAGE->set_button(forum_update_subscriptions_button($course->id, $id));
100 if ($edit != -1) {
101 $USER->subscriptionsediting = $edit;
103 } else {
104 unset($USER->subscriptionsediting);
106 echo $OUTPUT->header();
107 echo $OUTPUT->heading(get_string('forum', 'forum').' '.$strsubscribers);
108 if (empty($USER->subscriptionsediting)) {
109 echo $forumoutput->subscriber_overview(forum_subscribed_users($course, $forum, $currentgroup, $context), $forum, $course);
110 } else if (forum_is_forcesubscribed($forum)) {
111 $subscriberselector->set_force_subscribed(true);
112 echo $forumoutput->subscribed_users($subscriberselector);
113 } else {
114 echo $forumoutput->subscriber_selection_form($existingselector, $subscriberselector);
116 echo $OUTPUT->footer();