Test if a user is a teacher in one course

Test if a user is a teacher in one course

by Thomas Debay -
Number of replies: 4

Hello,

I am struggling with a little issue. I want to use an "if" function to test if the current user is enroled as a teacher in at least one of the courses available on my moodle installation.

I am trying with the has_capability function, but which capability and especially which context can I use to make my function work with all the users who are enroled in a course, even if they are not browsing this course page when the function is called?

I have found tons of information, but still nothing that answers my question. I understood that the legacy roles couldn't be used any longer, and I understand the limits of thinking in terms of roles rather than capabilities. However, what I need here is precisely to know if my user has a teacher role.

I would greatly appreciate your help to better understand this problem!

Average of ratings: -
In reply to Thomas Debay

Re: Test if a user is a teacher in one course

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Moodle isn't really designed to work with roles in this way - the idea is that a person can be a teacher in one context, but a student in another context (all on the same site).

But, if you really want to find out if a person has a 'teacher' role anywhere on the site, you can do the following:

$roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
$isteacheranywhere = $DB->record_exists('role_assignments', ['userid' => $USER->id, 'roleid' => $roleid]);

Note, this technically only tells you if they have the 'teacher' role allocated somewhere on the site, not that they are enrolled on a course as well. In reality, this is probably good enough for your purposes.


In reply to Davo Smith

Re: Test if a user is a teacher in one course

by Thomas Debay -

Thank you Davo!

I'm missing a little something ; I get an "Call to a member function get_field() on null" exception when I add your code.

I'm not quite sure what exactly I need to add to make it work...

In reply to Thomas Debay

Re: Test if a user is a teacher in one course

by Darko Miletić -

you probably did not add

global $DB;
In reply to Darko Miletić

Re: Test if a user is a teacher in one course

by Thomas Debay -

Of course! Thank you Darko, and thank you all again for your help!

In case this may be useful to others, I am using this code to modify the behaviour of the navigation drawer. For all students, I want it closed by default, but left in the last known position for teachers. So I modified the /theme/boost/layout/columns2.php like this, and it works like a charm.


global $DB;
$roleid = $DB->get_field('role', 'id', ['shortname' => 'editingteacher']);
$isteacheranywhere = $DB->record_exists('role_assignments', ['userid' => $USER->id, 'roleid' => $roleid]);
if (isloggedin() && $isteacheranywhere) {
    $navdraweropen = (get_user_preferences('drawer-open-nav', 'true') == 'true');
}else {

    $navdraweropen = false;
Average of ratings: Useful (1)