SQL query to find currently enrolled students

SQL query to find currently enrolled students

de John Houser -
Número de respuestas: 4
I've been writing some scripts that help to integrate our Moodle system with a separate, external workshop scheduling database which drives our website. One script requires that I extract from the Moodle database a list of currently enrolled students for a course, given a course ID number. The current query looks like this:

SELECT u.username, u.firstname, u.lastname, u.email, c.fullname workshop
FROM mdl_user u, mdl_role_assignments r, mdl_course c, mdl_context t
WHERE t.instanceid = c.id
AND r.contextid = t.id
AND r.userid = u.id
AND r.roleid = 5
AND u.deleted != 1
AND c.idnumber = $course_idnumber

The query works except that it returns both currently enrolled students and previously enrolled students. I've noted that mdl_context includes a column called contextlevel and that that column has one number for all the current students and another number for old students. But I don't know how to predict what the correct contextlevel should be for a given course. Can anyone explain how the contextlevel works?

Any help would be greatly appreciated!
Promedio de valoraciones: -
En respuesta a John Houser

Re: SQL query to find currently enrolled students

de Tim Hunt -
Imagen de Core developers Imagen de Documentation writers Imagen de Particularly helpful Moodlers Imagen de Peer reviewers Imagen de Plugin developers
Context level actually tells you what type of context you have. So if contextlevel = 50, then you have a course, and instanceid is a course id. If contextlevel = 10 then this is the site context, and there should only be one. context_level = CONTEXT_MODULE (these contstants are defined at the top of lib/accesslib.php) then instanceid points to the course_modules table.
En respuesta a John Houser

Re: SQL query to find currently enrolled students

de yuki yuki -
Your query is giving results who are locally assigned (suppose course->assign roles)...
but when a user assigned globally meaning assign roles in system (Users->Permission->Assign system roles) then how to get those users by query?. Can you please give me the hints/or that query?