Merge branch 'MDL-62899-search-icons-master' of https://github.com/dmitriim/moodle
[moodle.git] / search / classes / skip_future_documents_iterator.php
CommitLineData
2d2fcc1c 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
25namespace core_search;
26
27defined('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 */
42class 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
4b0facc9 49 /** @var mixed Current value of iterator */
50 protected $currentdoc;
51
52 /** @var bool True if current value is available */
53 protected $gotcurrent;
54
2d2fcc1c 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 public function current() {
4b0facc9 71 if (!$this->gotcurrent) {
72 $this->currentdoc = $this->parent->current();
73 $this->gotcurrent = true;
74 }
75 return $this->currentdoc;
2d2fcc1c 76 }
77
78 public function next() {
79 $this->parent->next();
4b0facc9 80 $this->gotcurrent = false;
2d2fcc1c 81 }
82
83 public function key() {
84 return $this->parent->key();
85 }
86
87 public function valid() {
00492448 88 // Check that the parent is valid.
2d2fcc1c 89 if (!$this->parent->valid()) {
90 return false;
91 }
92
4b0facc9 93 if ($doc = $this->current()) {
00492448
AN
94 // This document is valid if the modification date is before the cutoff.
95 return $doc->get('modified') <= $this->cutoff;
4b0facc9 96 } else {
97 // If the document is false/null, allow iterator to continue.
98 return true;
2d2fcc1c 99 }
2d2fcc1c 100 }
101
102 public function rewind() {
103 $this->parent->rewind();
4b0facc9 104 $this->gotcurrent = false;
2d2fcc1c 105 }
106}