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