Get all modules for a course in moodle

To create customised menu, we need to be able to run custom queries. Say I want to get all the modules for a course that I am enrolled, a query like this helps.

// say $course->id is the id of the course you want to look at

$sql = "select cm.id, m.name, cm.instance from {modules} m, {course_modules} cm where cm.module=m.id and cm.course = '$course->id' and cm.id in (SELECT cs.sequence FROM {course_sections} cs where cs.course = '$course->id')";
$res = $DB->get_records_sql($sql);

print_r($res);

I’ve created a simple relationship table. Hopefully, it helps.

course_relationships

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.