Change User Group after successful form submission

boyjah 08 Oct, 2015
I would like to change the user group in #__user_usergroup_map for registered users once they have successfully completed a Chronoforms V5 form in Joomla! 3.4.4

Would that be best done as a DB Save or as Custom Code? What is the php that I would need to accomplish that? I found 3 forum posts/How To articles about setting the user group, but I could not understand how to apply it to this case.

Specifically I would like to change the group_id = 11 if they are currently group_id = 12 when they have submitted the form.
GreyHead 08 Oct, 2015
Hi boyjah,

You can do this with PHP in a custom code action. In Joomla! 3 a user can belong to several groups so checking them returns an array . . . your code needs to edit this array and then save it again.
<?php
$user = JFactory::getUser();//get current user
$groups = $user->groups;
// some code here to edit the $groups array
$user->groups = $g;
$user->save();
?>

Bob
boyjah 08 Oct, 2015
Still not entirely certain how to make what you have kindly provide work, but I will tinker with it and see where it gets me.

Can you say why this would be better as a custom code action as compared to a DB Save action? I am still trying to understand in what situations to use one action over the other.
GreyHead 08 Oct, 2015
Hi boyjah,

If you are changing the core Joomla! tables - which you are here - it is IMHO almost always better to use the Joomla! methods to make the changes instead of writing directly to the tables.

Bob
boyjah 08 Oct, 2015
So that means using a custom code action over DB Save?
boyjah 09 Oct, 2015
The form I have created is available only to users with group_id of 12, so I already know that the user has an entry in user_usergroup_map with group_id of 12 (in addition to other possible entries with a different group_id).

Can anyone tell me why the following code in a Custom Code action will not change the group_id from 12 to 11 on submit?

<?php
$user = JFactory::getUser();
UPDATE #__user_usergroup_map SET `group_id` = '11' WHERE `user_id` = {$user->id} AND `group_id`='12';
?>

I am logged in as user with user_id of 461 and this is the record I want to affect:
GreyHead 09 Oct, 2015
Hi boyjah,

So that means using a custom code action over DB Save?
Yes

Your code won't work because you can't just stick a MySQL query inside <?php ?> tags and expect anything useful to happen. There are many other examples here . . . but I suggest that you use the Joomla! User methods as in my previous post.

Bob
boyjah 15 Oct, 2015
Answer
This is the code that works for my purposes. I hope it helps others! I put it in a Custom Code action in the On Submit section.

I wanted to change the group_id from 12 to 11 on submit of a specific form, accessible only to certain logged in users.

<?php
// Get the connected user
$user = JFactory::getUser();
// set the user in group ID = x
JUserHelper::setUserGroups($user->get('id'), array(2,11) );
?>
GreyHead 16 Oct, 2015
Hi boyjah,

That's probably OK but note that your code will remove them from any other groups - except 2 & 12 that they might have been subscribed to. It would be safer to get the existing list, remove 11, add 12 and re-save.

Bob
boyjah 16 Oct, 2015
I agree with you and I would love to be able to put something like that in place, but after spending days searching for a clear example of how to accomplish that and tinkering endlessly (as a novice) with all kinds of code, this is the only working solution I could come up with.
GreyHead 18 Oct, 2015
Hi boyjah,

For other readers here is some code that will check before adding or deleting:
<?php
// get current user
$user = JFactory::getUser();
$groups = $user->groups;

// add the new group 12 if it isn't already there
if ( !in_array(12, $groups) ) {
  $groups[] = 12;
}

// remove group 11 if it is present
if ( ($key = array_search(11, $groups) ) !== false ) {
    unset($groups[$key]);
}

// save the new groups array 
$user->groups = $groups;
$user->save();
?>
!! not tested and may need debugging

Bob

[[>> Later : fixed a typo in // comment <<]]
boyjah 23 Oct, 2015
Thank you! It now does what I needed and it does it PROPERLY!

Your code initially generated an error, but I adjusted the forward slash spacing at "/ /get current user" and that resolved it. So, for those watching at home, this is the code that I am using and that works for my case:
<?php
// get current user
$user = JFactory::getUser();
$groups = $user->groups;

// add the new group 11 if it isn't already there
if ( !in_array(11, $groups) ) {
  $groups[] = 11;
}

// remove group 12 if it is present
if ( ($key = array_search(12, $groups) ) !== false ) {
    unset($groups[$key]);
}

// save the new groups array 
$user->groups = $groups;
$user->save();
?>

I also added another Custom Code action at the end of On Submit to force logout the user so that they would need to login again for the new user group to take effect.
<?php
$app = JFactory::getApplication();
$error = $app->logout(); 
?>
This topic is locked and no more replies can be posted.