MDL-63689 workshop: Add support for removal of multiple context users
[moodle.git] / mod / forum / unsubscribeall.php
blob0130c0b67977a671cbe1bccfc375f12392d99331
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 * @package mod_forum
20 * @copyright 2008 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 require_once("../../config.php");
25 require_once("lib.php");
27 $confirm = optional_param('confirm', false, PARAM_BOOL);
29 $PAGE->set_url('/mod/forum/unsubscribeall.php');
31 // Do not autologin guest. Only proper users can have forum subscriptions.
32 require_login(null, false);
33 $PAGE->set_context(context_user::instance($USER->id));
35 $return = $CFG->wwwroot.'/';
37 if (isguestuser()) {
38 redirect($return);
41 $strunsubscribeall = get_string('unsubscribeall', 'forum');
42 $PAGE->navbar->add(get_string('modulename', 'forum'));
43 $PAGE->navbar->add($strunsubscribeall);
44 $PAGE->set_title($strunsubscribeall);
45 $PAGE->set_heading($COURSE->fullname);
46 echo $OUTPUT->header();
47 echo $OUTPUT->heading($strunsubscribeall);
49 if (data_submitted() and $confirm and confirm_sesskey()) {
50 $forums = \mod_forum\subscriptions::get_unsubscribable_forums();
52 foreach($forums as $forum) {
53 \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, context_module::instance($forum->cm), true);
55 $DB->delete_records('forum_discussion_subs', array('userid' => $USER->id));
56 $DB->set_field('user', 'autosubscribe', 0, array('id'=>$USER->id));
58 echo $OUTPUT->box(get_string('unsubscribealldone', 'forum'));
59 echo $OUTPUT->continue_button($return);
60 echo $OUTPUT->footer();
61 die;
63 } else {
64 $count = new stdClass();
65 $count->forums = count(\mod_forum\subscriptions::get_unsubscribable_forums());
66 $count->discussions = $DB->count_records('forum_discussion_subs', array('userid' => $USER->id));
68 if ($count->forums || $count->discussions) {
69 if ($count->forums && $count->discussions) {
70 $msg = get_string('unsubscribeallconfirm', 'forum', $count);
71 } else if ($count->forums) {
72 $msg = get_string('unsubscribeallconfirmforums', 'forum', $count);
73 } else if ($count->discussions) {
74 $msg = get_string('unsubscribeallconfirmdiscussions', 'forum', $count);
76 echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm'=>1)), $return);
77 echo $OUTPUT->footer();
78 die;
80 } else {
81 echo $OUTPUT->box(get_string('unsubscribeallempty', 'forum'));
82 echo $OUTPUT->continue_button($return);
83 echo $OUTPUT->footer();
84 die;