Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 {
  39      /**
  40       * The Site Admin pseudo-role.
  41       *
  42       * @var ROLE_SITEADMIN int
  43       */
  44      const ROLE_SITEADMIN = -1;
  45  
  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() {
  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;
  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  
 100          // Presence within the array is sufficient. Ignore any value.
 101          $values = array_flip($values);
 102  
 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  
 108          // Use a request cache to save on DB queries.
 109          // We may be checking multiple tours and they'll all be for the same userid, and contextid
 110          $cache = \cache::make_from_params(\cache_store::MODE_REQUEST, 'tool_usertours', 'filter_role');
 111  
 112          // Get all of the roles used in this context, including special roles such as user, and frontpageuser.
 113          $cachekey = "{$USER->id}_{$context->id}";
 114          $userroles = $cache->get($cachekey);
 115          if ($userroles === false) {
 116              $userroles = get_user_roles_with_special($context);
 117              $cache->set($cachekey, $userroles);
 118          }
 119  
 120          // Some special roles do not include the shortname.
 121          // Therefore we must fetch all roles too. Thankfully these don't actually change based on context.
 122          // They do require a DB call, so let's cache it.
 123          $cachekey = "allroles";
 124          $allroles = $cache->get($cachekey);
 125          if ($allroles === false) {
 126              $allroles = get_all_roles();
 127              $cache->set($cachekey, $allroles);
 128          }
 129  
 130          // Now we can check whether any of the user roles are in the list of allowed roles for this filter.
 131          foreach ($userroles as $role) {
 132              $shortname = $allroles[$role->roleid]->shortname;
 133              if (isset($values[$shortname])) {
 134                  return true;
 135              }
 136          }
 137  
 138          return false;
 139      }
 140  }