Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 310 and 402] [Versions 311 and 402] [Versions 39 and 402] [Versions 400 and 402]

   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  
  38  if ($edit === 1) {
  39      $url->param('edit', 'on');
  40  } else {
  41      $url->param('edit', 'off');
  42  }
  43  
  44  $PAGE->set_url($url);
  45  
  46  $forum = $DB->get_record('forum', array('id'=>$id), '*', MUST_EXIST);
  47  $course = $DB->get_record('course', array('id'=>$forum->course), '*', MUST_EXIST);
  48  if (! $cm = get_coursemodule_from_instance('forum', $forum->id, $course->id)) {
  49      $cm->id = 0;
  50  }
  51  
  52  require_login($course, false, $cm);
  53  
  54  $context = context_module::instance($cm->id);
  55  if (!has_capability('mod/forum:viewsubscribers', $context)) {
  56      throw new \moodle_exception('nopermissiontosubscribe', 'forum');
  57  }
  58  
  59  unset($SESSION->fromdiscussion);
  60  
  61  $params = array(
  62      'context' => $context,
  63      'other' => array('forumid' => $forum->id),
  64  );
  65  $event = \mod_forum\event\subscribers_viewed::create($params);
  66  $event->trigger();
  67  
  68  $forumoutput = $PAGE->get_renderer('mod_forum');
  69  $currentgroup = groups_get_activity_group($cm);
  70  $options = array('forumid'=>$forum->id, 'currentgroup'=>$currentgroup, 'context'=>$context);
  71  $existingselector = new mod_forum_existing_subscriber_selector('existingsubscribers', $options);
  72  $subscriberselector = new mod_forum_potential_subscriber_selector('potentialsubscribers', $options);
  73  $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
  74  
  75  if (data_submitted()) {
  76      require_sesskey();
  77      $subscribe = (bool)optional_param('subscribe', false, PARAM_RAW);
  78      $unsubscribe = (bool)optional_param('unsubscribe', false, PARAM_RAW);
  79      /** It has to be one or the other, not both or neither */
  80      if (!($subscribe xor $unsubscribe)) {
  81          throw new \moodle_exception('invalidaction');
  82      }
  83      if ($subscribe) {
  84          $users = $subscriberselector->get_selected_users();
  85          foreach ($users as $user) {
  86              if (!\mod_forum\subscriptions::subscribe_user($user->id, $forum)) {
  87                  throw new \moodle_exception('cannotaddsubscriber', 'forum', '', $user->id);
  88              }
  89          }
  90      } else if ($unsubscribe) {
  91          $users = $existingselector->get_selected_users();
  92          foreach ($users as $user) {
  93              if (!\mod_forum\subscriptions::unsubscribe_user($user->id, $forum)) {
  94                  throw new \moodle_exception('cannotremovesubscriber', 'forum', '', $user->id);
  95              }
  96          }
  97      }
  98      $subscriberselector->invalidate_selected_users();
  99      $existingselector->invalidate_selected_users();
 100      $subscriberselector->set_existing_subscribers($existingselector->find_users(''));
 101  }
 102  
 103  $strsubscribers = get_string("subscribers", "forum");
 104  $PAGE->navbar->add($strsubscribers, $url);
 105  $PAGE->set_title($strsubscribers);
 106  $PAGE->set_heading($COURSE->fullname);
 107  
 108  // Activate the secondary nav tab.
 109  $PAGE->set_secondary_active_tab("forumsubscriptions");
 110  
 111  // Output starts from here.
 112  $actionbar = new \mod_forum\output\subscription_actionbar($id, $url, $forum, $edit);
 113  $PAGE->activityheader->disable();
 114  echo $OUTPUT->header();
 115  if (!$PAGE->has_secondary_navigation()) {
 116      echo $OUTPUT->heading(get_string('forum', 'forum') . ' ' . $strsubscribers);
 117  }
 118  echo $forumoutput->subscription_actionbar($actionbar);
 119  
 120  if ($edit === 1 && !\mod_forum\subscriptions::is_forcesubscribed($forum)) {
 121      echo $OUTPUT->heading(get_string('managesubscriptionson', 'forum'), 2);
 122      echo $forumoutput->subscriber_selection_form($existingselector, $subscriberselector);
 123  } else {
 124      $subscribers = \mod_forum\subscriptions::fetch_subscribed_users($forum, $currentgroup, $context);
 125      if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
 126          $subscribers = mod_forum_filter_hidden_users($cm, $context, $subscribers);
 127      }
 128      echo $forumoutput->subscriber_overview($subscribers, $forum, $course);
 129  }
 130  
 131  echo $OUTPUT->footer();
 132  
 133  /**
 134   * Filters a list of users for whether they can see a given activity.
 135   * If the course module is hidden (closed-eye icon), then only users who have
 136   * the permission to view hidden activities will appear in the output list.
 137   *
 138   * @todo MDL-48625 This filtering should be handled in core libraries instead.
 139   *
 140   * @param stdClass $cm the course module record of the activity.
 141   * @param context_module $context the activity context, to save re-fetching it.
 142   * @param array $users the list of users to filter.
 143   * @return array the filtered list of users.
 144   */
 145  function mod_forum_filter_hidden_users(stdClass $cm, context_module $context, array $users) {
 146      if ($cm->visible) {
 147          return $users;
 148      } else {
 149          // Filter for users that can view hidden activities.
 150          $filteredusers = array();
 151          $hiddenviewers = get_users_by_capability($context, 'moodle/course:viewhiddenactivities');
 152          foreach ($hiddenviewers as $hiddenviewer) {
 153              if (array_key_exists($hiddenviewer->id, $users)) {
 154                  $filteredusers[$hiddenviewer->id] = $users[$hiddenviewer->id];
 155              }
 156          }
 157          return $filteredusers;
 158      }
 159  }