Moodle release 3.2rc2
[moodle.git] / message / classes / helper.php
CommitLineData
879e2bef
MN
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 * Contains helper class for the message area.
19 *
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
23 */
24
25namespace core_message;
26
27defined('MOODLE_INTERNAL') || die();
28
29/**
30 * Helper class for the message area.
31 *
32 * @copyright 2016 Mark Nelson <markn@moodle.com>
33 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
34 */
35class helper {
36
37 /**
38 * Helper function to retrieve the messages between two users
39 *
40 * @param int $userid the current user
41 * @param int $otheruserid the other user
dec0cd99 42 * @param int $timedeleted the time the message was deleted
879e2bef
MN
43 * @param int $limitfrom
44 * @param int $limitnum
45 * @param string $sort
ffd7798c
MN
46 * @param int $timefrom the time from the message being sent
47 * @param int $timeto the time up until the message being sent
879e2bef
MN
48 * @return array of messages
49 */
7b55aaa1 50 public static function get_messages($userid, $otheruserid, $timedeleted = 0, $limitfrom = 0, $limitnum = 0,
ffd7798c 51 $sort = 'timecreated ASC', $timefrom = 0, $timeto = 0) {
879e2bef
MN
52 global $DB;
53
ebc746dc
AN
54 $messageid = $DB->sql_concat("'message_'", 'id');
55 $messagereadid = $DB->sql_concat("'messageread_'", 'id');
56
57 $sql = "SELECT {$messageid} AS fakeid, id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
879e2bef
MN
58 smallmessage, notification, timecreated, 0 as timeread
59 FROM {message} m
dec0cd99
MN
60 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = ?)
61 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = ?))
879e2bef 62 AND notification = 0
fb1469d8 63 %where%
879e2bef 64 UNION ALL
ebc746dc 65 SELECT {$messagereadid} AS fakeid, id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
879e2bef
MN
66 smallmessage, notification, timecreated, timeread
67 FROM {message_read} mr
dec0cd99
MN
68 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = ?)
69 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = ?))
879e2bef 70 AND notification = 0
fb1469d8 71 %where%
879e2bef 72 ORDER BY $sort";
fb1469d8
RW
73 $params1 = array($userid, $otheruserid, $timedeleted,
74 $otheruserid, $userid, $timedeleted);
75
76 $params2 = array($userid, $otheruserid, $timedeleted,
77 $otheruserid, $userid, $timedeleted);
78 $where = array();
79
ffd7798c 80 if (!empty($timefrom)) {
fb1469d8 81 $where[] = 'AND timecreated >= ?';
ffd7798c
MN
82 $params1[] = $timefrom;
83 $params2[] = $timefrom;
fb1469d8
RW
84 }
85
ffd7798c 86 if (!empty($timeto)) {
fb1469d8 87 $where[] = 'AND timecreated <= ?';
ffd7798c
MN
88 $params1[] = $timeto;
89 $params2[] = $timeto;
fb1469d8
RW
90 }
91
92 $sql = str_replace('%where%', implode(' ', $where), $sql);
93 $params = array_merge($params1, $params2);
879e2bef
MN
94
95 return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
96 }
97
98 /**
de55cb1b 99 * Helper function to return an array of messages.
879e2bef
MN
100 *
101 * @param int $userid
102 * @param array $messages
de55cb1b 103 * @return array
879e2bef
MN
104 */
105 public static function create_messages($userid, $messages) {
106 // Store the messages.
107 $arrmessages = array();
108
8ec78c48
MN
109 // We always view messages from oldest to newest, ensure we have it in that order.
110 $lastmessage = end($messages);
111 $firstmessage = reset($messages);
112 if ($lastmessage->timecreated < $firstmessage->timecreated) {
113 $messages = array_reverse($messages);
114 }
115
879e2bef
MN
116 // Keeps track of the last day, month and year combo we were viewing.
117 $day = '';
118 $month = '';
119 $year = '';
120 foreach ($messages as $message) {
121 // Check if we are now viewing a different block period.
3090f52f 122 $displayblocktime = false;
879e2bef
MN
123 $date = usergetdate($message->timecreated);
124 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
125 $day = $date['mday'];
126 $month = $date['month'];
127 $year = $date['year'];
3090f52f 128 $displayblocktime = true;
879e2bef
MN
129 }
130 // Store the message to pass to the renderable.
131 $msg = new \stdClass();
3090f52f 132 $msg->id = $message->id;
879e2bef
MN
133 $msg->text = message_format_message_text($message);
134 $msg->currentuserid = $userid;
135 $msg->useridfrom = $message->useridfrom;
136 $msg->useridto = $message->useridto;
3090f52f 137 $msg->displayblocktime = $displayblocktime;
879e2bef 138 $msg->timecreated = $message->timecreated;
3090f52f 139 $msg->timeread = $message->timeread;
de55cb1b 140 $arrmessages[] = $msg;
879e2bef
MN
141 }
142
143 return $arrmessages;
144 }
145
146 /**
de55cb1b 147 * Helper function for creating a contact object.
879e2bef
MN
148 *
149 * @param \stdClass $contact
cd03b8d7 150 * @param string $prefix
de55cb1b 151 * @return \stdClass
879e2bef 152 */
cd03b8d7 153 public static function create_contact($contact, $prefix = '') {
bf58081d 154 global $PAGE;
879e2bef
MN
155
156 // Create the data we are going to pass to the renderable.
cd03b8d7 157 $userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix);
879e2bef
MN
158 $data = new \stdClass();
159 $data->userid = $userfields->id;
89a70ba1 160 $data->useridfrom = null;
879e2bef
MN
161 $data->fullname = fullname($userfields);
162 // Get the user picture data.
163 $userpicture = new \user_picture($userfields);
164 $userpicture->size = 1; // Size f1.
165 $data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
166 $userpicture->size = 0; // Size f2.
167 $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
168 // Store the message if we have it.
5bf0ff27 169 $data->ismessaging = false;
cd03b8d7
MN
170 $data->lastmessage = null;
171 $data->messageid = null;
879e2bef 172 if (isset($contact->smallmessage)) {
5bf0ff27 173 $data->ismessaging = true;
54d83992
RW
174 // Strip the HTML tags from the message for displaying in the contact area.
175 $data->lastmessage = clean_param($contact->smallmessage, PARAM_NOTAGS);
89a70ba1 176 $data->useridfrom = $contact->useridfrom;
cd03b8d7
MN
177 if (isset($contact->messageid)) {
178 $data->messageid = $contact->messageid;
179 }
879e2bef
MN
180 }
181 // Check if the user is online.
7b55aaa1 182 $data->isonline = self::is_online($userfields->lastaccess);
1f64514d
MN
183 $data->isblocked = isset($contact->blocked) ? (bool) $contact->blocked : false;
184 $data->isread = isset($contact->isread) ? (bool) $contact->isread : false;
c33b7d89 185 $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
879e2bef 186
de55cb1b 187 return $data;
879e2bef 188 }
bf58081d
MN
189
190 /**
191 * Helper function for checking the time meets the 'online' condition.
192 *
193 * @param int $lastaccess
194 * @return boolean
195 */
196 public static function is_online($lastaccess) {
197 global $CFG;
198
199 // Variable to check if we consider this user online or not.
200 $timetoshowusers = 300; // Seconds default.
201 if (isset($CFG->block_online_users_timetosee)) {
202 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
203 }
204 $time = time() - $timetoshowusers;
205
206 return $lastaccess >= $time;
207 }
79f6c36c
MN
208
209 /**
210 * Get providers preferences.
211 *
212 * @param array $providers
213 * @param int $userid
214 * @return \stdClass
215 */
216 public static function get_providers_preferences($providers, $userid) {
217 $preferences = new \stdClass();
218
219 // Get providers preferences.
220 foreach ($providers as $provider) {
221 foreach (array('loggedin', 'loggedoff') as $state) {
222 $linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name
223 . '_' . $state, '', $userid);
224 if ($linepref == '') {
225 continue;
226 }
227 $lineprefarray = explode(',', $linepref);
228 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
229 foreach ($lineprefarray as $pref) {
230 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
231 }
232 }
233 }
234
235 return $preferences;
236 }
237
238 /**
239 * Requires the JS libraries for the toggle contact button.
240 *
241 * @return void
242 */
243 public static function togglecontact_requirejs() {
244 global $PAGE;
245
246 static $done = false;
247 if ($done) {
248 return;
249 }
250
251 $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
252 $done = true;
253 }
254
255 /**
256 * Returns the attributes to place on a contact button.
257 *
258 * @param object $user User object.
259 * @param bool $iscontact
260 * @return array
261 */
262 public static function togglecontact_link_params($user, $iscontact = false) {
263 $params = array(
264 'data-userid' => $user->id,
265 'data-is-contact' => $iscontact,
266 'id' => 'toggle-contact-button',
267 'role' => 'button',
268 'class' => 'ajax-contact-button',
269 );
270
271 return $params;
272 }
ffd7798c
MN
273
274 /**
275 * Returns the cache key for the time created value of the last message between two users.
276 *
277 * @param int $userid
278 * @param int $user2id
279 * @return string
280 */
281 public static function get_last_message_time_created_cache_key($userid, $user2id) {
282 $ids = [$userid, $user2id];
283 sort($ids);
284 return implode('_', $ids);
285 }
eda6bc19 286}