MDL-54687 core_message: created page for new messaging interface
[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
42 * @param int $limitfrom
43 * @param int $limitnum
44 * @param string $sort
45 * @return array of messages
46 */
47 public static function get_messages($userid, $otheruserid, $limitfrom = 0, $limitnum = 0, $sort = 'timecreated ASC') {
48 global $DB;
49
50 $sql = "SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
51 smallmessage, notification, timecreated, 0 as timeread
52 FROM {message} m
53 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0)
54 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))
55 AND notification = 0
56 UNION ALL
57 SELECT id, useridfrom, useridto, subject, fullmessage, fullmessagehtml, fullmessageformat,
58 smallmessage, notification, timecreated, timeread
59 FROM {message_read} mr
60 WHERE ((useridto = ? AND useridfrom = ? AND timeusertodeleted = 0)
61 OR (useridto = ? AND useridfrom = ? AND timeuserfromdeleted = 0))
62 AND notification = 0
63 ORDER BY $sort";
64 $params = array($userid, $otheruserid, $otheruserid, $userid,
65 $userid, $otheruserid, $otheruserid, $userid);
66
67 return $DB->get_records_sql($sql, $params, $limitfrom, $limitnum);
68 }
69
70 /**
71 * Helper function to return an array of messages renderables to display in the message area.
72 *
73 * @param int $userid
74 * @param array $messages
75 * @return \core_message\output\message[]
76 */
77 public static function create_messages($userid, $messages) {
78 // Store the messages.
79 $arrmessages = array();
80
81 // Keeps track of the last day, month and year combo we were viewing.
82 $day = '';
83 $month = '';
84 $year = '';
85 foreach ($messages as $message) {
86 // Check if we are now viewing a different block period.
87 $blocktime = null;
88 $date = usergetdate($message->timecreated);
89 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
90 $day = $date['mday'];
91 $month = $date['month'];
92 $year = $date['year'];
93 $blocktime = userdate($message->timecreated, get_string('strftimedaydate'));
94 }
95 // Store the message to pass to the renderable.
96 $msg = new \stdClass();
97 $msg->text = message_format_message_text($message);
98 $msg->currentuserid = $userid;
99 $msg->useridfrom = $message->useridfrom;
100 $msg->useridto = $message->useridto;
101 $msg->blocktime = $blocktime;
102 $msg->timecreated = $message->timecreated;
103 $arrmessages[] = new \core_message\output\message($msg);
104 }
105
106 return $arrmessages;
107 }
108
109 /**
110 * Helper function for creating a contact renderable.
111 *
112 * @param \stdClass $contact
113 * @return \core_message\output\contact
114 */
115 public static function create_contact($contact) {
116 global $CFG, $PAGE;
117
118 // Variables to check if we consider this user online or not.
119 $timetoshowusers = 300; // Seconds default.
120 if (isset($CFG->block_online_users_timetosee)) {
121 $timetoshowusers = $CFG->block_online_users_timetosee * 60;
122 }
123 $time = time() - $timetoshowusers;
124
125 // Create the data we are going to pass to the renderable.
126 $userfields = \user_picture::unalias($contact, array('lastaccess'));
127 $data = new \stdClass();
128 $data->userid = $userfields->id;
129 $data->fullname = fullname($userfields);
130 // Get the user picture data.
131 $userpicture = new \user_picture($userfields);
132 $userpicture->size = 1; // Size f1.
133 $data->profileimageurl = $userpicture->get_url($PAGE)->out(false);
134 $userpicture->size = 0; // Size f2.
135 $data->profileimageurlsmall = $userpicture->get_url($PAGE)->out(false);
136 // Store the message if we have it.
137 if (isset($contact->smallmessage)) {
138 $data->lastmessage = $contact->smallmessage;
139 } else {
140 $data->lastmessage = null;
141 }
142 // Check if the user is online.
143 $data->isonline = $userfields->lastaccess >= $time;
144
145 return new \core_message\output\contact($data);
146 }
147}