Get prerequisite modules for a course in moodle

In moodle, we can setup dependency or prequisite for a module. Say if user doesn’t complete a quiz, he cannot take the next lesson. There can be a lot of criteria how we want to set this up. To find out if the user has completed certain quiz for example, we can run an sql as follows:

// $m->id is the module id, $USER->id is the user id.
$sql = "select gi.id, gi.itemname from {course_modules} cm, {course_modules_availability} cma, {grade_items} gi, {quiz_attempts} qa where cm.id = cma.coursemoduleid and cma.gradeitemid = gi.id and gi.iteminstance = qa.quiz and cm.id='$m->id' and qa.userid='$USER->id' and qa.state='finished'";

$dep = $DB->get_record_sql($sql);
print_r($dep);

This db diagram might help:

module_prequisite

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.