[user:reg-since] and [user:log-since] tokens return incorrect values

atmanning - March 31, 2009 - 04:02
Project:Token
Version:6.x-1.x-dev
Component:Code
Category:bug report
Priority:normal
Assigned:Unassigned
Status:active
Description

When trying to make the rules module work with numeric expressions, I tried to create a rule that updates a user's role 10 days after user registration.

While debugging the rules, I noticed that the token [reg-since] doesn't return the correct value which, according to the documentation, is supposed to be the number of days since the user registered.

The code should be changed in token_user.inc as follows:

$values['reg-since'] = $account->uid ? format_interval($account->created) : '';
change to
$values['reg-since'] = $account->uid ? intvall((time() - $account->created)/86400) : '';

similar for

$values['log-since'] = $account->uid ? format_interval($account->access) : '';
change to
$values['log-since'] = $account->uid ? intval((time() - $account->access)/86400) : '';

#1

atmanning - March 31, 2009 - 04:05
Title:reg-since and log-since return incorrect values» [user:reg-since] and [user:log-since] tokens return incorrect values

#2

greggles - April 10, 2009 - 17:55

Any chance you could provide this as a patch?

Also, I'm not sure this is the right thing to do because we probably should be using format_interval. Token is primarily designed to provide strings for users, not for php evaluation. Perhaps the "fix" here is to update the description to say "In human readable format describing the time period."

#3

majnoona - October 7, 2009 - 19:21

Any progress on this? Or is there a workaround?
I'm trying to create a rule that redirects users to their profile page if it's the first time they log in (reg-since == login-since).

thanks,
m

#4

Chadwick Wood - October 8, 2009 - 00:53

My workaround is to create my own little module that adds this behavior as an addition to tokens, rather than changing the core token module behavior. My code is just based off of atmanning's code. Here's the module code (sorry I'm crunched for time to include this as a whole module:

<?php
function user_token_extra_token_values($type, $object = NULL, $options = array()) {
 
$values = array();
  switch (
$type) {
    case
'user':
      if (isset(
$object)) {
       
$account = $object;
      }
      else {
        global
$user;
       
$account = user_load(array('uid' => $user->uid));
      }

     
$values['reg-since-int'] = $account->uid ? intval((time() - $account->created)/86400) : '';
     
$values['log-since-int'] = $account->uid ? intval((time() - $account->access)/86400) : '';
      break;
  }
  return
$values;
}

function
user_token_extra_token_list($type = 'all') {
  if (
$type == 'user' || $type == 'all') {
   
$tokens['user']['reg-since-int']      = t("Days since the user registered (as integer)");
   
$tokens['user']['log-since-int']      = t("Days since the user's last login (as integer)");

    return
$tokens;
  }
}
?>

Add a module with that code, and then you'll have the tokens [user:reg-since-int] and [user:log-since-int]. Please note that this code will round down to the nearest integer, so if a user logged in on the same day they registered, that token would return 0 even on that second login.

#5

majnoona - October 8, 2009 - 02:22

Grand!

Thanks for the quick reply!

 
 

Drupal is a registered trademark of Dries Buytaert.