1

My articles have a field collection field and one of the fields is an integer. Users can add as many fields as they want. I'd like to reward them some userpoints, equal to the total value in their saved fields, but with an overall maximum of 5.

e.g. A user saves a first field with a value of 3. Therefore, he's awarded 3 points. When he saves another field with a value of 4 afterwards, he must be rewarded 5-3=2 points.

I wrote a rule that sums all the old and new values in the integer fields via a loop (the results are saved as variables 'Old total' and 'New total') when a new field collection is saved. Before I can grant userpoints to the user, I have to do some calculations to prevent the total amount of earned points to be larger than 5. I tried doing that by adding the following PHP code in the 'Process value' box of a 'Grant points to user' action:

if (old-total > 5) {$value = 0;}
elseif (new-total > 5) {$value = 5 - old-total;}
else {$value = [field-collection-item:field-my-integer];}
return $value;

However, when the action runs, the code seems to be ignored, because the value doesn't change at all. I don't get any errors.

What did I do wrong? How can I solve this?

0

I was able to solve this problem after finding the answer on my question 'How to use variables in a PHP evaluation in a rule?'.

Instead of activating the Rule when an event "After saving a new field collection item" occured, rather use "After updating existing content" and grant the Userpoints for the first Field Collection item in a different Rule. Afterwards, follow the instructions in the referenced answer and use the code underneath.

<?php
if ($old_total > 5) {return 0;}
elseif ($new_total > 5) {return 5 - $old_total;}
elseif ($new_total == $old_total) {return 0;}
else {return $new_total;}
?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.