Closed (fixed)
Project:
User Points
Version:
7.x-2.x-dev
Component:
Code: userpoints
Priority:
Normal
Category:
Support request
Assigned:
Unassigned
Reporter:
Created:
14 Aug 2009 at 21:32 UTC
Updated:
14 Mar 2015 at 16:40 UTC
Jump to comment: Most recent
Comments
Comment #1
kbahey commentedHere is some code that would do that.
You need to create a file called mymodule.module and a corresponding mymodule.info file.
Put this in mymodule.module.
Place all this in a sites/all/modules/mymodule directory, and enable it.
Comment #2
djudd commentedThank you very much for this. I appreciate the help greatly.
Comment #3
Fidelix commentedCould this be used in the very same way to deduct points from a role?
Thanx!
Comment #4
giorgio79 commentedHope it is not a problem to repoen this.
I would like to set the points of users to an x amount at the start of each month.
This solution could be slow if there are a lot of users, could we perhaps run a straight SQL query against the userpoints table?
Also, I Would like to set the value the same to all users instead of adding to the existing value.
Comment #5
berdirYou can't just set the amount of points a user has to a fixed amount. The total is just a summary of all transactions, you do not want to set the amount without a corresponding transaction as this will lead to a very confusing UI as the transactions will list something different than the total a user currently has.
What you need to do for each user is first to query how many points he currently has and then give the missing amount of points.
Example code if you want users to receive 500 points:
And yes, this can be slow if you have many users. Possible ways to solve this problem:
- Only process a fixed number of users on each cron run and only save the time variable once you processed all users. This means that it could take a few hours until all users have their points, depending on how often you call cron.
- Not using hook_crun() but for example create a custom script (e.g. with drush) and then calling it separately. Or a custom page callback that starts a batch process.
Comment #6
berdirComment #7
giorgio79 commentedThanks Berdir, perhaps in this case I can do this when the user logs in with a rule, the first time in a month instead of using cron. This way it would be more economical.
Comment #8
berdirYes, that's actually quite a nice idea. You need to aware of two limitations of that, though:
- You can't make any public listings of those points, as they will only ever be "correct" for the current user, all others might or might not have the expected amount of points. But I guess you don't need this...
- Note that the login hook/event is only triggered when a user logs in explicitly by submitting the login form. It is *not* triggered when a user is automatically logged in through the cookie. And I am not sure if there is a way to detect the second case.
Also, please document this in the userpoints - rules documentation: http://drupal.org/node/873386
Comment #10
joncjordan commentedI know this is an old thread, but I figured I'd chime in with my rules-based solution for setting a fixed number of points periodically. Here are the actions:
1. Load points of a user
2. Add a variable: Type: Integer, Value: 750
3. Calculate a value: [variable-added] minus [loaded-points]
4. Grant points to a user using the calculated value variable
You can use whatever event you want, whether it is a rules scheduler or when user logs in, etc.