[user:reg-since] and [user:log-since] tokens return incorrect values
| Project: | Token |
| Version: | 6.x-1.x-dev |
| Component: | Code |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | active |
Jump to:
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
#2
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
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
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
Grand!
Thanks for the quick reply!