Executing the external DB enrollment plugin via the command line

At a customer’s request, I enabled the Moodle plugin “External DB enrollment” as documented on the Moodle doc page. This plugin is configured under Site Administration > Plugins > Enrollments > Manage enrol plugins.

 

The “External database” plugin is enabled as shown in the image.

Clicking on the “Settings” link reveals a detailed form where you specify the synchronization database location, connection parameters, and field mappings.

A PHP page, sync.php, located at webroot/instancename/enrol/database/cli/sync.php on my system, manages the synchronization process. Moodle uses this script to retrieve enrollment data from the external database and update accordingly.

Executing sync.php from the command line was crucial for my implementation. Since I’m using WAMP 2.2 on a Windows server, I created a Windows batch file for this purpose:

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

Running this batch file opens a command window with the following output:

This confirmed the database connection was working, but it didn’t show what was being synchronized. To get more details, I enabled the “Debug ADOdb” setting:

The batch file then produced a more detailed output:

This was encouraging but still not fully functional. After posting on the general help Moodle forum with details, I received a helpful suggestion: ensure the field mappings were accurate. After correcting one of the fields, running the batch file again yielded:

The Moodle documentation states the synchronization script is also executed upon user login. This behavior, triggered by the plugin, deviates from typical Moodle functionality. Upon login, Moodle calls the script to fetch updated enrollment data, ensuring accuracy. While convenient, this method incurs a slight performance cost due to the synchronization overhead on each login.

To verify this, I added test enrollment records directly to the master enrollment table (the external database). After logging out and back in, the course link for the test student appeared, confirming the synchronization process was working as intended.

Licensed under CC BY-NC-SA 4.0
Last updated on Oct 13, 2023 07:38 +0100