From d6f7480d02ba99e1943e1ee6fbdb84250b34b3e9 Mon Sep 17 00:00:00 2001 From: Daniel Neis Araujo <danielneis@gmail.com> Date: Tue, 21 Aug 2018 15:17:49 -0300 Subject: [PATCH] MDL-59986 enrol: database sync as scheduled task --- .../database/classes/task/sync_enrolments.php | 63 +++++++++++++++++++ enrol/database/cli/sync.php | 10 +++ enrol/database/db/tasks.php | 37 +++++++++++ enrol/database/lang/en/enrol_database.php | 1 + enrol/database/upgrade.txt | 4 ++ enrol/database/version.php | 3 +- 6 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 enrol/database/classes/task/sync_enrolments.php create mode 100644 enrol/database/db/tasks.php create mode 100644 enrol/database/upgrade.txt diff --git a/enrol/database/classes/task/sync_enrolments.php b/enrol/database/classes/task/sync_enrolments.php new file mode 100644 index 00000000000..444bfc6fa2c --- /dev/null +++ b/enrol/database/classes/task/sync_enrolments.php @@ -0,0 +1,63 @@ +<?php +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. + +/** + * Sync enrolments task + * @package enrol_database + * @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com> + * @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 <danielneis@gmail.com> + * @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 890a0f4ac03..605f7c086cc 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 @@ Sample cron entry: 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 00000000000..bd8e8e2aee8 --- /dev/null +++ b/enrol/database/db/tasks.php @@ -0,0 +1,37 @@ +<?php +// This file is part of Moodle - http://moodle.org/ +// +// Moodle is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// Moodle is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Moodle. If not, see <http://www.gnu.org/licenses/>. + +/** + * Task definition for enrol_database. + * @package enrol_database + * @copyright 2018 Daniel Neis Araujo <danielneis@gmail.com> + * @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 abfc4bd6a59..d6dba449a8d 100644 --- a/enrol/database/lang/en/enrol_database.php +++ b/enrol/database/lang/en/enrol_database.php @@ -70,6 +70,7 @@ $string['settingsheaderdb'] = 'External database connection'; $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 00000000000..8fccd35a67c --- /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 074f480e3ec..dc55e963cdb 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?