我想在Drupal 8上获得用户的状态。
使用以下代码,如果用户在线,则状态为在线。但是,如果用户已离线一个多小时,则状态将丢失。通常它应该处于离线状态。
此代码有什么问题?
谢谢
这是我在user.html.twig
文件中添加的内容:
<div class="bs-field-status">
{% if status == 'Online' %}
<i class="user-online fa fa-circle fa-lg"></i> Online
{% elseif status == 'Absent' %}
<i class="user-absent fa fa-circle fa-lg"></i> Absent
{% else %}
<i class="user-offline fa fa-circle fa-lg"></i> Offline
{% endif %}
</div>
bootstrap_sub.theme
:
<?php
/**
* @file
* Bootstrap sub-theme.
*
* Place your custom PHP code in this file.
*/
use Drupal\Core\Database\Database;
/**
* Implements hook_entity_presave().
*/
function bootstrap_sub_preprocess_user(&$variables) {
// get user object
$user = $variables['elements']['#user'];
//- The user has logged in at least once
if ($user->getLastLoginTime()) {
if (account_is_logged_in_less_then_thirty_minutes($user->id())) {
$status = 'Online';
}
else {
$status = 'Absent';
}
}
else {
$status = 'Offline';
}
$variables['status'] = $status;
}
/**
* @param $uid
*
* @return bool
*/
function account_is_logged_in_less_then_thirty_minutes($uid) {
$connection = Database::getConnection();
$query = $connection->select('sessions', 'sessions')
->fields('sessions', ['sid', 'uid', 'timestamp'])
->condition('sessions.uid', $uid, '=')
//- chef if the user was online in 30 minutes (60 * 30)
->condition('sessions.timestamp', \Drupal::time()
->getRequestTime() - (60 * 30), '>')
->execute();
//- Get result.
$results = $query->fetchAll(\PDO::FETCH_OBJ);
return (count($results) > 0) ? TRUE : FALSE;
}
答案 0 :(得分:0)
尝试此方法,您不必参与会话。由于Drupal用户对象提供了getLastAccessedTime()方法,可以为您解决问题。
$userLastAccessTime = $user->getLastAccessedTime();
$unixTime20MinBeforeNow = strtotime("-20 Minutes");
$unixTime1HourBeforeNow = strtotime("-1 Hours");
/*
* User last access time is more than last 10 minutes.
* User was actve withing last 30 minutes.
*/
if ($userLastAccessTime > $unixTime20MinBeforeNow) {
$status = 'Online';
} else if ($userLastAccessTime > $unixTime1HourBeforeNow) {
$status = 'Absent';
} else {
$status = 'Offline';
}