/** * Check whether the filter matches the specified tour and/or context. * * @param tour $tour The tour to check * @param context $context The context to check * @return boolean */ public static function filter_matches(tour $tour, context $context) { global $USER; $values = $tour->get_filter_values(self::get_filter_name()); if (empty($values)) { // There are no values configured. // No values means all. return true; } if (is_siteadmin()) { return true; } // Presence within the array is sufficient. Ignore any value. $values = array_flip($values); $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role'); $cachekey = "{$USER->id}_{$context->id}"; $userroles = $cache->get($cachekey); if ($userroles === false) { $userroles = get_user_roles_with_special($context); $cache->set($cachekey, $userroles); } foreach ($userroles as $role) { if (isset($values[$role->roleid])) { return true; } } return false; }
$PAGE->set_heading($course->fullname); } break; case CONTEXT_MODULE: $PAGE->set_heading($context->get_context_name(false)); $PAGE->set_cacheable(false); break; case CONTEXT_BLOCK: $PAGE->set_heading($PAGE->course->fullname); break; } // Get the list of the reported-on user's role assignments - must be after // the page setup code above, or the language might be wrong. $reportuser = $userselector->get_selected_user(); if (!is_null($reportuser)) { $roleassignments = get_user_roles_with_special($context, $reportuser->id); $rolenames = role_get_names($context); } echo $OUTPUT->header(); // Print heading. echo $OUTPUT->heading($title); // If a user has been chosen, show all the permissions for this user. if (!is_null($reportuser)) { echo $OUTPUT->box_start('generalbox boxaligncenter boxwidthwide'); if (!empty($roleassignments)) { echo $OUTPUT->heading(get_string('rolesforuser', 'core_role', fullname($reportuser)), 3); echo html_writer::start_tag('ul'); $systemcontext = context_system::instance(); foreach ($roleassignments as $ra) { $racontext = context::instance_by_id($ra->contextid); $link = html_writer::link($racontext->get_url(), $racontext->get_context_name());
/** * Check whether the filter matches the specified tour and/or context. * * @param tour $tour The tour to check * @param context $context The context to check * @return boolean */ public static function filter_matches(tour $tour, context $context) { global $USER; $values = $tour->get_filter_values(self::get_filter_name()); if (empty($values)) { // There are no values configured. // No values means all. return true; } // Presence within the array is sufficient. Ignore any value. $values = array_flip($values); if (isset($values[self::ROLE_SITEADMIN]) && is_siteadmin()) { // This tour has been restricted to a role including site admin, and this user is a site admin. return true; } // Use a request cache to save on DB queries. // We may be checking multiple tours and they'll all be for the same userid, and contextid $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role'); // Get all of the roles used in this context, including special roles such as user, and frontpageuser. $cachekey = "{$USER->id}_{$context->id}"; $userroles = $cache->get($cachekey); if ($userroles === false) { $userroles = get_user_roles_with_special($context); $cache->set($cachekey, $userroles); } // Some special roles do not include the shortname. // Therefore we must fetch all roles too. Thankfully these don't actually change based on context. // They do require a DB call, so let's cache it. $cachekey = "allroles"; $allroles = $cache->get($cachekey); if ($allroles === false) { $allroles = get_all_roles(); $cache->set($cachekey, $allroles); } // Now we can check whether any of the user roles are in the list of allowed roles for this filter. foreach ($userroles as $role) { $shortname = $allroles[$role->roleid]->shortname; if (isset($values[$shortname])) { return true; } } return false; }