Getting role based on only User Id

Getting role based on only User Id

by stephen dsouza -
Number of replies: 2

Currently in my system we have 4 roles : course admin, trainer, student, corporate user.


Once login we need to redirect to custom Dashboard where we will display custom functionalities.


So I'm wondering how we can implement that considering we have only user id.


I got option based on course

$coursecontext = get_context_instance(CONTEXT_COURSE, $course->id);

if (!has_capability('moodle/course:viewhiddensections', $coursecontext)) {

echo "is Student<br/>";
}


But in my case we have only user id. How we can implement this? I hope Moodle is not so complicated to do such small check.


Average of ratings: -
In reply to stephen dsouza

Re: Getting role based on only User Id

by Darko Miletić -
This is the code you should use:

$coursecontext = context_course::instance($course->id);
if (!has_capability('moodle/course:viewhiddensections', $coursecontext, $userid)) {
echo "is Student<br/>";
}

Average of ratings: Useful (1)
In reply to Darko Miletić

Re: Getting role based on only User Id

by Virgil Ashruf -

Additionally you can find information regarding the Access API on this Moodle Docs page: https://docs.moodle.org/dev/Access_API#has_capability.28.29

If you omit the user it will always check the current logged in $USER. If you add it, it will check the specific user.

You will find a lot of information in the Dev Docs regarding the core API's: https://docs.moodle.org/dev/Core_APIs