diff --git a/enrol/database/classes/task/sync_enrolments.php b/enrol/database/classes/task/sync_enrolments.php new file mode 100644 index 0000000000000..444bfc6fa2c5b --- /dev/null +++ b/enrol/database/classes/task/sync_enrolments.php @@ -0,0 +1,63 @@ +. + +/** + * Sync enrolments task + * @package enrol_database + * @copyright 2018 Daniel Neis Araujo + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace enrol_database\task; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Class sync_enrolments + * @package enrol_database + * @copyright 2018 Daniel Neis Araujo + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class sync_enrolments extends \core\task\scheduled_task { + + /** + * Name for this task. + * + * @return string + */ + public function get_name() { + return get_string('syncenrolmentstask', 'enrol_database'); + } + + /** + * Run task for synchronising users. + */ + public function execute() { + + $trace = new \text_progress_trace(); + + if (!enrol_is_enabled('database')) { + $trace->output('Plugin not enabled'); + return; + } + + $enrol = enrol_get_plugin('database'); + + // Update enrolments -- these handlers should autocreate courses if required. + $enrol->sync_courses($trace); + $enrol->sync_enrolments($trace); + } +} diff --git a/enrol/database/cli/sync.php b/enrol/database/cli/sync.php index 890a0f4ac03b4..605f7c086cc04 100644 --- a/enrol/database/cli/sync.php +++ b/enrol/database/cli/sync.php @@ -26,6 +26,8 @@ * - you need to change the "www-data" to match the apache user account * - use "su" if "sudo" not available * + * @deprecated since Moodle 3.7 MDL-59986 - please do not use this CLI script any more, use scheduled task instead. + * @todo MDL-63266 This will be deleted in Moodle 4.1. * @package enrol_database * @copyright 2010 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later @@ -65,6 +67,14 @@ die; } +cli_problem('[ENROL DATABASE] The sync enrolments cron script has been deprecated. Please use the scheduled task instead.'); + +// Abort execution of the CLI script if the enrol_database\task\sync_enrolments is enabled. +$task = \core\task\manager::get_scheduled_task('enrol_database\task\sync_enrolments'); +if (!$task->get_disabled()) { + cli_error('[ENROL DATABASE] The scheduled task sync_enrolments is enabled, the cron execution has been aborted.'); +} + if (!enrol_is_enabled('database')) { cli_error('enrol_database plugin is disabled, synchronisation stopped', 2); } diff --git a/enrol/database/db/tasks.php b/enrol/database/db/tasks.php new file mode 100644 index 0000000000000..bd8e8e2aee89c --- /dev/null +++ b/enrol/database/db/tasks.php @@ -0,0 +1,37 @@ +. + +/** + * Task definition for enrol_database. + * @package enrol_database + * @copyright 2018 Daniel Neis Araujo + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$tasks = array( + array( + 'classname' => '\enrol_database\task\sync_enrolments', + 'blocking' => 0, + 'minute' => 'R', + 'hour' => 'R', + 'day' => '*', + 'month' => '*', + 'dayofweek' => '*', + 'disabled' => 1 + ) +); diff --git a/enrol/database/lang/en/enrol_database.php b/enrol/database/lang/en/enrol_database.php index abfc4bd6a5989..d6dba449a8d72 100644 --- a/enrol/database/lang/en/enrol_database.php +++ b/enrol/database/lang/en/enrol_database.php @@ -70,6 +70,7 @@ $string['settingsheaderlocal'] = 'Local field mapping'; $string['settingsheaderremote'] = 'Remote enrolment sync'; $string['settingsheadernewcourses'] = 'Creation of new courses'; +$string['syncenrolmentstask'] = 'Synchronise external database enrolments task'; $string['remoteuserfield_desc'] = 'The name of the field in the remote table that we are using to match entries in the user table.'; $string['templatecourse'] = 'New course template'; $string['templatecourse_desc'] = 'Optional: auto-created courses can copy their settings from a template course. Type here the shortname of the template course.'; diff --git a/enrol/database/upgrade.txt b/enrol/database/upgrade.txt new file mode 100644 index 0000000000000..8fccd35a67c4b --- /dev/null +++ b/enrol/database/upgrade.txt @@ -0,0 +1,4 @@ +This files describes API changes in the enrol_database code. + +=== 3.7 === +* enrol/database/cli/sync.php script has been deprecated in favour of enrol_database\task\sync_enrolments task. diff --git a/enrol/database/version.php b/enrol/database/version.php index 074f480e3ecb9..dc55e963cdb8d 100644 --- a/enrol/database/version.php +++ b/enrol/database/version.php @@ -24,7 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2018120300; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2018122000; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2018112800; // Requires this Moodle version $plugin->component = 'enrol_database'; // Full name of the plugin (used for diagnostics) -//TODO: should we add cron sync?