MDL-55623 core_message: create helper function for WS to define messages
[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 */
dec0cd99 48 public static function get_messages($userid, $otheruserid, $timedeleted = 0, $limitfrom = 0, $limitnum = 0, $sort = 'timecreated ASC') {
879e2bef
MN
49 global $DB;
50
51 $sql = "SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
52 smallmessage, notification, timecreated, 0 as timeread
53 FROM {message} m
dec0cd99
MN
54 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = ?)
55 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = ?))
879e2bef
MN
56 AND notification = 0
57 UNION ALL
58 SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
59 smallmessage, notification, timecreated, timeread
60 FROM {message_read} mr
dec0cd99
MN
61 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = ?)
62 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = ?))
879e2bef
MN
63 AND notification = 0
64 ORDER BY $sort";
dec0cd99
MN
65 $params = array($userid, $otheruserid, $timedeleted,
66 $otheruserid, $userid, $timedeleted,
67 $userid, $otheruserid, $timedeleted,
68 $otheruserid, $userid, $timedeleted);
879e2bef
MN
69
70 return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
71 }
72
73 /**
74 * Helper function to return an array of messages renderables to display in the message area.
75 *
76 * @param int $userid
77 * @param array $messages
1e5f751f 78 * @return \core_message\output\messagearea\message[]
879e2bef
MN
79 */
80 public static function create_messages($userid, $messages) {
81 // Store the messages.
82 $arrmessages = array();
83
8ec78c48
MN
84 // We always view messages from oldest to newest, ensure we have it in that order.
85 $lastmessage = end($messages);
86 $firstmessage = reset($messages);
87 if ($lastmessage->timecreated < $firstmessage->timecreated) {
88 $messages = array_reverse($messages);
89 }
90
879e2bef
MN
91 // Keeps track of the last day, month and year combo we were viewing.
92 $day = '';
93 $month = '';
94 $year = '';
95 foreach ($messages as $message) {
96 // Check if we are now viewing a different block period.
3090f52f 97 $displayblocktime = false;
879e2bef
MN
98 $date = usergetdate($message->timecreated);
99 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
100 $day = $date['mday'];
101 $month = $date['month'];
102 $year = $date['year'];
3090f52f 103 $displayblocktime = true;
879e2bef
MN
104 }
105 // Store the message to pass to the renderable.
106 $msg = new \stdClass();
3090f52f 107 $msg->id = $message->id;
879e2bef
MN
108 $msg->text = message_format_message_text($message);
109 $msg->currentuserid = $userid;
110 $msg->useridfrom = $message->useridfrom;
111 $msg->useridto = $message->useridto;
3090f52f 112 $msg->displayblocktime = $displayblocktime;
879e2bef 113 $msg->timecreated = $message->timecreated;
3090f52f 114 $msg->timeread = $message->timeread;
1e5f751f 115 $arrmessages[] = new \core_message\output\messagearea\message($msg);
879e2bef
MN
116 }
117
118 return $arrmessages;
119 }
120
121 /**
122 * Helper function for creating a contact renderable.
123 *
124 * @param \stdClass $contact
cd03b8d7 125 * @param string $prefix
1e5f751f 126 * @return \core_message\output\messagearea\contact
879e2bef 127 */
cd03b8d7 128 public static function create_contact($contact, $prefix = '') {
bf58081d 129 global $PAGE;
879e2bef
MN
130
131 // Create the data we are going to pass to the renderable.
cd03b8d7 132 $userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix);
879e2bef
MN
133 $data = new \stdClass();
134 $data->userid = $userfields->id;
135 $data->fullname = fullname($userfields);
136 // Get the user picture data.
137 $userpicture = new \user_picture($userfields);
138 $userpicture->size = 1; // Size f1.
139 $data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
140 $userpicture->size = 0; // Size f2.
141 $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
142 // Store the message if we have it.
5bf0ff27 143 $data->ismessaging = false;
cd03b8d7
MN
144 $data->lastmessage = null;
145 $data->messageid = null;
879e2bef 146 if (isset($contact->smallmessage)) {
5bf0ff27 147 $data->ismessaging = true;
879e2bef 148 $data->lastmessage = $contact->smallmessage;
cd03b8d7
MN
149 if (isset($contact->messageid)) {
150 $data->messageid = $contact->messageid;
151 }
879e2bef
MN
152 }
153 // Check if the user is online.
bf58081d 154 $data->isonline = \core_message\helper::is_online($userfields->lastaccess);
dd0c1403 155 $data->isblocked = isset($contact->blocked) ? $contact->blocked : 0;
eda6bc19 156 $data->isread = isset($contact->isread) ? $contact->isread : 0;
c33b7d89 157 $data->unreadcount = isset($contact->unreadcount) ? $contact->unreadcount : null;
879e2bef 158
1e5f751f 159 return new \core_message\output\messagearea\contact($data);
879e2bef 160 }
bf58081d
MN
161
162 /**
163 * Helper function for checking the time meets the 'online' condition.
164 *
165 * @param int $lastaccess
166 * @return boolean
167 */
168 public static function is_online($lastaccess) {
169 global $CFG;
170
171 // Variable to check if we consider this user online or not.
172 $timetoshowusers = 300; // Seconds default.
173 if (isset($CFG->block_online_users_timetosee)) {
174 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
175 }
176 $time = time() - $timetoshowusers;
177
178 return $lastaccess >= $time;
179 }
eda6bc19 180}