Scheduled Moodle to LDAP sync using Block Configurable Reports and Tasks

There exist methods to provide for synchronizing Moodle to an LDAP directory and even provisioning of accounts. However, as far as I know, there are no methods to synchronize Moodle to LDAP. Why would you want to do this you ask? The answer is in this blog post.

The block called Configurable Reports is very handy to try out SQL scripts to extract data from Moodle and use the export_report function which was intended normally to export report as CSV, XLS, etc., to run PHP code to accomplish any functionality we wish.

In my case I wrote an export_report that synchronizes an LDAP to Moodle. However, this only runs on demand. How to run this periodically? Now Moodle has a Task manager that schedules and runs tasks as explained here. The plugin called configurable reports is currently not setup for tasks. I will show how to implement tasks in this plugin and to do simple hack to setup a SQL report with a custom export plugin as a schedulable task, in order to achieve a scheduled synchronization.

I setup my SQL report like any other as described in the documentation of the standard plugin.

For the export, I build a custom PHP function as explained in this blog post. Ensure that the function returns and does not die or exit when it finishes without errors.

I setup the main task class as shown below:

 * A scheduled task for Moodle to LDAP sync.
namespace block_configurable_reports\task;
 * A scheduled task class for Moodle to LDAP sync.
 * @copyright  2021 Madhu Avasarala
 * @license GNU GPL v3 or later
class sritoni_to_ldap_sync_task extends \core\task\scheduled_task 
     * Get a descriptive name for this task (shown to admins).
     * @return string
    public function get_name() 
        return "Sritoni to LDAP sync";
     * Run users sync.
    public function execute() 
        global $CFG, $DB;
        $id         = 130;     // id of report to be run in the task
        $download   = 1;       // forces the export_report program to be run
        $format     = "sync";  // name of php file containing the export_report desired
        $courseid   = 1;

// get the report

        $report = $DB->get_record('block_configurable_reports', ['id' => $id]);
        $reportclassname = 'report_'.$report->type;
        $reportclass = new $reportclassname($report);
        $reportclass->create_report();  // the report is created
        //core_php_time_limit::raise(); // gave error, couldn't find so commented out
        //raise_memory_limit(MEMORY_EXTRA); // same reason as above, not sure why
        $exportplugin = $CFG->dirroot.'/blocks/configurable_reports/export/'.$format.'/export.php';
        if (file_exists($exportplugin)) 
            export_report($reportclass->finalreport);  // this is where the sycn actually happens
Note that as recommended the task is created as a class in the classes folder so that it can be autoloaded by the plugin.
Finally, in the db folder we create a tasks.php file with the following:
 * Definition of my tasks.
defined('MOODLE_INTERNAL') || die();
$tasks = array(
                    'classname' => 'block_configurable_reports\task\sritoni_to_ldap_sync_task',
                    'blocking'  => 0,
                    'minute'    => '0',
                    'hour'      => '*/4',
                    'day'       => '*',
                    'month'     => '*',
                    'dayofweek' => '*',
The above is the specification of the task. In my case I am running it every 4 hours.
A version bump is needed for the plugin to register the tasks. You can go to moodle/site administration/scheduled tasks and check to see if it is listed. I ran it on demand to make sure it was running right and that’s it!
Obviously this can be as sophisticated as you need. A foreach looping over all specially marked reports can be extracted for running.
Posted in Configurable Reports, moodle, plugin.