This patch add a rule condition to the userpoints rules that allows to define an amount that is compared with the user points.

The condition is >= so if you negate it then you have <.

This condition associated with the userpoints awarded event, execute rule actions depending of userpoints amounts,
or if added to other rule, allows some events only if you have some level of points for example.

The userpoints contrib modules: role or admin_mail now can be done with this condition and the existing trigger.

Comments

off’s picture

cool

fago’s picture

Title: New condition to Userpoints Rules for checking amount of points for a user » improve user point rules integration
Issue tags: +rules integration

Don't tried it, but the looks fine.

@existing rules integration:
* Imo this could also go into a .rules.inc file, just the rules_invoke_event call would have to go to a .module.
* Loading the user account for the event should happen in a argument loading handler, so it gets loaded when necessary.

jcmarco’s picture

StatusFileSize
new5.8 KB

@fago
I have made changes in the patch with your recommendations:

- moved all rules but rules_invoke_event call to rules.inc file (it is needed to clear the cache in order to update the rules.inc)
- And you were right, it was not needed using the $account with a user_load, when it was just needed the uid.
- There is some code style cleaning

Hopefully this patch could be added to the module as I have to patch the new condition rules everytime there is a new dev release.

kbahey’s picture

Status: Needs review » Fixed

Committed.

Thank you!

drupup’s picture

Did I do something wrong?

I just did an uninstall/reinstall with the latest .dev, and after reassigning points and recreating rules it seems that the userpoints condition is returning TRUE no matter what the user's point level, FALSE if NEGATE is checked. Again, the user's actual point levels don't seem to be registering. This previously worked just fine.

This is adding a condition to a "User has logged in" rule. This was working before.

jcmarco’s picture

I have a rule that check userpoints number with a level:

ON event User was awarded points
IF
conditionUserpoints amount is >= than x indent

addAdd a condition
DO
actionAdd user role
actionShow a configurable message on the site

And it is working fine.

That last patch only added a new condition and left using the user_load was removed.
When I updated to new dev version I had issues loading the new rules.inc file, try to clean the cache.

drupup’s picture

Should have thought of the cache. But now that I've done that, it's gotten more interesting.

Condition works when it checks and verifies.

Condition DOESN'T work when it is set to negate.

Basically, I'm using point levels to add/subtract a user role. I've got two rules:

ADD ROLE

ON event User Has Logged In

Condition: User has role (Premium account)
Condition: Userpoint amount is >= 200

DO Add User Role (Create Special Offers)

This is working fine

REMOVE ROLE

ON event User Has Logged In

Condition: User has role (Create Special Offers)
Condition: NEGATE Userpoint amount is >= 200

DO Remove User Role (Create Special Offers)

THIS is not working. I take away the userpoint condition, and it works (so it's not the other condition, and it's not the action). Also, if I set the Userpoint conditions so the REMOVE ROLE condition is not negated (it looks for a low point total, the ADD ROLE rule condition looks for a higher total), both conditions respond correctly. Something is not right with how it's evaluating negated conditions.

As I said, this all was working before. I'm baffled. Perhaps I should backtrack and patch, but I'd rather not.

jcmarco’s picture

I don't know what to say.

I have created a rule that check when some points have been awarded, with the conditions that if it is not >= than a number then it shows a warning message.
After that I have configure to add a point with every new comment and I have been playing with that, also considering that if you delete a comment it deducts a point, so I have check that works fine.

Then I created a new rule with the action every time you see a page, and the condition that if you don't have >= than 310 points then show a warning message saying: 'Ey! Earn more points!!'. And while I don't have 310 points on each page I got the message, then I create a new comment I earn 1 point to have this number and I don't see any more the message.

Are you comparing with the same category of userpoints than the ones owned by the user?
Even you can try to delete the condition and create it again.

drupup’s picture

Belive me, I've tested this extensively, and something went seriously wrong when the functions got ported over to the rule.inc file. I don't use point categories, and I created a bunch of different rules using a bunch of different events and a bunch of different actions. NEGATE was not working correctly...sometimes it was never, sometimes it was all the time.

I created another rule, just to take the roles and the user login out of the equation, where opening a page granted points to the user. All actions granted points to the admin, rather than the logged in user.

Then I just took everything back to where it was before, went back to 6.x - 1.0 and put the functions back in userpoints.rules.module, and everything is working correctly again. Even highly complex mixes of conditions with and without NEGATED userpoint conditions are doing exactly what they are supposed to.

I wish I could offer more guidance on what's going on, but all I can do is report my experience as a user. I'm not sure you want to commit this just yet.

jcmarco’s picture

What rules version are you running?
I am testing with last Rules 6.x-1.x-dev (2009-Aug-20)

drupup’s picture

I'm using the 6.x-1.0. I'll try loading up the dev, load up the dev of Userpoints, and report back.

drupup’s picture

Okay: using an all-.dev setup, rules work for the uid=1 admin account, but not for any others. I created a second Drupal install, nothing but Rules and Userpoints as added modules, and I got the same results.

Again, revert to previous setup, and everything works correctly.

This account is hosted on Bluehost...but then again, lots of Drupal sites are hosted on Bluehost.

Went and did another uninstall/flush/reinstall, and yep, works for uid=1, no one else.

For the moment, I'm reverting.

jcmarco’s picture

No clues! I have tried as well with a registered user and last testing rules worked fine as well.

This is the rule I am playing with:

array (
  'rules' => 
  array (
    'rules_21' => 
    array (
      '#type' => 'rule',
      '#set' => 'event_userpoints_event_points_awarded',
      '#label' => 'Low ammount of points warning',
      '#active' => 1,
      '#weight' => '0',
      '#categories' => 
      array (
        0 => 'userpoints',
      ),
      '#status' => 'custom',
      '#conditions' => 
      array (
        0 => 
        array (
          '#settings' => 
          array (
            'amount' => '500',
            'type' => '33',
            '#argument map' => 
            array (
              'user' => 'user',
            ),
          ),
          '#weight' => 0,
          '#type' => 'condition',
          '#name' => 'userpoints_rules_amount',
          '#negate' => 1,
          '#info' => 
          array (
            'label' => 'Userpoints amount is >= than x',
            'arguments' => 
            array (
              'user' => 
              array (
                'type' => 'user',
                'label' => 'User',
              ),
            ),
            'module' => 'Userpoints',
            'eval input' => 
            array (
              0 => 'amount',
            ),
          ),
        ),
      ),
      '#actions' => 
      array (
        0 => 
        array (
          '#info' => 
          array (
            'label' => 'Show a configurable message on the site',
            'module' => 'System',
            'eval input' => 
            array (
              0 => 'message',
            ),
          ),
          '#name' => 'rules_action_drupal_message',
          '#settings' => 
          array (
            'message' => 'Ey!! [user:user-name] you have less than 500 points! baaaaad!',
            'error' => 1,
            '#eval input' => 
            array (
              'token_rules_input_evaluator' => 
              array (
                'message' => 
                array (
                  0 => 'user',
                ),
              ),
            ),
          ),
          '#type' => 'action',
          '#weight' => 0,
        ),
      ),
    ),
  ),
)

Try it but remember to change userpoints types to your ones.
Everytime you earn some points and you are below 500 you get a warning message.
Tested with registered user and admin user.

Check also permissions with:
view own userpoints
view userpoints

drupup’s picture

Check out the test site:

http://www.ecovision360.com/test/

Admin is

username: admin_test
password: 4nwvYjJ7

Authenticated user is

username: testuser
password: testuser

There's one added role: page creator

There are two rules

One adds the page creator role when the userpoints are >= 30
One removes the page creator role when the userpoints are NOT >= 30

Just figured out what's happening....the rule is evaluating the admin's account, not the account of the user who is logging in. That would explain why it was granting points to the admin account earlier. It evaluates to TRUE or FALSE based on the points in the admin account, not the points in the test user's account. Change the point totals for either of them and you'll see.

jcmarco’s picture

StatusFileSize
new1.75 KB

Thanks to @drupup and his perseverance, I was testing his configuration and I realized that the problem is when using actions/events/conditions different from the userpoints ones.
Userpoints uses internally user->uid and events/actions/conditions were running with the uid instead of $user as rules module does internally.
So when using userpoint events with userpoint conditions/actions everything worked fine.

But when using other modules actions/events, then their actions/events are using $account/$user, and they send this parameters to userpoint rules integration. So it fails reading the uid.

Please test this patch, I have tested it locally with the commented old test actions and a new one that cross actions from other modules with userpoint conditions and in my installation works fine.

jcmarco’s picture

Status: Fixed » Needs review
himagarwal’s picture

When the author of node/comment changes I want to subtract userpoints from the previous node/comment poster and add userpoints to new node/comment poster. But I think this is not possible with the rules or if I messed the whole thing. It is getting very complicated.

Features Requested:
1. It would be nice if default rules are already loaded when module gets installed

OR

2. After module installation there is a button (in userpoint rules setting page) saying load default rules and those who want default rules can click on button otherwise leave it as it is and create new rules on their own.

However, It was nice and simple in drupal 5 where rules were already defined.

jcmarco’s picture

@himagarwal
this rules integration just add some basic triggers/actions/conditions, any other complex rule or feature can be programmed in a module or trying to write a rule set.
Now, the condition added with last patch is really independent or any combination of modules-rules-conditions-behaviors you could define with the module rules and other modules.

IMHO requests #1 and #2 should go to the rules project where core rules are defined and default rules could be defined (rules module is very powerful and already give you options to do so, but you need to add them in a custom module or with a manual import).

drupup’s picture

jcmarco:

I'm guessing "perseverence" wasn't the first word that came to your mind (insert emoticon of your choice here...)

Anyway: QA is easy. It's the coding that's the hard part. And it looks like the coding is solid. I've set up a variety of fairly complex condition combinations, and a variety of multi-part actions, both involving the userpoints and not, and I haven't found a new way to break it. Not that I won't keep trying...

This is a really valuable addition to Drupal. Thanks for the work. Now I've got about 50 rules to go set up.

@himagarwal

I agree with above, you've got a new issue here, and the Rules project is the people who should see it. Good idea, just put it in front of the right set of eyes.

You'd best steel yourself for the future: Drupal is becoming a lot more powerful, but the price is that it's going to be less easy to use than it used to be (not that it was all that easy to begin with...). More flexibility, but less out-of-the-box stuff. I used to be able to hack some of my own patches...but the move to a more OO approach to coding is starting to leave designers/part-time hackers like me in the dust. I think the gain is worth the price.

Bilmar’s picture

I'm having a problem with the rules integration. I currently have it set so when an event takes place a point is deducted from the user.

Right now, no matter which user takes the action - the point is deducted from admin =(

Is anyone experiencing this same bug?

jcmarco’s picture

Have you tested patch #15?

Bilmar’s picture

I tried to implement the patch in #15 but userpoints stopped working properly..
I believe it is my lack of knowledge in patching.

Will continue to try to get this to work properly

seabhac siulach’s picture

Category: feature » task
Status: Needs review » Needs work

Can the patch in #15 above be added to the module?

The previous patch is not working, resulting in badges, etc. being awarded to the user awarding userpoints and not the person receiving the userpoints. In addition, the 'add role' action in rules is not working using this new userpoint condition, probably for the same reasons.

Thanks to all who have worked on this very useful addition to userpoints, by the way!

BenK’s picture

Hey everyone,

Patch #15 (by jcmarco) works beautifully for me. I have a totally different configuration than what is described in this thread, but I was still getting points awarded to the admin (user #1) instead of the appropriate user. Once I applied patch #15, everything worked properly.

So can Patch #15 be officially committed to the module ASAP?

Thanks,
Ben

kbahey’s picture

Status: Needs work » Fixed

I committed the patch in #15 to the 6.x-1.x-dev.

It will be available in the tarball in about 9 hours from now.

Please test the new tarball with the fix and report any problems in a new issue.

BenK’s picture

Thanks, kbahey, for the quick response!

Status: Fixed » Closed (fixed)
Issue tags: -rules integration

Automatically closed -- issue fixed for 2 weeks with no activity.