Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 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   * Base implementation of a contextlist.
  19   *
  20   * @package    core_privacy
  21   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_privacy\local\request;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Base implementation of a contextlist used to store a set of contexts.
  31   *
  32   * @copyright  2018 Andrew Nicols <andrew@nicols.co.uk>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  abstract class contextlist_base implements
  36          // Implement an Iterator to fetch the Context objects.
  37          \Iterator,
  38  
  39          // Implement the Countable interface to allow the number of returned results to be queried easily.
  40          \Countable {
  41  
  42      /**
  43       * @var array List of context IDs.
  44       *
  45       * Note: this must not be updated using set_contextids only as this
  46       * ensures uniqueness.
  47       */
  48      private $contextids = [];
  49  
  50      /**
  51       * @var string component the frankenstyle component name.
  52       */
  53      protected $component = '';
  54  
  55      /**
  56       * @var int Current position of the iterator.
  57       */
  58      protected $iteratorposition = 0;
  59  
  60      /**
  61       * Set the contextids.
  62       *
  63       * @param   array   $contextids The list of contexts.
  64       */
  65      protected function set_contextids(array $contextids) {
  66          $this->contextids = array_unique($contextids);
  67      }
  68  
  69      /**
  70       * Get the list of context IDs that relate to this request.
  71       *
  72       * @return  int[]
  73       */
  74      public function get_contextids() : array {
  75          return $this->contextids;
  76      }
  77  
  78      /**
  79       * Get the complete list of context objects that relate to this
  80       * request.
  81       *
  82       * @return  \context[]
  83       */
  84      public function get_contexts() : array {
  85          $contexts = [];
  86          foreach ($this->contextids as $contextid) {
  87              // It is possible that this context has been deleted and we now have subsequent calls being made with this
  88              // contextlist. Exceptions here will stop the further processing of this component and that is why we are
  89              // doing a try catch.
  90              try {
  91                  $contexts[] = \context::instance_by_id($contextid);
  92              } catch (\Exception $e) {
  93                  // Remove this context.
  94                  unset($this->contextids[$this->iteratorposition]);
  95              }
  96          }
  97  
  98          return $contexts;
  99      }
 100  
 101      /**
 102       * Sets the component for this contextlist.
 103       *
 104       * @param string $component the frankenstyle component name.
 105       */
 106      protected function set_component($component) {
 107          $this->component = $component;
 108      }
 109  
 110      /**
 111       * Get the name of the component to which this contextlist belongs.
 112       *
 113       * @return string the component name associated with this contextlist.
 114       */
 115      public function get_component() : string {
 116          return $this->component;
 117      }
 118  
 119      /**
 120       * Return the current context.
 121       *
 122       * @return  \context
 123       */
 124      public function current() {
 125          // It is possible that this context has been deleted and we now have subsequent calls being made with this
 126          // contextlist. Exceptions here will stop the further processing of this component and that is why we are
 127          // doing a try catch.
 128          try {
 129              $context = \context::instance_by_id($this->contextids[$this->iteratorposition]);
 130          } catch (\Exception $e) {
 131              // Remove this context.
 132              unset($this->contextids[$this->iteratorposition]);
 133              // Check to see if there are any more contexts left.
 134              if ($this->count()) {
 135                  // Move the pointer to the next record and try again.
 136                  $this->next();
 137                  $context = $this->current();
 138              } else {
 139                  // There are no more context ids left.
 140                  return;
 141              }
 142          }
 143          return $context;
 144      }
 145  
 146      /**
 147       * Return the key of the current element.
 148       *
 149       * @return  mixed
 150       */
 151      public function key() {
 152          return $this->iteratorposition;
 153      }
 154  
 155      /**
 156       * Move to the next context in the list.
 157       */
 158      public function next() {
 159          ++$this->iteratorposition;
 160      }
 161  
 162      /**
 163       * Check if the current position is valid.
 164       *
 165       * @return  bool
 166       */
 167      public function valid() {
 168          return isset($this->contextids[$this->iteratorposition]);
 169      }
 170  
 171      /**
 172       * Rewind to the first found context.
 173       *
 174       * The list of contexts is uniqued during the rewind.
 175       * The rewind is called at the start of most iterations.
 176       */
 177      public function rewind() {
 178          $this->iteratorposition = 0;
 179      }
 180  
 181      /**
 182       * Return the number of contexts.
 183       */
 184      public function count() {
 185          return count($this->contextids);
 186      }
 187  }