MDL-63466 core_message: deprecated methods and services
[moodle.git] / message / index.php
CommitLineData
eb5334ff 1<?php
eb5334ff 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/**
affefa8c 18 * A page displaying the user's contacts and messages
eb5334ff 19 *
6fbd60ef
AD
20 * @package core_message
21 * @copyright 2010 Andrew Davis
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
eb5334ff 23 */
24
02f2c7bd 25require_once('../config.php');
eb5334ff 26
66e1bdda 27require_login(null, false);
eb5334ff 28
4f0c2d00 29if (isguestuser()) {
eb5334ff 30 redirect($CFG->wwwroot);
31}
32
33if (empty($CFG->messaging)) {
34 print_error('disabled', 'message');
35}
cdf39255 36
66e1bdda
MN
37// The id of the user we want to view messages from.
38$id = optional_param('id', 0, PARAM_INT);
bcab42da 39
66e1bdda
MN
40// It's possible for someone with the right capabilities to view a conversation between two other users. For BC
41// we are going to accept other URL parameters to figure this out.
42$user1id = optional_param('user1', $USER->id, PARAM_INT);
43$user2id = optional_param('user2', $id, PARAM_INT);
81517613 44$contactsfirst = optional_param('contactsfirst', 0, PARAM_INT);
02f2c7bd 45
66e1bdda
MN
46$url = new moodle_url('/message/index.php');
47if ($id) {
48 $url->param('id', $id);
49} else {
50 if ($user1id) {
51 $url->param('user1', $user1id);
52 }
53 if ($user2id) {
54 $url->param('user2', $user2id);
08cd70cf 55 }
81517613
AG
56 if ($contactsfirst) {
57 $url->param('contactsfirst', $contactsfirst);
58 }
eb5334ff 59}
02f2c7bd 60$PAGE->set_url($url);
3fca9299 61
02f2c7bd
AD
62$user1 = null;
63$currentuser = true;
bcab42da 64if ($user1id != $USER->id) {
66e1bdda
MN
65 $user1 = core_user::get_user($user1id, '*', MUST_EXIST);
66 $currentuser = false;
02f2c7bd
AD
67} else {
68 $user1 = $USER;
eb5334ff 69}
e8e2d7f1 70
02f2c7bd
AD
71$user2 = null;
72if (!empty($user2id)) {
66e1bdda 73 $user2 = core_user::get_user($user2id, '*', MUST_EXIST);
02f2c7bd 74}
172186b8 75
3bcf6b3c 76$user2realuser = !empty($user2) && core_user::is_real_user($user2->id);
447df209 77$systemcontext = context_system::instance();
553319be 78if ($currentuser === false && !has_capability('moodle/site:readallmessages', $systemcontext)) {
66e1bdda 79 print_error('accessdenied', 'admin');
27a39763
AD
80}
81
880c5073 82$PAGE->set_context(context_user::instance($user1->id));
66e1bdda 83$PAGE->set_pagelayout('standard');
36d29c08
MN
84$strmessages = get_string('messages', 'message');
85if ($user2realuser) {
86 $user2fullname = fullname($user2);
87
88 $PAGE->set_title("$strmessages: $user2fullname");
89 $PAGE->set_heading("$strmessages: $user2fullname");
90} else {
91 $PAGE->set_title("{$SITE->shortname}: $strmessages");
92 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
93}
94
f495187d
AG
95// Remove the user node from the main navigation for this page.
96$usernode = $PAGE->navigation->find('users', null);
97$usernode->remove();
98
99$settings = $PAGE->settingsnav->find('messages', null);
879e2bef
MN
100$settings->make_active();
101
102// Get the renderer and the information we are going to be use.
103$renderer = $PAGE->get_renderer('core_message');
99c7f0a7 104$requestedconversation = false;
81517613
AG
105if ($contactsfirst) {
106 $conversations = \core_message\api::get_contacts($user1->id, 0, 20);
107} else {
108 $conversations = \core_message\api::get_conversations($user1->id, 0, 20);
109}
f5a174f4 110$messages = [];
879e2bef 111if (!$user2realuser) {
de55cb1b
MN
112 // If there are conversations, but the user has not chosen a particular one, then render the most recent one.
113 $user2 = new stdClass();
114 $user2->id = null;
115 if (!empty($conversations)) {
116 $contact = reset($conversations);
117 $user2->id = $contact->userid;
8c250260 118 }
f495187d 119} else {
de55cb1b
MN
120 // The user has specifically requested to see a conversation. Add the flag to
121 // the context so that we can render the messaging app appropriately - this is
122 // used for smaller screens as it allows the UI to be responsive.
123 $requestedconversation = true;
124}
125
126// Mark the conversation as read.
127if (!empty($user2->id)) {
703ab164 128 if ($currentuser && isset($conversations[$user2->id])) {
de55cb1b 129 // Mark the conversation we are loading as read.
74ad60bf
MN
130 if ($conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id])) {
131 \core_message\api::mark_all_messages_as_read($user1->id, $conversationid);
132 }
133
de55cb1b
MN
134 // Ensure the UI knows it's read as well.
135 $conversations[$user2->id]->isread = 1;
6c3b9541
MN
136 }
137
d1e8e69d
MN
138 // Get the conversationid.
139 if (!isset($conversationid)) {
140 if (!$conversationid = self::get_conversation_between_users($userids)) {
141 // If the conversationid doesn't exist, throw an exception.
142 throw new moodle_exception('conversationdoesntexist', 'core_message');
143 }
144 }
145
146 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversationid, 0, 20, 'timecreated DESC');
147 $messages = $convmessages['messages'];
148
149 // Keeps track of the last day, month and year combo we were viewing.
150 $day = '';
151 $month = '';
152 $year = '';
153
154 // Parse the messages to add missing fields for backward compatibility.
155 $messages = array_map(function($message) use ($user1, $user2, $USER, $day, $month, $year) {
156 // Add useridto.
157 if (empty($message->useridto)) {
158 if ($message->useridfrom == $user1->id) {
159 $message->useridto = $user2->id;
160 } else {
161 $message->useridto = $user1->id;
162 }
163 }
164
165 // Add currentuserid.
166 $message->currentuserid = $USER->id;
167
168 // Add displayblocktime.
169 $date = usergetdate($message->timecreated);
170 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
171 $day = $date['mday'];
172 $month = $date['month'];
173 $year = $date['year'];
174 $message->displayblocktime = true;
175 } else {
176 $message->displayblocktime = false;
177 }
178 // We don't have this information here so, for now, we leave an empty value.
179 // This is a temporary solution because a new UI is being built in MDL-63303.
180 $message->timeread = 0;
181
182 return $message;
183 }, $messages);
f495187d 184}
de55cb1b 185
ffd7798c
MN
186$pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;
187$pollmax = !empty($CFG->messagingmaxpoll) ? $CFG->messagingmaxpoll : MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS;
188$polltimeout = !empty($CFG->messagingtimeoutpoll) ? $CFG->messagingtimeoutpoll : MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS;
de55cb1b 189$messagearea = new \core_message\output\messagearea\message_area($user1->id, $user2->id, $conversations, $messages,
81517613 190 $requestedconversation, $contactsfirst, $pollmin, $pollmax, $polltimeout);
f495187d 191
879e2bef 192// Now the page contents.
02f2c7bd 193echo $OUTPUT->header();
66e1bdda 194echo $OUTPUT->heading(get_string('messages', 'message'));
7022b8d9
MN
195
196// Display a message if the messages have not been migrated yet.
197if (!get_user_preferences('core_message_migrate_data', false, $user1id)) {
198 $notify = new \core\output\notification(get_string('messagingdatahasnotbeenmigrated', 'message'),
199 \core\output\notification::NOTIFY_WARNING);
200 echo $OUTPUT->render($notify);
201}
202
879e2bef
MN
203// Display a message that the user is viewing someone else's messages.
204if (!$currentuser) {
205 $notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),
206 \core\output\notification::NOTIFY_WARNING);
207 echo $OUTPUT->render($notify);
02f2c7bd 208}
879e2bef 209echo $renderer->render($messagearea);
eb5334ff 210echo $OUTPUT->footer();