assign users to group from select on registration form

Assign to group from select on registration form

You can assign new users to groups depending on the value of a select-element on the registration form. There are several registration-components for Joomla which have options to add more then the default registration fields to the registration form. When during registration, there is no match, or nothing is selected, the user will be assigned to configured 'default usergroup' (if set). You can create a select-element on the form and make FUA read the value of the field during registration and thus assign the new user to one or more groups. Enter the select-elements name, and its conditional values as shown in this example:

 

Select on registration form:

 

HTML code of the select element:

<select name="cb_relationshiptocamp"> 
    <option value="camper">camper</option>
    <option value="parent">parent</option> 
    <option value="staff">staff</option> 
    <option value="other">other</option>
</select>

 

PHP code to assign the user to a group based on users selection:

$value = JRequest::getVar('cb_relationshiptocamp', '');
if($value=="camper"){
    $groups = '12'; 
}elseif($value=="parent"){
    $groups = '13'; 
}elseif($value=="staff"){
    $groups = '14'; 
}

First the value from the form-element is gathered, then a condition of each combination is tested.

In the above code == means 'is equal to'.

The syntax of the code is PHP.

   

Assigning users to more then one usergroup at registration

To assign users to more then one group at registration based on their selection on the registration form, check this code:

$value = JRequest::getVar('cb_relationshiptocamp', '');
if($value=="camper"){
   $groups = '12,45,67'; 
}elseif($value=="parent"){
   $groups = '13,67'; 
}elseif($value=="staff"){
   $groups = '14'; 
}

   

Reading the select when elements are parsed as an array

When all or some of the form-elements are parsed as an array, this is how to get the value of the field:

$form_elements = JRequest::getVar('params', null, 'post', 'array');
$value = $form_elements['cb_relationshiptocamp'];

In this example, the array of form-elements is called 'params'.

 

This is an example from CBsubs.

The HTML from a radio group:

<input type="radio" name="cbpplanE[0][selected][]" id="cbpplan1" value="1" mosReq="1" mosLabel="Membership Type" class="required" />
<input type="radio" name="cbpplanE[0][selected][]" id="cbpplan3" value="3" mosReq="1" mosLabel="Membership Type" class="required" />

code:

$form_elements = JRequest::getVar('cbpplanE', null, 'post', 'array');
$value = $form_elements[0]['selected'][0];
if($value=='1'){
$groups = '12';
}elseif($value=='3'){
$groups = '13';
}else{
$groups = '14';
}

   

Assigning groups with a multi-select element

When using a multiselect to assign users to groups, use code like this:

$values = JRequest::getVar('cb_multi', null, 'post', 'array');
foreach($values as $value){
    if($value=="old camper"){
       $groups .= '12,'; 
    }elseif($value=="parent"){
       $groups .= '13,'; 
    }elseif($value=="staff"){
       $groups .= '14,'; 
    }
}

   

Assign users from more then one form elements

If you are using more then one form elements together to assign users to groups, use the code like this:

$gender = JRequest::getVar('gender', '');
$team = JRequest::getVar('team', '');
if($gender=="man" && $team=="team1"){
    $groups = '12'; 
}elseif($gender=="man" && $team=="team2"){
    $groups = '13'; 
}elseif($gender=="woman" && $team=="team1"){
    $groups = '14'; 
}elseif($gender=="woman" && $team=="team2"){
    $groups = '15'; 
}

First the 2 values from the form-elements are gathered, then a condition of each combination is tested.

In the code && means AND. So the first condition would read:

if the gender is 'man' AND the team is 'team1' THEN assign to group 12.

You can also use || which means OR.

   

Making a difference when assigning from frontend or backend

When you want to assign groups differently when the profile is editted from the:

  • frontend (user edits profile)
  • backend (administrator edits a users profile)

$app = JFactory::getApplication();
$club = JRequest::getVar('club', '');
if($app->isAdmin()){
    //backend
    if($club=="Olveston"){
        $groups = '13';    
    }else{
        //default
        $groups = '12';
    }
}else{
    //frontend
    if($club=="Olveston"){
        $groups = '14';    
    }else{
        //default
        $groups = '12';
    }
}

   

Using the database

In some situations you might need to get data from the database.

In this example the form-element 'paid' was configured to only be used in the backend for administrators. On the frontend this field is not there, so we need to get the data straight from the database.

$app = JFactory::getApplication();
$club = JRequest::getVar('club', '');
$paid = "no";
if($app->isAdmin()){
    //backend
    $paid = JRequest::getVar('paid', '');
}else{
    //frontend
    $database = JFactory::getDBO();
    //get $paid from database
    $database->setQuery("SELECT cb_paid FROM #__comprofiler WHERE id='$user_id' LIMIT 1");
    $rows = $database->loadObjectList();
    foreach($rows as $row){    
        $paid = $row->cb_paid;
    }    
}
if($club=="Olveston" && $paid=="yes"){
    $groups = '13,14';
}elseif($club=="Olveston" && $paid=="no"){
    $groups = '13';
}else{
    //default
    $groups = '12';
}

   

When the userprofile is editted, assign to the current group(s)

When the user-profile is editted, assign the user to the groups the user is already assigned to like this:

$club = JRequest::getVar('club', '');
if($club=='Olveston'){
   $groups = '18'; 
}else{
   $groups = $current_groups; 
}

note that $current_groups can only be used in the 'code to be used when processing update form'-field. When used in the field above that (field 'code to be used when processing registration form') the user is not assigned to any group yet, so there are no current groups and the user will not be assigned to any groups at all. So only use $current_groups in the 'code to be used when processing update form'-field.

   
 
Follow Us On Twitter