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?