ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Assigning members (users) to member groups???

June 24, 2007 1:28pm

Subscribe [6]
  • #1 / Jun 24, 2007 1:28pm

    hothousegraphix

    851 posts

    I’m looking to establish a method for assigning users to member groups either during the site registration process (probably a custom dropdown field added to the registration form), or via some sort of an automated process (by evaluating each users e-mail address extension).

    I think ultimately allowing users the opportunity to make this choice during the registration process makes the most sense. I’m wondering if someone can point me in the direction of documentation that might provide an explanation as to this could be accomplished. Assuming I’m using a .(JavaScript must be enabled to view this email address), would it be possible to assign users to a member group using this, if so, how?

    **Just to add a piece of information, I’m currently using the Forum registration form to create all user accounts. Is it even possible to use custom profile fields in this type of situation, or would it be advised to return to using the main site registration form.**

    Thanks.

  • #2 / Jun 24, 2007 3:47pm

    hothousegraphix

    851 posts

    Is it even possible to use custom profile fields in this type of situation, or would it be advised to return to using the main site registration form.**


    To answer this portion…I’ve added a custom field to my registration form and can confirm that this addition is recognized in the forums registration form.

  • #3 / Jun 25, 2007 11:23am

    hothousegraphix

    851 posts

    So…in digging around, I’ve looked in the system/modules/members directory at all the scripts to see if I might find what I need - no luck. I’ve also looked in cp.admin.php to see how the cp is handeling the assignment of users to Member Groups, here I did see a few functions related to the member_groups but nothing that handled this aspect (at least that I could see).

    Again, I just want to be able to allow users to assign themselves to any one (from a set) of pre-defined Member Groups accessible from a Custom Profile Filed added to the user registration form. Would this even be possible. If not, would anyone have another suggestion that might allow me to accomplish this task.

    Any help would be much appreciated. 

    Thanks in advance.

  • #4 / Jun 25, 2007 11:46am

    Ingmar

    29245 posts

    No, I don’t think you can currently do this. Well, with an extension, perhaps. What I have done on occasion is let them select a custom pulldown field, and then use a simple SQL query to change their membergroup. Makes sense?

  • #5 / Jun 25, 2007 12:16pm

    hothousegraphix

    851 posts

    Good morning (from where I’m sitting), thanks for responding.

    No, I don’t think you can currently do this. Well, with an extension, perhaps. What I have done on occasion is let them select a custom pulldown field, and then use a simple SQL query to change their membergroup. Makes sense?

    Somewhat, I think…well…maybe not. Questions; I’m assuming what you’re suggesting is that the SQL query is an action perfomed by the Admin on pending members after the point of registration and not an automated process? Or, is this query triggered by some action?

    This is getting into areas that I have no experience in. I do understand the concepts but beyond that I have no particle experience in this area (DB stuff) so I apologize for what may seem like fairly basic questions.

  • #6 / Jun 25, 2007 12:23pm

    Ingmar

    29245 posts

    I’m assuming what you’re suggesting is that the SQL query is an action perfomed by the Admin on pending members after the point of registration and not an automated process? Or, is this query triggered by some action?

    Any way you like, really. You could have it triggered by certain templates, I have used the cron plugin to perform this automatically once an hour. So, people sign-up, they are assigned to the generic members groups, then after (at most) 1 hour re-assigned to another membergroup based on a custom profile field they selected. IF you wanted to go overboard you could do that check a couple of times an hour, too.

    This is getting into areas that I have no experience in. I do understand the concepts but beyond that I have no particle experience in this area (DB stuff) so I apologize for what may seem like fairly basic questions.

    It’s really only a simple query, basically re-assigning the membergroup-id.

  • #7 / Jun 25, 2007 1:24pm

    c.emerson

    36 posts

    I did this using an extension with the cp_members_validate_members hook within cp.members.php. Our site requires administrators to activate memberships so the group assignment is part of the process of manual activation.

    The extension processing code looks like this (where all the code in <> should be replaced with the real values to check):

    function set_<mycompany>_employee_member_group()
        {
            global $DB;
        // for each selected user being activated, check the users email address - is it <mycompany>.com? 
        // if so set members' group to the <mycompany> Employees group. 
        // if not, leave them in the default group and process the next member in the post array 
            foreach ($_POST as $key => $val)
                {        
                    if (strstr($key, 'toggle') AND ! is_array($val))
                        {                   
                if ($_POST['action'] == 'activate')
                    {
                    $query = $DB->query("SELECT email FROM exp_members WHERE member_id = '$val'");    
                    if (ereg("@<mycompanydom>", ($query->row['email'])))
                         $DB->query("UPDATE exp_members SET group_id = <mycompanyempgroup> WHERE member_id = '".$DB->escape_str($val)."'");
                    }
                }
            }
    
        }    //  END FUNCTION

    Could easily be expanded to handle various domains.
    Hope this helps.
    -C

  • #8 / Jun 25, 2007 1:56pm

    hothousegraphix

    851 posts

    So, using your method of applying the cron plugin, I suppose I could simply create my own timer are well, but…since this exists…why not use it.

    If I establish, say, 5 member_group options for my Custom Profile Field with the name of “groupChoice” to be added to my registrations form;

    group_1
    group_2
    group_3
    group_4
    group_5

    I would assume my evaluation logic will be something like:

    {exp:query sql="SELECT screen_name FROM member_group WHERE group_id = '5' "} // 5 being the group_id for the default group members

    foreach{screen_name};
    if{groupChoice} == group_1;
    {group_id} == group_1 ID;

    {/exp:query}

    I realize the above I just posted is an abomination, I’m just trying to sort this all out.
    How would I incorporate the query with the cron plugin tag? And then where is it best to place the cron plugin tag?

    {exp:cron minute="30" hour="0" which="db_cache" plugin="cron_clean_cache"}
    //I’d like to do every 30min but I’m not sure I’ve configured the tag correctly to do this.
    {/exp:cron}

  • #9 / Jun 25, 2007 2:22pm

    hothousegraphix

    851 posts

    I did this using an extension with the cp_members_validate_members hook within cp.members.php. Our site requires administrators to activate memberships so the group assignment is part of the process of manual activation.

    Wow…c.emerson…I think this may be just what I need. So…forgive my ignorance…you’re placing this function within the “cp_members_validate_members” process in “cp.members.php”?

    And in looking at the function is seems it’s called each time the registration form POSTS?

    You’re assigning to user groups based on domains. We’re thinking that because there are too many to account for in our situation, it makes more sense to allow the potential user signing up to select their group. If we’re establishing a Custom Profile field with the name of groupChoice, would I do something like:

    if ($_POST['action'] == 'activate')
                    {
                    $query = $DB->query("SELECT groupChoice FROM exp_members WHERE member_id = '$val'");    
                    if (ereg("group_1", ($query->row['groupChoice'])))
                         $DB->query("UPDATE exp_members SET group_id = <newgroup_1_ID> WHERE member_id = '".$DB->escape_str($val)."'");
                    }
                    else if (ereg("group_2", ($query->row['groupChoice'])))
                         $DB->query("UPDATE exp_members SET group_id = <newgroup_2_ID> WHERE member_id = '".$DB->escape_str($val)."'");
                    }
                    //and so on?
                }
  • #10 / Jun 25, 2007 3:02pm

    c.emerson

    36 posts

    you’re placing this function within the “cp_members_validate_members” process in “cp.members.php”

    Not exactly.  We did this using an extension - so no need to hack the cp modules. The extensions are in their own files separate from delivered EE code.
    On the extension side: The extension documentation is great - I was able to follow the docs easily to create an extension (I’ve a programming background but am new to PHP).  Refer to the documentation for the cp_members_validate_members hook (documentation is linked from within this post).

    seems it’s called each time the registration form POSTS

    This hook is invoked not when the member registers, but when an administrator goes into the “Activate Pending Members” form and clicks “Submit”.
    I believe custom member profile fields don’t get stored in the members table proper, so I think you will need to query the member_fields and member_data tables 😉  instead of members to get your groupChoice and then you can just use a PHP switch statement to set your group ID…
    assuming $user_groupChoice is the variable containing the users’ choice code may look something like  this:

    switch ($user_groupChoice)
      {
      case "1": 
         $new_group_id = <newgroup_1_ID>;
         break;
      case "2": 
         $new_group_id = <newgroup_2_ID>;
         break;
      ...
      default:
         break;
      }

    then add the code to update the members table with the new group_id using the $new_group_id variable set above.
    Don’t think you need the ereg function in your case. I used it to parse out part of the email variable.
    Make sense?

  • #11 / Jun 25, 2007 4:32pm

    hothousegraphix

    851 posts

    Ok…so this is a start…I get the fact that a whole bunch of this is not correct, but I’m hoping for a bit of hand holding (sorry, I suffer from what most designers do when thinking about programming…our eyes roll back into our head and we all sort of moan)

    I’m sure the query if completely incorrect. I haven’t been able to locate any documentation that would explain the table structure and how to access custom fields added to the registration form

    <?php>
    
    if ( ! defined('EXT'))
    {
        exit('Invalid file request');
    }
    
    class memberGroup_update
    {
        var $settings        = array();
        var $name            = 'memberGroup_update';
        var $version         = '1.0.0';
        var $description     = 'update users member group';
        var $settings_exist  = 'n';
        var $docs_url        = 'n';
    
    // END CLASS 
        
    function memberGroup_update($settings='')
    {
        $this->settings = $settings;
    }
    
    // -------------------------------
    //   Constructor 
    // -------------------------------
        
    function updateUsers_member_group()
    {
        global $DB;
        
        foreach ($_POST as $key => $val)
        {        
            if (strstr($key, 'toggle') AND ! is_array($val))
            {                   
                if ($_POST['action'] == 'activate')
                {
                    $query = $DB->query("SELECT groupChoice FROM exp_members_data_tables WHERE member_id = '$val'");   
                    
                    switch ($user_groupChoice)
                    {
                    case "1":
                       $new_group_id = '6';
                       break;
                    case "2":
                       $new_group_id = '7';
                       break;
                    case "3":
                       $new_group_id = '8';
                       break;
                    case "4":
                       $new_group_id = '9';
                       break;
                    case "5":
                       $new_group_id = '10';
                       break;
                    case "6":
                      default:
                      break;
                    } 
                 
                    $DB->query("UPDATE exp_members SET group_id = $new_group_id WHERE member_id = '".$DB->escape_str($val)."'");
                   
                    }
                }
            }
        }    
    }
    //  END CONSTRUCTOR 
      
        
    // --------------------------------
    //  Activate Extension
    // --------------------------------
    
    function activate_extension()
    {
        global $DB;
        
        $DB->query($DB->insert_string('exp_extensions',
            array(
                'extension_id' => '',
                'class'        => "memberGroup_update",
                'method'       => "updateUsers_member_group",//would this be the function call?
                'hook'         => "cp_members_validate_members",
                'settings'     => "",
                'priority'     => 10,
                'version'      => $this->version,
                'enabled'      => "y"
                )
            )
        );
    }
    
    // END
        
    <?>
  • #12 / Jun 25, 2007 5:39pm

    c.emerson

    36 posts

    You’re getting there…
    You are correct that the method in the activate matches the function where you actually do your extension work.
    I use the ‘Database Query Form’ in the CP to test out my SQL and find it extremely helpful! That along with the ‘Manage Database Tables’ utility will help you figure out the tables. This is a little tricky since you need a variable in the query - alternatively you could hardcode the field_id. I’m not positive on how to code the 2nd query to include the variable so below is a concept…maybe a more experienced member of the community can chime in with a better way to get your $user_groupChoice—regardless you need to replace the current $query with some code to populate $user_groupChoice:

    $query = $DB->query("select m_field_id from exp_member_fields where m_field_name='groupChoice'"); //get the # of your custom field groupChoice 
    $yourCustomFieldID = ($query->row['m_field_id'])     ; //store it in a variable 
    $yourCustomFieldSequence = 'm_field_id' . '_' . $yourCustomFieldID ; //append it to build the field name in the data table
    $query = $DB->query("select {$yourCustomFieldSequence} from exp_member_data where  member_id='$val'") ; //now grab the value of your custom field
    $user_groupChoice = ($query->row['{$yourCustomFieldSequence}']); //and load it into your variable

    Also—remember 1) to set $new_group_id for default too so you don’t blank group_id out or throw an error, 2) to add an update and disable function to your extension (disable is esp. helpful when testing your extension).

    Hope this helps.

  • #13 / Jun 25, 2007 6:00pm

    hothousegraphix

    851 posts

    I’ll work on this thru the evening…I appreciate the assistance a great deal.

  • #14 / Jun 26, 2007 6:38pm

    hothousegraphix

    851 posts

    Baby steps here. I’ve got the disable function and started messing around with digging up info on my custom fields in my DB. In following your example I understand the logic but I’m still pretty confused about how to incorporate the switch case for each member_gruop_id.

  • #15 / Jun 28, 2007 8:07pm

    hothousegraphix

    851 posts

    With a tremendous amount of assistance from c.emerson I’ve been able to get this extension functioning based on the “cp_members_validate_members” hook and it work’s wonderfully.

    I would now like to have the member group re-assignment occur during the account self-activation process. According to the EE docs the hook is available to accomplish this -  member_register_validate_members

    Below is the current function c.emerson helped me put together that is designed to work off the “cp_members_validate_members” hook.

    function memberGroup_update($settings='')
        // -------------------------------
        //   Constructor
        // -------------------------------
        {
            $this->settings = $settings;
        }
        function updateUsers_member_group()
        {
            global $DB;
            
            foreach ($_POST as $key => $val)
            {        
            if (strstr($key, 'toggle') AND ! is_array($val))
            {                   
                if ($_POST['action'] == 'activate')
                {
                $query = $DB->query("select m_field_id_1 from exp_member_data where  member_id='$val'") ; //now grab the value of your custom field 
                $user_groupChoice = ($query->row['m_field_id_1']); //and load it into your variable
                    echo $user_groupChoice; // TEST - see if we have our value
                    switch ($user_groupChoice)
                     {
                    case "group 1":
                        $new_group_id = '6';
                        echo $new_group_id; // TEST - see if we have our value
                        break;
                    case "group 2":
                        $new_group_id = '7';
                        echo $new_group_id; // TEST - see if we have our value
                        break;
                    case "group 3":
                        $new_group_id = '8';
                        echo $new_group_id; // TEST see if we have our value
                        break;
                    
                    default:           //default is the all others bucket
                        $new_group_id = '4'; //can't figure out how to assign so leave in 'pending';
                        break;
                    }                    
                        $DB->query("UPDATE exp_members SET group_id = $new_group_id WHERE member_id = '".$DB->escape_str($val)."'"); 
                    }
                }
            };

    I’ve made the most obvious change first, updating the hook in the ext. This change alone didn’t allow the ext. to re-assign my test member and I’m not too surprised.

    Could someone lend a bit of advice on how accessing the necessary info to perform this action will differ.

    I understand that in this new situation the registrant will be the entity initiating this change for just them? But I guess I need a bit of guidance here.

    Thanks

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases