weekly release 4.0dev
[moodle.git] / mod / forum / unsubscribeall.php
CommitLineData
cd4e6b17 1<?php
739da48c 2
8f685009
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/**
01030f1b 19 * @package mod_forum
8f685009
SH
20 * @copyright 2008 Petr Skoda (http://skodak.org)
21 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22 */
23
739da48c 24require_once("../../config.php");
25require_once("lib.php");
26
27$confirm = optional_param('confirm', false, PARAM_BOOL);
28
a6855934 29$PAGE->set_url('/mod/forum/unsubscribeall.php');
cd4e6b17 30
088cc24c
AD
31// Do not autologin guest. Only proper users can have forum subscriptions.
32require_login(null, false);
885650c7 33$PAGE->set_context(context_user::instance($USER->id));
739da48c 34
35$return = $CFG->wwwroot.'/';
36
37if (isguestuser()) {
38 redirect($return);
39}
40
41$strunsubscribeall = get_string('unsubscribeall', 'forum');
15ca5e5e 42$PAGE->navbar->add(get_string('modulename', 'forum'));
43$PAGE->navbar->add($strunsubscribeall);
44$PAGE->set_title($strunsubscribeall);
8c6ca696 45$PAGE->set_heading($COURSE->fullname);
15ca5e5e 46echo $OUTPUT->header();
f43f1834 47echo $OUTPUT->heading($strunsubscribeall);
739da48c 48
49if (data_submitted() and $confirm and confirm_sesskey()) {
59075a43 50 $forums = \mod_forum\subscriptions::get_unsubscribable_forums();
74a37e1d
AD
51
52 foreach($forums as $forum) {
e3bbfb52 53 \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, context_module::instance($forum->cm), true);
74a37e1d 54 }
1d8e8494 55 $DB->delete_records('forum_discussion_subs', array('userid' => $USER->id));
739da48c 56 $DB->set_field('user', 'autosubscribe', 0, array('id'=>$USER->id));
74a37e1d 57
698c90d0 58 echo $OUTPUT->box(get_string('unsubscribealldone', 'forum'));
9146b979 59 echo $OUTPUT->continue_button($return);
396fb912 60 echo $OUTPUT->footer();
739da48c 61 die;
702dc57b 62
739da48c 63} else {
1d8e8494
AN
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));
739da48c 67
1d8e8494
AN
68 if ($count->forums || $count->discussions) {
69 if ($count->forums && $count->discussions) {
70 $msg = get_string('unsubscribeallconfirm', 'forum', $count);
0a92904c 71 } else if ($count->forums) {
1d8e8494 72 $msg = get_string('unsubscribeallconfirmforums', 'forum', $count);
0a92904c 73 } else if ($count->discussions) {
1d8e8494
AN
74 $msg = get_string('unsubscribeallconfirmdiscussions', 'forum', $count);
75 }
59e28d8f 76 echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm'=>1)), $return);
396fb912 77 echo $OUTPUT->footer();
739da48c 78 die;
79
80 } else {
698c90d0 81 echo $OUTPUT->box(get_string('unsubscribeallempty', 'forum'));
9146b979 82 echo $OUTPUT->continue_button($return);
396fb912 83 echo $OUTPUT->footer();
739da48c 84 die;
85 }
86}