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   * Iterator for skipping search recordset documents that are in the future.
  19   *
  20   * @package core_search
  21   * @copyright 2017 The Open University
  22   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_search;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Iterator for skipping search recordset documents that are in the future.
  31   *
  32   * This iterator stops iterating if it receives a document that was modified later than the
  33   * specified cut-off time (usually current time).
  34   *
  35   * This iterator assumes that its parent iterator returns documents in modified order (which is
  36   * required to be the case for search indexing). This means we will stop retrieving data from the
  37   * recordset
  38   *
  39   * @copyright 2017 The Open University
  40   * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  41   */
  42  class skip_future_documents_iterator implements \Iterator {
  43      /** @var \Iterator Parent iterator */
  44      protected $parent;
  45  
  46      /** @var int Cutoff time; anything later than this will cause the iterator to stop */
  47      protected $cutoff;
  48  
  49      /** @var mixed Current value of iterator */
  50      protected $currentdoc;
  51  
  52      /** @var bool True if current value is available */
  53      protected $gotcurrent;
  54  
  55      /**
  56       * Constructor.
  57       *
  58       * @param \Iterator $parent Parent iterator, must return search documents in modified order
  59       * @param int $cutoff Cut-off time, default is current time
  60       */
  61      public function __construct(\Iterator $parent, $cutoff = 0) {
  62          $this->parent = $parent;
  63          if ($cutoff) {
  64              $this->cutoff = $cutoff;
  65          } else {
  66              $this->cutoff = time();
  67          }
  68      }
  69  
  70      #[\ReturnTypeWillChange]
  71      public function current() {
  72          if (!$this->gotcurrent) {
  73              $this->currentdoc = $this->parent->current();
  74              $this->gotcurrent = true;
  75          }
  76          return $this->currentdoc;
  77      }
  78  
  79      public function next(): void {
  80          $this->parent->next();
  81          $this->gotcurrent = false;
  82      }
  83  
  84      #[\ReturnTypeWillChange]
  85      public function key() {
  86          return $this->parent->key();
  87      }
  88  
  89      public function valid(): bool {
  90          // Check that the parent is valid.
  91          if (!$this->parent->valid()) {
  92              return false;
  93          }
  94  
  95          if ($doc = $this->current()) {
  96              // This document is valid if the modification date is before the cutoff.
  97              return $doc->get('modified') <= $this->cutoff;
  98          } else {
  99              // If the document is false/null, allow iterator to continue.
 100              return true;
 101          }
 102      }
 103  
 104      public function rewind(): void {
 105          $this->parent->rewind();
 106          $this->gotcurrent = false;
 107      }
 108  }