10

How to get context/role of logged in user in moodle? I am trying to implement a context-aware block. The block would suggest the right quizzes to its users based on their moods.

Role can be a teacher, student, teacher assistant or admin. I have already found the get_context_instance() & has_compatibility() functions, but I don't know how to use them for this purpose.

iankit
  • 8,806
  • 10
  • 50
  • 56
Ehsan
  • 4,334
  • 7
  • 39
  • 59
  • 1
    Roles and contexts are one of the most complicated concepts in Moodle, but the short version is a context generally applies to a single course, a single category, or the whole site. A role is granted within that context, and assigns capabilities to the user within that context. – Phil Lello Feb 01 '15 at 20:28

6 Answers6

10

checking user is an admin or not

$admins = get_admins();
$isadmin = false;
foreach($admins as $admin) {
    if ($USER->id == $admin->id) {
        $isadmin = true;
        break;
    }
}

use the result for functions

if ($isadmin) {
    echo "you are an admin";    
} else { 
    echo "you are not an amidn";
}
Mo.
  • 26,306
  • 36
  • 159
  • 225
9
$context = get_context_instance (CONTEXT_SYSTEM);
$roles = get_user_roles($context, $USER->id, false);
$role = key($roles);
$roleid = $roles[$role]->roleid;

it works to me

Beginner Pogrammer
  • 197
  • 2
  • 3
  • 12
6

In Moodle 2.x you may use 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);
Khyam Shahzad
  • 139
  • 1
  • 5
3

In moodle the roles are based on the context. I think this code snippet will be helpful for you.

global $COURSE, $USER;

$context = get_context_instance(CONTEXT_COURSE,$COURSE->id);

if (has_capability('moodle/legacy:student', $context, $USER->id, false) ) {
echo "Student";
}

if (has_capability('moodle/legacy:editingteacher', $context, $USER->id, false)) {
echo "is Teacher<br/>";
}
if (has_capability('moodle/legacy:admin', $context, $USER->id, false)) {
echo "is ADMIN<br/>";
}

Bear in mind that it is perfectly possible (but unlikely) to have a Moodle site without the default Student and Teacher roles

gnuwings
  • 950
  • 7
  • 8
  • This code is good, but there is a problem. Since I'm not in the view/course suppose I'm in the homepage and I want to find out what's the role. this won't work – Ehsan May 15 '12 at 13:50
1

You can test for which roles a user has in the following manner:

if (user_has_role_assignment($user1->id, $roleid))
    echo "User is a teacher in some course";

The role id of a teacher is usually 3, and the role id of a student is usually 5, but you can test this looking at the table in Site Administration-> Users -> Permissions -> Define Roles

Please note that one user can have various roles. The function user_has_role_assignment seems to test of which roles he has system wide.

CptCook
  • 121
  • 3
0

include the library 'accesslib.php' and then use the 'is_siteadmin()' function