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.itemname from {course_modules} cm, {course_modules_availability} cma, {grade_items} gi, {quiz_attempts} qa where = cma.coursemoduleid and cma.gradeitemid = and gi.iteminstance = qa.quiz and'$m->id' and qa.userid='$USER->id' and qa.state='finished'";

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

This db diagram might help:


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.


Creating a Moodle User via Web Services

We can do lots of stuff in moodle via api but I found the technical doc confusing and lacking…

Here, we’re going to allow external system to talk to moodle via soap, in fact to create a user in moodle. Here are the steps.

1. In moodle, go to site admin -> advance feature -> enable web services.

2. site admin -> web services -> external services -> add (add a new service called from_wordpress and checked “enabled” and “authorised users only”. After clicking on submit, you will be asked to add functions. Lets add “core_user_create_users” function for example.

3. Under site admin -> web services -> external services again, click on “authorized users” and add your admin user.

4. under site admin -> web services -> manage protocol, turn on the soap protocol and turn off the rest of the protocol for the sake of security.

5. under site admin -> web services -> manage token -> add, select the admin user and choose the from_wordpress service. You will now get a token that you can use in other apps.

6. now to connect to moodle via soap, your url should be something like this:


and using native php soap library, your code could be along the lines of:

$functionname = 'core_user_create_users';
$user1 = new stdClass();
$user1->username = 'abcd';
$user1->password =  'Password123!';
$user1->firstname = 'ali';
$user1->lastname = 'asdsad';
$user1->email = '';
$user1->city = 'apo';
$user1->country = 'addfa';
$user1->auth = 'manual';
$params = array($user1);

$serverurl = '';
///Do the main soap call
$client = new SoapClient($serverurl);
try {
  $resp = $client->__soapCall($functionname, array($params));
catch (SoapFault $e) {

* the moodle comes with minimal documentation at http://your_moodle_site/admin/webservice/documentation.php