Merge branch 'MDL-63968-master' of git://github.com/andrewnicols/moodle
[moodle.git] / message / index.php
blobe3f1083e710ef567345068b5708ae629b9e24b74
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 // Get the renderer and the information we are going to be use.
103 $renderer = $PAGE->get_renderer('core_message');
104 $requestedconversation = false;
105 if ($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);
110 // Format the conversations in the legacy style, as the get_conversations method has since been changed.
111 $conversations = \core_message\helper::get_conversations_legacy_formatter($conversations);
113 $messages = [];
114 if (!$user2realuser) {
115 // If there are conversations, but the user has not chosen a particular one, then render the most recent one.
116 $user2 = new stdClass();
117 $user2->id = null;
118 if (!empty($conversations)) {
119 $contact = reset($conversations);
120 $user2->id = $contact->userid;
122 } else {
123 // The user has specifically requested to see a conversation. Add the flag to
124 // the context so that we can render the messaging app appropriately - this is
125 // used for smaller screens as it allows the UI to be responsive.
126 $requestedconversation = true;
129 // Mark the conversation as read.
130 if (!empty($user2->id)) {
131 if ($currentuser && isset($conversations[$user2->id])) {
132 // Mark the conversation we are loading as read.
133 if ($conversationid = \core_message\api::get_conversation_between_users([$user1->id, $user2->id])) {
134 \core_message\api::mark_all_messages_as_read($user1->id, $conversationid);
137 // Ensure the UI knows it's read as well.
138 $conversations[$user2->id]->isread = 1;
141 // Get the conversationid.
142 if (!isset($conversationid)) {
143 if (!$conversationid = self::get_conversation_between_users($userids)) {
144 // If the conversationid doesn't exist, throw an exception.
145 throw new moodle_exception('conversationdoesntexist', 'core_message');
149 $convmessages = \core_message\api::get_conversation_messages($user1->id, $conversationid, 0, 20, 'timecreated DESC');
150 $messages = $convmessages['messages'];
152 // Keeps track of the last day, month and year combo we were viewing.
153 $day = '';
154 $month = '';
155 $year = '';
157 // Parse the messages to add missing fields for backward compatibility.
158 $messages = array_reverse($messages);
159 $day = '';
160 $month = '';
161 $year = '';
162 foreach ($messages as $message) {
163 // Add useridto.
164 if (empty($message->useridto)) {
165 if ($message->useridfrom == $user1->id) {
166 $message->useridto = $user2->id;
167 } else {
168 $message->useridto = $user1->id;
172 // Add currentuserid.
173 $message->currentuserid = $USER->id;
175 // Check if we are now viewing a different block period.
176 $message->displayblocktime = false;
177 $date = usergetdate($message->timecreated);
178 if ($day != $date['mday'] || $month != $date['month'] || $year != $date['year']) {
179 $day = $date['mday'];
180 $month = $date['month'];
181 $year = $date['year'];
182 $message->displayblocktime = true;
183 $message->blocktime = userdate($message->timecreated, get_string('strftimedaydate'));
186 // We don't have this information here so, for now, we leave an empty value.
187 // This is a temporary solution because a new UI is being built in MDL-63303.
188 $message->timeread = 0;
192 $pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;
193 $pollmax = !empty($CFG->messagingmaxpoll) ? $CFG->messagingmaxpoll : MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS;
194 $polltimeout = !empty($CFG->messagingtimeoutpoll) ? $CFG->messagingtimeoutpoll : MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS;
195 $messagearea = new \core_message\output\messagearea\message_area($user1->id, $user2->id, $conversations, $messages,
196 $requestedconversation, $contactsfirst, $pollmin, $pollmax, $polltimeout);
198 // Now the page contents.
199 echo $OUTPUT->header();
200 echo $OUTPUT->heading(get_string('messages', 'message'));
202 // Display a message if the messages have not been migrated yet.
203 if (!get_user_preferences('core_message_migrate_data', false, $user1id)) {
204 $notify = new \core\output\notification(get_string('messagingdatahasnotbeenmigrated', 'message'),
205 \core\output\notification::NOTIFY_WARNING);
206 echo $OUTPUT->render($notify);
209 // Display a message that the user is viewing someone else's messages.
210 if (!$currentuser) {
211 $notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),
212 \core\output\notification::NOTIFY_WARNING);
213 echo $OUTPUT->render($notify);
215 echo $renderer->render($messagearea);
216 echo $OUTPUT->footer();