在RSform中按用户组过滤

I am using RSform in Joomla to create a form to input data. The form has a dropdown to select the user and that field gets it data using the following code within the form.

What I would like to do is to have the list only show the names of users in a specific user group. The actual users are stored in the table #__users with a unique id called id, the groups are stored in table #__usergroups and the unique id is also called id. I want to be able to filter the list to only show those users in usergroup 2.

Can anyone help please?

Thanks in advance

// Prepare the empty array
$items = array();
// Prepare the database connection
$db = JFactory::getDbo();
// Keep this if you'd like a "Please select" option, otherwise comment or remove it
$items[] = "|Please Select[c]";

// Run the SQL query and store it in $results
$db->setQuery("SELECT id, name, email FROM #__users");
$results = $db->loadObjectList();

// Now, we need to convert the results into a readable RSForm! Pro format.
// The Items field will accept values in this format:
// value-to-be-stored|value-to-be-shown
// Eg. m|M-sized T-shirt
foreach ($results as $result) {
  $value = $result->name;
  $label = $result->name;
  $items[] = $value.'|'.$label;
}

// Multiple values are separated by new lines, so we need to do this now
$items = implode("
", $items);

// Now we need to return the value to the field
return $items;

You will need to map the users with the usergroup_map table like so:

$groupID = 2;
$db = JFactory::getDbo();

$query = $db->getQuery(true);  
$query->select('a.*')
      ->from($db->quoteName('#__users') . ' AS a')
      ->join('LEFT', $db->quoteName('#__user_usergroup_map') . 'AS map2 ON map2.user_id = a.id')
      ->group($db->quoteName(array('a.id', 'a.username')))
      ->where('map2.group_id = ' . (int) $groupID);   
$db->setQuery($query);
$results = $db->loadObjectList();

foreach($results as $result) {
    echo $results->username;
}

This will simply display all username's of user's that belong to the $groupID which is currently set to 2, and you can change this accordingly.

So your final and full code will be this:

$items = array();
$groupID = 2;
$db = JFactory::getDbo();

$query = $db->getQuery(true);  
$query->select('a.*')
      ->from($db->quoteName('#__users') . ' AS a')
      ->join('LEFT', $db->quoteName('#__user_usergroup_map') . 'AS map2 ON map2.user_id = a.id')
      ->group($db->quoteName(array('a.id', 'a.username')))
      ->where('map2.group_id = ' . (int) $groupID);    
$db->setQuery($query);
$results = $db->loadObjectList();

$items[] = "|Please Select[c]";

foreach ($results as $result) {
    $value = $result->username;
    $label = $result->username;
    $items[] = $value.'|'.$label;
}

$items = implode("
", $items);

return $items;

The user mapping to user_group is stored in #__user_usergroup_map table.

So in order to get all user ids that exist in a group you have to run the following query:

$db = JFactory::getDbo();
$query = $db->getQuery(true);

$user_group_id = 8; // user group id

$query->select($db->quoteName(array('user_id')));
$query->from($db->quoteName('#__user_usergroup_map'));
$query->where($db->quoteName('group_id') . ' = ' . $user_group_id );
$db->setQuery($query);

$results = $db->loadObjectList();

Hope this helps