Skip to content

Commit

Permalink
[Core] Add getLastLogin helper to user class
Browse files Browse the repository at this point in the history
This moves the query to get the last user login from the
dashboard code to the User class, where it more accurately
belongs.

(This was extracted from aces#5896 where it was necessary because
multiple modules use the last login time to determine if something
is new.)
  • Loading branch information
driusan committed Jan 7, 2020
1 parent 8269ee4 commit b48b654
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
10 changes: 1 addition & 9 deletions modules/dashboard/php/dashboard.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,7 @@ class Dashboard extends \NDB_Form
$site = $user->getSiteNames();

$userID = $user->getUsername();
$last_login = $DB->pselectOne(
"SELECT MAX(Login_timestamp)
FROM user_login_history
WHERE Login_timestamp < (SELECT MAX(Login_timestamp)
FROM user_login_history
WHERE userID=:UserID AND Success='Y')
AND userID=:UserID AND Success='Y'",
array('UserID' => $userID)
);
$last_login = $user->getLastLogin();

$siteID = $user->getCenterIDs();
$this->tpl_data['user_site'] = $siteID;
Expand Down
22 changes: 22 additions & 0 deletions php/libraries/User.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -532,5 +532,27 @@ class User extends UserPermissions
(string) $this->userInfo['Password_hash']
);
}


/**
* Return the last login of this user to the given database.
*
* @param \Database $DB The database connection to check
*
* @return string The date in YYYY-MM-DD format.
*/
function getLastLogin(\Database $DB)
{
return $DB->pselectOne(
"SELECT MAX(Login_timestamp)
FROM user_login_history
WHERE Login_timestamp <
(SELECT MAX(Login_timestamp)
FROM user_login_history
WHERE userID=:UserID AND Success='Y')
AND userID=:UserID AND Success='Y'",
array('UserID' => $this->getUsername())
);

}

0 comments on commit b48b654

Please sign in to comment.