MDL-41369 Set proper context and navigation path for user messages
[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
eb5334ff 60
08cd70cf
AD
61//for search
62$advancedsearch = optional_param('advanced', 0, PARAM_INT);
63
d54286e7
AD
64//if they have numerous contacts or are viewing course participants we might need to page through them
65$page = optional_param('page', 0, PARAM_INT);
66
447df209 67$url = new moodle_url('/message/index.php', array('user1' => $user1id));
02f2c7bd 68
02f2c7bd 69if ($user2id !== 0) {
25bd5c75 70 $url->param('user2', $user2id);
02f2c7bd 71
bcab42da 72 //Switch view back to contacts if:
73 //1) theyve searched and selected a user
74 //2) they've viewed recent messages or notifications and clicked through to a user
447df209 75 if ($viewing == MESSAGE_VIEW_SEARCH || $viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
25bd5c75 76 $viewing = MESSAGE_VIEW_CONTACTS;
08cd70cf 77 }
eb5334ff 78}
447df209
AD
79
80if ($viewing != MESSAGE_VIEW_UNREAD_MESSAGES) {
81 $url->param('viewing', $viewing);
82}
405f01ee 83
02f2c7bd 84$PAGE->set_url($url);
3fca9299 85
76267761
AD
86// Disable message notification popups while the user is viewing their messages
87$PAGE->set_popup_notification_allowed(false);
88
02f2c7bd
AD
89$user1 = null;
90$currentuser = true;
447df209 91$showactionlinks = true;
bcab42da 92if ($user1id != $USER->id) {
93 $user1 = $DB->get_record('user', array('id' => $user1id));
02f2c7bd
AD
94 if (!$user1) {
95 print_error('invaliduserid');
96 }
97 $currentuser = false;//if we're looking at someone else's messages we need to lock/remove some UI elements
447df209 98 $showactionlinks = false;
02f2c7bd
AD
99} else {
100 $user1 = $USER;
eb5334ff 101}
02f2c7bd 102unset($user1id);
e8e2d7f1 103
02f2c7bd
AD
104$user2 = null;
105if (!empty($user2id)) {
bcab42da 106 $user2 = $DB->get_record("user", array("id" => $user2id));
02f2c7bd
AD
107 if (!$user2) {
108 print_error('invaliduserid');
109 }
110}
111unset($user2id);
172186b8 112
447df209
AD
113$systemcontext = context_system::instance();
114
2e2d1977
AD
115if (!empty($user2) && $user1->id == $user2->id) {
116 print_error('invaliduserid');
117}
118
48e03792
AD
119// Is the user involved in the conversation?
120// Do they have the ability to read other user's conversations?
447df209 121if (!message_current_user_is_involved($user1, $user2) && !has_capability('moodle/site:readallmessages', $systemcontext)) {
27a39763
AD
122 print_error('accessdenied','admin');
123}
124
0e1bd87c
MG
125if (substr($viewing, 0, 7) == MESSAGE_VIEW_COURSE) {
126 $courseid = intval(substr($viewing, 7));
127 require_login($courseid);
128 require_capability('moodle/course:viewparticipants', context_course::instance($courseid));
129 $PAGE->set_pagelayout('incourse');
130} else {
131 $PAGE->set_pagelayout('course');
132 $PAGE->set_context(context_user::instance($user1->id));
133}
134if (!empty($user1->id) && $user1->id != $USER->id) {
135 $PAGE->navigation->extend_for_user($user1);
136}
137if (!empty($user2->id) && $user2->id != $USER->id) {
138 $PAGE->navigation->extend_for_user($user2);
139}
447df209 140
27a39763
AD
141/// Process any contact maintenance requests there may be
142if ($addcontact and confirm_sesskey()) {
ee5ae297 143 add_to_log(SITEID, 'message', 'add contact', 'index.php?user1='.$addcontact.'&amp;user2='.$USER->id, $addcontact);
27a39763 144 message_add_contact($addcontact);
25bd5c75 145 redirect($CFG->wwwroot . '/message/index.php?viewing=contacts&id='.$addcontact);
27a39763
AD
146}
147if ($removecontact and confirm_sesskey()) {
ee5ae297 148 add_to_log(SITEID, 'message', 'remove contact', 'index.php?user1='.$removecontact.'&amp;user2='.$USER->id, $removecontact);
27a39763
AD
149 message_remove_contact($removecontact);
150}
151if ($blockcontact and confirm_sesskey()) {
ee5ae297 152 add_to_log(SITEID, 'message', 'block contact', 'index.php?user1='.$blockcontact.'&amp;user2='.$USER->id, $blockcontact);
27a39763
AD
153 message_block_contact($blockcontact);
154}
155if ($unblockcontact and confirm_sesskey()) {
ee5ae297 156 add_to_log(SITEID, 'message', 'unblock contact', 'index.php?user1='.$unblockcontact.'&amp;user2='.$USER->id, $unblockcontact);
27a39763
AD
157 message_unblock_contact($unblockcontact);
158}
159
71666cf3 160//was a message sent? Do NOT allow someone looking at someone else's messages to send them.
02f2c7bd 161$messageerror = null;
447df209 162if ($currentuser && !empty($user2) && has_capability('moodle/site:sendmessage', $systemcontext)) {
02f2c7bd 163 // Check that the user is not blocking us!!
bcab42da 164 if ($contact = $DB->get_record('message_contacts', array('userid' => $user2->id, 'contactid' => $user1->id))) {
447df209 165 if ($contact->blocked and !has_capability('moodle/site:readallmessages', $systemcontext)) {
02f2c7bd
AD
166 $messageerror = get_string('userisblockingyou', 'message');
167 }
168 }
169 $userpreferences = get_user_preferences(NULL, NULL, $user2->id);
eb5334ff 170
02f2c7bd
AD
171 if (!empty($userpreferences['message_blocknoncontacts'])) { // User is blocking non-contacts
172 if (empty($contact)) { // We are not a contact!
77d187ed 173 $messageerror = get_string('userisblockingyounoncontact', 'message', fullname($user2));
02f2c7bd
AD
174 }
175 }
eb5334ff 176
02f2c7bd
AD
177 if (empty($messageerror)) {
178 $mform = new send_form();
179 $defaultmessage = new stdClass;
180 $defaultmessage->id = $user2->id;
181 $defaultmessage->message = '';
eb5334ff 182
d54286e7 183 //Check if the current user has sent a message
02f2c7bd 184 $data = $mform->get_data();
d54286e7 185 if (!empty($data) && !empty($data->message)) {
02f2c7bd
AD
186 if (!confirm_sesskey()) {
187 print_error('invalidsesskey');
188 }
bcab42da 189 $messageid = message_post_message($user1, $user2, $data->message, FORMAT_MOODLE);
02f2c7bd 190 if (!empty($messageid)) {
3a00a167 191 //including the id of the user sending the message in the logged URL so the URL works for admins
192 //note message ID may be misleading as the message may potentially get a different ID when moved from message to message_read
193 add_to_log(SITEID, 'message', 'write', 'index.php?user='.$user1->id.'&id='.$user2->id.'&history=1#m'.$messageid, $user1->id);
25bd5c75 194 redirect($CFG->wwwroot . '/message/index.php?viewing='.$viewing.'&id='.$user2->id);
02f2c7bd
AD
195 }
196 }
197 }
198}
172186b8 199
02f2c7bd
AD
200$strmessages = get_string('messages', 'message');
201if (!empty($user2)) {
202 $user2fullname = fullname($user2);
203
204 $PAGE->set_title("$strmessages: $user2fullname");
205 $PAGE->set_heading("$strmessages: $user2fullname");
206} else {
207 $PAGE->set_title("{$SITE->shortname}: $strmessages");
208 $PAGE->set_heading("{$SITE->shortname}: $strmessages");
eb5334ff 209}
172186b8 210
02f2c7bd
AD
211//now the page contents
212echo $OUTPUT->header();
213
214echo $OUTPUT->box_start('message');
215
216$countunread = 0; //count of unread messages from $user2
217$countunreadtotal = 0; //count of unread messages from all users
218
219//we're dealing with unread messages early so the contact list will accurately reflect what is read/unread
728114df 220$viewingnewmessages = false;
02f2c7bd
AD
221if (!empty($user2)) {
222 //are there any unread messages from $user2
223 $countunread = message_count_unread_messages($user1, $user2);
224 if ($countunread>0) {
225 //mark the messages we're going to display as read
226 message_mark_messages_read($user1->id, $user2->id);
25bd5c75 227 if($viewing == MESSAGE_VIEW_UNREAD_MESSAGES) {
728114df
AD
228 $viewingnewmessages = true;
229 }
02f2c7bd
AD
230 }
231}
232$countunreadtotal = message_count_unread_messages($user1);
233
447df209
AD
234if ($currentuser && $countunreadtotal == 0 && $viewing == MESSAGE_VIEW_UNREAD_MESSAGES && empty($user2)) {
235 // If the user has no unread messages, show the search box.
236 // We don't do this when a user is viewing another user's messages as search doesn't
237 // handle user A searching user B's messages properly.
25bd5c75 238 $viewing = MESSAGE_VIEW_SEARCH;
fe8373bc
AD
239}
240
02f2c7bd
AD
241$blockedusers = message_get_blocked_users($user1, $user2);
242$countblocked = count($blockedusers);
243
244list($onlinecontacts, $offlinecontacts, $strangers) = message_get_contacts($user1, $user2);
245
447df209 246message_print_contact_selector($countunreadtotal, $viewing, $user1, $user2, $blockedusers, $onlinecontacts, $offlinecontacts, $strangers, $showactionlinks, $page);
02f2c7bd 247
bcab42da 248echo html_writer::start_tag('div', array('class' => 'messagearea mdl-align'));
02f2c7bd
AD
249 if (!empty($user2)) {
250
bcab42da 251 echo html_writer::start_tag('div', array('class' => 'mdl-left messagehistory'));
02f2c7bd 252
728114df
AD
253 $visible = 'visible';
254 $hidden = 'hiddenelement'; //cant just use hidden as mform adds that class to its fieldset for something else
255
256 $recentlinkclass = $recentlabelclass = $historylinkclass = $historylabelclass = $visible;
bcab42da 257 if ($history == MESSAGE_HISTORY_ALL) {
02f2c7bd
AD
258 $displaycount = 0;
259
728114df
AD
260 $recentlabelclass = $historylinkclass = $hidden;
261 } else if($viewingnewmessages) {
262 //if user is viewing new messages only show them the new messages
263 $displaycount = $countunread;
264
265 $recentlabelclass = $historylabelclass = $hidden;
02f2c7bd 266 } else {
d54286e7 267 //default to only showing the last few messages
02f2c7bd
AD
268 $displaycount = MESSAGE_SHORTVIEW_LIMIT;
269
270 if ($countunread>MESSAGE_SHORTVIEW_LIMIT) {
271 $displaycount = $countunread;
272 }
728114df
AD
273
274 $recentlinkclass = $historylabelclass = $hidden;
02f2c7bd
AD
275 }
276
bcab42da 277 $messagehistorylink = html_writer::start_tag('div', array('class' => 'mdl-align messagehistorytype'));
02f2c7bd
AD
278 $messagehistorylink .= html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_ALL,
279 get_string('messagehistoryfull','message'),
bcab42da 280 array('class' => $historylinkclass));
02f2c7bd 281
bcab42da 282 $messagehistorylink .= html_writer::start_tag('span', array('class' => $historylabelclass));
02f2c7bd
AD
283 $messagehistorylink .= get_string('messagehistoryfull','message');
284 $messagehistorylink .= html_writer::end_tag('span');
285
286 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::link($PAGE->url->out(false).'&history='.MESSAGE_HISTORY_SHORT,
287 get_string('mostrecent','message'),
bcab42da 288 array('class' => $recentlinkclass));
02f2c7bd 289
bcab42da 290 $messagehistorylink .= html_writer::start_tag('span', array('class' => $recentlabelclass));
02f2c7bd
AD
291 $messagehistorylink .= get_string('mostrecent','message');
292 $messagehistorylink .= html_writer::end_tag('span');
a8320bc4 293
728114df 294 if ($viewingnewmessages) {
bcab42da 295 $messagehistorylink .= '&nbsp;|&nbsp;'.html_writer::start_tag('span');//, array('class' => $historyclass)
a813a748 296 $messagehistorylink .= get_string('unreadnewmessages','message',$displaycount);
728114df
AD
297 $messagehistorylink .= html_writer::end_tag('span');
298 }
299
02f2c7bd
AD
300 $messagehistorylink .= html_writer::end_tag('div');
301
447df209 302 message_print_message_history($user1, $user2, $search, $displaycount, $messagehistorylink, $viewingnewmessages, $showactionlinks);
02f2c7bd
AD
303 echo html_writer::end_tag('div');
304
305 //send message form
447df209 306 if ($currentuser && has_capability('moodle/site:sendmessage', $systemcontext)) {
bcab42da 307 echo html_writer::start_tag('div', array('class' => 'mdl-align messagesend'));
e41da832 308 if (!empty($messageerror)) {
77d187ed 309 echo html_writer::tag('span', $messageerror, array('id' => 'messagewarning'));
e41da832 310 } else {
77d187ed
AD
311 // Display a warning if the current user is blocking non-contacts and is about to message to a non-contact
312 // Otherwise they may wonder why they never get a reply
313 $blocknoncontacts = get_user_preferences('message_blocknoncontacts', '', $user1->id);
314 if (!empty($blocknoncontacts)) {
315 $contact = $DB->get_record('message_contacts', array('userid' => $user1->id, 'contactid' => $user2->id));
316 if (empty($contact)) {
317 $msg = get_string('messagingblockednoncontact', 'message', fullname($user2));
318 echo html_writer::tag('span', $msg, array('id' => 'messagewarning'));
319 }
320 }
321
e41da832
AD
322 $mform = new send_form();
323 $defaultmessage = new stdClass;
324 $defaultmessage->id = $user2->id;
325 $defaultmessage->message = '';
326 //$defaultmessage->messageformat = FORMAT_MOODLE;
327 $mform->set_data($defaultmessage);
328 $mform->display();
329 }
02f2c7bd
AD
330 echo html_writer::end_tag('div');
331 }
25bd5c75 332 } else if ($viewing == MESSAGE_VIEW_SEARCH) {
08cd70cf 333 message_print_search($advancedsearch, $user1);
25bd5c75 334 } else if ($viewing == MESSAGE_VIEW_RECENT_CONVERSATIONS) {
447df209 335 message_print_recent_conversations($user1, false, $showactionlinks);
25bd5c75 336 } else if ($viewing == MESSAGE_VIEW_RECENT_NOTIFICATIONS) {
bcab42da 337 message_print_recent_notifications($user1);
02f2c7bd
AD
338 }
339echo html_writer::end_tag('div');
340
341echo $OUTPUT->box_end();
342
eb5334ff 343echo $OUTPUT->footer();
0d3bb48d 344
02f2c7bd 345