Re: Databases: Query to find teacher of a particular course in db
I don't think there is a direct relation between a course and I teacher. I think the relation is more like:
course | context | role_assignments | user | |||
id | ---------> |
instanceid | userid | ----------> |
id | |
id |
-----------> |
contextid | ||||
contextlevel = ’50’ | roleid = ‘3’ |
So you could try some SQL like:
SELECT c.id, c.shortname, u.id, u.username, u.firstname || ' ' || u.lastname AS name FROM mdl_course c LEFT OUTER JOIN mdl_context cx ON c.id = cx.instanceid LEFT OUTER JOIN mdl_role_assignments ra ON cx.id = ra.contextid AND ra.roleid = '3' LEFT OUTER JOIN mdl_user u ON ra.userid = u.id WHERE cx.contextlevel = '50';
You may want to change u.firstname || ' ' || u.lastname AS name for CONCAT(u.firstname, ' ', u.lastname) AS name if you are using MySQL.
I hope that helped
Regards,
GV
Re: Databases: Query to find teacher of a particular course in db
תשובה ל: Re: Query to find teacher of a particular course in db
Hello thanks for this query.
I tried to understand what is exactly context level =50
what does it mean exactly? and what is the different between context level =50 to 40 ,30 and so on?
thanks..
תשובה ל: Re: Query to find teacher of a particular course in db
Hi again
Can someone answer me what does "context level =40,30,20, 10"
means? except from 50 that I understand it means the course context. correct me if I'm wrong.
so what are the other numbers referring to or mean?
thanks
Re: תשובה ל: Re: Query to find teacher of a particular course in db
// context definitions
define('CONTEXT_SYSTEM', 10);
define('CONTEXT_PERSONAL', 20);
define('CONTEXT_USER', 30);
define('CONTEXT_COURSECAT', 40);
define('CONTEXT_COURSE', 50);
define('CONTEXT_GROUP', 60);
define('CONTEXT_MODULE', 70);
define('CONTEXT_BLOCK', 80);
Peace - Anthony
תשובה ל: Re: תשובה ל: Re: Query to find teacher of a particular course in db
Ok
thanks a lot
Re: תשובה ל: Re: תשובה ל: Re: Query to find teacher of a particular course in db
but here roleid is use as 3. is its always so.... what if it get changed???
Re: תשובה ל: Re: תשובה ל: Re: Query to find teacher of a particular course in db
DinKar, try something like this:
SELECT c.id, c.fullname,u.lastname,r.name
FROM mdl_course c
JOIN mdl_context ct ON c.id = ct.instanceid
JOIN mdl_role_assignments ra ON ra.contextid = ct.id
JOIN mdl_user u ON u.id = ra.userid
JOIN mdl_role r ON r.id = ra.roleid
WHERE name = 'Teacher' AND id = 5
You can change this query and add new columns from the joined tables. That is the key. You can use any role name in the WHERE clause. You could use roleid, rather than a string like 'Teacher'. The roleid is typically 3 for a teacher. Change the id (courseid) to a real value, remove the AND clause for more results.
Re: תשובה ל: Re: Query to find teacher of a particular course in db
this is useful...thanks
Re: תשובה ל: Re: Query to find teacher of a particular course in db
Thanks James this is what i was looking for
Re: תשובה ל: Re: Query to find teacher of a particular course in db
Hi Miki,
Please use below link to see the diffrence.
https://docs.moodle.org/dev/Roles_and_modules
Thanks,
Swanand
Re: Databases: Query to find teacher of a particular course in db
Nice post , this is really helpful.
Re: Databases: Query to find teacher of a particular course in db
In your question, you need a query to get teacher. There is a way help you get contact of course of a specific course and print as html code without using query as the following:
$course = $DB->get_record('course', array('id' => $courseid), '*', MUST_EXIST);
require_once($CFG->libdir. '/coursecatlib.php');
$tmpCourse = new course_in_list($course);
var_dump($tmpCourse);
//Add teacher name
// display course contacts. See course_in_list::get_course_contacts()
if ($tmpCourse->has_course_contacts()) {
$html .= html_writer::start_tag('ul', array('class' => 'teachers'));
foreach ($tmpCourse->get_course_contacts() as $userid => $coursecontact) {
$name = $coursecontact['rolename'].': '.
html_writer::link(new moodle_url('/user/view.php',
array('id' => $userid, 'course' => SITEID)),
$coursecontact['username']);
$html .= html_writer::tag('li', $name);
}
$html .= html_writer::end_tag('ul'); // .teachers
}
Re: Databases: Query to find teacher of a particular course in db
Thank you for sharing, it works for me to export teacher name in excel.