Commit | Line | Data |
---|---|---|
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 | ||
25 | namespace core_message; | |
26 | ||
27 | defined('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 | */ | |
35 | class 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 | ||
e159b53b MN |
54 | $hash = self::get_conversation_hash([$userid, $otheruserid]); |
55 | ||
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]; | |
ebc746dc | 66 | |
883ce421 MN |
67 | if (empty($timedeleted)) { |
68 | $sql .= " LEFT JOIN {message_user_actions} mua | |
e159b53b MN |
69 | ON (mua.messageid = m.id |
70 | AND mua.userid = :userid2 | |
71 | AND mua.action = :deleteaction | |
72 | AND mua.timecreated is NOT NULL)"; | |
883ce421 MN |
73 | } else { |
74 | $sql .= " INNER JOIN {message_user_actions} mua | |
e159b53b MN |
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; | |
883ce421 | 80 | } |
fb1469d8 | 81 | |
e159b53b MN |
82 | $params['userid2'] = $userid; |
83 | $params['deleteaction'] = api::MESSAGE_ACTION_DELETED; | |
84 | ||
85 | $sql .= " WHERE mc.convhash = :convhash"; | |
fb1469d8 | 86 | |
ffd7798c | 87 | if (!empty($timefrom)) { |
e159b53b MN |
88 | $sql .= " AND m.timecreated >= :timefrom"; |
89 | $params['timefrom'] = $timefrom; | |
fb1469d8 RW |
90 | } |
91 | ||
ffd7798c | 92 | if (!empty($timeto)) { |
e159b53b MN |
93 | $sql .= " AND m.timecreated <= :timeto"; |
94 | $params['timeto'] = $timeto; | |
883ce421 MN |
95 | } |
96 | ||
97 | if (empty($timedeleted)) { | |
98 | $sql .= " AND mua.id is NULL"; | |
fb1469d8 RW |
99 | } |
100 | ||
883ce421 | 101 | $sql .= " ORDER BY m.$sort"; |
879e2bef | 102 | |
e159b53b MN |
103 | $messages = $DB->get_records_sql($sql, $params, $limitfrom, $limitnum); |
104 | foreach ($messages as &$message) { | |
105 | $message->useridto = ($message->useridfrom == $userid) ? $otheruserid : $userid; | |
106 | } | |
107 | ||
108 | return $messages; | |
879e2bef MN |
109 | } |
110 | ||
111 | /** | |
de55cb1b | 112 | * Helper function to return an array of messages. |
879e2bef MN |
113 | * |
114 | * @param int $userid | |
115 | * @param array $messages | |
de55cb1b | 116 | * @return array |
879e2bef MN |
117 | */ |
118 | public static function create_messages($userid, $messages) { | |
119 | // Store the messages. | |
120 | $arrmessages = array(); | |
121 | ||
8ec78c48 MN |
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); | |
127 | } | |
128 | ||
879e2bef MN |
129 | // Keeps track of the last day, month and year combo we were viewing. |
130 | $day = ''; | |
131 | $month = ''; | |
132 | $year = ''; | |
133 | foreach ($messages as $message) { | |
134 | // Check if we are now viewing a different block period. | |
3090f52f | 135 | $displayblocktime = false; |
879e2bef MN |
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']; | |
3090f52f | 141 | $displayblocktime = true; |
879e2bef MN |
142 | } |
143 | // Store the message to pass to the renderable. | |
144 | $msg = new \stdClass(); | |
3090f52f | 145 | $msg->id = $message->id; |
879e2bef MN |
146 | $msg->text = message_format_message_text($message); |
147 | $msg->currentuserid = $userid; | |
148 | $msg->useridfrom = $message->useridfrom; | |
149 | $msg->useridto = $message->useridto; | |
3090f52f | 150 | $msg->displayblocktime = $displayblocktime; |
879e2bef | 151 | $msg->timecreated = $message->timecreated; |
3090f52f | 152 | $msg->timeread = $message->timeread; |
de55cb1b | 153 | $arrmessages[] = $msg; |
879e2bef MN |
154 | } |
155 | ||
156 | return $arrmessages; | |
157 | } | |
158 | ||
159 | /** | |
de55cb1b | 160 | * Helper function for creating a contact object. |
879e2bef MN |
161 | * |
162 | * @param \stdClass $contact | |
cd03b8d7 | 163 | * @param string $prefix |
de55cb1b | 164 | * @return \stdClass |
879e2bef | 165 | */ |
cd03b8d7 | 166 | public static function create_contact($contact, $prefix = '') { |
bf58081d | 167 | global $PAGE; |
879e2bef MN |
168 | |
169 | // Create the data we are going to pass to the renderable. | |
cd03b8d7 | 170 | $userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix); |
879e2bef MN |
171 | $data = new \stdClass(); |
172 | $data->userid = $userfields->id; | |
89a70ba1 | 173 | $data->useridfrom = null; |
879e2bef MN |
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. | |
5bf0ff27 | 182 | $data->ismessaging = false; |
cd03b8d7 MN |
183 | $data->lastmessage = null; |
184 | $data->messageid = null; | |
879e2bef | 185 | if (isset($contact->smallmessage)) { |
5bf0ff27 | 186 | $data->ismessaging = true; |
54d83992 RW |
187 | // Strip the HTML tags from the message for displaying in the contact area. |
188 | $data->lastmessage = clean_param($contact->smallmessage, PARAM_NOTAGS); | |
89a70ba1 | 189 | $data->useridfrom = $contact->useridfrom; |
cd03b8d7 MN |
190 | if (isset($contact->messageid)) { |
191 | $data->messageid = $contact->messageid; | |
192 | } | |
879e2bef | 193 | } |
cb805753 MN |
194 | $data->isonline = null; |
195 | if (self::show_online_status($userfields)) { | |
196 | $data->isonline = self::is_online($userfields->lastaccess); | |
197 | } | |
1f64514d MN |
198 | $data->isblocked = isset($contact->blocked) ? (bool) $contact->blocked : false; |
199 | $data->isread = isset($contact->isread) ? (bool) $contact->isread : false; | |
c33b7d89 | 200 | $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null; |
879e2bef | 201 | |
de55cb1b | 202 | return $data; |
879e2bef | 203 | } |
bf58081d | 204 | |
cb805753 MN |
205 | /** |
206 | * Helper function for checking if we should show the user's online status. | |
207 | * | |
208 | * @param \stdClass $user | |
209 | * @return boolean | |
210 | */ | |
211 | public static function show_online_status($user) { | |
212 | global $CFG; | |
213 | ||
214 | require_once($CFG->dirroot . '/user/lib.php'); | |
215 | ||
216 | if ($lastaccess = user_get_user_details($user, null, array('lastaccess'))) { | |
217 | if (isset($lastaccess['lastaccess'])) { | |
218 | return true; | |
219 | } | |
220 | } | |
221 | ||
222 | return false; | |
223 | } | |
224 | ||
bf58081d MN |
225 | /** |
226 | * Helper function for checking the time meets the 'online' condition. | |
227 | * | |
228 | * @param int $lastaccess | |
229 | * @return boolean | |
230 | */ | |
231 | public static function is_online($lastaccess) { | |
232 | global $CFG; | |
233 | ||
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; | |
238 | } | |
239 | $time = time() - $timetoshowusers; | |
240 | ||
241 | return $lastaccess >= $time; | |
242 | } | |
79f6c36c MN |
243 | |
244 | /** | |
245 | * Get providers preferences. | |
246 | * | |
247 | * @param array $providers | |
248 | * @param int $userid | |
249 | * @return \stdClass | |
250 | */ | |
251 | public static function get_providers_preferences($providers, $userid) { | |
252 | $preferences = new \stdClass(); | |
253 | ||
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 == '') { | |
260 | continue; | |
261 | } | |
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; | |
266 | } | |
267 | } | |
268 | } | |
269 | ||
270 | return $preferences; | |
271 | } | |
272 | ||
273 | /** | |
274 | * Requires the JS libraries for the toggle contact button. | |
275 | * | |
276 | * @return void | |
277 | */ | |
278 | public static function togglecontact_requirejs() { | |
279 | global $PAGE; | |
280 | ||
281 | static $done = false; | |
282 | if ($done) { | |
283 | return; | |
284 | } | |
285 | ||
286 | $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button')); | |
287 | $done = true; | |
288 | } | |
289 | ||
290 | /** | |
291 | * Returns the attributes to place on a contact button. | |
292 | * | |
293 | * @param object $user User object. | |
294 | * @param bool $iscontact | |
295 | * @return array | |
296 | */ | |
297 | public static function togglecontact_link_params($user, $iscontact = false) { | |
298 | $params = array( | |
299 | 'data-userid' => $user->id, | |
300 | 'data-is-contact' => $iscontact, | |
301 | 'id' => 'toggle-contact-button', | |
302 | 'role' => 'button', | |
303 | 'class' => 'ajax-contact-button', | |
304 | ); | |
305 | ||
306 | return $params; | |
307 | } | |
ffd7798c | 308 | |
b2cd17e6 MN |
309 | /** |
310 | * Returns the conversation hash between users for easy look-ups in the DB. | |
311 | * | |
312 | * @param array $userids | |
313 | * @return string | |
314 | */ | |
315 | public static function get_conversation_hash(array $userids) { | |
316 | sort($userids); | |
317 | ||
318 | return sha1(implode('-', $userids)); | |
319 | } | |
320 | ||
ffd7798c MN |
321 | /** |
322 | * Returns the cache key for the time created value of the last message between two users. | |
323 | * | |
324 | * @param int $userid | |
325 | * @param int $user2id | |
326 | * @return string | |
327 | */ | |
328 | public static function get_last_message_time_created_cache_key($userid, $user2id) { | |
329 | $ids = [$userid, $user2id]; | |
330 | sort($ids); | |
331 | return implode('_', $ids); | |
332 | } | |
eda6bc19 | 333 | } |