MDL-55635 message: create notifications page
[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 /**
75 * Helper function to return an array of messages renderables to display in the message area.
76 *
77 * @param int $userid
78 * @param array $messages
1e5f751f 79 * @return \core_message\output\messagearea\message[]
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;
1e5f751f 116 $arrmessages[] = new \core_message\output\messagearea\message($msg);
879e2bef
MN
117 }
118
119 return $arrmessages;
120 }
121
122 /**
123 * Helper function for creating a contact renderable.
124 *
125 * @param \stdClass $contact
cd03b8d7 126 * @param string $prefix
1e5f751f 127 * @return \core_message\output\messagearea\contact
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;
879e2bef 150 $data->lastmessage = $contact->smallmessage;
89a70ba1 151 $data->useridfrom = $contact->useridfrom;
cd03b8d7
MN
152 if (isset($contact->messageid)) {
153 $data->messageid = $contact->messageid;
154 }
879e2bef
MN
155 }
156 // Check if the user is online.
7b55aaa1 157 $data->isonline = self::is_online($userfields->lastaccess);
dd0c1403 158 $data->isblocked = isset($contact->blocked) ? $contact->blocked : 0;
eda6bc19 159 $data->isread = isset($contact->isread) ? $contact->isread : 0;
c33b7d89 160 $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
879e2bef 161
1e5f751f 162 return new \core_message\output\messagearea\contact($data);
879e2bef 163 }
bf58081d
MN
164
165 /**
166 * Helper function for checking the time meets the 'online' condition.
167 *
168 * @param int $lastaccess
169 * @return boolean
170 */
171 public static function is_online($lastaccess) {
172 global $CFG;
173
174 // Variable to check if we consider this user online or not.
175 $timetoshowusers = 300; // Seconds default.
176 if (isset($CFG->block_online_users_timetosee)) {
177 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
178 }
179 $time = time() - $timetoshowusers;
180
181 return $lastaccess >= $time;
182 }
79f6c36c
MN
183
184 /**
185 * Get providers preferences.
186 *
187 * @param array $providers
188 * @param int $userid
189 * @return \stdClass
190 */
191 public static function get_providers_preferences($providers, $userid) {
192 $preferences = new \stdClass();
193
194 // Get providers preferences.
195 foreach ($providers as $provider) {
196 foreach (array('loggedin', 'loggedoff') as $state) {
197 $linepref = get_user_preferences('message_provider_' . $provider->component . '_' . $provider->name
198 . '_' . $state, '', $userid);
199 if ($linepref == '') {
200 continue;
201 }
202 $lineprefarray = explode(',', $linepref);
203 $preferences->{$provider->component.'_'.$provider->name.'_'.$state} = array();
204 foreach ($lineprefarray as $pref) {
205 $preferences->{$provider->component.'_'.$provider->name.'_'.$state}[$pref] = 1;
206 }
207 }
208 }
209
210 return $preferences;
211 }
212
213 /**
214 * Requires the JS libraries for the toggle contact button.
215 *
216 * @return void
217 */
218 public static function togglecontact_requirejs() {
219 global $PAGE;
220
221 static $done = false;
222 if ($done) {
223 return;
224 }
225
226 $PAGE->requires->js_call_amd('core_message/toggle_contact_button', 'enhance', array('#toggle-contact-button'));
227 $done = true;
228 }
229
230 /**
231 * Returns the attributes to place on a contact button.
232 *
233 * @param object $user User object.
234 * @param bool $iscontact
235 * @return array
236 */
237 public static function togglecontact_link_params($user, $iscontact = false) {
238 $params = array(
239 'data-userid' => $user->id,
240 'data-is-contact' => $iscontact,
241 'id' => 'toggle-contact-button',
242 'role' => 'button',
243 'class' => 'ajax-contact-button',
244 );
245
246 return $params;
247 }
eda6bc19 248}