Wednesday, April 23, 2014

Calling the external DB enrollement plugin from the command line

I have enabled, per my customers request, the moodle plugin external DB enrollment.  This is the plugin referenced in this moodle doc page and detailed in the moodle Site Admin | Plugins | Enrollments | Manage Enrol plugins settings.


Notice the External database is Enabled

After enabling the External database plugin, click on the Settings link to see this detailed form that needs to specify things like where the synching DB is located, how to connect to it, what fields to match etc.



There is a php page detailed in the moodle doc that handles the actual work of Moodle going to the external DB and pulling enrollment data and updating accordingly.  That script is located on my system at

webroot/instancename/enrol/database/cli/sync.php

Calling the sync.php file from the command line was the ticket for my implementation to work.

I am using WAMP 2.2 - thats a windows server.  I was able to test that the call to the sync.php script was working by creating a windows .bat file and calling the file like this:

Windows bat file calling the sync.php file

ECHO off

REM this bat file is called by cron, preferably or some other scheduled task.  This bat file
REM executes php and passes along a file for execution.
REM Details for the sync are defined in moodle admin plugins Enrolments External Database

REM c:\wamp\bin\php\php5.3.13\php.exe -f c:\wamp\www\student\enrol\database\cli\sync.php -h, --help

c:\wamp\bin\php\php5.3.13\php.exe -f c:\wamp\www\student\enrol\database\cli\sync.php -a

ECHO on

ECHO done synching....

@Pause

The executing of the batch file results in a command window opening and displaying:





That was useful to know the connection to the DB specified in the moodle page was working....but not helpful to see what was actually synching.   After switching on the Debug ADOdb setting


The batch file produced more detail.


Encouraging.....but still not quiet working.....I posted to the general help moodle forum with great detail, and was rewarded with a helpful hint, like ensuring your matching fields are correct.  After a change to one of the fields, another executing of the bat file resulted in:






According to the moodle doc page, the synch script is also called upon login. 

the sync.php script / function is called when the user logs in.  This is the result of turning on the plugin.  This is not normal moodle behavior.  When someone logs in, moodle calls the script and grabs that fresh enrollment data and updates appropriatly.  Nice.  Its a bit of overhead, having to call the script each time someone logs in, put synching does have its cost!  A slight performance hit.

I tested this theory by going to the master enrollment table (the external DB) and adding a couple enrollment records using a test account on the site.  I logged off and back on and there it was, the course link for the student.  Nice.

No comments:

Post a Comment