3

I am new to Moodle and I have the following code:

$userDetails = $DB->get_record('user', array('id' => $singleuser->userid)); 
    var_dump($userDetails);
    echo $userDetails->firstname." ".$userDetails->lastname

It is having the details of user table in Moodle. How can I find the role like Teacher, Admin, student?

Here I am having the userids $singleuser->userid . I am printing Users full name . I need the role along with it like the following:

echo $userDetails->firstname." ".$userDetails->lastname.$role.

How can I find role? Please guide me.

Moodle Version: 2.9.1

Santhucool
  • 1,656
  • 2
  • 36
  • 92

4 Answers4

2

You need to know where in Moodle you are asking for the role. Moodle does not (usually) have a global concept of a 'student' or 'teacher' - a teacher is a teacher on a specific course, a student is a student on a particular course.

Assuming you know the id of the course where you want to display the role(s) for, you can write the following:

$rolestr = array();
$context = context_course::instance($courseid);
$roles = get_user_roles($context, $singleuser->userid);
foreach ($roles as $role) {
    $rolestr[] = role_get_name($role, $context);
}
$rolestr = implode(', ', $rolestr);
echo "The users roles are {$rolestr} in course {$courseid}";
davosmith
  • 6,037
  • 2
  • 14
  • 23
  • Ty it worked. But in the case of Admin it is not getting role as admin instead it is getting blank. Thanks buddy. The basic requirements meet. The admin issue need to be handled. – Santhucool Aug 10 '15 at 07:00
  • is_siteadmin($userid) will tell you if the user is a site admin (these are handled slightly differently to roles, as being a site admin overrules all capability checks). – davosmith Aug 10 '15 at 08:54
2

moodle get user roles in static pages like about..etc

$context = context_module::instance($cm->id);

$roles = get_user_roles($context, $USER->id, true);

foreach ($roles as $role) { 

}
gp_sflover
  • 3,460
  • 5
  • 38
  • 48
Moby M
  • 910
  • 2
  • 7
  • 26
0

try this,

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

or try this,useing the function get_user_roles and this will return list of roles assigned to a particular user in context of course or site or module

$context = get_context_instance(CONTEXT_COURSE, $courseid, true);
$roles = get_user_roles($context, $USER->id, true);

You can also get the roles in context of module.

$context = get_context_instance(CONTEXT_MODULE, $cm->id, true);
$roles = get_user_roles($context, $USER->id, true);
  • unless this is a very old version of Moodle (2.2 or below), use context_module::instance($cm->id), instead of get_context_instance(CONTEXT_MODULE, $cm->id) – davosmith Aug 06 '15 at 14:00
  • @davosmith buddy can you give a proper answer? I have userid I need to find the role of that user in that particular course!! – Santhucool Aug 07 '15 at 09:03
  • above answers not working . Getting error as get_context_instance() is deprecated, please use context_xxxx::instance() instead. – Santhucool Aug 07 '15 at 09:07
0

I don't have courseid, will it be possible to get the role by adding below code.,

$context = context_system::instance();
$roles = get_user_roles($context, $userid, false);
$role = key($roles);
$roleid = $roles[$role]->roleid;
TRW
  • 876
  • 7
  • 23
Sunavi
  • 1
  • 1
    Are you asking a follow-up question, or is this supposed to be an answer to the original question? Please edit this answer to make that clearer. – cigien Jan 05 '22 at 13:45
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 05 '22 at 19:07