• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Add user to another usergroup

Add user to another usergroup 9 months 1 week ago #43535

  • info4344
  • info4344's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 6
Dear support,
I have a form for Registered users only and I wish to add submitters to another couple of Joomla usergroups whose IDs are 13 and 16.

How could I do that?
I installed the Plugin - Joomla! User Registration, but I do not think that it is the right solution. It seems for new users only.

Thanks in advance
:)
The administrator has disabled public write access.

Add user to another usergroup 9 months 1 week ago #43538

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 245
  • Thank you received: 64
This is a rather long and complicated answer but you'll need a custom script to do that, so this is essentially what you are trying to do:

1. Load Joomla framework: The script begins by loading the Joomla framework to get access to the Joomla API.
2. Define group IDs: The group IDs you want to add (13 and 16) are specified in an array.
3. Fetch current user groups: It retrieves the current groups the user is a member of.
4. Merge groups: Combines the current groups with the new ones, ensuring no duplicates.
5. Update database: It deletes the user's current group mappings and inserts the new group mappings.

But you'll need to break this into two parts, the first when the form loads and the second to update the user when the form is submitted. This is written for Joomla 4, if you are still on J3 it's slightly different.
Here's part 1which you put in PHP scripts - pre processing/script called before form is generated. You'll need to change the value of the $targetGroupName variable for yuor own group and later comment out the final echo line.
// Load the Joomla framework
use Joomla\CMS\Factory;
use Joomla\CMS\Session\Session;
 
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/../..'));
require_once(JPATH_BASE . '/includes/defines.php');
require_once(JPATH_BASE . '/includes/framework.php');
 
// Instantiate the application
$app = Factory::getApplication('site');
 
// Get the currently logged-in user
$user = Factory::getUser();
 
// Check if the user is logged in
if ($user->guest) {
    die("Error: No user is currently logged in.");
}
 
// Get the user ID of the currently logged-in user
$userId = $user->id;
 
// Define the target group name
$targetGroupName = 'Super Users';  // Change this to the desired group name
 
// Get the database connection
$db = Factory::getDbo();
 
// Fetch the ID of the target user group
$query = $db->getQuery(true)
    ->select($db->quoteName('id'))
    ->from($db->quoteName('#__usergroups'))
    ->where($db->quoteName('title') . ' = ' . $db->quote($targetGroupName));
$db->setQuery($query);
$targetGroupId = $db->loadResult();
 
// Check if the target group was found
if (!$targetGroupId) {
    die("Error: '$targetGroupName' user group not found.");
}
 
// Fetch the current user groups
$query = $db->getQuery(true)
    ->select($db->quoteName('group_id'))
    ->from($db->quoteName('#__user_usergroup_map'))
    ->where($db->quoteName('user_id') . ' = ' . $db->quote($userId));
$db->setQuery($query);
$currentGroups = $db->loadColumn();
 
// Store the necessary data in the session for later use
$app->setUserState('com_custom.form.userId', $userId);
$app->setUserState('com_custom.form.currentGroups', $currentGroups);
 
echo "Form loaded. User ID: $userId, Target Group ID: $targetGroupId";

Here's part two which goes in PHscripts - script called on form process, again comment out the last echo statement
// Load the Joomla framework
use Joomla\CMS\Factory;
use Joomla\CMS\Session\Session;
 
define('_JEXEC', 1);
define('JPATH_BASE', realpath(dirname(__FILE__) . '/../..'));
require_once(JPATH_BASE . '/includes/defines.php');
require_once(JPATH_BASE . '/includes/framework.php');
 
// Instantiate the application
$app = Factory::getApplication('site');
 
// Get the data from the session
$userId = $app->getUserState('com_custom.form.userId');
$currentGroups = $app->getUserState('com_custom.form.currentGroups');
 
// Define the group IDs to add
$groupIdsToAdd = [13, 16];
 
// Merge the current groups with the new group IDs, ensuring no duplicates
$newGroups = array_unique(array_merge($currentGroups, $groupIdsToAdd));
 
// Get the database connection
$db = Factory::getDbo();
 
// Remove all current group mappings for the user
$query = $db->getQuery(true)
    ->delete($db->quoteName('#__user_usergroup_map'))
    ->where($db->quoteName('user_id') . ' = ' . $db->quote($userId));
$db->setQuery($query);
$db->execute();
 
// Insert the new group mappings
$query = $db->getQuery(true)
    ->insert($db->quoteName('#__user_usergroup_map'))
    ->columns([$db->quoteName('user_id'), $db->quoteName('group_id')]);
 
foreach ($newGroups as $groupId) {
    $query->values($db->quote($userId) . ', ' . $db->quote($groupId));
}
 
$db->setQuery($query);
$db->execute();
 
echo "User ID $userId has been updated with the new groups: " . implode(', ', $newGroups);
 

That should do it or at least give you somewhere to start from.
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
Last Edit: 9 months 1 week ago by iceferret.
The administrator has disabled public write access.

Add user to another usergroup 9 months 1 week ago #43540

  • dragos
  • dragos's Avatar
  • OFFLINE
  • Administrator
  • Posts: 630
  • Thank you received: 114
Hello,

You can also try taking a look at this article.
The administrator has disabled public write access.
The following user(s) said Thank You: info5963

Add user to another usergroup 9 months 1 week ago #43543

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 245
  • Thank you received: 64
That's a heck of a lot easier, didn't know you could do that B)
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
The administrator has disabled public write access.

Add user to another usergroup 9 months 5 days ago #43552

  • info4344
  • info4344's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 6
Thank you very much.

My final script was this:
list($replace, $with) = RSFormProHelper::getReplacements($SubmissionId);
$userID = str_replace($replace, $with, '{global:userid}');
Joomla\CMS\User\UserHelper::addUserToGroup($userID, 13);

Script called after form has been processed
I hope this may help other users too.

Kind regards
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!