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
namespace mod_quiz\local;
18
 
19
/**
20
 * Cache manager for quiz overrides
21
 *
22
 * Override cache data is set via its data source, {@see \mod_quiz\cache\overrides}
23
 * @package   mod_quiz
24
 * @copyright 2024 Matthew Hilton <matthewhilton@catalyst-au.net>
25
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26
 */
27
class override_cache {
28
    /** @var string invalidation event used to purge data when reset_userdata is called, {@see \cache_helper::purge_by_event()} **/
29
    public const INVALIDATION_USERDATARESET = 'userdatareset';
30
 
31
    /**
32
     * Create override_cache object and link to quiz
33
     *
34
     * @param int $quizid The quiz to link this cache to
35
     */
36
    public function __construct(
37
        /** @var int $quizid ID of quiz cache is being operated on **/
38
        protected readonly int $quizid
39
    ) {
40
    }
41
 
42
    /**
43
     * Returns the override cache
44
     *
45
     * @return \cache
46
     */
47
    protected function get_cache(): \cache {
48
        return \cache::make('mod_quiz', 'overrides');
49
    }
50
 
51
    /**
52
     * Returns group cache key
53
     *
54
     * @param int $groupid
55
     * @return string the group cache key
56
     */
57
    protected function get_group_cache_key(int $groupid): string {
58
        return "{$this->quizid}_g_{$groupid}";
59
    }
60
 
61
    /**
62
     * Returns user cache key
63
     *
64
     * @param int $userid
65
     * @return string the user cache key
66
     */
67
    protected function get_user_cache_key(int $userid): string {
68
        return "{$this->quizid}_u_{$userid}";
69
    }
70
 
71
    /**
72
     * Returns the override value in the cache for the given group
73
     *
74
     * @param int $groupid group to get cached override data for
75
     * @return ?\stdClass override value in the cache for the given group, or null if there is none.
76
     */
77
    public function get_cached_group_override(int $groupid): ?\stdClass {
78
        $raw = $this->get_cache()->get($this->get_group_cache_key($groupid));
79
        return empty($raw) || !is_object($raw) ? null : (object) $raw;
80
    }
81
 
82
    /**
83
     * Returns the override value in the cache for the given user
84
     *
85
     * @param int $userid user to get cached override data for
86
     * @return ?\stdClass the override value in the cache for the given user, or null if there is none.
87
     */
88
    public function get_cached_user_override(int $userid): ?\stdClass {
89
        $raw = $this->get_cache()->get($this->get_user_cache_key($userid));
90
        return empty($raw) || !is_object($raw) ? null : (object) $raw;
91
    }
92
 
93
    /**
94
     * Deletes the cached override data for a given group
95
     *
96
     * @param int $groupid group to delete data for
97
     */
98
    public function clear_for_group(int $groupid): void {
99
        $this->get_cache()->delete($this->get_group_cache_key($groupid));
100
    }
101
 
102
    /**
103
     * Deletes the cached override data for the given user
104
     *
105
     * @param int $userid user to delete data for
106
     */
107
    public function clear_for_user(int $userid): void {
108
        $this->get_cache()->delete($this->get_user_cache_key($userid));
109
    }
110
 
111
    /**
112
     * Clears the cache for the given user and/or group.
113
     *
114
     * @param ?int $userid user to delete data for, or null.
115
     * @param ?int $groupid group to delete data for, or null.
116
     */
117
    public function clear_for(?int $userid = null, ?int $groupid = null): void {
118
        if (!empty($userid)) {
119
            $this->clear_for_user($userid);
120
        }
121
 
122
        if (!empty($groupid)) {
123
            $this->clear_for_group($groupid);
124
        }
125
    }
126
}