$assigned_users = [];
// to include profile fields in single query
$allusernames = get_all_user_name_fields(true, 'u');
if ($usercontexts = $DB->get_records_sql("SELECT c.instanceid, c.instanceid, $allusernames FROM {role_assignments} ra, {context} c, {user} u WHERE ra.userid = ? AND ra.contextid = c.id AND c.instanceid = u.id AND c.contextlevel = " . CONTEXT_USER, array($USER->id)))
{
foreach ($usercontexts as $usercontext)
{
$assigned_users[$usercontext->instanceid] = fullname($usercontext);
}
}