3

I want to get user role from user id. I am using loop in my code where i want to show all user except admin. i used below code but its not working.

$context = get_context_instance (CONTEXT_SYSTEM);
$roles = get_user_roles($context, $USER->id, false);
$role = key($roles);
$roleid = $roles[$role]->roleid;

Its provide me blank array as like screenshot. Also below my all code. https://prnt.sc/gq8p12

$allUsers = $DB->get_records('user');

    $SQL = "SELECT * FROM `".$CFG->prefix."config` WHERE `name` LIKE 'siteadmins'";
    $getSiteAdmins = $DB->get_record_sql($SQL);
    $explodeAdminIds = explode(',', $getSiteAdmins->value);    
    $context = get_context_instance (CONTEXT_SYSTEM);       

    if(!empty($allUsers))
    {
        foreach ($allUsers as $allUser) 
        {
            if(!in_array($allUser->id, $explodeAdminIds))
            {
                $roles = get_user_roles($context, $allUser->id, false);
                $role = key($roles);
                $roleid = $roles[$role]->roleid;

                echo 'USER ID -- '.$allUser->id.' >>> ';
                print_r($roles); echo '<br>';

                $name = '<a href="' . $CFG->wwwroot . '/user/profile.php?id='.$allUser->id.'&mid=4">'.$allUser->id.'_'.$allUser->firstname.' '.$allUser->lastname.'</a>';
                $confirmed = ($allUser->confirmed == 1) ? 'Active' : 'In-active';
                $table->data[] = array(
                    $i,
                    $name,
                    'Team Name',
                    $allUser->email,
                    $allUser->phone1,
                    'Role',
                    $confirmed,
                    //empty($coachusrarr)?'--':implode(',',$coachusrarr),   
                    //empty($tmpleaderarr)?'--':implode(',',$tmpleaderarr),   
                    //$coach,
                );
                $i++;
            }
        }
    }
Pritesh Mahajan
  • 4,974
  • 8
  • 39
  • 64

2 Answers2

4

The basic problem is that get_user_roles($context, $userid) will only get you a list of roles assigned at that particular context level. Very few users have roles assigned at the system context, it is much more usual for roles to be assigned at a course level. This allows users to have different roles in different courses (a teacher on one course, might be enrolled as a student on another course).

If you want to get all the roles for a user, then you're going to need to do something like this:

$roleassignments = $DB->get_records('role_assignments', ['userid' => $user->id]);

You can then loop through all the $roleassignments and extract the 'roleid' from them (alternatively, you could use the $DB->get_fieldset command, to extract the roleids directly).

Note also that you should be using context_system::instance() instead of the old get_context_instance(CONTEXT_SYSTEM) (unless you are using a very old and insecure version of Moodle).

For getting the site admins, use get_admins() (or, if you really want to access the config value, use $CFG->siteadmins).

davosmith
  • 6,037
  • 2
  • 14
  • 23
  • I have roleid 1 2 3 4 5 of different contextid So Can you let me know if roleid is 1 = admin , 2 = means teacher, 3 means student for a particular contextid ? – Omprakash Patel Nov 20 '18 at 07:20
  • You will need to look in the table mdl_role to find out what each roleid means. It will vary from site to site, depending on what custom roles are defined. _Usually_ 3 is editing teacher, 4 is non-editing teacher, 5 is student, but you should never rely on that and always check mdl_role. – davosmith Nov 20 '18 at 10:25
0

If you want to get a user role for a course by user id then this script will help you.

$context = context_course::instance($COURSE->id);
$roles = get_user_roles($context, $USER->id, true);
$role = key($roles);
$rolename = $roles[$role]->shortname;
Gufran Hasan
  • 8,910
  • 7
  • 38
  • 51