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 { | |
6511cf77 AN |
39 | /** |
40 | * The Site Admin pseudo-role. | |
41 | * | |
42 | * @var ROLE_SITEADMIN int | |
43 | */ | |
44 | const ROLE_SITEADMIN = -1; | |
45 | ||
001fc061 AN |
46 | /** |
47 | * The name of the filter. | |
48 | * | |
49 | * @return string | |
50 | */ | |
51 | public static function get_filter_name() { | |
52 | return 'role'; | |
53 | } | |
54 | ||
55 | /** | |
56 | * Retrieve the list of available filter options. | |
57 | * | |
58 | * @return array An array whose keys are the valid options | |
59 | * And whose values are the values to display | |
60 | */ | |
61 | public static function get_filter_options() { | |
6511cf77 AN |
62 | $allroles = role_get_names(null, ROLENAME_ALIAS); |
63 | ||
64 | $roles = []; | |
65 | foreach ($allroles as $role) { | |
66 | if ($role->archetype === 'guest') { | |
67 | // No point in including the 'guest' role as it isn't possible to show tours to a guest. | |
68 | continue; | |
69 | } | |
70 | $roles[$role->shortname] = $role->localname; | |
71 | } | |
72 | ||
73 | // Add the Site Administrator pseudo-role. | |
74 | $roles[self::ROLE_SITEADMIN] = get_string('administrator', 'core'); | |
75 | ||
76 | // Sort alphabetically too. | |
77 | \core_collator::asort($roles); | |
78 | ||
79 | return $roles; | |
001fc061 AN |
80 | } |
81 | ||
82 | /** | |
83 | * Check whether the filter matches the specified tour and/or context. | |
84 | * | |
85 | * @param tour $tour The tour to check | |
86 | * @param context $context The context to check | |
87 | * @return boolean | |
88 | */ | |
89 | public static function filter_matches(tour $tour, context $context) { | |
90 | global $USER; | |
91 | ||
92 | $values = $tour->get_filter_values(self::get_filter_name()); | |
93 | ||
94 | if (empty($values)) { | |
95 | // There are no values configured. | |
96 | // No values means all. | |
97 | return true; | |
98 | } | |
99 | ||
001fc061 AN |
100 | // Presence within the array is sufficient. Ignore any value. |
101 | $values = array_flip($values); | |
102 | ||
6511cf77 AN |
103 | if (isset($values[self::ROLE_SITEADMIN]) && is_siteadmin()) { |
104 | // This tour has been restricted to a role including site admin, and this user is a site admin. | |
105 | return true; | |
106 | } | |
107 | ||
001fc061 AN |
108 | $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role'); |
109 | $cachekey = "{$USER->id}_{$context->id}"; | |
110 | $userroles = $cache->get($cachekey); | |
111 | if ($userroles === false) { | |
112 | $userroles = get_user_roles_with_special($context); | |
113 | $cache->set($cachekey, $userroles); | |
114 | } | |
115 | ||
116 | foreach ($userroles as $role) { | |
117 | if (isset($values[$role->roleid])) { | |
118 | return true; | |
119 | } | |
120 | } | |
121 | ||
122 | return false; | |
123 | } | |
124 | } |