简体   繁体   English

Moodle如何找出登录用户的角色

[英]Moodle how to find out the role of the logged in user

How to get context/role of logged in user in moodle? 如何在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. 我已经找到了get_context_instance()has_compatibility()函数,但我不知道如何将它们用于此目的。

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";
}
$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 它对我有用

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. 在Moodle 2.x中,您可以使用函数get_user_roles ,这将返回在课程或站点或模块的上下文中分配给特定用户的角色列表。

$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);

In moodle the roles are based on the context. 在moodle中,角色基于上下文。 I think this code snippet will be helpful for you. 我认为这段代码会对您有所帮助。

global $COURSE, $USER; 全球$ 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 请记住,没有默认的学生和教师角色,完全有可能(但不太可能)拥有一个Moodle网站

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 教师的角色ID通常为3,学生的角色ID通常为5,但您可以在站点管理 - >用户 - >权限 - >定义角色中查看表格。

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. 函数user_has_role_assignment似乎测试了他在系统范围内的角色。

包括库'accesslib.php',然后使用'is_siteadmin()'函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM