Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  
   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   * @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
  22   */
  23  
  24  require_once("../../config.php");
  25  require_once ("lib.php");
  26  
  27  $confirm = optional_param('confirm', false, PARAM_BOOL);
  28  
  29  $PAGE->set_url('/mod/forum/unsubscribeall.php');
  30  
  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));
  34  
  35  $return = $CFG->wwwroot.'/';
  36  
  37  if (isguestuser()) {
  38      redirect($return);
  39  }
  40  
  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);
  48  
  49  if (data_submitted() and $confirm and confirm_sesskey()) {
  50      $forums = \mod_forum\subscriptions::get_unsubscribable_forums();
  51  
  52      foreach($forums as $forum) {
  53          \mod_forum\subscriptions::unsubscribe_user($USER->id, $forum, context_module::instance($forum->cm), true);
  54      }
  55      $DB->delete_records('forum_discussion_subs', array('userid' => $USER->id));
  56      $DB->set_field('user', 'autosubscribe', 0, array('id'=>$USER->id));
  57  
  58      echo $OUTPUT->box(get_string('unsubscribealldone', 'forum'));
  59      echo $OUTPUT->continue_button($return);
  60      echo $OUTPUT->footer();
  61      die;
  62  
  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));
  67  
  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);
  75          }
  76          echo $OUTPUT->confirm($msg, new moodle_url('unsubscribeall.php', array('confirm'=>1)), $return);
  77          echo $OUTPUT->footer();
  78          die;
  79  
  80      } else {
  81          echo $OUTPUT->box(get_string('unsubscribeallempty', 'forum'));
  82          echo $OUTPUT->continue_button($return);
  83          echo $OUTPUT->footer();
  84          die;
  85      }
  86  }