Beispiel #1
0
 /**
  * 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;
 }
Beispiel #2
0
 /**
  * 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;
 }
Beispiel #3
0
 /**
  * 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 $PAGE;
     $values = $tour->get_filter_values('theme');
     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);
     return isset($values[$PAGE->theme->name]);
 }
Beispiel #4
0
 /**
  * Prepare the filter values for the form.
  *
  * @param   tour            $tour       The tour to prepare values from
  * @param   stdClass        $data       The data value
  * @return  stdClass
  */
 public static function prepare_filter_values_for_form(tour $tour, \stdClass $data)
 {
     $filtername = static::get_filter_name();
     $key = "filter_{$filtername}";
     $values = $tour->get_filter_values($filtername);
     if (empty($values)) {
         $values = static::ANYVALUE;
     }
     $data->{$key} = $values;
     return $data;
 }