2009-09-29 03:54:14 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
2010-11-17 06:34:59 +00:00
|
|
|
* A page displaying the user's contacts and messages
|
2009-09-29 03:54:14 +00:00
|
|
|
*
|
2012-01-05 12:05:02 +07:00
|
|
|
* @package core_message
|
|
|
|
* @copyright 2010 Andrew Davis
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
2009-09-29 03:54:14 +00:00
|
|
|
*/
|
|
|
|
|
2010-06-29 07:27:24 +00:00
|
|
|
require_once('../config.php');
|
2009-09-29 03:54:14 +00:00
|
|
|
|
2016-09-23 13:28:42 +08:00
|
|
|
require_login(null, false);
|
2009-09-29 03:54:14 +00:00
|
|
|
|
2010-03-31 07:41:31 +00:00
|
|
|
if (isguestuser()) {
|
2009-09-29 03:54:14 +00:00
|
|
|
redirect($CFG->wwwroot);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($CFG->messaging)) {
|
|
|
|
print_error('disabled', 'message');
|
|
|
|
}
|
2005-01-31 07:11:42 +00:00
|
|
|
|
2016-09-23 13:28:42 +08:00
|
|
|
// The id of the user we want to view messages from.
|
|
|
|
$id = optional_param('id', 0, PARAM_INT);
|
2011-01-12 17:15:43 +08:00
|
|
|
|
2016-09-23 13:28:42 +08:00
|
|
|
// It's possible for someone with the right capabilities to view a conversation between two other users. For BC
|
|
|
|
// we are going to accept other URL parameters to figure this out.
|
|
|
|
$user1id = optional_param('user1', $USER->id, PARAM_INT);
|
|
|
|
$user2id = optional_param('user2', $id, PARAM_INT);
|
2016-11-16 14:45:26 +08:00
|
|
|
$contactsfirst = optional_param('contactsfirst', 0, PARAM_INT);
|
2010-06-29 07:27:24 +00:00
|
|
|
|
2016-09-23 13:28:42 +08:00
|
|
|
$url = new moodle_url('/message/index.php');
|
|
|
|
if ($id) {
|
|
|
|
$url->param('id', $id);
|
|
|
|
} else {
|
|
|
|
if ($user1id) {
|
|
|
|
$url->param('user1', $user1id);
|
|
|
|
}
|
|
|
|
if ($user2id) {
|
|
|
|
$url->param('user2', $user2id);
|
2010-11-02 06:09:10 +00:00
|
|
|
}
|
2016-11-16 14:45:26 +08:00
|
|
|
if ($contactsfirst) {
|
|
|
|
$url->param('contactsfirst', $contactsfirst);
|
|
|
|
}
|
2009-09-29 03:54:14 +00:00
|
|
|
}
|
2010-06-29 07:27:24 +00:00
|
|
|
$PAGE->set_url($url);
|
2004-12-31 02:17:44 +00:00
|
|
|
|
2010-06-29 07:27:24 +00:00
|
|
|
$user1 = null;
|
|
|
|
$currentuser = true;
|
2011-01-12 17:15:43 +08:00
|
|
|
if ($user1id != $USER->id) {
|
2016-09-23 13:28:42 +08:00
|
|
|
$user1 = core_user::get_user($user1id, '*', MUST_EXIST);
|
|
|
|
$currentuser = false;
|
2010-06-29 07:27:24 +00:00
|
|
|
} else {
|
|
|
|
$user1 = $USER;
|
2009-09-29 03:54:14 +00:00
|
|
|
}
|
2004-12-28 13:49:15 +00:00
|
|
|
|
2010-06-29 07:27:24 +00:00
|
|
|
$user2 = null;
|
|
|
|
if (!empty($user2id)) {
|
2016-09-23 13:28:42 +08:00
|
|
|
$user2 = core_user::get_user($user2id, '*', MUST_EXIST);
|
2010-06-29 07:27:24 +00:00
|
|
|
}
|
2004-12-16 12:40:36 +00:00
|
|
|
|
2013-08-30 14:28:17 +08:00
|
|
|
$user2realuser = !empty($user2) && core_user::is_real_user($user2->id);
|
2012-11-12 15:25:49 +08:00
|
|
|
$systemcontext = context_system::instance();
|
2015-03-03 12:38:46 +08:00
|
|
|
if ($currentuser === false && !has_capability('moodle/site:readallmessages', $systemcontext)) {
|
2016-09-23 13:28:42 +08:00
|
|
|
print_error('accessdenied', 'admin');
|
2010-10-08 02:31:57 +00:00
|
|
|
}
|
|
|
|
|
2015-03-16 13:13:07 +08:00
|
|
|
$PAGE->set_context(context_user::instance($user1->id));
|
2016-09-23 13:28:42 +08:00
|
|
|
$PAGE->set_pagelayout('standard');
|
2015-03-24 17:11:15 -07:00
|
|
|
$strmessages = get_string('messages', 'message');
|
|
|
|
if ($user2realuser) {
|
|
|
|
$user2fullname = fullname($user2);
|
|
|
|
|
|
|
|
$PAGE->set_title("$strmessages: $user2fullname");
|
|
|
|
$PAGE->set_heading("$strmessages: $user2fullname");
|
|
|
|
} else {
|
|
|
|
$PAGE->set_title("{$SITE->shortname}: $strmessages");
|
|
|
|
$PAGE->set_heading("{$SITE->shortname}: $strmessages");
|
|
|
|
}
|
|
|
|
|
2015-03-19 10:59:27 +08:00
|
|
|
// Remove the user node from the main navigation for this page.
|
|
|
|
$usernode = $PAGE->navigation->find('users', null);
|
|
|
|
$usernode->remove();
|
|
|
|
|
|
|
|
$settings = $PAGE->settingsnav->find('messages', null);
|
2016-05-31 19:34:29 +08:00
|
|
|
$settings->make_active();
|
|
|
|
|
|
|
|
// Get the renderer and the information we are going to be use.
|
|
|
|
$renderer = $PAGE->get_renderer('core_message');
|
2016-09-16 11:12:17 +08:00
|
|
|
$requestedconversation = false;
|
2016-11-16 14:45:26 +08:00
|
|
|
if ($contactsfirst) {
|
|
|
|
$conversations = \core_message\api::get_contacts($user1->id, 0, 20);
|
|
|
|
} else {
|
|
|
|
$conversations = \core_message\api::get_conversations($user1->id, 0, 20);
|
|
|
|
}
|
2016-10-18 02:00:09 +00:00
|
|
|
$messages = [];
|
2016-05-31 19:34:29 +08:00
|
|
|
if (!$user2realuser) {
|
2016-10-06 14:31:30 +08:00
|
|
|
// If there are conversations, but the user has not chosen a particular one, then render the most recent one.
|
|
|
|
$user2 = new stdClass();
|
|
|
|
$user2->id = null;
|
|
|
|
if (!empty($conversations)) {
|
|
|
|
$contact = reset($conversations);
|
|
|
|
$user2->id = $contact->userid;
|
2016-08-12 07:09:45 +00:00
|
|
|
}
|
2015-03-19 10:59:27 +08:00
|
|
|
} else {
|
2016-10-06 14:31:30 +08:00
|
|
|
// The user has specifically requested to see a conversation. Add the flag to
|
|
|
|
// the context so that we can render the messaging app appropriately - this is
|
|
|
|
// used for smaller screens as it allows the UI to be responsive.
|
|
|
|
$requestedconversation = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the conversation as read.
|
|
|
|
if (!empty($user2->id)) {
|
2016-10-14 07:13:58 +00:00
|
|
|
if ($currentuser && isset($conversations[$user2->id])) {
|
2016-10-06 14:31:30 +08:00
|
|
|
// Mark the conversation we are loading as read.
|
2016-09-22 12:30:28 +08:00
|
|
|
\core_message\api::mark_all_read_for_user($user1->id, $user2->id);
|
2016-10-06 14:31:30 +08:00
|
|
|
// Ensure the UI knows it's read as well.
|
|
|
|
$conversations[$user2->id]->isread = 1;
|
2016-08-15 15:07:54 +08:00
|
|
|
}
|
|
|
|
|
2016-10-06 14:31:30 +08:00
|
|
|
$messages = \core_message\api::get_messages($user1->id, $user2->id, 0, 20, 'timecreated DESC');
|
2015-03-19 10:59:27 +08:00
|
|
|
}
|
2016-10-06 14:31:30 +08:00
|
|
|
|
2016-11-10 12:47:20 +08:00
|
|
|
$pollmin = !empty($CFG->messagingminpoll) ? $CFG->messagingminpoll : MESSAGE_DEFAULT_MIN_POLL_IN_SECONDS;
|
|
|
|
$pollmax = !empty($CFG->messagingmaxpoll) ? $CFG->messagingmaxpoll : MESSAGE_DEFAULT_MAX_POLL_IN_SECONDS;
|
|
|
|
$polltimeout = !empty($CFG->messagingtimeoutpoll) ? $CFG->messagingtimeoutpoll : MESSAGE_DEFAULT_TIMEOUT_POLL_IN_SECONDS;
|
2016-10-06 14:31:30 +08:00
|
|
|
$messagearea = new \core_message\output\messagearea\message_area($user1->id, $user2->id, $conversations, $messages,
|
2016-11-16 14:45:26 +08:00
|
|
|
$requestedconversation, $contactsfirst, $pollmin, $pollmax, $polltimeout);
|
2015-03-19 10:59:27 +08:00
|
|
|
|
2016-05-31 19:34:29 +08:00
|
|
|
// Now the page contents.
|
2010-06-29 07:27:24 +00:00
|
|
|
echo $OUTPUT->header();
|
2016-09-23 13:28:42 +08:00
|
|
|
echo $OUTPUT->heading(get_string('messages', 'message'));
|
2016-05-31 19:34:29 +08:00
|
|
|
// Display a message that the user is viewing someone else's messages.
|
|
|
|
if (!$currentuser) {
|
|
|
|
$notify = new \core\output\notification(get_string('viewinganotherusersmessagearea', 'message'),
|
|
|
|
\core\output\notification::NOTIFY_WARNING);
|
|
|
|
echo $OUTPUT->render($notify);
|
2010-06-29 07:27:24 +00:00
|
|
|
}
|
2016-05-31 19:34:29 +08:00
|
|
|
echo $renderer->render($messagearea);
|
2009-09-29 03:54:14 +00:00
|
|
|
echo $OUTPUT->footer();
|