Thanks again, Paul. Your latest hint got me a lot further. Finally I just created a separate display page with my routine in it. This allowed me to insert a bunch of print() functions to check variables and progress and really get this thing debugged. Seems to be working like a champ.
Here's the final code, in case it will be of use to anybody else (please see questions and concerns below):
PHP Code:
// Add or remove user from secondary usergroup
// according to Pwd value in user's profile
global $vbulletin;
// #### SET THESE THREE STRINGS TO THE PROPER VALUES FOR YOUR SYSTEM ####
$org_mg = '12'; // membergroup # as string
$org_pwfield = 'field5'; // custom field# in profile as string
$org_pwphrase = 'org_member_password'; // vbphrase name containing pwd
// other string variables
$org_mglist = (''.$vbulletin->userinfo['membergroupids']); // current membergroups string
$org_userid = ($vbulletin->userinfo['userid']); // userid of user
$org_mg_pos = (strpos($org_mglist, $org_mg)); // find target membergroup position
// in group list - returns false
// if not found
// boolean variables
$org_grp = ($org_mg_pos !== false); // User is in target group (true/false)
$org_haspwd = ($vbulletin->userinfo[$org_pwfield] == $vbphrase[$org_pwphrase]); // User
// has correct password in profile (T/F)
if ($org_haspwd != $org_grp) // Need to update membergroups?
{
if (!$org_grp) // If not currently in target group
// add to group
{
if ($org_mglist != '')
{
$org_comma = ',';
}
$org_newmg = ($org_mglist . $org_comma . $org_mg);
}
else // else currently in target group
// and must remove
{
// ********* potential problem section *************
$org_srch = ($org_mg . ','); // search string to remove = mg#,
$org_newmg = str_replace($org_srch,'',$org_mglist);
$org_mglist = $org_newmg;
$org_srch = (',' . $org_mg); // search string to remove = ,mg#
$org_newmg = str_replace($org_srch,'',$org_mglist);
if ((strpos($org_newmg, $org_mg)) !== false)
{
$org_newmg = '';
}
// ******** end potential problem section **********
}
$updatefields = $vbulletin->db->query("
UPDATE user
SET membergroupids='$org_newmg'
WHERE userid=$org_userid
");
}
Couple of concerns/questions:
Had to frog around with the str_replace() function quite a bit. Couldn't get it to work recursively on itself as
PHP Code:
$a = str_replace($b,$c,$a));
Finally had to bring in a temp string to hold results:
PHP Code:
$d = str_replace($b,$c,$a));
$a = $d;
Second concern is the method used to remove target group from the list. (See portion of code labeled as 'potential problem section'. ) Happens to work okay for me because my target group is 2-digit group 12. So nulling out '12,' and ',12' works.
But if my target were a single digit number, say 3, nulling out '3,' and ',3' via str_replace() could mess up some two-digit group numbers:
'3,12,23,31' would become '12,21'
.. or even I could have problems if, heaven forbid, we ended up with three digit membergroup numbers.
So for MY use for now, this will work, but as a generic routine for others, it's still lacking.
Can anybody suggest a way of dealing with removing the target group that would solve this problem? (Maybe by working with the group numbers as an array?)