Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * This file is used to display and organise forum subscribers
  20   *
  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
  24   */
  25  
  26  require_once("../../config.php");
  27  require_once ("lib.php");
  28  
  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
  32  
  33  $url = new moodle_url('/mod/forum/subscribers.php', array('id'=>$id));
  34  if ($group !== 0) {
  35      $url->param('group', $group);
  36  }
  37  if ($edit !== 0) {
  38      $url->param('edit', $edit);
  39  }
  40  $PAGE->set_url($url);
  41  
  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;
  46  }
  47  
  48  require_login($course, false, $cm);
  49  
  50  $context = context_module::instance($cm->id);
  51  if (!has_capability('mod/forum:viewsubscribers', $context)) {
  52      print_error('nopermissiontosubscribe', 'forum');
  53  }
  54  
  55  unset($SESSION->fromdiscussion);
  56  
  57  $params = array(
  58      'context' => $context,
  59      'other' => array('forumid' => $forum->id),
  60  );
  61  $event = \mod_forum\event\subscribers_viewed::create($params);
  62  $event->trigger();
  63  
  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(''));
  70  
  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');
  78      }
  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);
  84              }
  85          }
  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);
  91              }
  92          }
  93      }
  94      $subscriberselector->invalidate_selected_users();
  95      $existingselector->invalidate_selected_users();
  96      $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
  97  }
  98  
  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;
 106      }
 107      $updatesubscriptionsbutton = forum_update_subscriptions_button($course->id, $id);
 108  } else {
 109      $updatesubscriptionsbutton = '';
 110      unset($USER->subscriptionsediting);
 111  }
 112  echo $OUTPUT->header();
 113  echo $OUTPUT->heading(get_string('forum', 'forum').' '.$strsubscribers);
 114  if (!empty($updatesubscriptionsbutton)) {
 115      echo \html_writer::div($updatesubscriptionsbutton, 'float-right');
 116  }
 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);
 121      }
 122      echo $forumoutput->subscriber_overview($subscribers, $forum, $course);
 123  } else {
 124      echo $forumoutput->subscriber_selection_form($existingselector, $subscriberselector);
 125  }
 126  if (!empty($updatesubscriptionsbutton)) {
 127      echo $updatesubscriptionsbutton;
 128  }
 129  echo $OUTPUT->footer();
 130  
 131  /**
 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.
 135   *
 136   * @todo MDL-48625 This filtering should be handled in core libraries instead.
 137   *
 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.
 142   */
 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];
 153              }
 154          }
 155          return $filteredusers;
 156      }
 157  }