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 |
namespace mod_scorm\cache;
|
|
|
19 |
use cache_definition;
|
|
|
20 |
|
|
|
21 |
/**
|
|
|
22 |
* Cache data source for the scorm elements.
|
|
|
23 |
*
|
|
|
24 |
* @package mod_scorm
|
|
|
25 |
* @copyright 2023 Catalyst IT Ltd
|
|
|
26 |
* @author Dan Marsden <dan@danmarsden.com>
|
|
|
27 |
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
28 |
*/
|
|
|
29 |
class elements implements \cache_data_source {
|
|
|
30 |
|
|
|
31 |
/** @var elements the singleton instance of this class. */
|
|
|
32 |
protected static $instance = null;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Returns an instance of the data source class that the cache can use for loading data using the other methods
|
|
|
36 |
* specified by this interface.
|
|
|
37 |
*
|
|
|
38 |
* @param cache_definition $definition
|
|
|
39 |
* @return object
|
|
|
40 |
*/
|
|
|
41 |
public static function get_instance_for_cache(cache_definition $definition): elements {
|
|
|
42 |
if (is_null(self::$instance)) {
|
|
|
43 |
self::$instance = new elements();
|
|
|
44 |
}
|
|
|
45 |
return self::$instance;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
/**
|
|
|
49 |
* Loads the data for the key provided ready formatted for caching.
|
|
|
50 |
*
|
|
|
51 |
* @param string|int $key The key to load.
|
|
|
52 |
* @return string What ever data should be returned, or null if it can't be loaded.
|
|
|
53 |
* @throws \coding_exception
|
|
|
54 |
*/
|
|
|
55 |
public function load_for_cache($key): ?string {
|
|
|
56 |
global $DB;
|
|
|
57 |
|
|
|
58 |
$element = $DB->get_field('scorm_element', 'id', ['element' => $key]);
|
|
|
59 |
// Return null instead of false, because false will not be cached.
|
|
|
60 |
return $element ?: null;
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
/**
|
|
|
64 |
* Loads several keys for the cache.
|
|
|
65 |
*
|
|
|
66 |
* @param array $keys An array of keys each of which will be string|int.
|
|
|
67 |
* @return array An array of matching data items.
|
|
|
68 |
*/
|
|
|
69 |
public function load_many_for_cache(array $keys): array {
|
|
|
70 |
global $DB;
|
|
|
71 |
list ($elementsql, $params) = $DB->get_in_or_equal($keys);
|
|
|
72 |
$sql = "SELECT element, id
|
|
|
73 |
FROM {scorm_element}
|
|
|
74 |
WHERE element ".$elementsql;
|
|
|
75 |
return $DB->get_records_sql_menu($sql, $params);
|
|
|
76 |
}
|
|
|
77 |
}
|