Merge branch 'wip-mdl-55074-fix' of https://github.com/rajeshtaneja/moodle
[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
46 * @return array of messages
47 */
7b55aaa1
MN
48 public static function get_messages($userid, $otheruserid, $timedeleted = 0, $limitfrom = 0, $limitnum = 0,
49 $sort = 'timecreated ASC') {
879e2bef
MN
50 global $DB;
51
52 $sql = "SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
53 smallmessage, notification, timecreated, 0 as timeread
54 FROM {message} m
dec0cd99
MN
55 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = ?)
56 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = ?))
879e2bef
MN
57 AND notification = 0
58 UNION ALL
59 SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
60 smallmessage, notification, timecreated, timeread
61 FROM {message_read} mr
dec0cd99
MN
62 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = ?)
63 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = ?))
879e2bef
MN
64 AND notification = 0
65 ORDER BY $sort";
dec0cd99
MN
66 $params = array($userid, $otheruserid, $timedeleted,
67 $otheruserid, $userid, $timedeleted,
68 $userid, $otheruserid, $timedeleted,
69 $otheruserid, $userid, $timedeleted);
879e2bef
MN
70
71 return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
72 }
73
74 /**
de55cb1b 75 * Helper function to return an array of messages.
879e2bef
MN
76 *
77 * @param int $userid
78 * @param array $messages
de55cb1b 79 * @return array
879e2bef
MN
80 */
81 public static function create_messages($userid, $messages) {
82 // Store the messages.
83 $arrmessages = array();
84
8ec78c48
MN
85 // We always view messages from oldest to newest, ensure we have it in that order.
86 $lastmessage = end($messages);
87 $firstmessage = reset($messages);
88 if ($lastmessage->timecreated < $firstmessage->timecreated) {
89 $messages = array_reverse($messages);
90 }
91
879e2bef
MN
92 // Keeps track of the last day, month and year combo we were viewing.
93 $day = '';
94 $month = '';
95 $year = '';
96 foreach ($messages as $message) {
97 // Check if we are now viewing a different block period.
3090f52f 98 $displayblocktime = false;
879e2bef
MN
99 $date = usergetdate($message->timecreated);
100 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
101 $day = $date['mday'];
102 $month = $date['month'];
103 $year = $date['year'];
3090f52f 104 $displayblocktime = true;
879e2bef
MN
105 }
106 // Store the message to pass to the renderable.
107 $msg = new \stdClass();
3090f52f 108 $msg->id = $message->id;
879e2bef
MN
109 $msg->text = message_format_message_text($message);
110 $msg->currentuserid = $userid;
111 $msg->useridfrom = $message->useridfrom;
112 $msg->useridto = $message->useridto;
3090f52f 113 $msg->displayblocktime = $displayblocktime;
879e2bef 114 $msg->timecreated = $message->timecreated;
3090f52f 115 $msg->timeread = $message->timeread;
de55cb1b 116 $arrmessages[] = $msg;
879e2bef
MN
117 }
118
119 return $arrmessages;
120 }
121
122 /**
de55cb1b 123 * Helper function for creating a contact object.
879e2bef
MN
124 *
125 * @param \stdClass $contact
cd03b8d7 126 * @param string $prefix
de55cb1b 127 * @return \stdClass
879e2bef 128 */
cd03b8d7 129 public static function create_contact($contact, $prefix = '') {
bf58081d 130 global $PAGE;
879e2bef
MN
131
132 // Create the data we are going to pass to the renderable.
cd03b8d7 133 $userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix);
879e2bef
MN
134 $data = new \stdClass();
135 $data->userid = $userfields->id;
89a70ba1 136 $data->useridfrom = null;
879e2bef
MN
137 $data->fullname = fullname($userfields);
138 // Get the user picture data.
139 $userpicture = new \user_picture($userfields);
140 $userpicture->size = 1; // Size f1.
141 $data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
142 $userpicture->size = 0; // Size f2.
143 $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
144 // Store the message if we have it.
5bf0ff27 145 $data->ismessaging = false;
cd03b8d7
MN
146 $data->lastmessage = null;
147 $data->messageid = null;
879e2bef 148 if (isset($contact->smallmessage)) {
5bf0ff27 149 $data->ismessaging = true;
54d83992
RW
150 // Strip the HTML tags from the message for displaying in the contact area.
151 $data->lastmessage = clean_param($contact->smallmessage, PARAM_NOTAGS);
89a70ba1 152 $data->useridfrom = $contact->useridfrom;
cd03b8d7
MN
153 if (isset($contact->messageid)) {
154 $data->messageid = $contact->messageid;
155 }
879e2bef
MN
156 }
157 // Check if the user is online.
7b55aaa1 158 $data->isonline = self::is_online($userfields->lastaccess);
1f64514d
MN
159 $data->isblocked = isset($contact->blocked) ? (bool) $contact->blocked : false;
160 $data->isread = isset($contact->isread) ? (bool) $contact->isread : false;
c33b7d89 161 $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
879e2bef 162
de55cb1b 163 return $data;
879e2bef 164 }
bf58081d
MN
165
166 /**
167 * Helper function for checking the time meets the 'online' condition.
168 *
169 * @param int $lastaccess
170 * @return boolean
171 */
172 public static function is_online($lastaccess) {
173 global $CFG;
174
175 // Variable to check if we consider this user online or not.
176 $timetoshowusers = 300; // Seconds default.
177 if (isset($CFG->block_online_users_timetosee)) {
178 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
179 }
180 $time = time() - $timetoshowusers;
181
182 return $lastaccess >= $time;
183 }
79f6c36c
MN
184
185 /**
186 * Get providers preferences.
187 *
188 * @param array $providers
189 * @param int $userid
190 * @return \stdClass
191 */
192 public static function get_providers_preferences($providers, $userid) {
193 $preferences = new \stdClass();
194
195 // Get providers preferences.
196 foreach ($providers as $provider) {
197 foreach (array('loggedin', 'loggedoff') as $state) {
198 $linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name
199 . '_' . $state, '', $userid);
200 if ($linepref == '') {
201 continue;
202 }
203 $lineprefarray = explode(',', $linepref);
204 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
205 foreach ($lineprefarray as $pref) {
206 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
207 }
208 }
209 }
210
211 return $preferences;
212 }
213
214 /**
215 * Requires the JS libraries for the toggle contact button.
216 *
217 * @return void
218 */
219 public static function togglecontact_requirejs() {
220 global $PAGE;
221
222 static $done = false;
223 if ($done) {
224 return;
225 }
226
227 $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
228 $done = true;
229 }
230
231 /**
232 * Returns the attributes to place on a contact button.
233 *
234 * @param object $user User object.
235 * @param bool $iscontact
236 * @return array
237 */
238 public static function togglecontact_link_params($user, $iscontact = false) {
239 $params = array(
240 'data-userid' => $user->id,
241 'data-is-contact' => $iscontact,
242 'id' => 'toggle-contact-button',
243 'role' => 'button',
244 'class' => 'ajax-contact-button',
245 );
246
247 return $params;
248 }
eda6bc19 249}