2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Contains helper class for the message area.
20 * @package core_message
21 * @copyright 2016 Mark Nelson <markn@moodle.com>
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 namespace core_message;
27 defined('MOODLE_INTERNAL') || die();
30 * Helper class for the message area.
32 * @copyright 2016 Mark Nelson <markn@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
38 * Helper function to retrieve the messages between two users
40 * @param int $userid the current user
41 * @param int $otheruserid the other user
42 * @param int $timedeleted the time the message was deleted
43 * @param int $limitfrom
44 * @param int $limitnum
46 * @param int $timefrom the time from the message being sent
47 * @param int $timeto the time up until the message being sent
48 * @return array of messages
50 public static function get_messages($userid, $otheruserid, $timedeleted = 0, $limitfrom = 0, $limitnum = 0,
51 $sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
54 $hash = self::get_conversation_hash([$userid, $otheruserid]);
56 $sql = "SELECT m.id, m.useridfrom, m.subject, m.fullmessage, m.fullmessagehtml,
57 m.fullmessageformat, m.smallmessage, m.timecreated, muaread.timecreated AS timeread
58 FROM {message_conversations} mc
59 INNER JOIN {messages} m
60 ON m.conversationid = mc.id
61 LEFT JOIN {message_user_actions} muaread
62 ON (muaread.messageid = m.id
63 AND muaread.userid = :userid1
64 AND muaread.action = :readaction)";
65 $params = ['userid1' => $userid, 'readaction' => api::MESSAGE_ACTION_READ, 'convhash' => $hash];
67 if (empty($timedeleted)) {
68 $sql .= " LEFT JOIN {message_user_actions} mua
69 ON (mua.messageid = m.id
70 AND mua.userid = :userid2
71 AND mua.action = :deleteaction
72 AND mua.timecreated is NOT NULL)";
74 $sql .= " INNER JOIN {message_user_actions} mua
75 ON (mua.messageid = m.id
76 AND mua.userid = :userid2
77 AND mua.action = :deleteaction
78 AND mua.timecreated = :timedeleted)";
79 $params['timedeleted'] = $timedeleted;
82 $params['userid2'] = $userid;
83 $params['deleteaction'] = api::MESSAGE_ACTION_DELETED;
85 $sql .= " WHERE mc.convhash = :convhash";
87 if (!empty($timefrom)) {
88 $sql .= " AND m.timecreated >= :timefrom";
89 $params['timefrom'] = $timefrom;
92 if (!empty($timeto)) {
93 $sql .= " AND m.timecreated <= :timeto";
94 $params['timeto'] = $timeto;
97 if (empty($timedeleted)) {
98 $sql .= " AND mua.id is NULL";
101 $sql .= " ORDER BY m.$sort";
103 $messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
104 foreach ($messages as &$message) {
105 $message->useridto = ($message->useridfrom == $userid) ? $otheruserid : $userid;
112 * Helper function to return an array of messages.
115 * @param array $messages
118 public static function create_messages($userid, $messages) {
119 // Store the messages.
120 $arrmessages = array();
122 // We always view messages from oldest to newest, ensure we have it in that order.
123 $lastmessage = end($messages);
124 $firstmessage = reset($messages);
125 if ($lastmessage->timecreated < $firstmessage->timecreated) {
126 $messages = array_reverse($messages);
129 // Keeps track of the last day, month and year combo we were viewing.
133 foreach ($messages as $message) {
134 // Check if we are now viewing a different block period.
135 $displayblocktime = false;
136 $date = usergetdate($message->timecreated);
137 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
138 $day = $date['mday'];
139 $month = $date['month'];
140 $year = $date['year'];
141 $displayblocktime = true;
143 // Store the message to pass to the renderable.
144 $msg = new \stdClass();
145 $msg->id = $message->id;
146 $msg->text = message_format_message_text($message);
147 $msg->currentuserid = $userid;
148 $msg->useridfrom = $message->useridfrom;
149 $msg->useridto = $message->useridto;
150 $msg->displayblocktime = $displayblocktime;
151 $msg->timecreated = $message->timecreated;
152 $msg->timeread = $message->timeread;
153 $arrmessages[] = $msg;
160 * Helper function for creating a contact object.
162 * @param \stdClass $contact
163 * @param string $prefix
166 public static function create_contact($contact, $prefix = '') {
169 // Create the data we are going to pass to the renderable.
170 $userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix);
171 $data = new \stdClass();
172 $data->userid = $userfields->id;
173 $data->useridfrom = null;
174 $data->fullname = fullname($userfields);
175 // Get the user picture data.
176 $userpicture = new \user_picture($userfields);
177 $userpicture->size = 1; // Size f1.
178 $data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
179 $userpicture->size = 0; // Size f2.
180 $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
181 // Store the message if we have it.
182 $data->ismessaging = false;
183 $data->lastmessage = null;
184 $data->messageid = null;
185 if (isset($contact->smallmessage)) {
186 $data->ismessaging = true;
187 // Strip the HTML tags from the message for displaying in the contact area.
188 $data->lastmessage = clean_param($contact->smallmessage, PARAM_NOTAGS);
189 $data->useridfrom = $contact->useridfrom;
190 if (isset($contact->messageid)) {
191 $data->messageid = $contact->messageid;
194 $data->isonline = null;
195 if (self::show_online_status($userfields)) {
196 $data->isonline = self::is_online($userfields->lastaccess);
198 $data->isblocked = isset($contact->blocked) ? (bool) $contact->blocked : false;
199 $data->isread = isset($contact->isread) ? (bool) $contact->isread : false;
200 $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
206 * Helper function for checking if we should show the user's online status.
208 * @param \stdClass $user
211 public static function show_online_status($user) {
214 require_once($CFG->dirroot . '/user/lib.php');
216 if ($lastaccess = user_get_user_details($user, null, array('lastaccess'))) {
217 if (isset($lastaccess['lastaccess'])) {
226 * Helper function for checking the time meets the 'online' condition.
228 * @param int $lastaccess
231 public static function is_online($lastaccess) {
234 // Variable to check if we consider this user online or not.
235 $timetoshowusers = 300; // Seconds default.
236 if (isset($CFG->block_online_users_timetosee)) {
237 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
239 $time = time() - $timetoshowusers;
241 return $lastaccess >= $time;
245 * Get providers preferences.
247 * @param array $providers
251 public static function get_providers_preferences($providers, $userid) {
252 $preferences = new \stdClass();
254 // Get providers preferences.
255 foreach ($providers as $provider) {
256 foreach (array('loggedin', 'loggedoff') as $state) {
257 $linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name
258 . '_' . $state, '', $userid);
259 if ($linepref == '') {
262 $lineprefarray = explode(',', $linepref);
263 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
264 foreach ($lineprefarray as $pref) {
265 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
274 * Requires the JS libraries for the toggle contact button.
278 public static function togglecontact_requirejs() {
281 static $done = false;
286 $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
291 * Returns the attributes to place on a contact button.
293 * @param object $user User object.
294 * @param bool $iscontact
297 public static function togglecontact_link_params($user, $iscontact = false) {
299 'data-userid' => $user->id,
300 'data-is-contact' => $iscontact,
301 'id' => 'toggle-contact-button',
303 'class' => 'ajax-contact-button',
310 * Returns the conversation hash between users for easy look-ups in the DB.
312 * @param array $userids
315 public static function get_conversation_hash(array $userids) {
318 return sha1(implode('-', $userids));
322 * Returns the cache key for the time created value of the last message between two users.
325 * @param int $user2id
328 public static function get_last_message_time_created_cache_key($userid, $user2id) {
329 $ids = [$userid, $user2id];
331 return implode('_', $ids);