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
namespace core_message;
18
 
1441 ariadna 19
use core_cache\data_source_interface;
20
use core_cache\definition;
1 efrain 21
 
22
/**
23
 * Cache data source for the time of the last message in a conversation.
24
 *
25
 * @package    core_message
26
 * @category   cache
27
 * @copyright  2016 Ryan Wyllie <ryan@moodle.com>
28
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
29
 */
1441 ariadna 30
class time_last_message_between_users implements data_source_interface {
1 efrain 31
 
32
    /** @var time_last_message_between_users the singleton instance of this class. */
33
    protected static $instance = null;
34
 
35
    /**
36
     * Returns an instance of the data source class that the cache can use for loading data using the other methods
1441 ariadna 37
     * specified by the data_source_interface interface.
1 efrain 38
     *
1441 ariadna 39
     * @param definition $definition
1 efrain 40
     * @return object
41
     */
1441 ariadna 42
    public static function get_instance_for_cache(definition $definition) {
1 efrain 43
        if (is_null(self::$instance)) {
44
            self::$instance = new time_last_message_between_users();
45
        }
46
        return self::$instance;
47
    }
48
 
49
    /**
50
     * Loads the data for the key provided ready formatted for caching.
51
     *
52
     * @param string|int $key The key to load.
53
     * @return mixed What ever data should be returned, or false if it can't be loaded.
54
     */
55
    public function load_for_cache($key) {
56
        $message = api::get_most_recent_conversation_message($key);
57
 
58
        if ($message) {
59
            return $message->timecreated;
60
        } else {
61
            return null;
62
        }
63
    }
64
 
65
    /**
66
     * Loads several keys for the cache.
67
     *
68
     * @param array $keys An array of keys each of which will be string|int.
69
     * @return array An array of matching data items.
70
     */
71
    public function load_many_for_cache(array $keys) {
72
        $results = [];
73
 
74
        foreach ($keys as $key) {
75
            $results[] = $this->load_for_cache($key);
76
        }
77
 
78
        return $results;
79
    }
80
}