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 contextlist_collection class object.
  19   *
  20   * The contextlist_collection is used to organize a collection of contextlists.
  21   *
  22   * @package core_privacy
  23   * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
  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 contextlist items.
  32   *
  33   * @copyright 2018 Jake Dallimore <jrhdallimore@gmail.com>
  34   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class contextlist_collection implements \Iterator, \Countable {
  37  
  38      /**
  39       * @var int $userid The ID of the user that the contextlist collection belongs to.
  40       */
  41      protected $userid = null;
  42  
  43      /**
  44       * @var array $contextlists the internal array of contextlist objects.
  45       */
  46      protected $contextlists = [];
  47  
  48      /**
  49       * @var int Current position of the iterator.
  50       */
  51      protected $iteratorposition = 0;
  52  
  53      /**
  54       * Constructor to create a new contextlist_collection.
  55       *
  56       * @param   int     $userid The userid to which this collection belongs.
  57       */
  58      public function __construct($userid) {
  59          $this->userid = $userid;
  60      }
  61  
  62      /**
  63       * Return the ID of the user whose collection this is.
  64       *
  65       * @return  int
  66       */
  67      public function get_userid() : int {
  68          return $this->userid;
  69      }
  70  
  71      /**
  72       * Add a contextlist to this collection.
  73       *
  74       * @param   contextlist_base $contextlist the contextlist to export.
  75       * @return  $this
  76       */
  77      public function add_contextlist(contextlist_base $contextlist) {
  78          $component = $contextlist->get_component();
  79          if (empty($component)) {
  80              throw new \moodle_exception("The contextlist must have a component set");
  81          }
  82          if (isset($this->contextlists[$component])) {
  83              throw new \moodle_exception("A contextlist has already been added for the '{$component}' component");
  84          }
  85  
  86          $this->contextlists[$component] = $contextlist;
  87  
  88          return $this;
  89      }
  90  
  91      /**
  92       * Get the contextlists in this collection.
  93       *
  94       * @return  array   the associative array of contextlists in this collection, indexed by component name.
  95       * E.g. mod_assign => contextlist, core_comment => contextlist.
  96       */
  97      public function get_contextlists() : array {
  98          return $this->contextlists;
  99      }
 100  
 101      /**
 102       * Get the contextlist for the specified component.
 103       *
 104       * @param   string      $component the frankenstyle name of the component to fetch for.
 105       * @return  contextlist_base|null
 106       */
 107      public function get_contextlist_for_component(string $component) {
 108          if (isset($this->contextlists[$component])) {
 109              return $this->contextlists[$component];
 110          }
 111  
 112          return null;
 113      }
 114  
 115      /**
 116       * Return the current contexlist.
 117       *
 118       * @return  \context
 119       */
 120      public function current() {
 121          $key = $this->get_key_from_position();
 122          return $this->contextlists[$key];
 123      }
 124  
 125      /**
 126       * Return the key of the current element.
 127       *
 128       * @return  mixed
 129       */
 130      public function key() {
 131          return $this->get_key_from_position();
 132      }
 133  
 134      /**
 135       * Move to the next context in the list.
 136       */
 137      public function next() {
 138          ++$this->iteratorposition;
 139      }
 140  
 141      /**
 142       * Check if the current position is valid.
 143       *
 144       * @return  bool
 145       */
 146      public function valid() {
 147          return ($this->iteratorposition < count($this->contextlists));
 148      }
 149  
 150      /**
 151       * Rewind to the first found context.
 152       *
 153       * The list of contexts is uniqued during the rewind.
 154       * The rewind is called at the start of most iterations.
 155       */
 156      public function rewind() {
 157          $this->iteratorposition = 0;
 158      }
 159  
 160      /**
 161       * Get the key for the current iterator position.
 162       *
 163       * @return  string
 164       */
 165      protected function get_key_from_position() {
 166          $keylist = array_keys($this->contextlists);
 167          if (isset($keylist[$this->iteratorposition])) {
 168              return $keylist[$this->iteratorposition];
 169          }
 170  
 171          return null;
 172      }
 173  
 174      /**
 175       * Return the number of contexts.
 176       */
 177      public function count() {
 178          return count($this->contextlists);
 179      }
 180  }