I'm no SQL genius. I am using MySQL for my site and was wondering if there was a way to add 250 points per month to users in a certain role.

I was thinking about a MySQL command run from Cron, for example, that would add 250 points to all users in a given role.

Can anyone lend a hand here?

Comments

kbahey’s picture

Title: Adding points to roles » Adding points to roles each month
Status: Active » Closed (fixed)

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


function mymodule_cron() {
  $timestamp = variable_get('mymodule_last_run', time());
  if (time() > ($timestamp + 30 * 24 * 60 * 60)) {
    $role_id = 4;    // Example role ID to check and reward
    $num_points = 5; // Number of points to award
    $result = db_query("SELECT uid FROM {users_roles} WHERE rid = %d", $role_id);
    while($data = db_fetch_object($result)) {
      $params = array (
        'uid' => $data->uid,
        'points' => $num_points,
      );
      userpoints_userpointsapi($params);
      variable_set('mymodule_last_run', time());
    }
  }
}

Place all this in a sites/all/modules/mymodule directory, and enable it.

djudd’s picture

Thank you very much for this. I appreciate the help greatly.

Fidelix’s picture

Could this be used in the very same way to deduct points from a role?

Thanx!

giorgio79’s picture

Title: Adding points to roles each month » Periodically adding points
Version: 6.x-1.x-dev » 7.x-2.x-dev
Component: Code: userpoints_basic » Code: userpoints
Status: Closed (fixed) » Active

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

berdir’s picture

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.

Example code if you want users to receive 500 points:

$points_to_give = 500 - userpoints_get_current_points($uid);
if ($points_to_give > 0) {
  // Grant points here.
}

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.

berdir’s picture

Status: Active » Fixed
giorgio79’s picture

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

berdir’s picture

Yes, 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

Status: Fixed » Closed (fixed)

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

joncjordan’s picture

Issue summary: View changes

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