Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401]

   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          $isdashboard = $targetmatch->compare(new \moodle_url('/my/'), URL_MATCH_BASE);
  88          $ismycourses = $targetmatch->compare(new \moodle_url('/my/courses.php'), URL_MATCH_BASE);
  89  
  90          $possiblematches = [];
  91          if ($isfrontpage) {
  92              $possiblematches = ['FRONTPAGE', 'FRONTPAGE_MY', 'FRONTPAGE_MYCOURSES', 'FRONTPAGE_MY_MYCOURSES'];
  93          } else if ($isdashboard) {
  94              $possiblematches = ['MY', 'FRONTPAGE_MY', 'MY_MYCOURSES', 'FRONTPAGE_MY_MYCOURSES'];
  95          } else if ($ismycourses) {
  96              $possiblematches = ['MYCOURSES', 'FRONTPAGE_MYCOURSES', 'MY_MYCOURSES', 'FRONTPAGE_MY_MYCOURSES'];
  97          }
  98  
  99          $target = $targetmatch->out_as_local_url();
 100          return array_filter($tours, function($tour) use ($possiblematches, $target) {
 101              if (in_array($tour->pathmatch, $possiblematches)) {
 102                  return true;
 103              }
 104              $pattern = preg_quote($tour->pathmatch, '@');
 105              if (strpos($pattern, '%') !== false) {
 106                  // The URL match format is something like: /my/%.
 107                  // We need to find all the URLs which match the first part of the pattern.
 108                  $pattern = str_replace('%', '.*', $pattern);
 109              } else {
 110                  // The URL match format is something like: /my/courses.php.
 111                  // We need to find all the URLs which match with whole pattern.
 112                  $pattern .= '$';
 113              }
 114              return !!preg_match("@{$pattern}@", $target);
 115          });
 116      }
 117  
 118      /**
 119       * Notify of changes to any tour to clear the tour cache.
 120       */
 121      public static function notify_tour_change() {
 122          $cache = \cache::make('tool_usertours', self::CACHENAME_TOUR);
 123          $cache->delete(self::CACHEKEY_TOUR);
 124      }
 125  
 126      /**
 127       * Fetch the tour data for the specified tour.
 128       *
 129       * @param   int         $tourid         The ID of the tour to fetch steps for
 130       */
 131      public static function get_stepdata($tourid) {
 132          global $DB;
 133  
 134          $cache = \cache::make('tool_usertours', self::CACHENAME_STEP);
 135  
 136          $data = $cache->get($tourid);
 137          if ($data === false) {
 138              $sql = <<<EOF
 139                  SELECT s.*
 140                    FROM {tool_usertours_steps} s
 141                   WHERE s.tourid = :tourid
 142                ORDER BY s.sortorder ASC
 143  EOF;
 144  
 145              $data = $DB->get_records_sql($sql, ['tourid' => $tourid]);
 146              $cache->set($tourid, $data);
 147          }
 148  
 149          return $data;
 150      }
 151      /**
 152       * Notify of changes to any step to clear the step cache for that tour.
 153       *
 154       * @param   int         $tourid         The ID of the tour to clear the step cache for
 155       */
 156      public static function notify_step_change($tourid) {
 157          $cache = \cache::make('tool_usertours', self::CACHENAME_STEP);
 158          $cache->delete($tourid);
 159      }
 160  }