Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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 stdClass
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
        // Ignore getting data if this is a cache invalidation - {@see \cache_helper::purge_by_event()}.
68
        if ($key == 'lastinvalidation') {
69
            return null;
70
        }
71
 
72
        [$quizid, $ug, $ugid] = explode('_', $key);
73
        $quizid = (int) $quizid;
74
 
75
        switch ($ug) {
76
            case 'u':
77
                $userid = (int) $ugid;
78
                $override = $DB->get_record(
79
                    'quiz_overrides',
80
                    ['quiz' => $quizid, 'userid' => $userid],
81
                    'timeopen, timeclose, timelimit, attempts, password'
82
                );
83
                break;
84
            case 'g':
85
                $groupid = (int) $ugid;
86
                $override = $DB->get_record(
87
                    'quiz_overrides',
88
                    ['quiz' => $quizid, 'groupid' => $groupid],
89
                    'timeopen, timeclose, timelimit, attempts, password'
90
                );
91
                break;
92
            default:
93
                throw new \coding_exception('Invalid cache key');
94
        }
95
 
96
        // Return null instead of false, because false will not be cached.
97
        return $override ?: null;
98
    }
99
 
100
    /**
101
     * Loads several keys for the cache.
102
     *
103
     * @param array $keys An array of keys each of which will be string|int.
104
     * @return array An array of matching data items.
105
     */
106
    public function load_many_for_cache(array $keys) {
107
        $results = [];
108
 
109
        foreach ($keys as $key) {
110
            $results[] = $this->load_for_cache($key);
111
        }
112
 
113
        return $results;
114
    }
115
}