I updated my site to drupal 7 and now I'm testing the modules.
I see that the userpoints role modules is not working as it should.
When users get enough points to get a new role nothing happens.

Any help?

CommentFileSizeAuthor
#7 RuleScreen.JPG84.85 KBqsurti
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

qsurti’s picture

Is anyone looking in this issue? I also do not see any role change on achieving required points for a user.

Is there any other alternative to this issue?

andrenoronha’s picture

There's another issue here: http://drupal.org/node/544764
It seems the module has no one to work on it, but I came up with a solution using rules at #8.

Give it a try.

qsurti’s picture

I am a bit naive in programming. Can you please explain what the terms 'roles[x]' and '$level' stand for? Also if you can simplify it for only one change, say from 'authenticated user' to 'editor'. Many thanks.

andrenoronha’s picture

Each role has its unique ID. Go to you roles page and check the role id by mouse hovering the 'edit role' link. You'll got: /admin/people/permissions/roles/edit/X. Where X is the role id.

My roles are based upon levels. You won't need this.
here you go:

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

//Check if the user is already an editor
if ($account->roles[x]) $editor = TRUE;

//If the user has points enough and it's not an editor yet, give them the role
if ($loaded_points > 100 && $editor == FALSE) {
  if ($role = user_role_load_by_name('Editor')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
  $msg = 'Now you're our new editor.';
}

//Show message
drupal_set_message($msg);

- Replace x for your role id.
- Replace 100 (at $loaded_points > 100) with the right amount of points required for the user to become an editor.
- Check if the name of your role is really "Editor" as I have written in the code.

I guess that's it.

qsurti’s picture

Following is the complete rules script I exported based on your above suggestions;

{ "rules_star_role_is_added" : {
"LABEL" : "Award Star Role to User with php script",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "userpoints_rules", "rules", "php" ],
"ON" : [ "userpoints_event_points_awarded_after" ],
"DO" : [
{ "userpoints_rules_get_current_points" : {
"USING" : { "user" : [ "userpoints-transaction:user" ], "tid" : "all" },
"PROVIDE" : { "loaded_points" : { "loaded_points" : "Number of points in the specified category." } }
}
},
{ "php_eval" : { "code" : "\/\/Get variables and load user\r\n$uid = [userpoints-transaction:user:uid];\r\n$account = user_load($uid);\r\n\r\n\/\/Check if the user is already a Star Member\r\nif ($account-\u003eroles[5]) $level = TRUE;\r\n\r\n\/\/If the user has points enough and it's not an editor yet, give them the role\r\nif ($loaded_points \u003e 6 \u0026\u0026 $level == FALSE) {\r\n if ($role = user_role_load_by_name('Star Member')) { user_multiple_role_edit(array($uid), 'add_role', $role-\u003erid); }\r\n $msg = 'Now you're a new Star Member.';\r\n}\r\n\r\n\/\/Show message\r\ndrupal_set_message($msg);" } }
]
}
}

I have set the $load_points > 6.

I later edited one of the authenticated users userpoints by resubmitting the same points showing 2 points. His has total points=7 in two transactions of 2 and 5 points. I then checked if his role has changed to 'Star Member'. Nothing has happened.

Is something missing in my actions?

andrenoronha’s picture

give me a print of the rule screen, it's easier.

qsurti’s picture

FileSize
84.85 KB

Is this what you want to see?

qsurti’s picture

The php code in above is follows;

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

//Check if the user is already a Star Member
if ($account->roles[4]) $level = TRUE;

//If the user has points enough and it's not an editor yet, give them the role
if ($loaded_points > 6 && $level == FALSE) {
if ($role = user_role_load_by_name('Star Member')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
$msg = 'Now you're a new Star Member.';
}

//Show message
drupal_set_message($msg);

qsurti’s picture

Hi a.luiz.n,

I have succeeded in getting the script working by deleting the last $msg part from the php script. The entire script looks like follows;

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

//Check if the user is already a Star Member
if ($account->roles[4]) $level1 = TRUE;

//If the user has points enough and it's not an editor yet, give them the role
if ($loaded_points > 6 && $level1 == FALSE) {
if ($role = user_role_load_by_name('Star Member')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
}

However, I receive following Notices when I edit the userpoints through admin panel.

Notice: Undefined offset: 4 in eval() (line 6 of E:\web\drupal\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code).
Notice: Undefined variable: level1 in eval() (line 9 of E:\web\drupal\sites\all\modules\rules\modules\php.eval.inc(146) : eval()'d code).

Do they pose any serious problem or shall I leave them unattended or is there a way to get them eliminated?

Thanks for all your assistance till date.

andrenoronha’s picture

Try this:

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

//If the user has points enough, give them the role
if (($loaded_points > 6) && (!isset($account->roles[4]))) {
  if ($role = user_role_load_by_name('Star Member')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
}
qsurti’s picture

Thanks a lot. Everything is working fine now without showing errors.

Will the code also work in reverse if the user's points drop and he loose the Star Member role and drops down to authenticated user with the following code.

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

//If the user has points enough, give them the role
if (($loaded_points < 7) && (!isset($account->roles[2]))) {
  if ($role = user_role_load_by_name('authenticated user')) { user_multiple_role_edit(array($uid), 'add_role', $role->rid); }
}

I tried but could not succeed.

andrenoronha’s picture

You can't give the role "authenticated user" to a user cause it's not really a common role. All the users have it.
You have to remove the Star member role, changing the oparation of the function 'user_multiple_role_edit' from 'add role' to 'remove_role'. Try this:

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

//If the user loses points, take off the role
if (($loaded_points < 7) && (isset($account->roles[4]))) {
  if ($role = user_role_load_by_name('Star Member')) { user_multiple_role_edit(array($uid), 'remove_role', $role->rid); }
}
qsurti’s picture

Thanks a.luiz.n, I tried and it works.

Do you know anyone who can port EMF Interspire module from Drupal6 to D7? Please have a look at the issue at;
http://drupal.org/node/1331062

Berdir’s picture

Status: Active » Closed (duplicate)