MDL-54687 core_message: return online status for messages and profile

This commit is contained in:
Mark Nelson 2016-08-12 11:35:30 +08:00
parent cd03b8d7f2
commit bf58081d59
6 changed files with 35 additions and 12 deletions

View File

@ -315,7 +315,7 @@ class api {
if ($user = \core_user::get_user($otheruserid)) {
// Create the data we are going to pass to the renderable.
$userfields = user_get_user_details($user, null, array('city', 'country', 'email',
'profileimageurl', 'profileimageurlsmall'));
'profileimageurl', 'profileimageurlsmall', 'lastaccess'));
$data = new \stdClass();
$data->userid = $userfields['id'];
$data->fullname = $userfields['fullname'];
@ -325,6 +325,11 @@ class api {
$data->profileimageurl = isset($userfields['profileimageurl']) ? $userfields['profileimageurl'] : '';
$data->profileimageurlsmall = isset($userfields['profileimageurlsmall']) ?
$userfields['profileimageurlsmall'] : '';
if (isset($userfields['lastaccess'])) {
$data->isonline = \core_message\helper::is_online($userfields['lastaccess']);
} else {
$data->isonline = 0;
}
// Check if the contact has been blocked.
$contact = $DB->get_record('message_contacts', array('userid' => $userid, 'contactid' => $otheruserid));
if ($contact) {

View File

@ -126,14 +126,7 @@ class helper {
* @return \core_message\output\messagearea\contact
*/
public static function create_contact($contact, $prefix = '') {
global $CFG, $PAGE;
// Variables to check if we consider this user online or not.
$timetoshowusers = 300; // Seconds default.
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
}
$time = time() - $timetoshowusers;
global $PAGE;
// Create the data we are going to pass to the renderable.
$userfields = \user_picture::unalias($contact, array('lastaccess'), $prefix . 'id', $prefix);
@ -156,9 +149,28 @@ class helper {
}
}
// Check if the user is online.
$data->isonline = $userfields->lastaccess >= $time;
$data->isonline = \core_message\helper::is_online($userfields->lastaccess);
$data->isread = isset($contact->isread) ? $contact->isread : 0;
return new \core_message\output\messagearea\contact($data);
}
/**
* Helper function for checking the time meets the 'online' condition.
*
* @param int $lastaccess
* @return boolean
*/
public static function is_online($lastaccess) {
global $CFG;
// Variable to check if we consider this user online or not.
$timetoshowusers = 300; // Seconds default.
if (isset($CFG->block_online_users_timetosee)) {
$timetoshowusers = $CFG->block_online_users_timetosee * 60;
}
$time = time() - $timetoshowusers;
return $lastaccess >= $time;
}
}

View File

@ -66,9 +66,11 @@ class messages implements templatable, renderable {
public function __construct($currentuserid, $otheruserid, $messages) {
global $DB;
$ufields = get_all_user_name_fields(true) . ', lastaccess';
$this->currentuserid = $currentuserid;
$this->otheruserid = $otheruserid;
$this->otheruser = $DB->get_record('user', array('id' => $otheruserid));
$this->otheruser = $DB->get_record('user', array('id' => $otheruserid), $ufields);
$this->messages = $messages;
}
@ -80,6 +82,7 @@ class messages implements templatable, renderable {
$data->currentuserid = $this->currentuserid;
$data->otheruserid = $this->otheruserid;
$data->otheruserfullname = fullname($this->otheruser);
$data->isonline = \core_message\helper::is_online($this->otheruser->lastaccess);
$data->messages = array();
foreach ($this->messages as $message) {
$data->messages[] = $message->export_for_template($output);

View File

@ -65,6 +65,7 @@ class profile implements templatable, renderable {
$data->currentuserid = $this->currentuserid;
$data->otheruserid = $this->otheruser->userid;
$data->fullname = $this->otheruser->fullname;
$data->isonline = $this->otheruser->isonline;
$data->email = $this->otheruser->email;
if (!empty($this->otheruser->country)) {
$data->country = get_string($this->otheruser->country, 'countries');

View File

@ -885,6 +885,7 @@ class core_message_external extends external_api {
'currentuserid' => new external_value(PARAM_INT, 'The current user\'s id'),
'otheruserid' => new external_value(PARAM_INT, 'The other user\'s id'),
'otheruserfullname' => new external_value(PARAM_NOTAGS, 'The other user\'s fullname'),
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
'messages' => new external_multiple_structure(
new external_single_structure(
array(
@ -1032,6 +1033,7 @@ class core_message_external extends external_api {
'fullname' => new external_value(PARAM_NOTAGS, 'The user\'s name'),
'profileimageurl' => new external_value(PARAM_URL, 'User picture URL'),
'profileimageurlsmall' => new external_value(PARAM_URL, 'Small user picture URL'),
'isonline' => new external_value(PARAM_BOOL, 'The user\'s online status'),
'isblocked' => new external_value(PARAM_BOOL, 'Is the user blocked?'),
'iscontact' => new external_value(PARAM_BOOL, 'Is the user a contact?')
)

View File

@ -3,7 +3,7 @@
<div class="name">
{{otheruserfullname}}
</div>
<div class="status">
<div class="status {{#isonline}}online{{/isonline}}">
<span class="offline-text">{{#str}} offline, message {{/str}}</span>
<span class="online-text">{{#str}} online, message {{/str}}</span>
</div>