1441 |
ariadna |
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 core_ai\cache;
|
|
|
18 |
|
|
|
19 |
use cache_definition;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Cache class for AI policy.
|
|
|
23 |
*
|
|
|
24 |
* @package core_ai
|
|
|
25 |
* @copyright 2024 Matt Porritt <matt.porritt@moodle.com>
|
|
|
26 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
27 |
*/
|
|
|
28 |
class policy implements \cache_data_source {
|
|
|
29 |
/** @var policy|null the singleton instance of this class. */
|
|
|
30 |
protected static ?policy $instance = null;
|
|
|
31 |
|
|
|
32 |
#[\Override]
|
|
|
33 |
public static function get_instance_for_cache(cache_definition $definition): policy {
|
|
|
34 |
if (is_null(self::$instance)) {
|
|
|
35 |
self::$instance = new self();
|
|
|
36 |
}
|
|
|
37 |
return self::$instance;
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
#[\Override]
|
|
|
41 |
public function load_for_cache($key) {
|
|
|
42 |
global $DB;
|
|
|
43 |
|
|
|
44 |
return $DB->record_exists('ai_policy_register', ['userid' => $key]);
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
#[\Override]
|
|
|
48 |
public function load_many_for_cache(array $keys): array {
|
|
|
49 |
global $DB;
|
|
|
50 |
$return = [];
|
|
|
51 |
[$insql, $inparams] = $DB->get_in_or_equal($keys);
|
|
|
52 |
$sql = "SELECT userid
|
|
|
53 |
FROM {ai_policy_register}
|
|
|
54 |
WHERE userid " . $insql;
|
|
|
55 |
|
|
|
56 |
$results = $DB->get_fieldset_sql($sql, $inparams);
|
|
|
57 |
foreach ($keys as $key) {
|
|
|
58 |
$return[$key] = in_array($key, $results);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
return $return;
|
|
|
62 |
}
|
|
|
63 |
}
|