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 401] [Versions 311 and 402] [Versions 311 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file defines the userlist_collection class object.
  19   *
  20   * The userlist_collection is used to organize a collection of userlists.
  21   *
  22   * @package core_privacy
  23   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  24   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  namespace core_privacy\local\request;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  /**
  31   * A collection of userlist items.
  32   *
  33   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class userlist_collection implements \Iterator, \Countable {
  37  
  38      /**
  39       * @var \context $context The context that the userlist collection belongs to.
  40       */
  41      protected $context = null;
  42  
  43      /**
  44       * @var array $userlists the internal array of userlist objects.
  45       */
  46      protected $userlists = [];
  47  
  48      /**
  49       * @var int Current position of the iterator.
  50       */
  51      protected $iteratorposition = 0;
  52  
  53      /**
  54       * Constructor to create a new userlist_collection.
  55       *
  56       * @param   \context    $context The context to which this collection belongs.
  57       */
  58      public function __construct(\context $context) {
  59          $this->context = $context;
  60      }
  61  
  62      /**
  63       * Return the context that this collection relates to.
  64       *
  65       * @return  int
  66       */
  67      public function get_context() : \context {
  68          return $this->context;
  69      }
  70  
  71      /**
  72       * Add a userlist to this collection.
  73       *
  74       * @param   userlist_base $userlist the userlist to export.
  75       * @return  $this
  76       */
  77      public function add_userlist(userlist_base $userlist) : userlist_collection {
  78          $component = $userlist->get_component();
  79          if (isset($this->userlists[$component])) {
  80              throw new \moodle_exception("A userlist has already been added for the '{$component}' component");
  81          }
  82  
  83          $this->userlists[$component] = $userlist;
  84  
  85          return $this;
  86      }
  87  
  88      /**
  89       * Get the userlists in this collection.
  90       *
  91       * @return  array   the associative array of userlists in this collection, indexed by component name.
  92       * E.g. mod_assign => userlist, core_comment => userlist.
  93       */
  94      public function get_userlists() : array {
  95          return $this->userlists;
  96      }
  97  
  98      /**
  99       * Get the userlist for the specified component.
 100       *
 101       * @param   string      $component the frankenstyle name of the component to fetch for.
 102       * @return  userlist_base|null
 103       */
 104      public function get_userlist_for_component(string $component) {
 105          if (isset($this->userlists[$component])) {
 106              return $this->userlists[$component];
 107          }
 108  
 109          return null;
 110      }
 111  
 112      /**
 113       * Return the current contexlist.
 114       *
 115       * @return  \user
 116       */
 117      public function current() {
 118          $key = $this->get_key_from_position();
 119          return $this->userlists[$key];
 120      }
 121  
 122      /**
 123       * Return the key of the current element.
 124       *
 125       * @return  mixed
 126       */
 127      public function key() {
 128          return $this->get_key_from_position();
 129      }
 130  
 131      /**
 132       * Move to the next user in the list.
 133       */
 134      public function next() {
 135          ++$this->iteratorposition;
 136      }
 137  
 138      /**
 139       * Check if the current position is valid.
 140       *
 141       * @return  bool
 142       */
 143      public function valid() {
 144          return ($this->iteratorposition < count($this->userlists));
 145      }
 146  
 147      /**
 148       * Rewind to the first found user.
 149       *
 150       * The list of users is uniqued during the rewind.
 151       * The rewind is called at the start of most iterations.
 152       */
 153      public function rewind() {
 154          $this->iteratorposition = 0;
 155      }
 156  
 157      /**
 158       * Get the key for the current iterator position.
 159       *
 160       * @return  string
 161       */
 162      protected function get_key_from_position() {
 163          $keylist = array_keys($this->userlists);
 164          if (isset($keylist[$this->iteratorposition])) {
 165              return $keylist[$this->iteratorposition];
 166          }
 167  
 168          return null;
 169      }
 170  
 171      /**
 172       * Return the number of users.
 173       */
 174      public function count() {
 175          return count($this->userlists);
 176      }
 177  }