vBulletin Mods

The Official vBulletin Modifications Site
https://www.vbulletin.org/forum/showthread.php?t=258320

Making mass changes to users based on secondary usergroups?
by abjds.com
04 Feb 2011 03:43

My short question is -- is there any way to mass-move or otherwise mass-alter users based on the secondary usergroups that they belong to?

I've looked around in both 'Move Users' and 'Promotions', but both of these only list users or make changes to users based on their primary user group, which doesn't help me.

Basically, we have several publicly joinable usergroups (set up as secondary usergroups) which allow our users to access various different parts of the forum when they join them. They are all secondary so that our users can choose to join more than one without affecting their basic forum usage. However due to lack of activity in some of them, we'd like to "merge" them into other, more active usergroups.

Since I can't find any nice simple "merge usergroups" option (and if there is one, thwap me and point me towards it!) I figured a mass-move of the users from the inactive usergroups to the active usergroups was my next best choice... but I simple can't find anything which allows me to select them all based off the secondary usergroups. :/ Everything seems to run off primary usergroups only.

Is there any way at all to do this, or any mod that I've overlooked that might help me? Or is my only choice going to be to alter someone else's mod to better suit my needs? (I'd prefer not to if I can avoid it -- I'm just a hack when it comes to backend stuff... but I don't consider the idea of manually editing hundreds of user accounts as a viable alternative.)

Thanks in advance for any advice! :D

kh99 04 Feb 2011 13:33

I'm not an expert on doing things through the ACP so there could be a way that I don't know about. But looking at the code, it looks like to add someone to a secondary group you'd just have to add the new groupid to the membergroupid field for that user in the 'user' database table. (membergroupid is a text field that's a comma-separated list of secondary groups).

So what you could do is add everyone who's in one secondary group to another secondary group by doing a mysql query like this:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.

where 9 is the group you want to inactivate and 10 is the group you want to merge in to. (Also, if you have a database prefix defined you'd need to add that before 'user' on the first line).

Then when that's done you can just delete the old group in the ACP.

I've tried this on a test site, but of course it might be a good idea to have a database backup before doing this, just in case. Also if you wanted you could try it first with one or a few users by adding " AND userid IN(1, 2, 3)" to the end of the query (putting the userids you want in place of 1,2,3 of course :)).

...and of course you can't delete the old group before you've run the query for all users.

ETA: I added "AND NOT FIND_IN_SET(10, membergroupids)" to the above code to avoid listing the same group twice in case a user is already in the second group. But if you've already done it I'm pretty sure it's OK because when it checks if a user is in a group it doesn't matter if it's there twice, and when it removes the user from a group it seems to handle the possibility of it being listed more than once.

abjds.com 06 Feb 2011 02:55

kh99, thank you SO MUCH! You're fantastic. That's exactly what I wanted to do and I've tested it out on my forum as well (using a couple of test users) and it worked like a charm.

Thank you again, so much, you've saved me a lot of hassle!! I'm just not familiar at all with working directly with the SQL so the idea didn't occur to me.

Thank you!!! :D

tsptom 06 Dec 2012 19:11

I know it is frowned upon to wake old threads at vb.com, but the above code was very helpful for what I am trying to do - add member to secondary usergroups systemically.

To take it a step further, does anyone know how you would remove a specific secondary usergoup for a member systemically?

I want to be able to add and remove members from specific secondary usergroups based on being a paying subscriber. The above codes helps me set them when they subscribe. When they cancel they need to be removed from that usergroup systemically.

Any ideas? Thanks.

kh99 06 Dec 2012 19:31

As long-winded as that above post is, I didn't mention the fact that there are some things which might need to be updated when the groups change, so if you want to do it in code the best way is to use the datamanager. And when removing a group, the way the vb code does it is to get an array of current ids in membergroupids, remove the one being deleted, and set that field to the new array.

So, if you're doing this as part of a vbulletin script (as a plugin or a "vbulletin powered" page), you'd want to do something like:


Block Disabled:      (Update License Status)  
Suspended or Unlicensed Members Cannot View Code.



If you're not using a vbulletin script, then you could still read the membergroupids field, build a new list, and update it, but be aware that some things might not get updated correctly (I'm not sure offhand what they are - you'd need to study includes/class_dm_user.php and see what happens when usergroups are changed).

tsptom 06 Dec 2012 21:42

OK, thanks for your help. It would be for a non-VB php script so I'll do some hunting around for possible issues in other vb modules.

kh99 06 Dec 2012 22:14

OK. A couple of functions with code that might help (if you're interested in copying it instead of rewriting it): in includes/functions.php, function fetch_membergroupids_array() takes the string from the membergroupids field and turns it in to an array (optionally adding in the main groupid, which you wouldn't want to do if you're only updating the secondary groups). Also, in includes/class_dm.php, part of function verify_list() does the opposite - takes the array and makes a comma-separated list.

tsptom 07 Dec 2012 17:27

Quote:

Originally Posted by kh99 (Post 2388984)
Also, in includes/class_dm.php, part of function verify_list() does the opposite - takes the array and makes a comma-separated list.

Is that what the "implode" does?

Thanks again.

kh99 07 Dec 2012 17:29

Right. http://us2.php.net/manual/en/function.implode.php

Edit: And by the way, I happened to have a reason to be looking at includes/class_dm_user.php this morning, and if you look at function pre_save() in that file, you can see what's done if any usergroups are changed. It looks like the only things to worry about are if you add or delete the "displaygroup", or if the changes would affect the user's ranks.

tsptom 08 Dec 2012 21:11

If anyone is interested, I found this on removing from sets. Seems to work...

UPDATE user
SET membergroupids=
TRIM(BOTH ',' FROM REPLACE(CONCAT(',', membergroupids, ','), CONCAT(',', 'value', ','), ','))
WHERE username = ....


All times are GMT. The time now is 23:31.

Powered by vBulletin® Version 3.8.14
Copyright © 2021, MH Sub I, LLC dba vBulletin. All Rights Reserved. vBulletin® is a registered trademark of MH Sub I, LLC
Copyright ©2001 - , vbulletin.org. All rights reserved.