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

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

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:

http://your_moodle_install/webserver/soap/server.php?wsdl=1&wstoken=your_token

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

// SETUP - NEED TO BE CHANGED
$functionname = 'core_user_create_users';
$user1 = new stdClass();
$user1->username = 'abcd';
$user1->password =  'Password123!';
$user1->firstname = 'ali';
$user1->lastname = 'asdsad';
$user1->email = 'asd@asd.com';
$user1->city = 'apo';
$user1->country = 'addfa';
$user1->auth = 'manual';
$params = array($user1);

// SOAP CALL
$serverurl = 'http://your_moodle_install.com/webservice/soap/server.php?wsdl=1&wstoken=your_token';
///Do the main soap call
$client = new SoapClient($serverurl);
try {
  $resp = $client->__soapCall($functionname, array($params));
}
catch (SoapFault $e) {
  exit($e->faultstring);
}

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