weekly release 3.10.1+
[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 25require_once('../config.php');
eb5334ff 26
66e1bdda 27require_login(null, false);
eb5334ff 28
4f0c2d00 29if (isguestuser()) {
eb5334ff 30 redirect($CFG->wwwroot);
31}
32
33if (empty($CFG->messaging)) {
34 print_error('disabled', 'message');
35}
cdf39255 36
66e1bdda
MN
37// The id of the user we want to view messages from.
38$id = optional_param('id', 0, PARAM_INT);
6cb33ce7 39$view = optional_param('view', null, PARAM_ALPHANUM);
fd998fc6
MN
40// It's possible a user may come from a link where these parameters are specified.
41// We no longer support viewing another user's messaging area (that can be achieved
42// via the 'Log-in as' feature). The 'user2' value takes preference over 'id'.
43$userid = optional_param('user2', $id, PARAM_INT);
d5d4a5aa 44$conversationid = optional_param('convid', null, PARAM_INT);
bcab42da 45
fd998fc6
MN
46if (!core_user::is_real_user($userid)) {
47 $userid = null;
eb5334ff 48}
d5d4a5aa
MN
49// You can specify either a user, or a conversation, not both.
50if ($userid) {
51 $conversationid = \core_message\api::get_conversation_between_users([$USER->id, $userid]);
52} else if ($conversationid) {
53 // Check that the user belongs to the conversation.
54 if (!\core_message\api::is_user_in_conversation($USER->id, $conversationid)) {
55 $conversationid = null;
56 }
57}
3fca9299 58
fd998fc6 59if ($userid) {
06d046c1 60 if (!\core_message\api::can_send_message($userid, $USER->id)) {
fd998fc6
MN
61 throw new moodle_exception('Can not contact user');
62 }
02f2c7bd 63}
172186b8 64
fd998fc6
MN
65$url = new moodle_url('/message/index.php');
66if ($userid) {
67 $url->param('id', $userid);
27a39763 68}
fd998fc6
MN
69$PAGE->set_url($url);
70$PAGE->set_context(context_user::instance($USER->id));
7cbea160 71$PAGE->set_pagelayout('mydashboard');
fd998fc6 72
36d29c08 73$strmessages = get_string('messages', 'message');
36d29c08 74
fd998fc6
MN
75$PAGE->set_title("$strmessages");
76$PAGE->set_heading("$strmessages");
36d29c08 77
f495187d
AG
78// Remove the user node from the main navigation for this page.
79$usernode = $PAGE->navigation->find('users', null);
80$usernode->remove();
81
82$settings = $PAGE->settingsnav->find('messages', null);
879e2bef
MN
83$settings->make_active();
84
02f2c7bd 85echo $OUTPUT->header();
7022b8d9 86// Display a message if the messages have not been migrated yet.
fd998fc6 87if (!get_user_preferences('core_message_migrate_data', false)) {
7022b8d9
MN
88 $notify = new \core\output\notification(get_string('messagingdatahasnotbeenmigrated', 'message'),
89 \core\output\notification::NOTIFY_WARNING);
90 echo $OUTPUT->render($notify);
91}
6cb33ce7 92echo \core_message\helper::render_messaging_widget(false, $userid, $conversationid, $view);
eb5334ff 93echo $OUTPUT->footer();