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 | |
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 | |
78 | * @return \core_message\output\message[] | |
79 | */ | |
80 | public static function create_messages($userid, $messages) { | |
81 | // Store the messages. | |
82 | $arrmessages = array(); | |
83 | ||
84 | // Keeps track of the last day, month and year combo we were viewing. | |
85 | $day = ''; | |
86 | $month = ''; | |
87 | $year = ''; | |
88 | foreach ($messages as $message) { | |
89 | // Check if we are now viewing a different block period. | |
3090f52f | 90 | $displayblocktime = false; |
879e2bef MN |
91 | $date = usergetdate($message->timecreated); |
92 | if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) { | |
93 | $day = $date['mday']; | |
94 | $month = $date['month']; | |
95 | $year = $date['year']; | |
3090f52f | 96 | $displayblocktime = true; |
879e2bef MN |
97 | } |
98 | // Store the message to pass to the renderable. | |
99 | $msg = new \stdClass(); | |
3090f52f | 100 | $msg->id = $message->id; |
879e2bef MN |
101 | $msg->text = message_format_message_text($message); |
102 | $msg->currentuserid = $userid; | |
103 | $msg->useridfrom = $message->useridfrom; | |
104 | $msg->useridto = $message->useridto; | |
3090f52f | 105 | $msg->displayblocktime = $displayblocktime; |
879e2bef | 106 | $msg->timecreated = $message->timecreated; |
3090f52f | 107 | $msg->timeread = $message->timeread; |
879e2bef MN |
108 | $arrmessages[] = new \core_message\output\message($msg); |
109 | } | |
110 | ||
111 | return $arrmessages; | |
112 | } | |
113 | ||
114 | /** | |
115 | * Helper function for creating a contact renderable. | |
116 | * | |
117 | * @param \stdClass $contact | |
118 | * @return \core_message\output\contact | |
119 | */ | |
120 | public static function create_contact($contact) { | |
121 | global $CFG, $PAGE; | |
122 | ||
123 | // Variables to check if we consider this user online or not. | |
124 | $timetoshowusers = 300; // Seconds default. | |
125 | if (isset($CFG->block_online_users_timetosee)) { | |
126 | $timetoshowusers = $CFG->block_online_users_timetosee * 60; | |
127 | } | |
128 | $time = time() - $timetoshowusers; | |
129 | ||
130 | // Create the data we are going to pass to the renderable. | |
131 | $userfields = \user_picture::unalias($contact, array('lastaccess')); | |
132 | $data = new \stdClass(); | |
133 | $data->userid = $userfields->id; | |
134 | $data->fullname = fullname($userfields); | |
135 | // Get the user picture data. | |
136 | $userpicture = new \user_picture($userfields); | |
137 | $userpicture->size = 1; // Size f1. | |
138 | $data->profileimageurl = $userpicture->get_url($PAGE)->out(false); | |
139 | $userpicture->size = 0; // Size f2. | |
140 | $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false); | |
141 | // Store the message if we have it. | |
142 | if (isset($contact->smallmessage)) { | |
143 | $data->lastmessage = $contact->smallmessage; | |
144 | } else { | |
145 | $data->lastmessage = null; | |
146 | } | |
147 | // Check if the user is online. | |
148 | $data->isonline = $userfields->lastaccess >= $time; | |
eda6bc19 | 149 | $data->isread = isset($contact->isread) ? $contact->isread : 0; |
879e2bef MN |
150 | |
151 | return new \core_message\output\contact($data); | |
152 | } | |
eda6bc19 | 153 | } |