Show if a user is online, idle, or offline on their account page?
I guess the problem lies here, for starters:

//- The user has logged in at least once
  if ($user-&gt;getLastLoginTime()) {
    if (account_is_logged_in_less_then_thirty_minutes($user-&gt;id())) {
      $status = 'Online';
    }
    else {
      $status = 'Absent';
    }
  }
  else {
    $status = 'Offline';
  }


The if ($user-&gt;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(&amp;$variables) {
  // get user object
 $user = $variables['elements']['#user'];

 $last = $user-&gt;getLastAccessedTime();
 $now = \Drupal::time()-&gt;getRequestTime();

 switch ($last) {
   case ($last &gt; ($now - 900)) :
     $variables['status'] = 'Online';
     break;
   case (($last &lt; ($now - 900)) &amp;&amp; $last &gt; ($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.