MDL-62065 core_access: First deprecation of get roles on exact context
[moodle.git] / admin / tool / dataprivacy / classes / data_registry.php
CommitLineData
5efc1f9e
DM
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 * Data registry business logic methods. Mostly internal stuff.
19 *
20 * All methods should be considered part of the internal tool_dataprivacy API
21 * unless something different is specified.
22 *
23 * @package tool_dataprivacy
24 * @copyright 2018 David Monllao
25 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 */
27
28namespace tool_dataprivacy;
29
30use coding_exception;
31use tool_dataprivacy\purpose;
32use tool_dataprivacy\category;
33use tool_dataprivacy\contextlevel;
34use tool_dataprivacy\context_instance;
35
36defined('MOODLE_INTERNAL') || die();
37
38require_once($CFG->libdir . '/coursecatlib.php');
39
40/**
41 * Data registry business logic methods. Mostly internal stuff.
42 *
43 * @copyright 2018 David Monllao
44 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 */
46class data_registry {
47
48 /**
49 * @var array Inheritance between context levels.
50 */
51 private static $contextlevelinheritance = [
52 CONTEXT_USER => [CONTEXT_SYSTEM],
53 CONTEXT_COURSECAT => [CONTEXT_SYSTEM],
54 CONTEXT_COURSE => [CONTEXT_COURSECAT, CONTEXT_SYSTEM],
55 CONTEXT_MODULE => [CONTEXT_COURSE, CONTEXT_COURSECAT, CONTEXT_SYSTEM],
56 CONTEXT_BLOCK => [CONTEXT_COURSE, CONTEXT_COURSECAT, CONTEXT_SYSTEM],
57 ];
58
59 /**
60 * Returns purpose and category var names from a context class name
61 *
5efc1f9e
DM
62 * @param string $classname
63 * @return string[]
64 */
65 public static function var_names_from_context($classname) {
66 return [
67 $classname . '_purpose',
68 $classname . '_category',
69 ];
70 }
71
72 /**
73 * Returns the default purpose id and category id for the provided context level.
74 *
75 * The caller code is responsible of checking that $contextlevel is an integer.
76 *
5efc1f9e
DM
77 * @param int $contextlevel
78 * @return int|false[]
79 */
80 public static function get_defaults($contextlevel) {
81
82 $classname = \context_helper::get_class_for_level($contextlevel);
83 list($purposevar, $categoryvar) = self::var_names_from_context($classname);
84
85 $purposeid = get_config('tool_dataprivacy', $purposevar);
86 $categoryid = get_config('tool_dataprivacy', $categoryvar);
87
88 if (empty($purposeid)) {
89 $purposeid = false;
90 }
91 if (empty($categoryid)) {
92 $categoryid = false;
93 }
94
95 return [$purposeid, $categoryid];
96 }
97
98 /**
99 * Are data registry defaults set?
100 *
101 * At least the system defaults need to be set.
102 *
5efc1f9e
DM
103 * @return bool
104 */
105 public static function defaults_set() {
106 list($purposeid, $categoryid) = self::get_defaults(CONTEXT_SYSTEM);
107 if (empty($purposeid) || empty($categoryid)) {
108 return false;
109 }
110 return true;
111 }
112
113 /**
114 * Returns all site categories that are visible to the current user.
115 *
5efc1f9e
DM
116 * @return \coursecat[]
117 */
118 public static function get_site_categories() {
119 global $DB;
120
121 if (method_exists('\coursecat', 'get_all')) {
122 $categories = \coursecat::get_all(['returnhidden' => true]);
123 } else {
124 // Fallback (to be removed once this gets integrated into master).
125 $ids = $DB->get_fieldset_select('course_categories', 'id', '');
126 $categories = \coursecat::get_many($ids);
127 }
128
129 foreach ($categories as $key => $category) {
130 if (!$category->is_uservisible()) {
131 unset($categories[$key]);
132 }
133 }
134 return $categories;
135 }
136
137 /**
138 * Returns the roles assigned to the provided level.
139 *
140 * Important to note that it returns course-level assigned roles
141 * if the provided context level is below course.
142 *
5efc1f9e
DM
143 * @param \context $context
144 * @return array
145 */
146 public static function get_subject_scope(\context $context) {
147
148 if ($contextcourse = $context->get_course_context(false)) {
5a3280a1 149 // Below course level we look at module or block level roles + course-assigned roles.
df536ab1 150 $courseroles = get_roles_used_in_context($contextcourse, false);
151 $roles = $courseroles + get_roles_used_in_context($context, false);
5efc1f9e 152 } else {
5a3280a1
DM
153 // We list category + system for others (we don't work with user instances so no need to work about them).
154 $roles = get_roles_used_in_context($context);
5efc1f9e
DM
155 }
156
157 return array_map(function($role) {
158 if ($role->name) {
159 return $role->name;
160 } else {
161 return $role->shortname;
162 }
163 }, $roles);
164 }
165
166 /**
167 * Returns the effective value given a context instance
168 *
5efc1f9e
DM
169 * @param \context $context
170 * @param string $element 'category' or 'purpose'
171 * @param int|false $forcedvalue Use this value as if this was this context instance value.
172 * @return persistent|false It return a 'purpose' instance or a 'category' instance, depending on $element
173 */
174 public static function get_effective_context_value(\context $context, $element, $forcedvalue=false) {
175
176 if ($element !== 'purpose' && $element !== 'category') {
177 throw new coding_exception('Only \'purpose\' and \'category\' are supported.');
178 }
179 $fieldname = $element . 'id';
180
181 if ($forcedvalue === false) {
182 $instance = context_instance::get_record_by_contextid($context->id, false);
183
184 if (!$instance) {
185 // If the instance does not have a value defaults to not set, so we grab the context level default as its value.
186 $instancevalue = context_instance::NOTSET;
187 } else {
188 $instancevalue = $instance->get($fieldname);
189 }
190 } else {
191 $instancevalue = $forcedvalue;
192 }
193
194 // Not set.
195 if ($instancevalue == context_instance::NOTSET) {
196
197 // The effective value varies depending on the context level.
198 if ($context->contextlevel == CONTEXT_USER) {
199 // Use the context level value as we don't allow people to set specific instances values.
200 return self::get_effective_contextlevel_value($context->contextlevel, $element);
201 } else {
202 // Use the default context level value.
a8a69050
DM
203 list($purposeid, $categoryid) = self::get_effective_default_contextlevel_purpose_and_category(
204 $context->contextlevel
205 );
5efc1f9e
DM
206 return self::get_element_instance($element, $$fieldname);
207 }
208 }
209
210 // Specific value for this context instance.
211 if ($instancevalue != context_instance::INHERIT) {
212 return self::get_element_instance($element, $instancevalue);
213 }
214
215 // This context is using inherited so let's return the parent effective value.
216 $parentcontext = $context->get_parent_context();
217 if (!$parentcontext) {
218 return false;
219 }
220
221 // The forced value should not be transmitted to parent contexts.
222 return self::get_effective_context_value($parentcontext, $element);
223 }
224
225 /**
226 * Returns the effective value for a context level.
227 *
228 * Note that this is different from the effective default context level
229 * (see get_effective_default_contextlevel_purpose_and_category) as this is returning
230 * the value set in the data registry, not in the defaults page.
231 *
232 * @param int $contextlevel
233 * @param string $element 'category' or 'purpose'
234 * @param int $forcedvalue Use this value as if this was this context level purpose.
235 * @return \tool_dataprivacy\purpose|false
236 */
237 public static function get_effective_contextlevel_value($contextlevel, $element, $forcedvalue = false) {
238
239 if ($element !== 'purpose' && $element !== 'category') {
240 throw new coding_exception('Only \'purpose\' and \'category\' are supported.');
241 }
242 $fieldname = $element . 'id';
243
244 if ($contextlevel != CONTEXT_SYSTEM && $contextlevel != CONTEXT_USER) {
245 throw new \coding_exception('Only context_system and context_user values can be retrieved, no other context levels ' .
246 'have a purpose or a category.');
247 }
248
249 if ($forcedvalue === false) {
250 $instance = contextlevel::get_record_by_contextlevel($contextlevel, false);
251 if (!$instance) {
a8a69050
DM
252 // If the context level does not have a value defaults to not set, so we grab the context level default as
253 // its value.
5efc1f9e
DM
254 $instancevalue = context_instance::NOTSET;
255 } else {
256 $instancevalue = $instance->get($fieldname);
257 }
258 } else {
259 $instancevalue = $forcedvalue;
260 }
261
262 // Not set -> Use the default context level value.
263 if ($instancevalue == context_instance::NOTSET) {
264 list($purposeid, $categoryid) = self::get_effective_default_contextlevel_purpose_and_category($contextlevel);
265 return self::get_element_instance($element, $$fieldname);
266 }
267
268 // Specific value for this context instance.
269 if ($instancevalue != context_instance::INHERIT) {
270 return self::get_element_instance($element, $instancevalue);
271 }
272
273 if ($contextlevel == CONTEXT_SYSTEM) {
274 throw new coding_exception('Something went wrong, system defaults should be set and we should already have a value.');
275 }
276
277 // If we reach this point is that we are inheriting so get the parent context level and repeat.
278 $parentcontextlevel = reset(self::$contextlevelinheritance[$contextlevel]);
279
280 // Forced value are intentionally not passed as the force value should only affect the immediate context level.
281 return self::get_effective_contextlevel_value($parentcontextlevel, $element);
282 }
283
284 /**
285 * Returns the effective default purpose and category for a context level.
286 *
287 * @param int $contextlevel
288 * @param int $forcedpurposevalue Use this value as if this was this context level purpose.
289 * @param int $forcedcategoryvalue Use this value as if this was this context level category.
290 * @return int[]
291 */
dd26f1be
JP
292 public static function get_effective_default_contextlevel_purpose_and_category($contextlevel, $forcedpurposevalue = false,
293 $forcedcategoryvalue = false) {
5efc1f9e
DM
294
295 list($purposeid, $categoryid) = self::get_defaults($contextlevel);
296
297 // Honour forced values.
298 if ($forcedpurposevalue) {
299 $purposeid = $forcedpurposevalue;
300 }
301 if ($forcedcategoryvalue) {
302 $categoryid = $forcedcategoryvalue;
303 }
304
305 // Not set == INHERIT for defaults.
306 if ($purposeid == context_instance::INHERIT || $purposeid == context_instance::NOTSET) {
307 $purposeid = false;
308 }
309 if ($categoryid == context_instance::INHERIT || $categoryid == context_instance::NOTSET) {
310 $categoryid = false;
311 }
312
313 if ($contextlevel != CONTEXT_SYSTEM && ($purposeid === false || $categoryid === false)) {
314 foreach (self::$contextlevelinheritance[$contextlevel] as $parent) {
315
316 list($parentpurposeid, $parentcategoryid) = self::get_defaults($parent);
317 // Not set == INHERIT for defaults.
318 if ($parentpurposeid == context_instance::INHERIT || $parentpurposeid == context_instance::NOTSET) {
319 $parentpurposeid = false;
320 }
321 if ($parentcategoryid == context_instance::INHERIT || $parentcategoryid == context_instance::NOTSET) {
322 $parentcategoryid = false;
323 }
324
325 if ($purposeid === false && $parentpurposeid) {
326 $purposeid = $parentpurposeid;
327 }
328
329 if ($categoryid === false && $parentcategoryid) {
330 $categoryid = $parentcategoryid;
331 }
332 }
333 }
334
335 // They may still be false, but we return anyway.
336 return [$purposeid, $categoryid];
337 }
338
339 /**
340 * Returns an instance of the provided element.
341 *
342 * @throws \coding_exception
343 * @param string $element The element name 'purpose' or 'category'
344 * @param int $id The element id
345 * @return \core\persistent
346 */
347 private static function get_element_instance($element, $id) {
348
349 if ($element !== 'purpose' && $element !== 'category') {
350 throw new coding_exception('No other elements than purpose and category are allowed');
351 }
352
353 $classname = '\tool_dataprivacy\\' . $element;
354 return new $classname($id);
355 }
356}