Skip to main content
2 of 3
deleted 5 characters in body
Aram Boyajyan
  • 11.2k
  • 3
  • 37
  • 60

Two options:

  1. Just as you said, a custom rules action:

     function MY_MODULE_rules_action_info() {
       return array(
         'MYMODULE_action_update_last_access' => array(
           'label' => t('Update last accessed timestamp'),
           'group' => t('Custom'),
           'parameter' => array(
             'account' => array(
               'type' => 'user',
               'label' => t('User'),
               'description' => t('User account whose last accessed timestamp will be updated.'),
             ),
           ),
         ),
       );
     }
    
     function MYMODULE_action_update_last_access($account) {
       // There might be another way to update the last accessed date, but you can
       // also do it directly in the database.
       //
       // NOTE: the code below will update the last access to current timestamp.
       // You could also do something like this, to get a random value in the last
       // 24 hours:
       //    $timestamp = rand(REQUEST_TIME - 24 * 60 * 60, REQUEST_TIME);
       $timestamp = REQUEST_TIME;
       // Update the data in database.
       db_update('users')
         ->fields(array('access' => $timestamp))
         ->condition('uid', $account->uid)
         ->execute();
     }
    
  2. Use hook_cron() implementation in a custom module. This can be cleaner if you don't need to configure this more than once.

     function MYMODULE_cron() {
       // Decide how often you want to do this, e.g. daily, weekly, etc.
       // Below is an example for daily trigger..
       $last_updated = variable_get('MYMODULE_var_last_time_access_updated', NULL);
       if (!$last_updated || $last_updated != format_date(REQUEST_TIME, 'custom', 'z')) {
         // Define user IDs which need to be updated.
         $accounts_to_update = array(1, 2, 3);
         // Update them.
         foreach ($accounts_to_update as $account_to_update) {
           // See #1 above; I'll use a random timestamp in last 12 hours this time.
           $timestamp = rand(REQUEST_TIME - 12 * 60 * 60, REQUEST_TIME);
           // Execute the query.
           db_update('users')
             ->fields(array('access' => $timestamp))
             ->condition('uid', $account_to_update)
             ->execute();
         }
       }
     }
    
Aram Boyajyan
  • 11.2k
  • 3
  • 37
  • 60