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