I'm trying to use rules to send an email to all users with a specific role in an organic group when someone requests to join the group.
This code almost works. It returns all users from a specific group ($GID) with the specified role ($RID), but it doesn't restrict the role to that group. In other words, if they have the appropriate role in another group, they're included in the results.
$current_members = db_select('og_membership', 'om');
$current_members->join('og_users_roles', 'ogur', 'om.etid = ogur.uid');
$current_members->condition('om.gid', $GID);
$current_members->condition('om.entity_type', 'user');
$current_members->condition('ogur.rid', $RID);
$result = $current_members->execute();
while ($res = $result->fetchAssoc()) { $members[] = $res['etid']; }
// Remove duplicate items.
$members = array_keys(array_flip($members));
What do I need to change to restrict the results so that it only returns users with the specified role ID within the specified group?