I dont know if you have ever used this module: http://drupal.org/project/user_badges

It will be GREAT to be able to integrate User Karma with this feature: you could identify easily all those "trusted" users.

In fact, I can assing manually all badges using th "save roles" button of user badges module, but it could be awesome that the process is automatic.

Maybe you could add this feature even without using an extra module?

THANK YOU very much for your work with this Karma module, and its "add-ons" (Extra Voting Forms and so on). We hope to be able to implement it in our community very soon.

CommentFileSizeAuthor
#20 user_carma_update_roles.patch895 bytesneochief

Comments

mercmobily’s picture

Hi,

The best way to interface with "badges" is through the roles.
It would be awkward to have User Badges-specific code in User Karma... it would bloat the code, and... for no real reason!

Please let me know if I am missing something (which is always possible)

Bye!

Merc,

mercmobily’s picture

Status: Active » Closed (works as designed)

Hi,

I looked into this more, and yes, I can confirm that assigning a badge to a role really is the way to go.
If you think otherwise, please convince me by explaining exactly how it should work, AND reopening this request.

Bye,

Merc.

unicorn84’s picture

Status: Closed (works as designed) » Active

Hello again Merc. Of course that it is the way to go. But there is a problem: the process is not automatic. I will try to explain it to you using an example (Im sorry, English isnt my native language).

You can check how badges work in our site: http://beta.pesepe.com/node/21658 : a user can see if he is being answered by an Administrator, Moderator...

I think that its very very usefull to identify those trusted users using a "badge". If so, any user who is asking at the forum, can know if he is being answered by a "trusted" user, just looking at the badge.

I can assing a badge to a role, but when Karma User module changes the role to an user, the badge is not assigned automatically. I have to go to "user_badges" module, and click on the "save roles" button. It would be great to add a check in Karma User module to assing that badge automatically.

I think that this is not related with "user_badges" module. It works ok (even if I change a role manually, by editing someones profile), the only problem comes when User Karma changes a role.

I hope that you understand me, thank you for reading (and the fast answer).

mercmobily’s picture

Hi,

Which function shall I call in user_badge to get this change to happen?
I am not _really_ willing to call a user_badge function specifically. It would lead to insanity. What I can do, though, is call a hook user_karma_event($op,$user) so that you can have:

your_model_user_karma_event()

defined, and get anything you like to happen (call the right function, etc.).

Then, it's up to the user_badge module to act upon that. Or up to you to write a 10 line module that just has that hook.

Does it sound reasonable? Please let me know, however, which function you need to call in user_badge to "refresh" a user. Please be specific :-D

Bye,

Merc.

P.S.
Going to bed :-D

unicorn84’s picture

Ive been sniffing into user_badges src, and it seems that the "magic" function is function user_badges_save_roles($roles):

When I execute it, all roles previously modified by User Karma get the correct badges. I copy the code, hope that it helps:

function user_badges_save_roles($roles) {
if (is_array($roles)) {
$success = TRUE;
db_query('DELETE FROM {user_badges_roles}');
db_query("DELETE FROM {user_badges_user} WHERE type='role'");
foreach ($roles as $rid => $bid) {
if ($bid) {
$success = $success && db_query('INSERT INTO {user_badges_roles} (rid, bid) VALUES (%d, %d)', $rid, $bid);
// Authenticated user, rid 2 has no entry in the users_role table
if($rid == 2) {
$success = $success && db_query("INSERT INTO {user_badges_user} (uid, bid, type) SELECT uid, %d, 'role' FROM {users} WHERE uid > 0", $bid);
}
else {
$success = $success && db_query("INSERT INTO {user_badges_user} (uid, bid, type) SELECT uid, %d, 'role' FROM {users_roles} WHERE rid=%d", $bid, $rid);
}
}
}
if ($success) {
drupal_set_message(t('Roles saved.'));
}
else {
drupal_set_message(t('There was a problem saving roles to the database'));
}
}
}

Maybe you could add a new option in User Karma settings to integrate your module with the badges one? Of course, desactivated by default. I really think that this will be an awesome feature :-).

Thanks again pal.

mercmobily’s picture

Hi,

Hummmmm
I really, really need something that gets a $user rather than the $role, because it's a *user* that gets an updated in Karma. Otherwise, it wouldn't make sense.

Can you please either find a function in user_badges or open a support request in user_badges pointing them to this comment, where you ask basically if there's a function in user_badges which takes a $user (or $uid) as a parameter, and recalculates the badge for *that* user?

As I said, I won't put user_badge's specific code in user_karma. However, I will add a hook - I just want to make sure the hook is useful to you.

Bye,

Merc.

unicorn84’s picture

Of course, it makes more sense to assing a badge to an user in this case. Ive found this piece of code, I think that it will be more usefull:

/**
* Add a badge to user.
*
* @param $uid
* User ID.
* @param $bid
* Badge ID.
* @param $type
* Whether set as part of the role, or individually assigned ('user', 'role').
*
* @return unknown
*/
function user_badges_user_add_badge($uid, $bid, $type = NULL){
return db_query('INSERT INTO {user_badges_user} (uid, bid, type) VALUES (%d, %d, \'%s\')', $uid, $bid, $type);
}

function user_badges_user_remove_badge($uid, $bid, $type = NULL){
if (is_null($type)){
return db_query('DELETE FROM {user_badges_user} WHERE uid=%d AND bid=%d', $uid, $bid);
}
else {
return db_query('DELETE FROM {user_badges_user} WHERE uid=%d AND bid=%d AND type=\'%s\'', $uid, $bid, $type);
}
}

As far as I understand, this one recalculates the badge for a specific user. Just tell me if you have enough with that information. If you need something more, just tell me and I will post at User Badges support forum.

mercmobily’s picture

Hi,

I am not after a function that will add a specific function.
I need a function that only takes the $uid, and - once it has it - it recalculate any badges the user might have according to his/her roles.
I looked at the code of user_badges, and they don't actually seem to have a clear function that will recalculate a user's badges. The only spot where the role assignment happens is in user_badges_user(). So, it might actually be impossible to do this with the current implementation of user_badges.

I have added this to user_karma:

module_invoke_all('user_karma_recalculate_user',$uid);

This means that now any module will be able to intercept a karma recalculation for a user. I don't think I can -- and should -- go further than this. However, I will help you by writing a tiny custom module that intercepts the hook and calls the badge-recalculating function, if this function actually exists.

Bye,

Merc.

mercmobily’s picture

Status: Active » Fixed

Hi,

Well, marking as "fixed" for the time being. I "fixed" it meaning that now there's a hook to deal with this. Now it's up to custom glue modules to do the magic.

As far as I can tell, creating a hook for user_badges is basically impossible the way user_badges is created. HOWEVER, I might well be wrong -- if that's the case, please open a new issue specifying that you'd like to see a module that does exactly that, and I'll write it.

(In its basic form, it will be something like 10 lines)

If user_badges DOESN'T provide a neat, clean function to recalculate a user's batch, I will close that issue as a "won't fix", since I am unable to patch User Badges right now (the list of feature requests and bug lists in Drigg is only getting longer, need to look after it)

Bye,

Merc.

unicorn84’s picture

I see that this is not as easy as I thought... I will post in user_badges forum, if I have no reply in a few days, I will close this issue as "wont fix". I understand that you have too much work (Ive seen the Drigg forum). I dont want to drive you crazy.

Thanks a lot for your fast answers Merc.

As I said, if Heine cannot provide that function, I will close this as "wont fix".

mercmobily’s picture

Hi,

It _pains_ me to close these issues like this. It REALLY does. It's friday night, and I have to *restrain* myself not to write a patch for user_badges to put the code for user-recalculation in a separate function, and write the tiny glue module. But, that will take about 4 hours (between coding, testing etc.) and... well, there is no guarantee the patch will be accepted into User Badges any time soon, if at all :-(
And then the glue module would be useless...

Sorry. I really can't. Drigg is _huge_ as it is. I am making sure we don't drown in feature requests and bug reports, and... :-(

Bye,

Merc.

unicorn84’s picture

Status: Fixed » Closed (won't fix)

No news.

niteman’s picture

Title: Integration with user badges » Calling user_hooks in role change
Status: Closed (won't fix) » Active

Excuse me for reopening this issue, but I think its a karma issue and it seems not hard to fix.

I've been reading around for similar issues in other modules and I've found this two:
http://drupal.org/node/161822#comment-584642 (where user badges author points the spot)
http://drupal.org/node/161825 (the fixed issue in a similar module)

It seems that only thing to be done is calling
user_multiple_role_edit(array($uid), 'add_role', $rid);
and
user_multiple_role_edit(array($uid), 'remove_role', $rid);

These 2 api hooks will do the magic.

Thanks in advance and excuse me also for my bad english

niteman’s picture

Version: 5.x-1.8 » 5.x-1.9
Category: feature » bug

Just a follow up to update version and category of this bug

niteman’s picture

Version: 5.x-1.9 » 5.x-1.13
mercmobily’s picture

Hi,

Sorry for taking so long to reply!
Hey, I wasn't even aware of the functions:

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

Are you absolutely sure that by calling these all the right hooks will be called ? When do you think I should call them?

THANKS A LOT!!!

Merc.

mercmobily’s picture

Hi,

Hang on a sec... these are functions that do API-wise what I do manually.

Are you suggesting I change the module so that I use them?

Merc.

niteman’s picture

Hi,

Yes i suggest you to use these functions (I'm not a developer, just a sysadmin/webmaster, so I don't know what they do exactly althoug I suposse it is explained in the documentation) because that way other role aware modules can hook the changes in a standard fashion.

In other words, as far as I know, if your module call these functions another module like user badges can act properly changing the badge assigned to the user. Or other module that congrat users in role adquisition (by example) can send a message...

Thanks in advance for taking this in consideration and again excuse me for my english.

Best regards

mercmobily’s picture

Hi,

I think you are right, I will look into it!

Merc.

neochief’s picture

Version: 5.x-1.13 » 6.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new895 bytes

Simply and functional ;)

niteman’s picture

I gess the patch may be incomplete since in user_karma_roles_mass_recalculation() there is a raw role deletion that in my opinion sould be also handled using http://api.drupal.org/api/function/user_multiple_role_edit

Something like changing the delete line for a select of users with the role asigned followed by an ser_multiple_role_edit(array($uid), 'remove_role', $rid);

I will try to manually patch 5.x version (sorry but we are stuck on 5.x yet) and write a follow up.

Best regards and again sorry for my bad english

niteman’s picture

Status: Needs review » Reviewed & tested by the community

I've reviewed the patch and it works great in 5.x, since I still think function user_karma_roles_mass_recalculation() should be also pathced.

BR

mercmobily’s picture

Hi,

Is anybody here able to complete a working patch for the 6.x version of this module?

Bye,

Merc.

neochief’s picture

My patch was for 6.x

mercmobily’s picture

Hi Neochief,

What do you think about comment #21? Would you at all be able to create a patch that avoids those manual deletions in roles?
I am asking you because you are familiar with the patch, and are probably running a site with it...

Thanks,

Merc.

neochief’s picture

Sorry, ignore my comment, I didn't read properly ;)

mercmobily’s picture

Hi,

Which comment shall I not read? And did you see my previous comment, about #21? Any thoughts?

Merc.

mercmobily’s picture

Status: Reviewed & tested by the community » Fixed

Hi,

Guys, I had a test run with this, and in a decently busy site (10000 users) without running queries by hand like I did, it's basically impossible to run the mass recalculation without killing the server.

If you look at the code, you will notice that a lot was done to make sure that I optimise the number of queries. The big delete at the beginning of the mass recalculation is there for a reason.

So, in order to get the hook_user happening, I added this:

    // This is just so that the update hook is called. It's a bit
    // naughty, really, to do it this way but...
    $u_object = user_load( array('uid' => $uid) );
    $array=array();
    user_module_invoke('update', $array, $u_object);

Yeah, it's a bit naughty, but it does the job in the most optimised way. It's still 16000 extra queries compared to before, but hey...

Please test it! I don't use badges, so I don't actually know if this actually does the job. I am 99.5% sure it does, which is why I am marking this as fixed.

This was the last standing bug of User Karma.

Yay!

Merc.

mercmobily’s picture

Status: Fixed » Closed (fixed)