Commit | Line | Data |
---|---|---|
001fc061 AN |
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 | * Theme filter. | |
19 | * | |
20 | * @package tool_usertours | |
21 | * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk> | |
22 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
23 | */ | |
24 | ||
25 | namespace tool_usertours\local\filter; | |
26 | ||
27 | defined('MOODLE_INTERNAL') || die(); | |
28 | ||
29 | use tool_usertours\tour; | |
30 | use context; | |
31 | ||
32 | /** | |
33 | * Theme filter. | |
34 | * | |
35 | * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk> | |
36 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
37 | */ | |
38 | class role extends base { | |
39 | /** | |
40 | * The name of the filter. | |
41 | * | |
42 | * @return string | |
43 | */ | |
44 | public static function get_filter_name() { | |
45 | return 'role'; | |
46 | } | |
47 | ||
48 | /** | |
49 | * Retrieve the list of available filter options. | |
50 | * | |
51 | * @return array An array whose keys are the valid options | |
52 | * And whose values are the values to display | |
53 | */ | |
54 | public static function get_filter_options() { | |
55 | return role_get_names(null, ROLENAME_ALIAS, true); | |
56 | } | |
57 | ||
58 | /** | |
59 | * Check whether the filter matches the specified tour and/or context. | |
60 | * | |
61 | * @param tour $tour The tour to check | |
62 | * @param context $context The context to check | |
63 | * @return boolean | |
64 | */ | |
65 | public static function filter_matches(tour $tour, context $context) { | |
66 | global $USER; | |
67 | ||
68 | $values = $tour->get_filter_values(self::get_filter_name()); | |
69 | ||
70 | if (empty($values)) { | |
71 | // There are no values configured. | |
72 | // No values means all. | |
73 | return true; | |
74 | } | |
75 | ||
76 | if (is_siteadmin()) { | |
77 | return true; | |
78 | } | |
79 | ||
80 | // Presence within the array is sufficient. Ignore any value. | |
81 | $values = array_flip($values); | |
82 | ||
83 | $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role'); | |
84 | $cachekey = "{$USER->id}_{$context->id}"; | |
85 | $userroles = $cache->get($cachekey); | |
86 | if ($userroles === false) { | |
87 | $userroles = get_user_roles_with_special($context); | |
88 | $cache->set($cachekey, $userroles); | |
89 | } | |
90 | ||
91 | foreach ($userroles as $role) { | |
92 | if (isset($values[$role->roleid])) { | |
93 | return true; | |
94 | } | |
95 | } | |
96 | ||
97 | return false; | |
98 | } | |
99 | } |