Skip to content

Commit

Permalink
Merge branch 'MDL-50890_master' of git://github.com/dmonllao/moodle
Browse files Browse the repository at this point in the history
  • Loading branch information
David Monllao committed Sep 16, 2015
2 parents 5090d26 + 6bc490e commit 04fadbe
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 7 deletions.
66 changes: 66 additions & 0 deletions enrol/flatfile/classes/task/flatfile_sync_task.php
@@ -0,0 +1,66 @@
<?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.
*
* @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;

}

}
15 changes: 15 additions & 0 deletions enrol/flatfile/cli/sync.php
Expand Up @@ -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
Expand Down
37 changes: 37 additions & 0 deletions enrol/flatfile/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/>.

/**
* 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' => '*'
)
);
1 change: 1 addition & 0 deletions enrol/flatfile/lang/en/enrol_flatfile.php
Expand Up @@ -29,6 +29,7 @@
$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';
Expand Down
5 changes: 0 additions & 5 deletions enrol/flatfile/lib.php
Expand Up @@ -161,11 +161,6 @@ public function enrol_user(stdClass $instance, $userid, $roleid = null, $timesta
}
}

public function cron() {
$trace = new text_progress_trace();
$this->sync($trace);
}

/**
* Execute synchronisation.
* @param progress_trace
Expand Down
31 changes: 31 additions & 0 deletions enrol/flatfile/tests/flatfile_test.php
Expand Up @@ -467,4 +467,35 @@ public function test_expired() {
$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)));
}
}
3 changes: 1 addition & 2 deletions enrol/flatfile/version.php
Expand Up @@ -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;

0 comments on commit 04fadbe

Please sign in to comment.