MDL-64066 behat: more fixes to dialog names, old "add" steps
[moodle.git] / message / index.php
blob090381d3a74caf212289f929f879b5f4ef5b74fc
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.
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/>.
17 /**
18 * A page displaying the user's contacts and messages
20 * @package core_message
21 * @copyright 2010 Andrew Davis
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 require_once('../config.php');
27 require_login(null, false);
29 if (isguestuser()) {
30 redirect($CFG->wwwroot);
33 if (empty($CFG->messaging)) {
34 print_error('disabled', 'message');
37 // The id of the user we want to view messages from.
38 $id = optional_param('id', 0, PARAM_INT);
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);
44 $contactsfirst = optional_param('contactsfirst', 0, PARAM_INT);
46 $url = new moodle_url('/message/index.php');
47 if ($id) {
48 $url->param('id', $id);
49 } else {
50 if ($user1id) {
51 $url->param('user1', $user1id);
53 if ($user2id) {
54 $url->param('user2', $user2id);
56 if ($contactsfirst) {
57 $url->param('contactsfirst', $contactsfirst);
60 $PAGE->set_url($url);
62 $user1 = null;
63 $currentuser = true;
64 if ($user1id != $USER->id) {
65 $user1 = core_user::get_user($user1id, '*', MUST_EXIST);
66 $currentuser = false;
67 } else {
68 $user1 = $USER;
71 $user2 = null;
72 if (!empty($user2id)) {
73 $user2 = core_user::get_user($user2id, '*', MUST_EXIST);
76 $user2realuser = !empty($user2) && core_user::is_real_user($user2->id);
77 $systemcontext = context_system::instance();
78 if ($currentuser === false && !has_capability('moodle/site:readallmessages', $systemcontext)) {
79 print_error('accessdenied', 'admin');
82 $PAGE->set_context(context_user::instance($user1->id));
83 $PAGE->set_pagelayout('standard');
84 $strmessages = get_string('messages', 'message');
85 if ($user2realuser) {
86 $user2fullname = fullname($user2);
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");
95 // Remove the user node from the main navigation for this page.
96 $usernode = $PAGE->navigation->find('users', null);
97 $usernode->remove();
99 $settings = $PAGE->settingsnav->find('messages', null);
100 $settings->make_active();
102 if ($currentuser) {
103 // We're in the pprocess of deprecating this page however we haven't replaced the functionality
104 // for the admin (or user with correct capabilities) to view other user's conversations. For the
105 // time being this page will simply open the message drawer unless it's the admin user case just
106 // mentioned. In that case we will render the old UI for backwards compatibility.
107 echo $OUTPUT->header();
108 echo $OUTPUT->heading(get_string('messages', 'message'));
109 $conversationid = empty($user2id) ? null : \core_message\api::get_conversation_between_users([$USER->id, $user2id]);
110 if (empty($conversationid) && !empty($user2id)) {
111 $PAGE->requires->js_call_amd('core_message/message_drawer_helper', 'createConversationWithUser', [$user2id]);
112 } else if (!empty($conversationid)) {
113 $PAGE->requires->js_call_amd('core_message/message_drawer_helper', 'showConversation', [$conversationid]);
114 } else {
115 $PAGE->requires->js_call_amd('core_message/message_drawer_helper', 'show');
117 echo $OUTPUT->footer();
118 exit();
121 // The only time we should get here is if it's an admin type user viewing another user's messages.
123 // Get the renderer and the information we are going to be use.
124 $renderer = $PAGE->get_renderer('core_message');
125 $requestedconversation = false;
126 if ($contactsfirst) {
127 $conversations = \core_message\api::get_contacts($user1->id, 0, 20);
128 } else {
129 $conversations = \core_message\api::get_conversations($user1->id, 0, 20);
131 // Format the conversations in the legacy style, as the get_conversations method has since been changed.
132 $conversations = \core_message\helper::get_conversations_legacy_formatter($conversations);
134 $messages = [];
135 if (!$user2realuser) {
136 // If there are conversations, but the user has not chosen a particular one, then render the most recent one.
137 $user2 = new stdClass();
138 $user2->id = null;
139 if (!empty($conversations)) {
140 $contact = reset($conversations);
141 $user2->id = $contact->userid;
143 } else {
144 // The user has specifically requested to see a conversation. Add the flag to
145 // the context so that we can render the messaging app appropriately - this is
146 // used for smaller screens as it allows the UI to be responsive.
147 $requestedconversation = true;
150 // Mark the conversation as read.
151 if (!empty($user2->id)) {
152 $hasbeenreadallmessages = false;
153 if ($currentuser && isset($conversations[$user2->id])) {
154 // Mark the conversation we are loading as read.
155 if ($conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id])) {
156 \core_message\api::mark_all_messages_as_read($user1->id, $conversationid);
157 $hasbeenreadallmessages = true;
160 // Ensure the UI knows it's read as well.
161 $conversations[$user2->id]->isread = 1;
164 // Get the conversationid.
165 if (!isset($conversationid)) {
166 if (!$conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id])) {
167 // If the individual conversationid doesn't exist, create it.
168 $conversation = \core_message\api::create_conversation(
169 \core_message\api::MESSAGE_CONVERSATION_TYPE_INDIVIDUAL,
170 [$user1->id, $user2->id]
172 $conversationid = $conversation->id;
176 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversationid, 0, 20, 'timecreated DESC');
177 $messages = [];
178 if (!empty($convmessages)) {
179 $messages = $convmessages['messages'];
181 // Parse the messages to add missing fields for backward compatibility.
182 $messages = array_reverse($messages);
183 // Keeps track of the last day, month and year combo we were viewing.
184 $day = '';
185 $month = '';
186 $year = '';
187 foreach ($messages as $message) {
188 // Add useridto.
189 if (empty($message->useridto)) {
190 if ($message->useridfrom == $user1->id) {
191 $message->useridto = $user2->id;
192 } else {
193 $message->useridto = $user1->id;
197 // Add currentuserid.
198 $message->currentuserid = $USER->id;
200 // Check if we are now viewing a different block period.
201 $message->displayblocktime = false;
202 $date = usergetdate($message->timecreated);
203 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
204 $day = $date['mday'];
205 $month = $date['month'];
206 $year = $date['year'];
207 $message->displayblocktime = true;
208 $message->blocktime = userdate($message->timecreated, get_string('strftimedaydate'));
211 // We don't have this information here so, for now, we leave an empty value or the current time.
212 // This is a temporary solution because a new UI is being built in MDL-63303.
213 $message->timeread = 0;
214 if ($hasbeenreadallmessages && $message->useridfrom != $user1->id) {
215 // As all the messages sent by the other user have been marked as read previously, we will change
216 // timeread to the current time to avoid the last message will be duplicated after calling to the
217 // core_message_data_for_messagearea_messages via javascript.
218 // We only need to change that to the other user, because for the current user, messages are always
219 // marked as unread.
220 $message->timeread = time();
226 $pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;
227 $pollmax = !empty($CFG->messagingmaxpoll) ? $CFG->messagingmaxpoll : MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS;
228 $polltimeout = !empty($CFG->messagingtimeoutpoll) ? $CFG->messagingtimeoutpoll : MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS;
229 $messagearea = new \core_message\output\messagearea\message_area($user1->id, $user2->id, $conversations, $messages,
230 $requestedconversation, $contactsfirst, $pollmin, $pollmax, $polltimeout);
232 // Now the page contents.
233 echo $OUTPUT->header();
234 echo $OUTPUT->heading(get_string('messages', 'message'));
236 // Display a message if the messages have not been migrated yet.
237 if (!get_user_preferences('core_message_migrate_data', false, $user1id)) {
238 $notify = new \core\output\notification(get_string('messagingdatahasnotbeenmigrated', 'message'),
239 \core\output\notification::NOTIFY_WARNING);
240 echo $OUTPUT->render($notify);
243 // Display a message that the user is viewing someone else's messages.
244 if (!$currentuser) {
245 $notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),
246 \core\output\notification::NOTIFY_WARNING);
247 echo $OUTPUT->render($notify);
249 echo $renderer->render($messagearea);
250 echo $OUTPUT->footer();