Adding User to Usergroups

saketjain 02 Jul, 2014
Hello,

Following the tutorial at: http://www.chronoengine.com/faqs/61-cfv4/cfv4-tutorials/4618-how-can-i-add-a-user-to-a-user-group.html, I have been unable to add my users to custom usergroups.

The code I used is:
<?php
    if ( !isset($form->data['membership_type']) ||
         !$form->data['membership_type'] ) {
        // there is no membership_type selected
        return false;
    }
    $group_array = array (
        '0' => '11', //Corporate
        '1' => '12', //Individual
        '2' => '13', //Student
    );

    $group_id = $group_array[$form->data['membership_type']];
    $user_id  = $form->data['_PLUGINS_']['joomla_registration']['id'];
    jimport( 'joomla.user.helper' );
    JUserHelper::addUserToGroup($user_id, $group_id);
?>



The structure of Events and Actions is such that upon "Joomla! Registration" (Which I do on clicking a "Submit" button), there are further events: "On success" and "On failure". The above code is run "On success" and is a "Custom Code".

In "Joomla! Registration", I have set User Groups to 10. And that is what is being assigned in the database.

Any help would be greatly appreciated!
GreyHead 02 Jul, 2014
Hi saketjain,

The code looks OK to me. I suggest that you add a Debugger action to see exactly what values are being set and used in the function. It may be that CFv5 no longer adds the new User ID to the same parameter as CFv4 :-(

Bob
saketjain 02 Jul, 2014
Thank you! I'll try doing that and see what's the output.

I do have one clarification that I needed to make. While the group_id is being set as 10(the value set in the joomla! registration), it is also being set as 12 (the correct value).
Now 10 is also a parent of 12.

My question is, is this normal behavior?
GreyHead 02 Jul, 2014
Hi saketjain,

Sorry. I have no idea. I looked at the code and ChronoForms isn't adding any extra groups but it is possible that the Joomla! Registration process adds a parent group as well.

Bob
Max_admin 02 Jul, 2014
The Joomla registration action should add the user to the configured groups only, I have some code which will add the user to extra groups, this is for v5 only:

<?php
$user = \GCore\Libs\Base::getUser();
$user_id = $user["id"];
if(!\GCore\Admin\Models\GroupUser::getInstance()->field('user_id', array('user_id' => $user_id, 'group_id' => 10))){
	\GCore\Admin\Models\GroupUser::getInstance()->save(array('user_id' => $user_id, 'group_id' => 10), array('new' => true));
}


The code above adds the logged in user to the group with id = 10

Regards,
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.