Show if a user is online, idle, or offline on their account page?











up vote
3
down vote

favorite
2












I want to display the status of the user on the account page.



I added the code below in the .theme file of my theme.



It works, but users are never displayed offline.



Why ? Is there a mistake ?



Thank you



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_subtheme_front_office.theme :



<?php
/**
* @file
* Bootstrap sub-theme.
*
* Place your custom PHP code in this file.
*/

use DrupalCoreDatabaseDatabase;

/**
* Implements hook_entity_presave().
*/
function bootstrap_subtheme_front_office_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;
}









share|improve this question









New contributor




mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I think that the biggest issue here is cache. If you need some realtime info a preprocess hook shouldn't be the way to go. Instead I'd recommend to expose an explicitly uncached custom route that takes a uid as argumemt, and then returns a JSON response built from a callback that holds your code. And finally add some JS to your site that requests that route and places the outcome somewhere in your markup.
    – leymannx
    4 hours ago










  • @leymannx Thank you. I am not looking for a specific status. If there is a shift of 3 minutes it is OK
    – mathmath
    3 hours ago






  • 1




    What's been done in a preprocess hook normally is cached until you flush the Drupal cache for the next time if you don't implement some kind of a cache busting logic.
    – leymannx
    3 hours ago








  • 1




    If you don't do it the AJAX way you'll need to add something like $variables['#cache']['max-age'] = strtotime('+3 minutes'); in the preprocess hook, switch off the page cache module, and make sure any reverse proxy sitting in front of the site is configured to react. You might find the AJAX route to be less of a bother in the end
    – Clive
    2 hours ago















up vote
3
down vote

favorite
2












I want to display the status of the user on the account page.



I added the code below in the .theme file of my theme.



It works, but users are never displayed offline.



Why ? Is there a mistake ?



Thank you



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_subtheme_front_office.theme :



<?php
/**
* @file
* Bootstrap sub-theme.
*
* Place your custom PHP code in this file.
*/

use DrupalCoreDatabaseDatabase;

/**
* Implements hook_entity_presave().
*/
function bootstrap_subtheme_front_office_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;
}









share|improve this question









New contributor




mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.




















  • I think that the biggest issue here is cache. If you need some realtime info a preprocess hook shouldn't be the way to go. Instead I'd recommend to expose an explicitly uncached custom route that takes a uid as argumemt, and then returns a JSON response built from a callback that holds your code. And finally add some JS to your site that requests that route and places the outcome somewhere in your markup.
    – leymannx
    4 hours ago










  • @leymannx Thank you. I am not looking for a specific status. If there is a shift of 3 minutes it is OK
    – mathmath
    3 hours ago






  • 1




    What's been done in a preprocess hook normally is cached until you flush the Drupal cache for the next time if you don't implement some kind of a cache busting logic.
    – leymannx
    3 hours ago








  • 1




    If you don't do it the AJAX way you'll need to add something like $variables['#cache']['max-age'] = strtotime('+3 minutes'); in the preprocess hook, switch off the page cache module, and make sure any reverse proxy sitting in front of the site is configured to react. You might find the AJAX route to be less of a bother in the end
    – Clive
    2 hours ago













up vote
3
down vote

favorite
2









up vote
3
down vote

favorite
2






2





I want to display the status of the user on the account page.



I added the code below in the .theme file of my theme.



It works, but users are never displayed offline.



Why ? Is there a mistake ?



Thank you



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_subtheme_front_office.theme :



<?php
/**
* @file
* Bootstrap sub-theme.
*
* Place your custom PHP code in this file.
*/

use DrupalCoreDatabaseDatabase;

/**
* Implements hook_entity_presave().
*/
function bootstrap_subtheme_front_office_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;
}









share|improve this question









New contributor




mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I want to display the status of the user on the account page.



I added the code below in the .theme file of my theme.



It works, but users are never displayed offline.



Why ? Is there a mistake ?



Thank you



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_subtheme_front_office.theme :



<?php
/**
* @file
* Bootstrap sub-theme.
*
* Place your custom PHP code in this file.
*/

use DrupalCoreDatabaseDatabase;

/**
* Implements hook_entity_presave().
*/
function bootstrap_subtheme_front_office_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;
}






8 theming users






share|improve this question









New contributor




mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 4 hours ago









Kevin

17.1k848106




17.1k848106






New contributor




mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 5 hours ago









mathmath

161




161




New contributor




mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






mathmath is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












  • I think that the biggest issue here is cache. If you need some realtime info a preprocess hook shouldn't be the way to go. Instead I'd recommend to expose an explicitly uncached custom route that takes a uid as argumemt, and then returns a JSON response built from a callback that holds your code. And finally add some JS to your site that requests that route and places the outcome somewhere in your markup.
    – leymannx
    4 hours ago










  • @leymannx Thank you. I am not looking for a specific status. If there is a shift of 3 minutes it is OK
    – mathmath
    3 hours ago






  • 1




    What's been done in a preprocess hook normally is cached until you flush the Drupal cache for the next time if you don't implement some kind of a cache busting logic.
    – leymannx
    3 hours ago








  • 1




    If you don't do it the AJAX way you'll need to add something like $variables['#cache']['max-age'] = strtotime('+3 minutes'); in the preprocess hook, switch off the page cache module, and make sure any reverse proxy sitting in front of the site is configured to react. You might find the AJAX route to be less of a bother in the end
    – Clive
    2 hours ago


















  • I think that the biggest issue here is cache. If you need some realtime info a preprocess hook shouldn't be the way to go. Instead I'd recommend to expose an explicitly uncached custom route that takes a uid as argumemt, and then returns a JSON response built from a callback that holds your code. And finally add some JS to your site that requests that route and places the outcome somewhere in your markup.
    – leymannx
    4 hours ago










  • @leymannx Thank you. I am not looking for a specific status. If there is a shift of 3 minutes it is OK
    – mathmath
    3 hours ago






  • 1




    What's been done in a preprocess hook normally is cached until you flush the Drupal cache for the next time if you don't implement some kind of a cache busting logic.
    – leymannx
    3 hours ago








  • 1




    If you don't do it the AJAX way you'll need to add something like $variables['#cache']['max-age'] = strtotime('+3 minutes'); in the preprocess hook, switch off the page cache module, and make sure any reverse proxy sitting in front of the site is configured to react. You might find the AJAX route to be less of a bother in the end
    – Clive
    2 hours ago
















I think that the biggest issue here is cache. If you need some realtime info a preprocess hook shouldn't be the way to go. Instead I'd recommend to expose an explicitly uncached custom route that takes a uid as argumemt, and then returns a JSON response built from a callback that holds your code. And finally add some JS to your site that requests that route and places the outcome somewhere in your markup.
– leymannx
4 hours ago




I think that the biggest issue here is cache. If you need some realtime info a preprocess hook shouldn't be the way to go. Instead I'd recommend to expose an explicitly uncached custom route that takes a uid as argumemt, and then returns a JSON response built from a callback that holds your code. And finally add some JS to your site that requests that route and places the outcome somewhere in your markup.
– leymannx
4 hours ago












@leymannx Thank you. I am not looking for a specific status. If there is a shift of 3 minutes it is OK
– mathmath
3 hours ago




@leymannx Thank you. I am not looking for a specific status. If there is a shift of 3 minutes it is OK
– mathmath
3 hours ago




1




1




What's been done in a preprocess hook normally is cached until you flush the Drupal cache for the next time if you don't implement some kind of a cache busting logic.
– leymannx
3 hours ago






What's been done in a preprocess hook normally is cached until you flush the Drupal cache for the next time if you don't implement some kind of a cache busting logic.
– leymannx
3 hours ago






1




1




If you don't do it the AJAX way you'll need to add something like $variables['#cache']['max-age'] = strtotime('+3 minutes'); in the preprocess hook, switch off the page cache module, and make sure any reverse proxy sitting in front of the site is configured to react. You might find the AJAX route to be less of a bother in the end
– Clive
2 hours ago




If you don't do it the AJAX way you'll need to add something like $variables['#cache']['max-age'] = strtotime('+3 minutes'); in the preprocess hook, switch off the page cache module, and make sure any reverse proxy sitting in front of the site is configured to react. You might find the AJAX route to be less of a bother in the end
– Clive
2 hours ago










3 Answers
3






active

oldest

votes

















up vote
1
down vote













The ultimate problem with Drupal and user status is cache, but here is some code I am working with.



if ($variables['accessTime'] > (intval(time()) - 800)) {
$variables['sessionState'] = 'active';
}
else {
$variables['sessionState'] = 'inactive';
}


Then in Twig you can do stuff like:



{% if sessionState == active %}

{% endif %}


or



{%
set activeClass = [
sessionState == 'active' ? 'Active' : 'Not Active'
]
%}





share|improve this answer























  • Why do you need to target the page title? I don't get that? This deals with showing the users status on their account page, basically users looking at other user profiles. They're using the proper preprocess function, the code itself is not accurate.
    – Kevin
    4 hours ago










  • Maybe I misread the question let me update my answer. Doesn't need to say so much anyway.
    – Prestosaurus
    4 hours ago










  • @Prestosaurus The status of the user will be displayed at several places on the site. Conversation, comment, product, store, node, group, ... wherever his name appears. I want the code to be user with the TWIG code of my question
    – mathmath
    4 hours ago










  • I think you're going to have a harder time doing that sitewide, because your cache times will decrease dramatically just to ensure you're showing an accurate status. I thought you just wanted this on their profile.
    – Kevin
    4 hours ago








  • 1




    @mathmath Kind of where my mind was going. Following this question, and please see: drupal.stackexchange.com/questions/271119/… as well as drupal.stackexchange.com/questions/273350/…
    – Prestosaurus
    4 hours ago


















up vote
1
down vote













I guess the problem lies here, for starters:



//- 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';
}


The if ($user->getLastLoginTime()) { will be TRUE always, after the user has logged in once. This will prevent the else from ever being hit, thus, 'Offline' never gets set.



I don't know what the difference is for you in terms of time between "Absent" or "Offline", but it sounds like you'd want to figure another calculation here to know what to set status to.



This can be simplified to:



function bootstrap_subtheme_front_office_preprocess_user(&$variables) {
// get user object
$user = $variables['elements']['#user'];

$last = $user->getLastAccessedTime();
$now = Drupal::time()->getRequestTime();

switch ($last) {
case ($last > ($now - 900)) :
$variables['status'] = 'Online';
break;
case (($last < ($now - 900)) && $last > ($now - 1800)) :
$variables['status'] = 'Absent';
break;
default:
$variables['status'] = 'Offline';
break;
}
}


Then break down smaller functions to evaulate the time against how many minutes ago they were active... use it in the switch above.



getLastAccessedTime() does:




The timestamp when the account last accessed the site.



A value of 0 means the user has never accessed the site.




So you don’t need a db_select.






share|improve this answer























  • I am trying to put the user absent after 15 minutes of inactivity and offline after 30 minutes of inactivity (or when disconnected)
    – mathmath
    5 hours ago












  • With the access value can we do 3 status? from 0 to 15 minutes "online", from 15 to 30 minustes "absent" (means unavailable), 30 minutes superior "offline".
    – mathmath
    5 hours ago










  • The access value is the timestamp when the account last accessed the site according to the documentation. I don't know how frequently it is written to.
    – Kevin
    5 hours ago












  • So fill in the blanks, this is just boilerplate code.
    – Kevin
    4 hours ago










  • This answer here proves that its the method you want, not the last login time: drupal.stackexchange.com/a/21873/57
    – Kevin
    4 hours ago


















up vote
0
down vote













As Prestosaurus in their answer already said, the biggest issue here is cache. What's been done in preprocess hooks will be cached until you flush Drupal's theme cache the next time. You'll need to ensure the cache gets busted by reducing the max age of your piece of markup (not recommended) or by ajaxifying it completely (recommended).





I just created User Online Status. This module contains a new pseudo field for user entities, a route to return a given user's online status as a non-cached JSON response, and some JS that checks that response and prints the online status into the pseudo field.



It still needs proper description, screenshots and tests, though.




  1. Download and enable the module.

  2. Go to http://d8.localhost/user/1 to see the online status of user 1.


Optionally: Go to http://d8.localhost/admin/config/people/accounts/display to adjust the "Online Status" field's weight.





Credits for the switch snippet to Kevin Quillen (answer).






share|improve this answer























  • I'll add a theme tomorrow, so everybody can create their own template to add a label or customize the markup.
    – leymannx
    1 hour ago










  • This is great news, it is late I will test it tomorrow. It is obvious that drupal 8 does not include this feature in the kernel. Yet drupal has always managed users. Does your module manage the idle status ? Thank you
    – mathmath
    1 hour ago






  • 1




    Maybe you should replace case ($last > ($now - 900)): by case ($last >= ($now - 900)): otherwise there will be a white at 900 seconds
    – mathmath
    1 hour ago











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "220"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});






mathmath is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f273973%2fshow-if-a-user-is-online-idle-or-offline-on-their-account-page%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote













The ultimate problem with Drupal and user status is cache, but here is some code I am working with.



if ($variables['accessTime'] > (intval(time()) - 800)) {
$variables['sessionState'] = 'active';
}
else {
$variables['sessionState'] = 'inactive';
}


Then in Twig you can do stuff like:



{% if sessionState == active %}

{% endif %}


or



{%
set activeClass = [
sessionState == 'active' ? 'Active' : 'Not Active'
]
%}





share|improve this answer























  • Why do you need to target the page title? I don't get that? This deals with showing the users status on their account page, basically users looking at other user profiles. They're using the proper preprocess function, the code itself is not accurate.
    – Kevin
    4 hours ago










  • Maybe I misread the question let me update my answer. Doesn't need to say so much anyway.
    – Prestosaurus
    4 hours ago










  • @Prestosaurus The status of the user will be displayed at several places on the site. Conversation, comment, product, store, node, group, ... wherever his name appears. I want the code to be user with the TWIG code of my question
    – mathmath
    4 hours ago










  • I think you're going to have a harder time doing that sitewide, because your cache times will decrease dramatically just to ensure you're showing an accurate status. I thought you just wanted this on their profile.
    – Kevin
    4 hours ago








  • 1




    @mathmath Kind of where my mind was going. Following this question, and please see: drupal.stackexchange.com/questions/271119/… as well as drupal.stackexchange.com/questions/273350/…
    – Prestosaurus
    4 hours ago















up vote
1
down vote













The ultimate problem with Drupal and user status is cache, but here is some code I am working with.



if ($variables['accessTime'] > (intval(time()) - 800)) {
$variables['sessionState'] = 'active';
}
else {
$variables['sessionState'] = 'inactive';
}


Then in Twig you can do stuff like:



{% if sessionState == active %}

{% endif %}


or



{%
set activeClass = [
sessionState == 'active' ? 'Active' : 'Not Active'
]
%}





share|improve this answer























  • Why do you need to target the page title? I don't get that? This deals with showing the users status on their account page, basically users looking at other user profiles. They're using the proper preprocess function, the code itself is not accurate.
    – Kevin
    4 hours ago










  • Maybe I misread the question let me update my answer. Doesn't need to say so much anyway.
    – Prestosaurus
    4 hours ago










  • @Prestosaurus The status of the user will be displayed at several places on the site. Conversation, comment, product, store, node, group, ... wherever his name appears. I want the code to be user with the TWIG code of my question
    – mathmath
    4 hours ago










  • I think you're going to have a harder time doing that sitewide, because your cache times will decrease dramatically just to ensure you're showing an accurate status. I thought you just wanted this on their profile.
    – Kevin
    4 hours ago








  • 1




    @mathmath Kind of where my mind was going. Following this question, and please see: drupal.stackexchange.com/questions/271119/… as well as drupal.stackexchange.com/questions/273350/…
    – Prestosaurus
    4 hours ago













up vote
1
down vote










up vote
1
down vote









The ultimate problem with Drupal and user status is cache, but here is some code I am working with.



if ($variables['accessTime'] > (intval(time()) - 800)) {
$variables['sessionState'] = 'active';
}
else {
$variables['sessionState'] = 'inactive';
}


Then in Twig you can do stuff like:



{% if sessionState == active %}

{% endif %}


or



{%
set activeClass = [
sessionState == 'active' ? 'Active' : 'Not Active'
]
%}





share|improve this answer














The ultimate problem with Drupal and user status is cache, but here is some code I am working with.



if ($variables['accessTime'] > (intval(time()) - 800)) {
$variables['sessionState'] = 'active';
}
else {
$variables['sessionState'] = 'inactive';
}


Then in Twig you can do stuff like:



{% if sessionState == active %}

{% endif %}


or



{%
set activeClass = [
sessionState == 'active' ? 'Active' : 'Not Active'
]
%}






share|improve this answer














share|improve this answer



share|improve this answer








edited 4 hours ago

























answered 4 hours ago









Prestosaurus

490111




490111












  • Why do you need to target the page title? I don't get that? This deals with showing the users status on their account page, basically users looking at other user profiles. They're using the proper preprocess function, the code itself is not accurate.
    – Kevin
    4 hours ago










  • Maybe I misread the question let me update my answer. Doesn't need to say so much anyway.
    – Prestosaurus
    4 hours ago










  • @Prestosaurus The status of the user will be displayed at several places on the site. Conversation, comment, product, store, node, group, ... wherever his name appears. I want the code to be user with the TWIG code of my question
    – mathmath
    4 hours ago










  • I think you're going to have a harder time doing that sitewide, because your cache times will decrease dramatically just to ensure you're showing an accurate status. I thought you just wanted this on their profile.
    – Kevin
    4 hours ago








  • 1




    @mathmath Kind of where my mind was going. Following this question, and please see: drupal.stackexchange.com/questions/271119/… as well as drupal.stackexchange.com/questions/273350/…
    – Prestosaurus
    4 hours ago


















  • Why do you need to target the page title? I don't get that? This deals with showing the users status on their account page, basically users looking at other user profiles. They're using the proper preprocess function, the code itself is not accurate.
    – Kevin
    4 hours ago










  • Maybe I misread the question let me update my answer. Doesn't need to say so much anyway.
    – Prestosaurus
    4 hours ago










  • @Prestosaurus The status of the user will be displayed at several places on the site. Conversation, comment, product, store, node, group, ... wherever his name appears. I want the code to be user with the TWIG code of my question
    – mathmath
    4 hours ago










  • I think you're going to have a harder time doing that sitewide, because your cache times will decrease dramatically just to ensure you're showing an accurate status. I thought you just wanted this on their profile.
    – Kevin
    4 hours ago








  • 1




    @mathmath Kind of where my mind was going. Following this question, and please see: drupal.stackexchange.com/questions/271119/… as well as drupal.stackexchange.com/questions/273350/…
    – Prestosaurus
    4 hours ago
















Why do you need to target the page title? I don't get that? This deals with showing the users status on their account page, basically users looking at other user profiles. They're using the proper preprocess function, the code itself is not accurate.
– Kevin
4 hours ago




Why do you need to target the page title? I don't get that? This deals with showing the users status on their account page, basically users looking at other user profiles. They're using the proper preprocess function, the code itself is not accurate.
– Kevin
4 hours ago












Maybe I misread the question let me update my answer. Doesn't need to say so much anyway.
– Prestosaurus
4 hours ago




Maybe I misread the question let me update my answer. Doesn't need to say so much anyway.
– Prestosaurus
4 hours ago












@Prestosaurus The status of the user will be displayed at several places on the site. Conversation, comment, product, store, node, group, ... wherever his name appears. I want the code to be user with the TWIG code of my question
– mathmath
4 hours ago




@Prestosaurus The status of the user will be displayed at several places on the site. Conversation, comment, product, store, node, group, ... wherever his name appears. I want the code to be user with the TWIG code of my question
– mathmath
4 hours ago












I think you're going to have a harder time doing that sitewide, because your cache times will decrease dramatically just to ensure you're showing an accurate status. I thought you just wanted this on their profile.
– Kevin
4 hours ago






I think you're going to have a harder time doing that sitewide, because your cache times will decrease dramatically just to ensure you're showing an accurate status. I thought you just wanted this on their profile.
– Kevin
4 hours ago






1




1




@mathmath Kind of where my mind was going. Following this question, and please see: drupal.stackexchange.com/questions/271119/… as well as drupal.stackexchange.com/questions/273350/…
– Prestosaurus
4 hours ago




@mathmath Kind of where my mind was going. Following this question, and please see: drupal.stackexchange.com/questions/271119/… as well as drupal.stackexchange.com/questions/273350/…
– Prestosaurus
4 hours ago












up vote
1
down vote













I guess the problem lies here, for starters:



//- 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';
}


The if ($user->getLastLoginTime()) { will be TRUE always, after the user has logged in once. This will prevent the else from ever being hit, thus, 'Offline' never gets set.



I don't know what the difference is for you in terms of time between "Absent" or "Offline", but it sounds like you'd want to figure another calculation here to know what to set status to.



This can be simplified to:



function bootstrap_subtheme_front_office_preprocess_user(&$variables) {
// get user object
$user = $variables['elements']['#user'];

$last = $user->getLastAccessedTime();
$now = Drupal::time()->getRequestTime();

switch ($last) {
case ($last > ($now - 900)) :
$variables['status'] = 'Online';
break;
case (($last < ($now - 900)) && $last > ($now - 1800)) :
$variables['status'] = 'Absent';
break;
default:
$variables['status'] = 'Offline';
break;
}
}


Then break down smaller functions to evaulate the time against how many minutes ago they were active... use it in the switch above.



getLastAccessedTime() does:




The timestamp when the account last accessed the site.



A value of 0 means the user has never accessed the site.




So you don’t need a db_select.






share|improve this answer























  • I am trying to put the user absent after 15 minutes of inactivity and offline after 30 minutes of inactivity (or when disconnected)
    – mathmath
    5 hours ago












  • With the access value can we do 3 status? from 0 to 15 minutes "online", from 15 to 30 minustes "absent" (means unavailable), 30 minutes superior "offline".
    – mathmath
    5 hours ago










  • The access value is the timestamp when the account last accessed the site according to the documentation. I don't know how frequently it is written to.
    – Kevin
    5 hours ago












  • So fill in the blanks, this is just boilerplate code.
    – Kevin
    4 hours ago










  • This answer here proves that its the method you want, not the last login time: drupal.stackexchange.com/a/21873/57
    – Kevin
    4 hours ago















up vote
1
down vote













I guess the problem lies here, for starters:



//- 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';
}


The if ($user->getLastLoginTime()) { will be TRUE always, after the user has logged in once. This will prevent the else from ever being hit, thus, 'Offline' never gets set.



I don't know what the difference is for you in terms of time between "Absent" or "Offline", but it sounds like you'd want to figure another calculation here to know what to set status to.



This can be simplified to:



function bootstrap_subtheme_front_office_preprocess_user(&$variables) {
// get user object
$user = $variables['elements']['#user'];

$last = $user->getLastAccessedTime();
$now = Drupal::time()->getRequestTime();

switch ($last) {
case ($last > ($now - 900)) :
$variables['status'] = 'Online';
break;
case (($last < ($now - 900)) && $last > ($now - 1800)) :
$variables['status'] = 'Absent';
break;
default:
$variables['status'] = 'Offline';
break;
}
}


Then break down smaller functions to evaulate the time against how many minutes ago they were active... use it in the switch above.



getLastAccessedTime() does:




The timestamp when the account last accessed the site.



A value of 0 means the user has never accessed the site.




So you don’t need a db_select.






share|improve this answer























  • I am trying to put the user absent after 15 minutes of inactivity and offline after 30 minutes of inactivity (or when disconnected)
    – mathmath
    5 hours ago












  • With the access value can we do 3 status? from 0 to 15 minutes "online", from 15 to 30 minustes "absent" (means unavailable), 30 minutes superior "offline".
    – mathmath
    5 hours ago










  • The access value is the timestamp when the account last accessed the site according to the documentation. I don't know how frequently it is written to.
    – Kevin
    5 hours ago












  • So fill in the blanks, this is just boilerplate code.
    – Kevin
    4 hours ago










  • This answer here proves that its the method you want, not the last login time: drupal.stackexchange.com/a/21873/57
    – Kevin
    4 hours ago













up vote
1
down vote










up vote
1
down vote









I guess the problem lies here, for starters:



//- 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';
}


The if ($user->getLastLoginTime()) { will be TRUE always, after the user has logged in once. This will prevent the else from ever being hit, thus, 'Offline' never gets set.



I don't know what the difference is for you in terms of time between "Absent" or "Offline", but it sounds like you'd want to figure another calculation here to know what to set status to.



This can be simplified to:



function bootstrap_subtheme_front_office_preprocess_user(&$variables) {
// get user object
$user = $variables['elements']['#user'];

$last = $user->getLastAccessedTime();
$now = Drupal::time()->getRequestTime();

switch ($last) {
case ($last > ($now - 900)) :
$variables['status'] = 'Online';
break;
case (($last < ($now - 900)) && $last > ($now - 1800)) :
$variables['status'] = 'Absent';
break;
default:
$variables['status'] = 'Offline';
break;
}
}


Then break down smaller functions to evaulate the time against how many minutes ago they were active... use it in the switch above.



getLastAccessedTime() does:




The timestamp when the account last accessed the site.



A value of 0 means the user has never accessed the site.




So you don’t need a db_select.






share|improve this answer














I guess the problem lies here, for starters:



//- 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';
}


The if ($user->getLastLoginTime()) { will be TRUE always, after the user has logged in once. This will prevent the else from ever being hit, thus, 'Offline' never gets set.



I don't know what the difference is for you in terms of time between "Absent" or "Offline", but it sounds like you'd want to figure another calculation here to know what to set status to.



This can be simplified to:



function bootstrap_subtheme_front_office_preprocess_user(&$variables) {
// get user object
$user = $variables['elements']['#user'];

$last = $user->getLastAccessedTime();
$now = Drupal::time()->getRequestTime();

switch ($last) {
case ($last > ($now - 900)) :
$variables['status'] = 'Online';
break;
case (($last < ($now - 900)) && $last > ($now - 1800)) :
$variables['status'] = 'Absent';
break;
default:
$variables['status'] = 'Offline';
break;
}
}


Then break down smaller functions to evaulate the time against how many minutes ago they were active... use it in the switch above.



getLastAccessedTime() does:




The timestamp when the account last accessed the site.



A value of 0 means the user has never accessed the site.




So you don’t need a db_select.







share|improve this answer














share|improve this answer



share|improve this answer








edited 2 hours ago

























answered 5 hours ago









Kevin

17.1k848106




17.1k848106












  • I am trying to put the user absent after 15 minutes of inactivity and offline after 30 minutes of inactivity (or when disconnected)
    – mathmath
    5 hours ago












  • With the access value can we do 3 status? from 0 to 15 minutes "online", from 15 to 30 minustes "absent" (means unavailable), 30 minutes superior "offline".
    – mathmath
    5 hours ago










  • The access value is the timestamp when the account last accessed the site according to the documentation. I don't know how frequently it is written to.
    – Kevin
    5 hours ago












  • So fill in the blanks, this is just boilerplate code.
    – Kevin
    4 hours ago










  • This answer here proves that its the method you want, not the last login time: drupal.stackexchange.com/a/21873/57
    – Kevin
    4 hours ago


















  • I am trying to put the user absent after 15 minutes of inactivity and offline after 30 minutes of inactivity (or when disconnected)
    – mathmath
    5 hours ago












  • With the access value can we do 3 status? from 0 to 15 minutes "online", from 15 to 30 minustes "absent" (means unavailable), 30 minutes superior "offline".
    – mathmath
    5 hours ago










  • The access value is the timestamp when the account last accessed the site according to the documentation. I don't know how frequently it is written to.
    – Kevin
    5 hours ago












  • So fill in the blanks, this is just boilerplate code.
    – Kevin
    4 hours ago










  • This answer here proves that its the method you want, not the last login time: drupal.stackexchange.com/a/21873/57
    – Kevin
    4 hours ago
















I am trying to put the user absent after 15 minutes of inactivity and offline after 30 minutes of inactivity (or when disconnected)
– mathmath
5 hours ago






I am trying to put the user absent after 15 minutes of inactivity and offline after 30 minutes of inactivity (or when disconnected)
– mathmath
5 hours ago














With the access value can we do 3 status? from 0 to 15 minutes "online", from 15 to 30 minustes "absent" (means unavailable), 30 minutes superior "offline".
– mathmath
5 hours ago




With the access value can we do 3 status? from 0 to 15 minutes "online", from 15 to 30 minustes "absent" (means unavailable), 30 minutes superior "offline".
– mathmath
5 hours ago












The access value is the timestamp when the account last accessed the site according to the documentation. I don't know how frequently it is written to.
– Kevin
5 hours ago






The access value is the timestamp when the account last accessed the site according to the documentation. I don't know how frequently it is written to.
– Kevin
5 hours ago














So fill in the blanks, this is just boilerplate code.
– Kevin
4 hours ago




So fill in the blanks, this is just boilerplate code.
– Kevin
4 hours ago












This answer here proves that its the method you want, not the last login time: drupal.stackexchange.com/a/21873/57
– Kevin
4 hours ago




This answer here proves that its the method you want, not the last login time: drupal.stackexchange.com/a/21873/57
– Kevin
4 hours ago










up vote
0
down vote













As Prestosaurus in their answer already said, the biggest issue here is cache. What's been done in preprocess hooks will be cached until you flush Drupal's theme cache the next time. You'll need to ensure the cache gets busted by reducing the max age of your piece of markup (not recommended) or by ajaxifying it completely (recommended).





I just created User Online Status. This module contains a new pseudo field for user entities, a route to return a given user's online status as a non-cached JSON response, and some JS that checks that response and prints the online status into the pseudo field.



It still needs proper description, screenshots and tests, though.




  1. Download and enable the module.

  2. Go to http://d8.localhost/user/1 to see the online status of user 1.


Optionally: Go to http://d8.localhost/admin/config/people/accounts/display to adjust the "Online Status" field's weight.





Credits for the switch snippet to Kevin Quillen (answer).






share|improve this answer























  • I'll add a theme tomorrow, so everybody can create their own template to add a label or customize the markup.
    – leymannx
    1 hour ago










  • This is great news, it is late I will test it tomorrow. It is obvious that drupal 8 does not include this feature in the kernel. Yet drupal has always managed users. Does your module manage the idle status ? Thank you
    – mathmath
    1 hour ago






  • 1




    Maybe you should replace case ($last > ($now - 900)): by case ($last >= ($now - 900)): otherwise there will be a white at 900 seconds
    – mathmath
    1 hour ago















up vote
0
down vote













As Prestosaurus in their answer already said, the biggest issue here is cache. What's been done in preprocess hooks will be cached until you flush Drupal's theme cache the next time. You'll need to ensure the cache gets busted by reducing the max age of your piece of markup (not recommended) or by ajaxifying it completely (recommended).





I just created User Online Status. This module contains a new pseudo field for user entities, a route to return a given user's online status as a non-cached JSON response, and some JS that checks that response and prints the online status into the pseudo field.



It still needs proper description, screenshots and tests, though.




  1. Download and enable the module.

  2. Go to http://d8.localhost/user/1 to see the online status of user 1.


Optionally: Go to http://d8.localhost/admin/config/people/accounts/display to adjust the "Online Status" field's weight.





Credits for the switch snippet to Kevin Quillen (answer).






share|improve this answer























  • I'll add a theme tomorrow, so everybody can create their own template to add a label or customize the markup.
    – leymannx
    1 hour ago










  • This is great news, it is late I will test it tomorrow. It is obvious that drupal 8 does not include this feature in the kernel. Yet drupal has always managed users. Does your module manage the idle status ? Thank you
    – mathmath
    1 hour ago






  • 1




    Maybe you should replace case ($last > ($now - 900)): by case ($last >= ($now - 900)): otherwise there will be a white at 900 seconds
    – mathmath
    1 hour ago













up vote
0
down vote










up vote
0
down vote









As Prestosaurus in their answer already said, the biggest issue here is cache. What's been done in preprocess hooks will be cached until you flush Drupal's theme cache the next time. You'll need to ensure the cache gets busted by reducing the max age of your piece of markup (not recommended) or by ajaxifying it completely (recommended).





I just created User Online Status. This module contains a new pseudo field for user entities, a route to return a given user's online status as a non-cached JSON response, and some JS that checks that response and prints the online status into the pseudo field.



It still needs proper description, screenshots and tests, though.




  1. Download and enable the module.

  2. Go to http://d8.localhost/user/1 to see the online status of user 1.


Optionally: Go to http://d8.localhost/admin/config/people/accounts/display to adjust the "Online Status" field's weight.





Credits for the switch snippet to Kevin Quillen (answer).






share|improve this answer














As Prestosaurus in their answer already said, the biggest issue here is cache. What's been done in preprocess hooks will be cached until you flush Drupal's theme cache the next time. You'll need to ensure the cache gets busted by reducing the max age of your piece of markup (not recommended) or by ajaxifying it completely (recommended).





I just created User Online Status. This module contains a new pseudo field for user entities, a route to return a given user's online status as a non-cached JSON response, and some JS that checks that response and prints the online status into the pseudo field.



It still needs proper description, screenshots and tests, though.




  1. Download and enable the module.

  2. Go to http://d8.localhost/user/1 to see the online status of user 1.


Optionally: Go to http://d8.localhost/admin/config/people/accounts/display to adjust the "Online Status" field's weight.





Credits for the switch snippet to Kevin Quillen (answer).







share|improve this answer














share|improve this answer



share|improve this answer








edited 1 hour ago

























answered 2 hours ago









leymannx

6,74942658




6,74942658












  • I'll add a theme tomorrow, so everybody can create their own template to add a label or customize the markup.
    – leymannx
    1 hour ago










  • This is great news, it is late I will test it tomorrow. It is obvious that drupal 8 does not include this feature in the kernel. Yet drupal has always managed users. Does your module manage the idle status ? Thank you
    – mathmath
    1 hour ago






  • 1




    Maybe you should replace case ($last > ($now - 900)): by case ($last >= ($now - 900)): otherwise there will be a white at 900 seconds
    – mathmath
    1 hour ago


















  • I'll add a theme tomorrow, so everybody can create their own template to add a label or customize the markup.
    – leymannx
    1 hour ago










  • This is great news, it is late I will test it tomorrow. It is obvious that drupal 8 does not include this feature in the kernel. Yet drupal has always managed users. Does your module manage the idle status ? Thank you
    – mathmath
    1 hour ago






  • 1




    Maybe you should replace case ($last > ($now - 900)): by case ($last >= ($now - 900)): otherwise there will be a white at 900 seconds
    – mathmath
    1 hour ago
















I'll add a theme tomorrow, so everybody can create their own template to add a label or customize the markup.
– leymannx
1 hour ago




I'll add a theme tomorrow, so everybody can create their own template to add a label or customize the markup.
– leymannx
1 hour ago












This is great news, it is late I will test it tomorrow. It is obvious that drupal 8 does not include this feature in the kernel. Yet drupal has always managed users. Does your module manage the idle status ? Thank you
– mathmath
1 hour ago




This is great news, it is late I will test it tomorrow. It is obvious that drupal 8 does not include this feature in the kernel. Yet drupal has always managed users. Does your module manage the idle status ? Thank you
– mathmath
1 hour ago




1




1




Maybe you should replace case ($last > ($now - 900)): by case ($last >= ($now - 900)): otherwise there will be a white at 900 seconds
– mathmath
1 hour ago




Maybe you should replace case ($last > ($now - 900)): by case ($last >= ($now - 900)): otherwise there will be a white at 900 seconds
– mathmath
1 hour ago










mathmath is a new contributor. Be nice, and check out our Code of Conduct.










draft saved

draft discarded


















mathmath is a new contributor. Be nice, and check out our Code of Conduct.













mathmath is a new contributor. Be nice, and check out our Code of Conduct.












mathmath is a new contributor. Be nice, and check out our Code of Conduct.
















Thanks for contributing an answer to Drupal Answers!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f273973%2fshow-if-a-user-is-online-idle-or-offline-on-their-account-page%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu