Skip to content

Commit

Permalink
MDL-59986 enrol: database sync as scheduled task
Browse files Browse the repository at this point in the history
  • Loading branch information
danielneis committed Jan 17, 2019
1 parent c092f75 commit d6f7480
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 2 deletions.
63 changes: 63 additions & 0 deletions 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);
}
}
10 changes: 10 additions & 0 deletions enrol/database/cli/sync.php
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
37 changes: 37 additions & 0 deletions 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
)
);
1 change: 1 addition & 0 deletions enrol/database/lang/en/enrol_database.php
Expand Up @@ -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.';
Expand Down
4 changes: 4 additions & 0 deletions 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.
3 changes: 1 addition & 2 deletions enrol/database/version.php
Expand Up @@ -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?

0 comments on commit d6f7480

Please sign in to comment.