Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

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

   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      #[\ReturnTypeWillChange]
 118      public function current() {
 119          $key = $this->get_key_from_position();
 120          return $this->userlists[$key];
 121      }
 122  
 123      /**
 124       * Return the key of the current element.
 125       *
 126       * @return  mixed
 127       */
 128      #[\ReturnTypeWillChange]
 129      public function key() {
 130          return $this->get_key_from_position();
 131      }
 132  
 133      /**
 134       * Move to the next user in the list.
 135       */
 136      public function next(): void {
 137          ++$this->iteratorposition;
 138      }
 139  
 140      /**
 141       * Check if the current position is valid.
 142       *
 143       * @return  bool
 144       */
 145      public function valid(): bool {
 146          return ($this->iteratorposition < count($this->userlists));
 147      }
 148  
 149      /**
 150       * Rewind to the first found user.
 151       *
 152       * The list of users is uniqued during the rewind.
 153       * The rewind is called at the start of most iterations.
 154       */
 155      public function rewind(): void {
 156          $this->iteratorposition = 0;
 157      }
 158  
 159      /**
 160       * Get the key for the current iterator position.
 161       *
 162       * @return  string
 163       */
 164      protected function get_key_from_position() {
 165          $keylist = array_keys($this->userlists);
 166          if (isset($keylist[$this->iteratorposition])) {
 167              return $keylist[$this->iteratorposition];
 168          }
 169  
 170          return null;
 171      }
 172  
 173      /**
 174       * Return the number of users.
 175       */
 176      public function count(): int {
 177          return count($this->userlists);
 178      }
 179  }