Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 400] [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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   * Cache manager.
  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;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  /**
  30   * Cache manager.
  31   *
  32   * @copyright  2016 Andrew Nicols <andrew@nicols.co.uk>
  33   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  34   */
  35  class cache {
  36      /**
  37       * @var CACHENAME_TOUR      The name of the cache used for storing tours.
  38       */
  39      const CACHENAME_TOUR = 'tourdata';
  40  
  41      /**
  42       * @var CACHEKEY_TOUR       The name of the key used for storing tours.
  43       */
  44      const CACHEKEY_TOUR = 'tours';
  45  
  46      /**
  47       * @var CACHENAME_STEP      The name of the cache used for storing steps.
  48       */
  49      const CACHENAME_STEP = 'stepdata';
  50  
  51      /**
  52       * Fetch all enabled tours.
  53       */
  54      public static function get_enabled_tourdata() {
  55          global $DB;
  56  
  57          $cache = \cache::make('tool_usertours', self::CACHENAME_TOUR);
  58  
  59          $data = $cache->get(self::CACHEKEY_TOUR);
  60          if ($data === false) {
  61              $sql = <<<EOF
  62                  SELECT t.*
  63                    FROM {tool_usertours_tours} t
  64                   WHERE t.enabled = 1
  65                     AND t.id IN (SELECT s.tourid FROM {tool_usertours_steps} s GROUP BY s.tourid)
  66                ORDER BY t.sortorder ASC
  67  EOF;
  68  
  69              $data = $DB->get_records_sql($sql);
  70              $cache->set('tours', $data);
  71          }
  72  
  73          return $data;
  74      }
  75  
  76      /**
  77       * Fetch all enabled tours matching the specified target.
  78       *
  79       * @param   moodle_url  $targetmatch    The URL to match.
  80       */
  81      public static function get_matching_tourdata(\moodle_url $targetmatch) {
  82          $tours = self::get_enabled_tourdata();
  83  
  84          // Attempt to determine whether this is the front page.
  85          // This is a special case because the frontpage uses a shortened page path making it difficult to detect exactly.
  86          $isfrontpage = $targetmatch->compare(new \moodle_url('/'), URL_MATCH_BASE);
  87          $target = $targetmatch->out_as_local_url();
  88          return array_filter($tours, function($tour) use ($isfrontpage, $target) {
  89              if ($isfrontpage && $tour->pathmatch === 'FRONTPAGE') {
  90                  return true;
  91              }
  92              $pattern = preg_quote($tour->pathmatch, '@');
  93              $pattern = str_replace('%', '.*', $pattern);
  94              return !!preg_match("@{$pattern}@", $target);
  95          });
  96      }
  97  
  98      /**
  99       * Notify of changes to any tour to clear the tour cache.
 100       */
 101      public static function notify_tour_change() {
 102          $cache = \cache::make('tool_usertours', self::CACHENAME_TOUR);
 103          $cache->delete(self::CACHEKEY_TOUR);
 104      }
 105  
 106      /**
 107       * Fetch the tour data for the specified tour.
 108       *
 109       * @param   int         $tourid         The ID of the tour to fetch steps for
 110       */
 111      public static function get_stepdata($tourid) {
 112          global $DB;
 113  
 114          $cache = \cache::make('tool_usertours', self::CACHENAME_STEP);
 115  
 116          $data = $cache->get($tourid);
 117          if ($data === false) {
 118              $sql = <<<EOF
 119                  SELECT s.*
 120                    FROM {tool_usertours_steps} s
 121                   WHERE s.tourid = :tourid
 122                ORDER BY s.sortorder ASC
 123  EOF;
 124  
 125              $data = $DB->get_records_sql($sql, ['tourid' => $tourid]);
 126              $cache->set($tourid, $data);
 127          }
 128  
 129          return $data;
 130      }
 131      /**
 132       * Notify of changes to any step to clear the step cache for that tour.
 133       *
 134       * @param   int         $tourid         The ID of the tour to clear the step cache for
 135       */
 136      public static function notify_step_change($tourid) {
 137          $cache = \cache::make('tool_usertours', self::CACHENAME_STEP);
 138          $cache->delete($tourid);
 139      }
 140  }