MDL-50890 enrol_flatfile: Replace cron processing with scheduled task

Replace enrolment plugin flatfile cron processing with scheduled task
This commit is contained in:
Troy Williams 2015-07-21 14:21:10 +12:00 committed by David Monllao
parent f495510548
commit a771854c46
7 changed files with 148 additions and 7 deletions

View File

@ -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/>.
/**
* Scheduled task for processing flatfile enrolments.
*
* @package enrol_flatfile
* @copyright 2014 Troy Williams
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_flatfile\task;
defined('MOODLE_INTERNAL') || die;
/**
* Simple task to run sync enrolments.
*/
class flatfile_sync_task extends \core\task\scheduled_task {
/**
* Get a descriptive name for this task (shown to admins).
*
* @return string
*/
public function get_name() {
return get_string('flatfilesync', 'enrol_flatfile');
}
/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
global $CFG;
require_once($CFG->dirroot . '/enrol/flatfile/lib.php');
if (!enrol_is_enabled('flatfile')) {
return 2;
}
// Instance of enrol_flatfile_plugin.
$plugin = enrol_get_plugin('flatfile');
$result = $plugin->sync(new \null_progress_trace());
return $result;
}
}

View File

@ -22,6 +22,21 @@
* - you need to change the "www-data" to match the apache user account
* - use "su" if "sudo" not available
*
* Update
*
* This plugin now has a enrolment sync scheduled task. Scheduled tasks were
* introduced in Moodle 2.7. It is possible to override the scheduled tasks
* configuration and run a single scheduled task immediately using the
* admin/tool/task/cli/schedule_task.php script. This is the recommended
* method to use for immediate enrollment synchronisation.
*
* Usage help:
* $ php admin/tool/task/cli/schedule_task.php -h
*
* Execute task:
* $ sudo -u www-data /usr/bin/php admin/tool/task/cli/scheduled_task.php /
* --execute=\\enrol_flatfile\\task\\flatfile_sync_task
*
* @package enrol_flatfile
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

View File

@ -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/>.
/**
* Definition of flatfile enrolment scheduled tasks.
*
* @package enrol_flatfile
* @copyright 1999 onwards Martin Dougiamas {@link http://moodle.com}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => '\enrol_flatfile\task\flatfile_sync_task',
'blocking' => 0,
'minute' => '*',
'hour' => '*',
'day' => '*',
'dayofweek' => '*',
'month' => '*'
)
);

View File

@ -29,6 +29,7 @@ $string['filelockedmail'] = 'The text file you are using for file-based enrolmen
$string['filelockedmailsubject'] = 'Important error: Enrolment file';
$string['flatfile:manage'] = 'Manage user enrolments manually';
$string['flatfile:unenrol'] = 'Unenrol users from the course manually';
$string['flatfilesync'] = 'Flat file enrolment sync';
$string['location'] = 'File location';
$string['location_desc'] = 'Specify full path to the enrolment file. The file is automatically deleted after processing.';
$string['notifyadmin'] = 'Notify administrator';

View File

@ -161,11 +161,6 @@ class enrol_flatfile_plugin extends enrol_plugin {
}
}
public function cron() {
$trace = new text_progress_trace();
$this->sync($trace);
}
/**
* Execute synchronisation.
* @param progress_trace

View File

@ -467,4 +467,35 @@ class enrol_flatfile_testcase extends advanced_testcase {
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
$this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
}
/**
* Flatfile enrolment sync task test.
*/
public function test_flatfile_sync_task() {
global $CFG, $DB;
$this->resetAfterTest();
$flatfileplugin = enrol_get_plugin('flatfile');
$trace = new null_progress_trace();
$this->enable_plugin();
$file = "$CFG->dataroot/enrol.txt";
$flatfileplugin->set_config('location', $file);
$studentrole = $DB->get_record('role', array('shortname' => 'student'));
$this->assertNotEmpty($studentrole);
$user1 = $this->getDataGenerator()->create_user(array('idnumber' => 'u1'));
$course1 = $this->getDataGenerator()->create_course(array('idnumber' => 'c1'));
$context1 = context_course::instance($course1->id);
$data =
"add,student,u1,c1";
file_put_contents($file, $data);
$task = new enrol_flatfile\task\flatfile_sync_task;
$task->execute();
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid' => $studentrole->id)));
}
}

View File

@ -25,7 +25,6 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2015051100; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2015051101; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2015050500; // Requires this Moodle version
$plugin->component = 'enrol_flatfile'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 60;