[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/mod/forum/ -> subscribers.php (source)

   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      $PAGE->set_button(forum_update_subscriptions_button($course->id, $id));
 108  } else {
 109      unset($USER->subscriptionsediting);
 110  }
 111  echo $OUTPUT->header();
 112  echo $OUTPUT->heading(get_string('forum', 'forum').' '.$strsubscribers);
 113  if (empty($USER->subscriptionsediting)) {
 114      $subscribers = \mod_forum\subscriptions::fetch_subscribed_users($forum, $currentgroup, $context);
 115      if (\mod_forum\subscriptions::is_forcesubscribed($forum)) {
 116          $subscribers = mod_forum_filter_hidden_users($cm, $context, $subscribers);
 117      }
 118      echo $forumoutput->subscriber_overview($subscribers, $forum, $course);
 119  } else {
 120      echo $forumoutput->subscriber_selection_form($existingselector, $subscriberselector);
 121  }
 122  echo $OUTPUT->footer();
 123  
 124  /**
 125   * Filters a list of users for whether they can see a given activity.
 126   * If the course module is hidden (closed-eye icon), then only users who have
 127   * the permission to view hidden activities will appear in the output list.
 128   *
 129   * @todo MDL-48625 This filtering should be handled in core libraries instead.
 130   *
 131   * @param stdClass $cm the course module record of the activity.
 132   * @param context_module $context the activity context, to save re-fetching it.
 133   * @param array $users the list of users to filter.
 134   * @return array the filtered list of users.
 135   */
 136  function mod_forum_filter_hidden_users(stdClass $cm, context_module $context, array $users) {
 137      if ($cm->visible) {
 138          return $users;
 139      } else {
 140          // Filter for users that can view hidden activities.
 141          $filteredusers = array();
 142          $hiddenviewers = get_users_by_capability($context, 'moodle/course:viewhiddenactivities');
 143          foreach ($hiddenviewers as $hiddenviewer) {
 144              if (array_key_exists($hiddenviewer->id, $users)) {
 145                  $filteredusers[$hiddenviewer->id] = $users[$hiddenviewer->id];
 146              }
 147          }
 148          return $filteredusers;
 149      }
 150  }


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1