I have a role called Authenticated +1. Which means, for 1 point, users belong to that role. I give users 1 point for registering. Now, they get the email saying "Congratulations! You have joined a new role". But they do not really belong to that role! It simply doesnt work. If I look into Users list, those new users are just Authenticated, but none get the new role "Authenticated +1". They are not really upgraded.

The email is sent on line 258 in userpoints_role.module:

userpoints_role_send_mail('join', $uid, $role_point);

But the next line apparently does not really work:

user_multiple_role_edit(array($uid), 'add_role', $rid);

This line is supposed to upgrade the role for real, but it doesn't.

Comments

Anonymous’s picture

Problem is, in user.module on lines 350-358, the $roles is empty. Because userpoints (apparently) does not set the $roles again.

In the user.module code, then without $array['roles']containing the new data, the user roles are deleted again. Just after they were stored by user points, they are immediately deleted.

	if (isset($array['roles']) && is_array($array['roles'])) {
      db_query('DELETE FROM {users_roles} WHERE uid = %d', $array['uid']);
      foreach (array_keys($array['roles']) as $rid) {
        if (!in_array($rid, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
          db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $array['uid'], $rid);
        }
      }
    }

Have tried a lot to solve this, but I really can't find out why this happens. The new $roles is not passed on to user.module.

andrenoronha’s picture

I got this same issue on version 7 of the module. Have you got any solution?

qsurti’s picture

I believe the solution lies with the authors of the userpoint module. I have been searching around for past 10 days for a solution to upgrade a user based on userpoints earned but have failed in every attempt.

dRaz’s picture

This module is vital to my website concept.

Please can somebody from the dev team comment as to when a new release will be issued?

The last comment by a maintainer on any issue was 15 weeks ago....I am not sure we will see a new release.....do we have anybody willing to take on the project if we receive no reply?

berdir’s picture

The 6.x-1.x branch of this project is more or less dead. (and 7.x-1.x is stalled too but much more stable.. the parts which are there, at least)

The modules in here are just a collection of modules that someone has developed at some point, it has been added and that's it.

If someone would be willing to take this over, it would have already happened I guess.

If you need this, your best change at this point is probably trying to pay someone to work on this, I simply don't have enough time at the moment.

dRaz’s picture

Berdir, thank you for the response.

Sorry to hear that you do not have the time as your knowledge of userpoints will be surely missed with any integrating modules going forward.

Unfortunately I do not have the coding knowledge to take this on, hopefully other members will.

qsurti’s picture

Berdir. Thanks for being honest and straight forward.

Who do you think will be willing to help and what do you think it would cost to get the update?

andrenoronha’s picture

Hey guys, I came up with a solution using Rules.

1. Create a rule that will react on the event "User was awarded points"
2. Add the action "Load points of a user". Data selector for the user: "userpoints-transaction:user"
3. Add another action "Execute custom PHP code" and here is my code:

//Get variables and load user
$uid = [userpoints-transaction:user:uid];
$account = user_load($uid);

//I used this to define which level (role) the user is.
//Change the roles ids to fit your needs
if ($account->roles[22]) $level = 5;
else if ($account->roles[21]) $level = 4;
else if ($account->roles[20]) $level = 3;
else if ($account->roles[19]) $level = 2;
else $level = 1;

//Now, upon my point limits of each level, I add the role and setup a message
//Change the role names and point limits to fit your needs
if ($loaded_points > 100 && $level == 1) {
  if ($role = user_role_load_by_name('Level 2')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're Level 2.';
}
if ($loaded_points > 400 && $level <= 2) {
  if ($role = user_role_load_by_name('Level 3')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're Level 3.';
}
if ($loaded_points > 1600 && $level <= 3) {
  if ($role = user_role_load_by_name('Level 4')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're Level 4.';
}
if ($loaded_points > 6400 && $level <= 4) {
  if ($role = user_role_load_by_name('Level 5')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're Level 5.';
}

//Show message
drupal_set_message($msg);

The $loaded_points variable comes from the other action I setup on step 2.
user_role_load_by_name and user_multiple_role_edit are new functions on Drupal 7.

If anyone knows a better and lighter way of doing that, feel free to change it.

qsurti’s picture

Will this code work on D7 version?

andrenoronha’s picture

It's only for D7

qsurti’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
dRaz’s picture

Luiz - Great work :)

Thank you so much for spending the time to post this. I am a believer in posting solutions too so kudos to you sir :)

I do have an issue though....

I have the following roles:

1 - anon
2 - auth
3 - admin
4 - user1
5 - user2

I have used the following code:

//Get variables and load user
$uid = [userpoints-transaction:user:uid];
$account = user_load($uid);

//I used this to define which level (role) the user is.
//Change the roles ids to fit your needs
if ($account->roles[5]) $level = 3;
else if ($account->roles[4]) $level = 2;
else $level = 1;

//Now, upon my point limits of each level, I add the role and setup a message
//Change the role names and point limits to fit your needs
if ($loaded_points > 200 && $level == 1) {
  if ($role = user_role_load_by_name('user1')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're user1.';
}
if ($loaded_points >800 && $level <= 2) {
  if ($role = user_role_load_by_name('=user2')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're user2.';
}

//Show message
drupal_set_message($msg);

Now.....

- I was an auth user with 0 points.
- I created content until I hit 200 and voila! I was upgraded to user1 (although no message?????)
- I continued to create content until 800 and even 900 but it does not upgrade to user2.

Can you help me crack this please :)

Many thanks.

Shaun

andrenoronha’s picture

Shaun,

try changing the line:

  if ($role = user_role_load_by_name('=user2')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }

to:

  if ($role = user_role_load_by_name('user2')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }

I hope the bug was the '=' caracter. :)

But about the message not being shown, I really dont know what's happening... For me it works good.
Try any message without the variable just to check the function. Like: drupal_set_message('hello');

andrenoronha’s picture

Here is another exemple of the code I've been working with qsurti:
http://drupal.org/node/1293016#comment-5185762

dRaz’s picture

Hu Luiz,

I had already changed that code - was a typo oops :)

Still not working though.....

I thought the issue could lie with this piece of code:

if ($loaded_points >800 && $level <= 2)

why do you include the "<" here?

Any further help? :)

andrenoronha’s picture

Try this now:

//Get variables and load user
$uid = [userpoints-transaction:user:uid];
$account = user_load($uid);

//Where role 4 is user1 and role 5 is user2.
if ($loaded_points > 200 && !isset($account->roles[4])) {
  if ($role = user_role_load_by_name('user1')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're user1.';
}
if ($loaded_points > 800 && !isset($account->roles[5])) {
  if ($role = user_role_load_by_name('user2')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're user2.';
}

//Show message
if ($msg) drupal_set_message($msg);
berdir’s picture

Status: Active » Closed (duplicate)

No idea why nobody ever looked at the source and tried to fix the actual error, the bug was rather trivial.

The bug is fixed in #443196: All roles are granted when a user earns points