MDL-36941 core: final deprecation of functions
[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
883ce421
MN
54 $sql = "SELECT m.id, m.useridfrom, mdm.userid as useridto, m.subject, m.fullmessage, m.fullmessagehtml,
55 m.fullmessageformat, m.smallmessage, m.timecreated, 0 as timeread
56 FROM {messages} m
57 INNER JOIN {message_conversations} md
58 ON md.id = m.conversationid
59 INNER JOIN {message_conversation_members} mdm
60 ON mdm.conversationid = md.id";
61 $params = [];
ebc746dc 62
883ce421
MN
63 if (empty($timedeleted)) {
64 $sql .= " LEFT JOIN {message_user_actions} mua
65 ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ? AND mua.timecreated is NOT NULL)";
66 $params[] = $userid;
67 $params[] = api::MESSAGE_ACTION_DELETED;
68 } else {
69 $sql .= " INNER JOIN {message_user_actions} mua
70 ON (mua.messageid = m.id AND mua.userid = ? AND mua.action = ? AND mua.timecreated = ?)";
71 $params[] = $userid;
72 $params[] = api::MESSAGE_ACTION_DELETED;
73 $params[] = $timedeleted;
74 }
fb1469d8 75
883ce421
MN
76 $sql .= " WHERE (m.useridfrom = ? AND mdm.userid = ? OR m.useridfrom = ? AND mdm.userid = ?)";
77 $params = array_merge($params, [$userid, $otheruserid, $otheruserid, $userid]);
fb1469d8 78
ffd7798c 79 if (!empty($timefrom)) {
883ce421
MN
80 $sql .= " AND m.timecreated >= ?";
81 $params[] = $timefrom;
fb1469d8
RW
82 }
83
ffd7798c 84 if (!empty($timeto)) {
883ce421
MN
85 $sql .= " AND m.timecreated <= ?";
86 $params[] = $timeto;
87 }
88
89 if (empty($timedeleted)) {
90 $sql .= " AND mua.id is NULL";
fb1469d8
RW
91 }
92
883ce421 93 $sql .= " ORDER BY m.$sort";
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 180 }
cb805753
MN
181 $data->isonline = null;
182 if (self::show_online_status($userfields)) {
183 $data->isonline = self::is_online($userfields->lastaccess);
184 }
1f64514d
MN
185 $data->isblocked = isset($contact->blocked) ? (bool) $contact->blocked : false;
186 $data->isread = isset($contact->isread) ? (bool) $contact->isread : false;
c33b7d89 187 $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
879e2bef 188
de55cb1b 189 return $data;
879e2bef 190 }
bf58081d 191
cb805753
MN
192 /**
193 * Helper function for checking if we should show the user's online status.
194 *
195 * @param \stdClass $user
196 * @return boolean
197 */
198 public static function show_online_status($user) {
199 global $CFG;
200
201 require_once($CFG->dirroot . '/user/lib.php');
202
203 if ($lastaccess = user_get_user_details($user, null, array('lastaccess'))) {
204 if (isset($lastaccess['lastaccess'])) {
205 return true;
206 }
207 }
208
209 return false;
210 }
211
bf58081d
MN
212 /**
213 * Helper function for checking the time meets the 'online' condition.
214 *
215 * @param int $lastaccess
216 * @return boolean
217 */
218 public static function is_online($lastaccess) {
219 global $CFG;
220
221 // Variable to check if we consider this user online or not.
222 $timetoshowusers = 300; // Seconds default.
223 if (isset($CFG->block_online_users_timetosee)) {
224 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
225 }
226 $time = time() - $timetoshowusers;
227
228 return $lastaccess >= $time;
229 }
79f6c36c
MN
230
231 /**
232 * Get providers preferences.
233 *
234 * @param array $providers
235 * @param int $userid
236 * @return \stdClass
237 */
238 public static function get_providers_preferences($providers, $userid) {
239 $preferences = new \stdClass();
240
241 // Get providers preferences.
242 foreach ($providers as $provider) {
243 foreach (array('loggedin', 'loggedoff') as $state) {
244 $linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name
245 . '_' . $state, '', $userid);
246 if ($linepref == '') {
247 continue;
248 }
249 $lineprefarray = explode(',', $linepref);
250 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
251 foreach ($lineprefarray as $pref) {
252 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
253 }
254 }
255 }
256
257 return $preferences;
258 }
259
260 /**
261 * Requires the JS libraries for the toggle contact button.
262 *
263 * @return void
264 */
265 public static function togglecontact_requirejs() {
266 global $PAGE;
267
268 static $done = false;
269 if ($done) {
270 return;
271 }
272
273 $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
274 $done = true;
275 }
276
277 /**
278 * Returns the attributes to place on a contact button.
279 *
280 * @param object $user User object.
281 * @param bool $iscontact
282 * @return array
283 */
284 public static function togglecontact_link_params($user, $iscontact = false) {
285 $params = array(
286 'data-userid' => $user->id,
287 'data-is-contact' => $iscontact,
288 'id' => 'toggle-contact-button',
289 'role' => 'button',
290 'class' => 'ajax-contact-button',
291 );
292
293 return $params;
294 }
ffd7798c
MN
295
296 /**
297 * Returns the cache key for the time created value of the last message between two users.
298 *
299 * @param int $userid
300 * @param int $user2id
301 * @return string
302 */
303 public static function get_last_message_time_created_cache_key($userid, $user2id) {
304 $ids = [$userid, $user2id];
305 sort($ids);
306 return implode('_', $ids);
307 }
eda6bc19 308}