| 1 | 
           efrain | 
           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 | 
           namespace tool_usertours\local\filter;
  | 
        
        
            | 
            | 
           18 | 
              | 
        
        
            | 
            | 
           19 | 
           use tool_usertours\tour;
  | 
        
        
            | 
            | 
           20 | 
           use context;
  | 
        
        
            | 
            | 
           21 | 
              | 
        
        
            | 
            | 
           22 | 
           /**
  | 
        
        
            | 
            | 
           23 | 
            * Theme filter.
  | 
        
        
            | 
            | 
           24 | 
            *
  | 
        
        
            | 
            | 
           25 | 
            * @package    tool_usertours
  | 
        
        
            | 
            | 
           26 | 
            * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  | 
        
        
            | 
            | 
           27 | 
            * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  | 
        
        
            | 
            | 
           28 | 
            */
  | 
        
        
            | 
            | 
           29 | 
           class role extends base {
  | 
        
        
            | 
            | 
           30 | 
               /**
  | 
        
        
            | 
            | 
           31 | 
                * The Site Admin pseudo-role.
  | 
        
        
            | 
            | 
           32 | 
                *
  | 
        
        
            | 
            | 
           33 | 
                * @var ROLE_SITEADMIN int
  | 
        
        
            | 
            | 
           34 | 
                */
  | 
        
        
            | 
            | 
           35 | 
               const ROLE_SITEADMIN = -1;
  | 
        
        
            | 
            | 
           36 | 
              | 
        
        
            | 
            | 
           37 | 
               /**
  | 
        
        
            | 
            | 
           38 | 
                * The name of the filter.
  | 
        
        
            | 
            | 
           39 | 
                *
  | 
        
        
            | 
            | 
           40 | 
                * @return  string
  | 
        
        
            | 
            | 
           41 | 
                */
  | 
        
        
            | 
            | 
           42 | 
               public static function get_filter_name() {
  | 
        
        
            | 
            | 
           43 | 
                   return 'role';
  | 
        
        
            | 
            | 
           44 | 
               }
  | 
        
        
            | 
            | 
           45 | 
              | 
        
        
            | 
            | 
           46 | 
               /**
  | 
        
        
            | 
            | 
           47 | 
                * Retrieve the list of available filter options.
  | 
        
        
            | 
            | 
           48 | 
                *
  | 
        
        
            | 
            | 
           49 | 
                * @return  array                   An array whose keys are the valid options
  | 
        
        
            | 
            | 
           50 | 
                *                                  And whose values are the values to display
  | 
        
        
            | 
            | 
           51 | 
                */
  | 
        
        
            | 
            | 
           52 | 
               public static function get_filter_options() {
  | 
        
        
            | 
            | 
           53 | 
                   $allroles = role_get_names(null, ROLENAME_ALIAS);
  | 
        
        
            | 
            | 
           54 | 
              | 
        
        
            | 
            | 
           55 | 
                   $roles = [];
  | 
        
        
            | 
            | 
           56 | 
                   foreach ($allroles as $role) {
  | 
        
        
            | 
            | 
           57 | 
                       if ($role->archetype === 'guest') {
  | 
        
        
            | 
            | 
           58 | 
                           // No point in including the 'guest' role as it isn't possible to show tours to a guest.
  | 
        
        
            | 
            | 
           59 | 
                           continue;
  | 
        
        
            | 
            | 
           60 | 
                       }
  | 
        
        
            | 
            | 
           61 | 
                       $roles[$role->shortname] = $role->localname;
  | 
        
        
            | 
            | 
           62 | 
                   }
  | 
        
        
            | 
            | 
           63 | 
              | 
        
        
            | 
            | 
           64 | 
                   // Add the Site Administrator pseudo-role.
  | 
        
        
            | 
            | 
           65 | 
                   $roles[self::ROLE_SITEADMIN] = get_string('administrator', 'core');
  | 
        
        
            | 
            | 
           66 | 
              | 
        
        
            | 
            | 
           67 | 
                   // Sort alphabetically too.
  | 
        
        
            | 
            | 
           68 | 
                   \core_collator::asort($roles);
  | 
        
        
            | 
            | 
           69 | 
              | 
        
        
            | 
            | 
           70 | 
                   return $roles;
  | 
        
        
            | 
            | 
           71 | 
               }
  | 
        
        
            | 
            | 
           72 | 
              | 
        
        
            | 
            | 
           73 | 
               /**
  | 
        
        
            | 
            | 
           74 | 
                * Check whether the filter matches the specified tour and/or context.
  | 
        
        
            | 
            | 
           75 | 
                *
  | 
        
        
            | 
            | 
           76 | 
                * @param   tour        $tour       The tour to check
  | 
        
        
            | 
            | 
           77 | 
                * @param   context     $context    The context to check
  | 
        
        
            | 
            | 
           78 | 
                * @return  boolean
  | 
        
        
            | 
            | 
           79 | 
                */
  | 
        
        
            | 
            | 
           80 | 
               public static function filter_matches(tour $tour, context $context) {
  | 
        
        
            | 
            | 
           81 | 
                   global $USER;
  | 
        
        
            | 
            | 
           82 | 
              | 
        
        
            | 
            | 
           83 | 
                   $values = $tour->get_filter_values(self::get_filter_name());
  | 
        
        
            | 
            | 
           84 | 
              | 
        
        
            | 
            | 
           85 | 
                   if (empty($values)) {
  | 
        
        
            | 
            | 
           86 | 
                       // There are no values configured.
  | 
        
        
            | 
            | 
           87 | 
                       // No values means all.
  | 
        
        
            | 
            | 
           88 | 
                       return true;
  | 
        
        
            | 
            | 
           89 | 
                   }
  | 
        
        
            | 
            | 
           90 | 
              | 
        
        
            | 
            | 
           91 | 
                   // Presence within the array is sufficient. Ignore any value.
  | 
        
        
            | 
            | 
           92 | 
                   $values = array_flip($values);
  | 
        
        
            | 
            | 
           93 | 
              | 
        
        
            | 
            | 
           94 | 
                   if (isset($values[self::ROLE_SITEADMIN]) && is_siteadmin()) {
  | 
        
        
            | 
            | 
           95 | 
                       // This tour has been restricted to a role including site admin, and this user is a site admin.
  | 
        
        
            | 
            | 
           96 | 
                       return true;
  | 
        
        
            | 
            | 
           97 | 
                   }
  | 
        
        
            | 
            | 
           98 | 
              | 
        
        
            | 
            | 
           99 | 
                   // Use a request cache to save on DB queries.
  | 
        
        
            | 
            | 
           100 | 
                   // We may be checking multiple tours and they'll all be for the same userid, and contextid.
  | 
        
        
            | 
            | 
           101 | 
                   $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role');
  | 
        
        
            | 
            | 
           102 | 
              | 
        
        
            | 
            | 
           103 | 
                   // Get all of the roles used in this context, including special roles such as user, and frontpageuser.
  | 
        
        
            | 
            | 
           104 | 
                   $cachekey = "{$USER->id}_{$context->id}";
  | 
        
        
            | 
            | 
           105 | 
                   $userroles = $cache->get($cachekey);
  | 
        
        
            | 
            | 
           106 | 
                   if ($userroles === false) {
  | 
        
        
            | 
            | 
           107 | 
                       $userroles = get_user_roles_with_special($context);
  | 
        
        
            | 
            | 
           108 | 
                       $cache->set($cachekey, $userroles);
  | 
        
        
            | 
            | 
           109 | 
                   }
  | 
        
        
            | 
            | 
           110 | 
              | 
        
        
            | 
            | 
           111 | 
                   // Some special roles do not include the shortname.
  | 
        
        
            | 
            | 
           112 | 
                   // Therefore we must fetch all roles too. Thankfully these don't actually change based on context.
  | 
        
        
            | 
            | 
           113 | 
                   // They do require a DB call, so let's cache it.
  | 
        
        
            | 
            | 
           114 | 
                   $cachekey = "allroles";
  | 
        
        
            | 
            | 
           115 | 
                   $allroles = $cache->get($cachekey);
  | 
        
        
            | 
            | 
           116 | 
                   if ($allroles === false) {
  | 
        
        
            | 
            | 
           117 | 
                       $allroles = get_all_roles();
  | 
        
        
            | 
            | 
           118 | 
                       $cache->set($cachekey, $allroles);
  | 
        
        
            | 
            | 
           119 | 
                   }
  | 
        
        
            | 
            | 
           120 | 
              | 
        
        
            | 
            | 
           121 | 
                   // Now we can check whether any of the user roles are in the list of allowed roles for this filter.
  | 
        
        
            | 
            | 
           122 | 
                   foreach ($userroles as $role) {
  | 
        
        
            | 
            | 
           123 | 
                       $shortname = $allroles[$role->roleid]->shortname;
  | 
        
        
            | 
            | 
           124 | 
                       if (isset($values[$shortname])) {
  | 
        
        
            | 
            | 
           125 | 
                           return true;
  | 
        
        
            | 
            | 
           126 | 
                       }
  | 
        
        
            | 
            | 
           127 | 
                   }
  | 
        
        
            | 
            | 
           128 | 
              | 
        
        
            | 
            | 
           129 | 
                   return false;
  | 
        
        
            | 
            | 
           130 | 
               }
  | 
        
        
            | 
            | 
           131 | 
           }
  |