From d89f36cc9a1c6101a6961f24e9f2d0a6ddbecf94 Mon Sep 17 00:00:00 2001 From: Rich Lott / Artful Robot Date: Sun, 3 Dec 2023 13:15:47 +0000 Subject: [PATCH] standalone: implement User fields when_last_accessed and when_updated When last accessed tracks when the logged in user last made a PHP request to the site, limited to one update per minute. When updated tracks when a user record was updated. --- .../CRM/Standaloneusers/BAO/User.php | 32 +++++++++++++++++++ ext/standaloneusers/standaloneusers.php | 9 ++++++ 2 files changed, 41 insertions(+) create mode 100644 ext/standaloneusers/CRM/Standaloneusers/BAO/User.php diff --git a/ext/standaloneusers/CRM/Standaloneusers/BAO/User.php b/ext/standaloneusers/CRM/Standaloneusers/BAO/User.php new file mode 100644 index 0000000000..70f96fd1fb --- /dev/null +++ b/ext/standaloneusers/CRM/Standaloneusers/BAO/User.php @@ -0,0 +1,32 @@ +action, ['create', 'edit']) + && empty($event->params['when_updated'])) { + // Track when_updated. + $event->params['when_updated'] = date('YmdHis'); + } + } + + public static function updateLastAccessed() { + $sess = CRM_Core_Session::singleton(); + $ufID = (int) $sess->get('ufID'); + CRM_Core_DAO::executeQuery("UPDATE civicrm_uf_match SET when_last_accessed = NOW() WHERE id = $ufID"); + $sess->set('lastAccess', time()); + } + +} diff --git a/ext/standaloneusers/standaloneusers.php b/ext/standaloneusers/standaloneusers.php index dff4299e3f..2b7b2e7931 100644 --- a/ext/standaloneusers/standaloneusers.php +++ b/ext/standaloneusers/standaloneusers.php @@ -17,6 +17,15 @@ use CRM_Standaloneusers_ExtensionUtil as E; */ function standaloneusers_civicrm_config(&$config) { _standaloneusers_civix_civicrm_config($config); + $sess = CRM_Core_Session::singleton(); + + if (!empty($sess->get('ufID'))) { + // Logged in user is making a request. + if (empty($sess->get('lastAccess')) || (time() - $sess->get('lastAccess')) >= 60) { + // Once a minute, update the when_last_accessed field + CRM_Standaloneusers_BAO_User::updateLastAccessed(); + } + } } /** -- 2.25.1