Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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 assign overrides.
  19   *
  20   * @package   mod_assign
  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_assign\cache;
  28  
  29  use cache_definition;
  30  
  31  /**
  32   * Class assign_overrides
  33   *
  34   * @package   mod_assign
  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          [$assignid, $ug, $ugid] = explode('_', $key);
  68          $assignid = (int) $assignid;
  69  
  70          switch ($ug) {
  71              case 'u':
  72                  $userid = (int) $ugid;
  73                  $override = $DB->get_record(
  74                      'assign_overrides',
  75                      ['assignid' => $assignid, 'userid' => $userid],
  76                      'duedate, cutoffdate, allowsubmissionsfromdate'
  77                  );
  78                  break;
  79              case 'g':
  80                  $groupid = (int) $ugid;
  81                  $override = $DB->get_record(
  82                      'assign_overrides',
  83                      ['assignid' => $assignid, 'groupid' => $groupid],
  84                      'sortorder, duedate, cutoffdate, allowsubmissionsfromdate'
  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  }