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