MDL-61827 auth: Change Facebook Graph API v2.8 to v.2.12 in OAuth2
[moodle.git] / message / index.php
blob65349b3003b0a98781f7b4e8a9e6a945d3ef9610
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 \core_message\api::mark_all_read_for_user($user1->id, $user2->id);
131 // Ensure the UI knows it's read as well.
132 $conversations[$user2->id]->isread = 1;
135 $messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 20, 'timecreated DESC');
138 $pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;
139 $pollmax = !empty($CFG->messagingmaxpoll) ? $CFG->messagingmaxpoll : MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS;
140 $polltimeout = !empty($CFG->messagingtimeoutpoll) ? $CFG->messagingtimeoutpoll : MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS;
141 $messagearea = new \core_message\output\messagearea\message_area($user1->id, $user2->id, $conversations, $messages,
142 $requestedconversation, $contactsfirst, $pollmin, $pollmax, $polltimeout);
144 // Now the page contents.
145 echo $OUTPUT->header();
146 echo $OUTPUT->heading(get_string('messages', 'message'));
147 // Display a message that the user is viewing someone else's messages.
148 if (!$currentuser) {
149 $notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),
150 \core\output\notification::NOTIFY_WARNING);
151 echo $OUTPUT->render($notify);
153 echo $renderer->render($messagearea);
154 echo $OUTPUT->footer();