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.instance from {modules} m, {course_modules} cm where and cm.course = '$course->id' and in (SELECT cs.sequence FROM {course_sections} cs where cs.course = '$course->id')";
$res = $DB->get_records_sql($sql);


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


Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of 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.