How can I get the list of teachers of a course?

How can I get the list of teachers of a course?

by Daniele Cordella -
Number of replies: 17
Picture of Core developers Picture of Plugin developers
How can I get the list of teachers of a course?

I tried
$teachers = get_users_by_capability($context, 'moodle/course:update');
but, of course, I got admins too.

How can I narrow the set?
Thanks in advance.
Average of ratings: -
In reply to Daniele Cordella

Re: How can I get the list of teachers of a course?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
This is what I use today
but I think it is ugly at your eyes.

$admins = get_users_by_capability($context, 'moodle/course:doanything');
$admins_id = array();
foreach ($admins as $id => $admin) {
$admins_id[] = $admin->id;
}

$heads = get_users_by_capability($context, 'moodle/course:update');
$heads_id = array();
foreach ($heads as $id => $head) {
$heads_id[] = $head->id;
}

$teachers_id = array_diff($heads_id, $admins_id);

Any better way?
Thanks.
In reply to Daniele Cordella

Re: How can I get the list of teachers of a course?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
I believe I understood.
One more time I found the solution having a walk going to pick up children at school instead of pushing keyboard buttons.
The problem is that I have to define what I mean with the word "Teacher".
Teacher is the person target of a specific capability. And I am happy if an admin has this specific capability or if a guy belonging to the "teacher" legacy has not.
What I need is the list of people with the capability xxx. So, the question: "How can I select teachers?" is meaningless, because it should be "Who owns the capability xxx?"
So, in my specific case, I just need to define the new capability, to assign it to the legacy "Teacher", for instance, and then getting all the people target of it.
Fine. I believe this is the final solution. Do you agree?
In reply to Daniele Cordella

Re: How can I get the list of teachers of a course?

by Alejandro Michavila Pallarés -
Hi,

I want to get the list of teachers of a course, and I'm using this code:

$role = $DB->get_record('role', array('shortname' => 'teacher'));
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$teachers = get_role_users($role->id, $context);

But get_role_users() function doesn't return anything.

Am I doing something wrong?, I don't know which is the problem. I'm using Moodle 2.0.

Any ideas?, thanks.
In reply to Alejandro Michavila Pallarés

Re: How can I get the list of teachers of a course?

by Iñaki Arenaza -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Alejandro, the shortname for the teacher role is 'editingteacher' (don't ask me why smile). Maybe this could be your problem.

Saludos.
Iñaki.
Average of ratings: Useful (1)
In reply to Iñaki Arenaza

Re: How can I get the list of teachers of a course?

by Alejandro Michavila Pallarés -

This is indeed my problem Iñaki, thanks.

In reply to Alejandro Michavila Pallarés

Re: How can I get the list of teachers of a course?

by A Guy -

You can get a list of courses for each teacher this way. Just modify it to get the teachers for the courses.

 

Find the roleid for a teacher in your moodle app by looking at the mdl_role_assignments table where 5 is the roleid for a teacher (you have to check your own database table) and $user_id is the teacher's id. Then use that to plug into the following query.

$sql = "select contextid from mdl_role_assignments where roleid=5 and userid=$user_id";
$res = mysql_query($sql);
$arr = mysql_fetch_array($res);
$contextid = $arr[contextid];

 

After you found the contextid you have to query the mdl_context with the id and a level. A level for a course is 50. There is documentation on that.

$sql = "select instanceid from mdl_context where id=".$contextid." and contextlevel=50";
$res = mysql_query($sql);
while($arr = mysql_fetch_array($res)){
$instanceid = $arr[instanceid];

 

Then query the mdl_courses table with the instanceid you above just above to get the courses.

$sql = "select id,shortname,fullname from mdl_course where id=$instanceid";

 

Spin out the results.

Average of ratings: Useful (1)
In reply to A Guy

Re: How can I get the list of teachers of a course?

by Alejandro Michavila Pallarés -

Hi Tommy, thanks for your help!.

I prefer to use the API method:

$role = $DB->get_record('role', array('shortname' => 'editingteacher'));
$context = get_context_instance(CONTEXT_COURSE, $courseid);
$teachers = get_role_users($role->id, $context);

But getting the "editingteacher" role instead of the "teacher" role as you can see above.

Average of ratings: Useful (1)
In reply to Alejandro Michavila Pallarés

Re: How can I get the list of teachers of a course?

by A Guy -

Im glad you found something that works for you, Alejandro. Using the API functions are definitely easier than other options. I thought you were saying that the functions didn't work for your needs, hence your request on how to do this (I now see you needed help on using them). I have experienced this several times through the customization work we are doing on Moodle. And this requires learning how the API functions work with the tables, which is how we learned what I sent to you. Then you can write your own ext/subclasses/override the functions to accomplish whatever you need to.  But it all depends upon how heavily you are customizing your Moodle install . . . which route you go. Anyway glad you found what you were looking for

Average of ratings: Useful (1)
In reply to Alejandro Michavila Pallarés

Re: How can I get the list of teachers of a course?

by Mike Shurance -

How do you display the teacher's name?

$teachers->lastname   ??

 

 

In reply to Mike Shurance

Re: How can I get the list of teachers of a course?

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

foreach ($teachers as $teacher) {
echo fullname($teacher);
}

In reply to Daniele Cordella

Re: How can I get the list of teachers of a course?

by Paul Holden -
Picture of Core developers Picture of Moodle HQ Picture of Moodle Workplace team Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers Picture of Testers
You could use get_role_users if you know which role you want to list.
In reply to Paul Holden

Re: How can I get the list of teachers of a course?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
You are right Paul... but I am afraid I don't have to count on the existence of the "Teacher" role. What is if somewhere they remove (and this is allowed by Moodle) the "Teacher" role?

The problem seems to become deeper. A good question that I have to ask myself is: "What do you mean with the word 'Teacher'?"

Thank for your suggestion.
In reply to Paul Holden

Re: How can I get the list of teachers of a course?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Thanks Paul. Your suggestion is a good starting point. Thanks.
In reply to Daniele Cordella

Re: How can I get the list of teachers of a course?

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
This is an astonishingly difficult problem. There is no good solution in Moodle 1.x - there is a possibility that there might be one in Moodle 2.

Note that the get_users_by_capability function has a $doanything parameter, I think if you pass this as false, it should exclude admins for you. (You should make sure admins don't have any capabilities other than doanything, in order for this logic to work - that should be the case if they are correctly configured.)

My current preferred solution would be to do the get_users_by_capability then filter the list with a separate query to check whether those users have roles actually on the course context (as opposed to the category or site contexts). This excludes not just admins but also any other super-users. (E.g. we give some people ability to edit all sites within a category, but they aren't 'teachers', neither are they admins.) Note that this may cause performance problems if you have a very large number of teachers or admins (more than a thousand or so on a course) - these problems can be avoided with careful coding, but even we don't have that many tutors on a course, so I doubt other sites do either...

This is not 'correct' behaviour because it might be possible to have somebody assigned at category or site level who really is the teacher for a bunch of courses (that's the difference between your solution that just excludes admins, and mine that excludes anyone without a role on the actual course). I don't believe either solution is right or wrong. Mine works better for our installation/setup of Moodle. smile But yours might work better in other use cases.

Finally, some parts of the code include lists of roles that should or shouldn't be included in particular displays (as an alternative to the capability approach). I mentioned above that you shouldn't use a single role, but if you provide config option with a list of allowed roles, that can work and is perhaps the most reliable approach in the current situation, however it does involve adding extra configuration for your feature (whatever it is). See the 'gradebookroles' config option for an example of this.


As I mentioned above, there has been discussion about a possible solution for Moodle 2 (where there's a bit of a proliferation of gradebookroles-style options, one of which is mine): some way to indicate that people are actually 'participants' in a course rather than just having capabilities to do things in it. As far as I know this isn't yet resolved or implemented, it was just mentioned at the last developer meeting.


(The situation is even worse for students where course:view is the right capability - then you might have lots of people with roles at site level that let them view all courses - for example we have helpdesk staff in that position.)


Finally - I got a bit theoretical in this mail - but for an actual practical answer to your question, have you tried looking at the code of places where Moodle does display the list of teachers, to see how it does it? Might be best to copy that.

--sam
Average of ratings: Useful (1)
In reply to sam marshall

Re: How can I get the list of teachers of a course?

by Daniele Cordella -
Picture of Core developers Picture of Plugin developers
Fantastic Sam, thanks. You are always opening my eyes.
I didn't supect I was opening a so hot problem! sorpreso

Yes I tried to read at Moodle core source code.
I went to read moodle19/admin/roles/assign.php, for instance, but I found that there core developers are working foreach(exising user role). I can't be sure that the "Teacher" role exists into each Moodle installation, if I want to create a generally working tool that I can share with other moodlers.

Thanks for your long explanation. I go to test your note of the second paragraph (Note that the get_users_by_capability function has a $doanything parameter, I think if you pass this as false, it should exclude admins for you.)
Thanks again.
In reply to Daniele Cordella

Re: How can I get the list of teachers of a course?

by Juan Leyva -
Picture of Core developers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers

There is a CFG option called coursemanager ($CFG->coursemanager) that is an array of roles.

yourmoodle.com/admin/settings.php?section=coursemanager

This is an optional preference for showing the managers of a course in the frontpage list or course description.

If at least one role is checked it's a good way to locate the "teachers" of the courses.
Take care with this preference because in a different moodle site it can be empty

Regards


Average of ratings: Useful (1)