Pages

Wednesday, July 7, 2010

Getting roles of logged in users in Moodle LMS

global $COURSE, $USER;

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

if (has_capability('moodle/legacy:student', $context, $USER->id, false) ) {
echo "I'm a student";
}
if (has_capability('moodle/legacy:teacher', $context, $USER->id, false) ) {
echo "I'm an Assistant Teacher";
}
if (has_capability('moodle/legacy:editingteacher', $context, $USER->id, false)) {
echo "I'm  a teacher";
}
if (has_capability('moodle/legacy:admin', $context, $USER->id, false)) {
echo "I'm Admin";
}

6 comments:

  1. Nice!, thanks for taking the time to publish this article.

    ReplyDelete
  2. Ok, but it doesn't work on moodle 2.2 because legacy.
    Any solution to do this without legacy?

    ReplyDelete
  3. This doesn't work for Moodle 2.3. Do you know solution?

    ReplyDelete