Drupal 6.19
Rules 6.x-1.3
Token 6.x-1.15
Userpoints 6.x-1.1 (for demonstration and because I use it with rules, but any token value changing rules action could cause a similar problem in my opinion)

I am using Rules 6.x to administer the logic of how many userpoints to add/subtract for a user based on her/his "actions" (like creating a node, commenting, etc.) on a website. Thanks for the module: I found it a valuable tool to avoid coding userpoints integration for modules without such, but which have rules integration though.

Reproducing the problem:
1.) Creating a rule caused by any event, e.g. adding a node.
1.1) Add any condition, e.g. node type = Story.
1.2) Add two actions in this order:
1.2.a) Add any amount of userpoints for the acting user, e.g. 15.
1.2.b) Display the current userpoints with a rules display message action, using, for once, the token [account:userpoints], and, for demonstration, in the same message after the token the php code " echo userpoints_get_current_points($account->uid); ". I would expect the result of the php code to be of the exact same amount as of the token.

2.) Duplicate rule 1.) just for an easy example (there could be different but overlapping conditions on the two rules, but both rules must be for the same event).

3.) When the two rules are in place and active, create a node of type Story to trigger both rules.

4.) Both couple of actions are performed.

4.1) Userpoints itself displays its own messages (if not disabled in the userpoints administration) of how many points are added and the resulting total for both rules. This displays the correct total of userpoints for each rule.

4.2) The two display message actions wrongly show the same value in case of the token and the correct different values in case of the php code.

4.3) I assume the problem lies in: that the evaluation of tokens in rules is optimized to be done only once for an event, regardless of the number of rules, conditions and actions. Maybe this assumption is too simplified as I have not debugged through the rules code, just a thought.

4.4) The correct behaviour in my opinion should be to evaluate tokens for each action as the action might change the value of certain tokens like in this example.

4.5) My current workaround is to use php to evaluate the value instead of the token.

5.) Could this behaviour be corrected in rules? Would that have a big impact on the speed of rules with tokens?

Thanks again for such a valuable module.

Comments

fago’s picture

Component: Module Integrations » Miscellaneous
Status: Active » Closed (works as designed)

I'd say this is by design as token implements a caching system. We could disable that in general in Rules, bad that would hurt performance. As possible solution though, I'd suggest doing an action "Clear token cache" which one can invoke manually if necessary. This should probably go into the token module though - or even into another project.

meichr’s picture

Fago, thanks for the explanation, I hadn't thought about the token module doing the caching. The performance impact problem makes sense to me, not to disable token cache in rules.

To take up your suggestions for a solution I created a feature request issue at the token module.

Christian.