2

My question is pretty straightforward: how can I check whether a user's current amount of User Points is negative with the Rules module? I'd like to create an action that should be activated when the user is granted User Points. The event is set, but I don't know how I can set the condition.

Edit: I enabled the PHP filter module in order to get the 'Execute custom PHP code' option in my Rule's conditions. (I didn't know that I had to do this before...) I used this code to check whether the user's User Points are negative:

global $user;
$num_pts = userpoints_get_current_points($user->uid, 'all');
$neg_pts = $num_pts < 0;

Unfortunately, it's always evaluated as 'false', even when the amount of User Points is negative. I've also tried this:

$num_pts = userpoints_get_current_points([userpoints-transaction:user]->uid, 'all');
$neg_pts = $num_pts < 0;

But that didn't work either (same reason). I don't get any error messages in both cases.

Are these code snippets wrong? How does the correct one look like?

2

Have a look at this Rules example (in Rules export format):

{ "rules_display_userpoints_after_updating_content" : {
    "LABEL" : "Display userpoints after updating content",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "userpoints_rules", "rules", "rules_conditional" ],
    "ON" : { "node_update" : [] },
    "DO" : [
      { "userpoints_rules_get_current_points" : {
          "USING" : { "user" : [ "site:current-user" ], "tid" : "all" },
          "PROVIDE" : { "loaded_points" : { "total_points" : "Number of points in all categories together" } }
        }
      },
      { "drupal_message" : { "message" : "You now have [total-points:value] points" } },
      { "CONDITIONAL" : [
          {
            "IF" : { "NOT data_is" : { "data" : [ "total-points" ], "op" : "\u003C", "value" : "20" } },
            "DO" : [
              { "drupal_message" : { "message" : "You have sufficient points (you still have [total-points:value] ...)." } }
            ]
          },
          { "ELSE" : [
              { "drupal_message" : { "message" : "You DO NOT have sufficient points (you only have [total-points:value] ...)." } }
            ]
          }
        ]
      }
    ]
  }
}

It does retrieve, as the very first Rules Action (not Rules Condition!) the current amount of user points of a user. If the amount is at least 20, it will display a message starting with "You have sufficient points ...", otherwise the message starts with "You DO NOT have sufficient points ...".

So you do not need custom PHP code (in your Rules Condition) to get this to work. And you don't need to enable the PHP filter for this ...). Any time you can avoid enabling the PHP filter you should take it (have a look at "What are the downsides of using PHP Filter code in blocks, nodes, views-args, etc?" to understand why).

In this example I'm using the Conditional Rules module, though you could also rework the Rules Action to performing Rules Components (in which you add the appropriate Rules Conditions), so that you don't need this extra module. For more details on that, refer to the answer to "What's the alternative to using the Conditional Rules module?".

The clue to make this rule work is the very first Rules Action "Load user points of a user". Refer to my answer to "How to access current amount of earned userpoints in a Rules Condition?" for more details on this.

2
global $user;
$num_pts = userpoints_get_current_points($user->uid, 'all');
$neg_pts = $num_pts < 0;
  • When you're working with a Contributed module, it's always a good idea to quickly look thru all of the functions that the author defines in their module. In this case, the author left lots of awesome utility functions that make it easy to integrate your modules with the User Points module. – tigertrussell Oct 14 '13 at 21:14
  • Where should I paste this code? – Jeroen Oct 14 '13 at 21:15
  • Almost wherever you want. If you've got the PHP Filter installed, you could create a Page, set the Text Format to PHP, and then drop this code in. You could also write your own custom Block, which would allow you to make something like a red/green flag if the user has negative/positive user points, then display that wherever you want. – tigertrussell Oct 14 '13 at 21:23
  • Also, if you want to toy with the code to test its effectiveness or the other things you can do, I'd suggest getting the Devel module and adding the "Execute PHP" block somewhere to your site -- then you can enter this code and hit "Execute" and see what it does! – tigertrussell Oct 14 '13 at 21:24
  • Sounds cool, but how can I use it with the Rules module? Is there an extra data selector going to be provided? – Jeroen Oct 14 '13 at 21:31
0

I found the correct code, based on @tigerrussell's answer:

global $user;
$num_pts = userpoints_get_current_points($user->uid, 'all');
if ($num_pts < 0) {return TRUE;}

Just use this snippet in an Execute custom PHP code condition. It's as simple as that! However, it is recommanded to avoid PHP code in Rules. Therefore, I would suggest to install the Conditional Rules module, as explained in Pierre's 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.