I would like to set the user points of members of my site to an x amount at the start of each month. What would be the best way for this?

The closest I found so far was this: http://drupal.org/node/549640

There are a few issues with this though: 1. It adds 5 points, instead of setting it to 5 universally 2. It uses a while loop which can take long if there are a lot of users

I am thinking of a quick and dirty UPDATE SQL query that does this in one go.


you could extend the userpoints module to have some actions exposed to VBO then execute that periodically with Rules. Depends how much time you have, but it would be a nice addition to that module..

| improve this answer | |

The question has been answered by me in the linked issue at http://drupal.org/node/549640 (comment 5-7). Quoting the answer here so that it can be found easier and the question can be marked as resolved.

You 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.

$points_to_give = 500 - userpoints_get_current_points($uid);
if ($points_to_give > 0) {
  // Grant points here.
  • 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.

The follow-up comments are then about an alternative solution, which is to correct the number of points a user has when he logs in the first time in a new month.

| improve this answer | |

A rules-based solution. 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
| 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.