I need to check in my module whether the user is assigned to a particular course. Is there any function which will do it for me?
And another question - where moodle stores informations about correlations between users and courses?
Thanks a lot for help
Best regards
AvantaR
How to check whether the user is assigned to a particular course?
by Krzysztof Szala -
Number of replies: 2
In reply to Krzysztof Szala
Re: How to check whether the user is assigned to a particular course?
by Jay Knight -
I think has_capability('moodle/course:visibility', $coursecontext) will do it in 1.9... 2.0 has an is_enrolled() funtion.
In the db to see if a user is enrolled in a course, you have to do a crazy join between user, role_assignments, context, and course. I use this in one place in an external system:
Select Distinct c.* FROM {$db}.role_assignments r JOIN {$db}.context x ON (r.contextid = x.id) JOIN {$db}.course c ON (x.instanceid = c.id) WHERE r.userid='{$moodle_user_id}' AND x.contextlevel = 50 AND visible=1 AND category!=0
I'm not completely sure that there aren't some things I'm not checking for here, so be careful!
In the db to see if a user is enrolled in a course, you have to do a crazy join between user, role_assignments, context, and course. I use this in one place in an external system:
Select Distinct c.* FROM {$db}.role_assignments r JOIN {$db}.context x ON (r.contextid = x.id) JOIN {$db}.course c ON (x.instanceid = c.id) WHERE r.userid='{$moodle_user_id}' AND x.contextlevel = 50 AND visible=1 AND category!=0
I'm not completely sure that there aren't some things I'm not checking for here, so be careful!
In reply to Jay Knight
Odp: Re: How to check whether the user is assigned to a particular course?
by Krzysztof Szala -
OMG, it really looks crazy ...
Anyway, thanks a lot, i'll try your solution
Best regards
AvantaR
Anyway, thanks a lot, i'll try your solution
Best regards
AvantaR