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:
<?php
/**
* 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 http://www.gnu.org/copyleft/gpl.html 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;
require_once("../../config.php");
require_once($CFG->dirroot."/blocks/configurable_reports/locallib.php");
$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]);
require_once($CFG->dirroot.'/blocks/configurable_reports/report.class.php');
require_once($CFG->dirroot.'/blocks/configurable_reports/reports/'.$report->type.'/report.class.php');
$reportclassname = 'report_'.$report->type;
$reportclass = new $reportclassname($report);
$reportclass->setForExport(true);
$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))
{
require_once($exportplugin);
export_report($reportclass->finalreport); // this is where the sycn actually happens
}
}
}
<?php
/**
* Definition of my tasks.
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => 'block_configurable_reports\task\sritoni_to_ldap_sync_task',
'blocking' => 0,
'minute' => '0',
'hour' => '*/4',
'day' => '*',
'month' => '*',
'dayofweek' => '*',
)
);