MDL-45932 enrol_imsenterprise: Implement Scheduled Task for IMS Enrolment plugin.

This commit is contained in:
David Ligne 2014-08-05 13:41:09 -04:00
parent d29fb4ac65
commit 302b8abdd9
5 changed files with 117 additions and 2 deletions

View File

@ -0,0 +1,54 @@
<?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/>.
/**
* A scheduled task.
*
* @package enrol_imsenterprise
* @copyright 2014 Universite de Montreal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace enrol_imsenterprise\task;
/**
* Simple task to run the IMS Enterprise enrolment cron.
*
* @copyright 2014 Universite de Montreal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class cron_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('imsenterprisecrontask', 'enrol_imsenterprise');
}
/**
* Do the job.
* Throw exceptions on errors (the job will be retried).
*/
public function execute() {
global $CFG;
require_once($CFG->dirroot . '/enrol/imsenterprise/lib.php');
$ims = new \enrol_imsenterprise_plugin();
$ims->cron();
}
}

View File

@ -0,0 +1,38 @@
<?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 IMS Enterprise enrolment scheduled tasks.
*
* @package enrol_imsenterprise
* @category task
* @copyright 2014 Universite de Montreal
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$tasks = array(
array(
'classname' => 'enrol_imsenterprise\task\cron_task',
'blocking' => 0,
'minute' => '10',
'hour' => '*',
'day' => '*',
'month' => '*',
'dayofweek' => '*'
)
);

View File

@ -68,7 +68,8 @@ class enrol_imsenterprise_plugin extends enrol_plugin {
* Read in an IMS Enterprise file.
* Originally designed to handle v1.1 files but should be able to handle
* earlier types as well, I believe.
*
* This cron feature has been converted to a scheduled task and it can now be scheduled
* from the UI.
*/
public function cron() {
global $CFG;
@ -280,6 +281,7 @@ class enrol_imsenterprise_plugin extends enrol_plugin {
if ($createnewcourses) {
require_once("$CFG->dirroot/course/lib.php");
}
// Process tag contents.
$group = new stdClass();
if (preg_match('{<sourcedid>.*?<id>(.+?)</id>.*?</sourcedid>}is', $tagcontents, $matches)) {

View File

@ -347,4 +347,26 @@ class enrol_imsenterprise_testcase extends advanced_testcase {
// Setting the file path in CFG.
$this->imsplugin->set_config('imsfilelocation', $xmlfilepath);
}
/**
* IMS Enterprise enrolment task test.
*/
public function test_imsenterprise_cron_task() {
global $DB;
$prevnusers = $DB->count_records('user');
$user1 = new StdClass();
$user1->username = 'u1';
$user1->email = 'u1@u1.org';
$user1->firstname = 'U';
$user1->lastname = '1';
$users = array($user1);
$this->set_xml_file($users);
$task = new enrol_imsenterprise\task\cron_task();
$task->execute();
$this->assertEquals(($prevnusers + 1), $DB->count_records('user'));
}
}

View File

@ -27,4 +27,3 @@ defined('MOODLE_INTERNAL') || die();
$plugin->version = 2014072200; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2014072200; // Requires this Moodle version
$plugin->component = 'enrol_imsenterprise';
$plugin->cron = 60;