Proyectos de Subversion Moodle

Rev

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