2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
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
25 namespace tool_usertours\local\filter;
27 defined('MOODLE_INTERNAL') || die();
29 use tool_usertours\tour;
35 * @copyright 2016 Andrew Nicols <andrew@nicols.co.uk>
36 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 class role extends base {
40 * The name of the filter.
44 public static function get_filter_name() {
49 * Retrieve the list of available filter options.
51 * @return array An array whose keys are the valid options
52 * And whose values are the values to display
54 public static function get_filter_options() {
55 return role_get_names(null, ROLENAME_ALIAS, true);
59 * Check whether the filter matches the specified tour and/or context.
61 * @param tour $tour The tour to check
62 * @param context $context The context to check
65 public static function filter_matches(tour $tour, context $context) {
68 $values = $tour->get_filter_values(self::get_filter_name());
71 // There are no values configured.
72 // No values means all.
80 // Presence within the array is sufficient. Ignore any value.
81 $values = array_flip($values);
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);
91 foreach ($userroles as $role) {
92 if (isset($values[$role->roleid])) {