MDL-54708 message: notification popover respects message preferences
[moodle.git] / message / index.php
CommitLineData
eb5334ff 1<?php
eb5334ff 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.
13//
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/>.
16
17/**
affefa8c 18 * A page displaying the user's contacts and messages
eb5334ff 19 *
6fbd60ef
AD
20 * @package core_message
21 * @copyright 2010 Andrew Davis
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
eb5334ff 23 */
24
02f2c7bd
AD
25require_once('../config.php');
26require_once('lib.php');
27require_once('send_form.php');
eb5334ff 28
29require_login(0, false);
30
4f0c2d00 31if (isguestuser()) {
eb5334ff 32 redirect($CFG->wwwroot);
33}
34
35if (empty($CFG->messaging)) {
36 print_error('disabled', 'message');
37}
cdf39255 38
25bd5c75 39//'viewing' is the preferred URL parameter but we'll still accept usergroup in case its referenced externally
40$usergroup = optional_param('usergroup', MESSAGE_VIEW_UNREAD_MESSAGES, PARAM_ALPHANUMEXT);
41$viewing = optional_param('viewing', $usergroup, PARAM_ALPHANUMEXT);
bcab42da 42
02f2c7bd 43$history = optional_param('history', MESSAGE_HISTORY_SHORT, PARAM_INT);
a8320bc4 44$search = optional_param('search', '', PARAM_CLEAN); //TODO: use PARAM_RAW, but make sure we use s() and p() properly
02f2c7bd 45
3a00a167 46//the same param as 1.9 and the param we have been logging. Use this parameter.
25bd5c75 47$user1id = optional_param('user1', $USER->id, PARAM_INT);
3a00a167 48//2.0 shipped using this param. Retaining it only for compatibility. It should be removed.
49$user1id = optional_param('user', $user1id, PARAM_INT);
50
51//the same param as 1.9 and the param we have been logging. Use this parameter.
25bd5c75 52$user2id = optional_param('user2', 0, PARAM_INT);
53//The class send_form supplies the receiving user id as 'id'
3a00a167 54$user2id = optional_param('id', $user2id, PARAM_INT);
02f2c7bd 55
eb5334ff 56$addcontact = optional_param('addcontact', 0, PARAM_INT); // adding a contact
57$removecontact = optional_param('removecontact', 0, PARAM_INT); // removing a contact
58$blockcontact = optional_param('blockcontact', 0, PARAM_INT); // blocking a contact
59$unblockcontact = optional_param('unblockcontact', 0, PARAM_INT); // unblocking a contact
36d29c08
MN
60$deletemessageid = optional_param('deletemessageid', 0, PARAM_INT);
61$deletemessageconfirm = optional_param('deletemessageconfirm', 0, PARAM_BOOL);
62if ($deletemessageid) {
63 $deletemessagetype = required_param('deletemessagetype', PARAM_ALPHAEXT);
64}
eb5334ff 65
08cd70cf
AD
66//for search
67$advancedsearch = optional_param('advanced', 0, PARAM_INT);
68
d54286e7
AD
69//if they have numerous contacts or are viewing course participants we might need to page through them
70$page = optional_param('page', 0, PARAM_INT);
71
447df209 72$url = new moodle_url('/message/index.php', array('user1' => $user1id));
02f2c7bd 73
02f2c7bd 74if ($user2id !== 0) {
25bd5c75 75 $url->param('user2', $user2id);
02f2c7bd 76
bcab42da 77 //Switch view back to contacts if:
78 //1) theyve searched and selected a user
79 //2) they've viewed recent messages or notifications and clicked through to a user
447df209 80 if ($viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
25bd5c75 81 $viewing = MESSAGE_VIEW_CONTACTS;
08cd70cf 82 }
eb5334ff 83}
447df209
AD
84
85if ($viewing != MESSAGE_VIEW_UNREAD_MESSAGES) {
86 $url->param('viewing', $viewing);
87}
405f01ee 88
02f2c7bd 89$PAGE->set_url($url);
3fca9299 90
76267761
AD
91// Disable message notification popups while the user is viewing their messages
92$PAGE->set_popup_notification_allowed(false);
93
02f2c7bd
AD
94$user1 = null;
95$currentuser = true;
447df209 96$showactionlinks = true;
bcab42da 97if ($user1id != $USER->id) {
98 $user1 = $DB->get_record('user', array('id' => $user1id));
02f2c7bd
AD
99 if (!$user1) {
100 print_error('invaliduserid');
101 }
102 $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
447df209 103 $showactionlinks = false;
02f2c7bd
AD
104} else {
105 $user1 = $USER;
eb5334ff 106}
02f2c7bd 107unset($user1id);
e8e2d7f1 108
02f2c7bd
AD
109$user2 = null;
110if (!empty($user2id)) {
3bcf6b3c 111 $user2 = core_user::get_user($user2id);
02f2c7bd
AD
112 if (!$user2) {
113 print_error('invaliduserid');
114 }
115}
116unset($user2id);
172186b8 117
3bcf6b3c
RT
118$user2realuser = !empty($user2) && core_user::is_real_user($user2->id);
119$showactionlinks = $showactionlinks && $user2realuser;
447df209
AD
120$systemcontext = context_system::instance();
121
553319be 122if ($currentuser === false && !has_capability('moodle/site:readallmessages', $systemcontext)) {
27a39763
AD
123 print_error('accessdenied','admin');
124}
125
0e1bd87c
MG
126if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) {
127 $courseid = intval(substr($viewing, 7));
128 require_login($courseid);
129 require_capability('moodle/course:viewparticipants', context_course::instance($courseid));
130 $PAGE->set_pagelayout('incourse');
131} else {
369484bf 132 $PAGE->set_pagelayout('standard');
0e1bd87c 133}
880c5073
AG
134// Page context should always be set to user.
135$PAGE->set_context(context_user::instance($user1->id));
0e1bd87c
MG
136if (!empty($user1->id) && $user1->id != $USER->id) {
137 $PAGE->navigation->extend_for_user($user1);
138}
9a90e7c5 139if (!empty($user2->id) && $user2realuser && ($user2->id != $USER->id)) {
0e1bd87c
MG
140 $PAGE->navigation->extend_for_user($user2);
141}
447df209 142
36d29c08
MN
143$strmessages = get_string('messages', 'message');
144if ($user2realuser) {
145 $user2fullname = fullname($user2);
146
147 $PAGE->set_title("$strmessages: $user2fullname");
148 $PAGE->set_heading("$strmessages: $user2fullname");
149} else {
150 $PAGE->set_title("{$SITE->shortname}: $strmessages");
151 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
152}
153
27a39763
AD
154/// Process any contact maintenance requests there may be
155if ($addcontact and confirm_sesskey()) {
27a39763 156 message_add_contact($addcontact);
25bd5c75 157 redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact);
27a39763
AD
158}
159if ($removecontact and confirm_sesskey()) {
27a39763
AD
160 message_remove_contact($removecontact);
161}
162if ($blockcontact and confirm_sesskey()) {
27a39763
AD
163 message_block_contact($blockcontact);
164}
165if ($unblockcontact and confirm_sesskey()) {
27a39763
AD
166 message_unblock_contact($unblockcontact);
167}
36d29c08
MN
168if ($deletemessageid and confirm_sesskey()) {
169 // Check that the message actually exists.
170 if ($message = $DB->get_record($deletemessagetype, array('id' => $deletemessageid))) {
171 // Check that we are allowed to delete this message.
172 if (message_can_delete_message($message, $user1->id)) {
173 if (!$deletemessageconfirm) {
174 $confirmurl = new moodle_url('/message/index.php', array('user1' => $user1->id, 'user2' => $user2->id,
175 'viewing' => $viewing, 'deletemessageid' => $message->id, 'deletemessagetype' => $deletemessagetype,
176 'deletemessageconfirm' => 1, 'sesskey' => sesskey()));
177 $confirmbutton = new single_button($confirmurl, get_string('delete'), 'post');
178 $strdeletemessage = get_string('deletemessage', 'message');
179 $PAGE->set_title($strdeletemessage);
180 echo $OUTPUT->header();
181 echo $OUTPUT->heading($strdeletemessage);
182 echo $OUTPUT->confirm(get_string('deletemessageconfirmation', 'message'), $confirmbutton, $url);
183 echo $OUTPUT->footer();
184 exit();
185 }
186 message_delete_message($message, $user1->id);
187 }
188 }
189 redirect($url);
190}
27a39763 191
71666cf3 192//was a message sent? Do NOT allow someone looking at someone else's messages to send them.
02f2c7bd 193$messageerror = null;
9a90e7c5 194if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $systemcontext)) {
02f2c7bd 195 // Check that the user is not blocking us!!
cd0c9ac8
RW
196 if (message_is_user_blocked($user2, $user1)) {
197 $messageerror = get_string('userisblockingyou', 'message');
02f2c7bd 198 }
cd0c9ac8
RW
199 // Check that we're not non-contact block by the user.
200 if (message_is_user_non_contact_blocked($user2, $user1)) {
201 $messageerror = get_string('userisblockingyounoncontact', 'message', fullname($user2));
02f2c7bd 202 }
eb5334ff 203
02f2c7bd
AD
204 if (empty($messageerror)) {
205 $mform = new send_form();
206 $defaultmessage = new stdClass;
207 $defaultmessage->id = $user2->id;
8f0137e1 208 $defaultmessage->viewing = $viewing;
02f2c7bd 209 $defaultmessage->message = '';
eb5334ff 210
d54286e7 211 //Check if the current user has sent a message
02f2c7bd 212 $data = $mform->get_data();
d54286e7 213 if (!empty($data) && !empty($data->message)) {
02f2c7bd
AD
214 if (!confirm_sesskey()) {
215 print_error('invalidsesskey');
216 }
bcab42da 217 $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE);
02f2c7bd 218 if (!empty($messageid)) {
3a00a167 219 //including the id of the user sending the message in the logged URL so the URL works for admins
220 //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read
25bd5c75 221 redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id);
02f2c7bd
AD
222 }
223 }
224 }
225}
172186b8 226
02f2c7bd 227$strmessages = get_string('messages', 'message');
3bcf6b3c 228if ($user2realuser) {
02f2c7bd
AD
229 $user2fullname = fullname($user2);
230
231 $PAGE->set_title("$strmessages: $user2fullname");
02f2c7bd
AD
232} else {
233 $PAGE->set_title("{$SITE->shortname}: $strmessages");
eb5334ff 234}
45367bdf 235$PAGE->set_heading(fullname($user1));
172186b8 236
f495187d
AG
237// Remove the user node from the main navigation for this page.
238$usernode = $PAGE->navigation->find('users', null);
239$usernode->remove();
240
241$settings = $PAGE->settingsnav->find('messages', null);
879e2bef
MN
242$settings->make_active();
243
244// Get the renderer and the information we are going to be use.
245$renderer = $PAGE->get_renderer('core_message');
246if (!$user2realuser) {
247 $conversations = \core_message\api::get_conversations($user1->id);
248 $messages = null;
f495187d 249} else {
879e2bef
MN
250 $conversations = \core_message\api::get_conversations($user1->id, $user2->id);
251 $messages = \core_message\api::get_messages($user1->id, $user2->id);
f495187d 252}
879e2bef 253$messagearea = new \core_message\output\message_area_page($user1->id, $conversations, $messages);
f495187d 254
879e2bef 255// Now the page contents.
02f2c7bd 256echo $OUTPUT->header();
879e2bef
MN
257// Display a message that the user is viewing someone else's messages.
258if (!$currentuser) {
259 $notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),
260 \core\output\notification::NOTIFY_WARNING);
261 echo $OUTPUT->render($notify);
02f2c7bd 262}
879e2bef 263echo $renderer->render($messagearea);
eb5334ff 264echo $OUTPUT->footer();