Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moodle how to find out the role of the logged in user

Tags:

php

moodle

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.

like image 314
Ehsan Avatar asked May 14 '12 09:05

Ehsan


People also ask

How do I find my current user ID in Moodle?

The global variable $USER will work everywhere in Moodle. If within a function or a class method and not in the global scope you need to put : global $USER; at the beginning of the function.


1 Answers

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";
}
like image 181
Mo. Avatar answered Sep 24 '22 00:09

Mo.