Thursday 8 January 2015

Moodle: Number of courses in which a user is enrolled

Hello,

Moodle is a software designed to create online courses. You can add a lot of tailor-made plugins.

One of the most interesting info that would be interesting could be... The number of courses in which a user is enrolled. We can know it by (tested with version 2.6.2):

global $DB;
$sql_count = 'SELECT COUNT(c.id)
FROM mdl_course AS c
JOIN mdl_context AS ct ON c.id = ct.instanceid
JOIN mdl_role_assignments AS ra ON ra.contextid = ct.id
JOIN mdl_user AS u ON u.id = ra.userid
JOIN mdl_role AS r ON r.id = ra.roleid
WHERE u.id = ?';
$courses_count = $DB->count_records_sql($sql_count, array($idUser));


Regards!

No comments:

Post a Comment