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