2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
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);
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
);
45 $url = new moodle_url('/message/index.php');
47 $url->param('id', $id);
50 $url->param('user1', $user1id);
53 $url->param('user2', $user2id);
60 if ($user1id != $USER->id
) {
61 $user1 = core_user
::get_user($user1id, '*', MUST_EXIST
);
68 if (!empty($user2id)) {
69 $user2 = core_user
::get_user($user2id, '*', MUST_EXIST
);
72 $user2realuser = !empty($user2) && core_user
::is_real_user($user2->id
);
73 $systemcontext = context_system
::instance();
74 if ($currentuser === false && !has_capability('moodle/site:readallmessages', $systemcontext)) {
75 print_error('accessdenied', 'admin');
78 $PAGE->set_context(context_user
::instance($user1->id
));
79 $PAGE->set_pagelayout('standard');
80 $strmessages = get_string('messages', 'message');
82 $user2fullname = fullname($user2);
84 $PAGE->set_title("$strmessages: $user2fullname");
85 $PAGE->set_heading("$strmessages: $user2fullname");
87 $PAGE->set_title("{$SITE->shortname}: $strmessages");
88 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
91 // Remove the user node from the main navigation for this page.
92 $usernode = $PAGE->navigation
->find('users', null);
95 $settings = $PAGE->settingsnav
->find('messages', null);
96 $settings->make_active();
98 // Get the renderer and the information we are going to be use.
99 $renderer = $PAGE->get_renderer('core_message');
100 $requestedconversation = false;
101 $conversations = \core_message\api
::get_conversations($user1->id
, 0, 20);
103 if (!$user2realuser) {
104 // If there are conversations, but the user has not chosen a particular one, then render the most recent one.
105 $user2 = new stdClass();
107 if (!empty($conversations)) {
108 $contact = reset($conversations);
109 $user2->id
= $contact->userid
;
112 // The user has specifically requested to see a conversation. Add the flag to
113 // the context so that we can render the messaging app appropriately - this is
114 // used for smaller screens as it allows the UI to be responsive.
115 $requestedconversation = true;
118 // Mark the conversation as read.
119 if (!empty($user2->id
)) {
120 if ($currentuser && isset($conversations[$user2->id
])) {
121 // Mark the conversation we are loading as read.
122 \core_message\api
::mark_all_read_for_user($user1->id
, $user2->id
);
123 // Ensure the UI knows it's read as well.
124 $conversations[$user2->id
]->isread
= 1;
127 $messages = \core_message\api
::get_messages($user1->id
, $user2->id
, 0, 20, 'timecreated DESC');
130 $messagearea = new \core_message\output\messagearea\
message_area($user1->id
, $user2->id
, $conversations, $messages,
131 $requestedconversation);
133 // Now the page contents.
134 echo $OUTPUT->header();
135 echo $OUTPUT->heading(get_string('messages', 'message'));
136 // Display a message that the user is viewing someone else's messages.
138 $notify = new \core\output\notification
(get_string('viewinganotherusersmessagearea', 'message'),
139 \core\output\notification
::NOTIFY_WARNING
);
140 echo $OUTPUT->render($notify);
142 echo $renderer->render($messagearea);
143 echo $OUTPUT->footer();