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 401 and 402] [Versions 401 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 data source for the quiz overrides.
  19   *
  20   * @package   mod_quiz
  21   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  22   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  declare(strict_types=1);
  26  
  27  namespace mod_quiz\cache;
  28  
  29  use cache_definition;
  30  
  31  /**
  32   * Class quiz_overrides
  33   *
  34   * @package   mod_quiz
  35   * @copyright 2021 Shamim Rezaie <shamim@moodle.com>
  36   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  37   */
  38  class overrides implements \cache_data_source {
  39  
  40      /** @var overrides the singleton instance of this class. */
  41      protected static $instance = null;
  42  
  43      /**
  44       * Returns an instance of the data source class that the cache can use for loading data using the other methods
  45       * specified by this interface.
  46       *
  47       * @param cache_definition $definition
  48       * @return object
  49       */
  50      public static function get_instance_for_cache(cache_definition $definition): overrides {
  51          if (is_null(self::$instance)) {
  52              self::$instance = new overrides();
  53          }
  54          return self::$instance;
  55      }
  56  
  57      /**
  58       * Loads the data for the key provided ready formatted for caching.
  59       *
  60       * @param string|int $key The key to load.
  61       * @return mixed What ever data should be returned, or false if it can't be loaded.
  62       * @throws \coding_exception
  63       */
  64      public function load_for_cache($key) {
  65          global $DB;
  66  
  67          [$quizid, $ug, $ugid] = explode('_', $key);
  68          $quizid = (int) $quizid;
  69  
  70          switch ($ug) {
  71              case 'u':
  72                  $userid = (int) $ugid;
  73                  $override = $DB->get_record(
  74                      'quiz_overrides',
  75                      ['quiz' => $quizid, 'userid' => $userid],
  76                      'timeopen, timeclose, timelimit, attempts, password'
  77                  );
  78                  break;
  79              case 'g':
  80                  $groupid = (int) $ugid;
  81                  $override = $DB->get_record(
  82                      'quiz_overrides',
  83                      ['quiz' => $quizid, 'groupid' => $groupid],
  84                      'timeopen, timeclose, timelimit, attempts, password'
  85                  );
  86                  break;
  87              default:
  88                  throw new \coding_exception('Invalid cache key');
  89          }
  90  
  91          // Return null instead of false, because false will not be cached.
  92          return $override ?: null;
  93      }
  94  
  95      /**
  96       * Loads several keys for the cache.
  97       *
  98       * @param array $keys An array of keys each of which will be string|int.
  99       * @return array An array of matching data items.
 100       */
 101      public function load_many_for_cache(array $keys) {
 102          $results = [];
 103  
 104          foreach ($keys as $key) {
 105              $results[] = $this->load_for_cache($key);
 106          }
 107  
 108          return $results;
 109      }
 110  }