Merge branch 'MDL-59015-master' of git://github.com/mihailges/moodle
[moodle.git] / message / index.php
blobac33533b8b02ae2bf2eff55d2ac4de85c151ca4f
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 $messages = [];
111 if (!$user2realuser) {
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;
119 } else {
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;
126 // Mark the conversation as read.
127 if (!empty($user2->id)) {
128 if ($currentuser && isset($conversations[$user2->id])) {
129 // Mark the conversation we are loading as read.
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);
134 // Ensure the UI knows it's read as well.
135 $conversations[$user2->id]->isread = 1;
138 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 20, 'timecreated DESC');
141 $pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;
142 $pollmax = !empty($CFG->messagingmaxpoll) ? $CFG->messagingmaxpoll : MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS;
143 $polltimeout = !empty($CFG->messagingtimeoutpoll) ? $CFG->messagingtimeoutpoll : MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS;
144 $messagearea = new \core_message\output\messagearea\message_area($user1->id, $user2->id, $conversations, $messages,
145 $requestedconversation, $contactsfirst, $pollmin, $pollmax, $polltimeout);
147 // Now the page contents.
148 echo $OUTPUT->header();
149 echo $OUTPUT->heading(get_string('messages', 'message'));
151 // Display a message if the messages have not been migrated yet.
152 if (!get_user_preferences('core_message_migrate_data', false, $user1id)) {
153 $notify = new \core\output\notification(get_string('messagingdatahasnotbeenmigrated', 'message'),
154 \core\output\notification::NOTIFY_WARNING);
155 echo $OUTPUT->render($notify);
158 // Display a message that the user is viewing someone else's messages.
159 if (!$currentuser) {
160 $notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),
161 \core\output\notification::NOTIFY_WARNING);
162 echo $OUTPUT->render($notify);
164 echo $renderer->render($messagearea);
165 echo $OUTPUT->footer();