diff --git a/enrol/flatfile/classes/task/flatfile_sync_task.php b/enrol/flatfile/classes/task/flatfile_sync_task.php new file mode 100644 index 00000000000..a34da882a21 --- /dev/null +++ b/enrol/flatfile/classes/task/flatfile_sync_task.php @@ -0,0 +1,66 @@ +. + +/** + * 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. + * + * @copyright 2014 Troy Williams + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +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; + } + + // Instance of enrol_flatfile_plugin. + $plugin = enrol_get_plugin('flatfile'); + $result = $plugin->sync(new \null_progress_trace()); + return $result; + + } + +} diff --git a/enrol/flatfile/cli/sync.php b/enrol/flatfile/cli/sync.php index 59df7e0dec9..2b248c10c3d 100644 --- a/enrol/flatfile/cli/sync.php +++ b/enrol/flatfile/cli/sync.php @@ -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/schedule_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 diff --git a/enrol/flatfile/db/tasks.php b/enrol/flatfile/db/tasks.php new file mode 100644 index 00000000000..a78fd91bb56 --- /dev/null +++ b/enrol/flatfile/db/tasks.php @@ -0,0 +1,37 @@ +. + +/** + * 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' => '15', + 'hour' => '*', + 'day' => '*', + 'dayofweek' => '*', + 'month' => '*' + ) +); diff --git a/enrol/flatfile/lang/en/enrol_flatfile.php b/enrol/flatfile/lang/en/enrol_flatfile.php index 7fd19012863..a1c99227d02 100644 --- a/enrol/flatfile/lang/en/enrol_flatfile.php +++ b/enrol/flatfile/lang/en/enrol_flatfile.php @@ -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'; diff --git a/enrol/flatfile/lib.php b/enrol/flatfile/lib.php index 41e0577a1a5..24b3cee5945 100644 --- a/enrol/flatfile/lib.php +++ b/enrol/flatfile/lib.php @@ -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 diff --git a/enrol/flatfile/tests/flatfile_test.php b/enrol/flatfile/tests/flatfile_test.php index 93ed0ef7845..0ab21c9fa11 100644 --- a/enrol/flatfile/tests/flatfile_test.php +++ b/enrol/flatfile/tests/flatfile_test.php @@ -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))); + } } diff --git a/enrol/flatfile/version.php b/enrol/flatfile/version.php index efc367cde45..e7e8b864fef 100644 --- a/enrol/flatfile/version.php +++ b/enrol/flatfile/version.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2015051100; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2015090700; // The current plugin version (Date: YYYYMMDDRR) $plugin->requires = 2015050500; // Requires this Moodle version $plugin->component = 'enrol_flatfile'; // Full name of the plugin (used for diagnostics) -$plugin->cron = 60;