Merge branch 'w02_MDL-37187_m25_flatfile' of git://github.com/skodak/moodle

This commit is contained in:
Dan Poltawski
2013-01-16 11:53:00 +08:00
8 changed files with 1364 additions and 299 deletions
+74
View File
@@ -0,0 +1,74 @@
<?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/>.
/**
* Flatfile CLI tool.
*
* Notes:
* - it is required to use the web server account when executing PHP CLI scripts
* - you need to change the "www-data" to match the apache user account
* - use "su" if "sudo" not available
*
* @package enrol_flatfile
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require(__DIR__.'/../../../config.php');
require_once("$CFG->libdir/clilib.php");
// Now get cli options.
list($options, $unrecognized) = cli_get_params(array('verbose'=>false, 'help'=>false), array('v'=>'verbose', 'h'=>'help'));
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help']) {
$help =
"Process flat file enrolments, update expiration and send notifications.
Options:
-v, --verbose Print verbose progress information
-h, --help Print out this help
Example:
\$ sudo -u www-data /usr/bin/php enrol/flatfile/cli/sync.php
";
echo $help;
die;
}
if (!enrol_is_enabled('flatfile')) {
cli_error('enrol_flatfile plugin is disabled, synchronisation stopped', 2);
}
/** @var $plugin enrol_flatfile_plugin */
$plugin = enrol_get_plugin('flatfile');
if (empty($options['verbose'])) {
$trace = new null_progress_trace();
} else {
$trace = new text_progress_trace();
}
$result = $plugin->sync($trace);
exit($result);
+43
View File
@@ -0,0 +1,43 @@
<?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/>.
/**
* Capabilities for manual enrolment plugin.
*
* @package enrol_flatfile
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
/* Manage enrolments of users - requires allowmodifications enabled. */
'enrol/flatfile:manage' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
)
),
/* Unenrol anybody (including self) - requires allowmodifications enabled */
'enrol/flatfile:unenrol' => array(
'captype' => 'write',
'contextlevel' => CONTEXT_COURSE,
'archetypes' => array(
)
),
);
+1 -3
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,8 +17,7 @@
/**
* Keeps track of upgrades to the enrol_flatfile plugin
*
* @package enrol
* @subpackage flatfile
* @package enrol_flatfile
* @copyright 2010 Aparup Banerjee <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
+23 -17
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -16,35 +15,42 @@
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for component 'enrol_flatfile', language 'en', branch 'MOODLE_20_STABLE'
* Strings for component 'enrol_flatfile', language 'en'.
*
* @package enrol
* @subpackage flatfile
* @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
*/
$string['encoding'] = 'File encoding';
$string['expiredaction'] = 'Enrolment expiration action';
$string['expiredaction_help'] = 'Select action to carry out when user enrolment expires. Please note that some user data and settings are purged from course during course unenrolment.';
$string['filelockedmail'] = 'The text file you are using for file-based enrolments ({$a}) can not be deleted by the cron process. This usually means the permissions are wrong on it. Please fix the permissions so that Moodle can delete the file, otherwise it might be processed repeatedly.';
$string['filelockedmailsubject'] = 'Important error: Enrolment file';
$string['flatfile:manage'] = 'Manage user enrolments manually';
$string['flatfile:unenrol'] = 'Unenrol users from the course manually';
$string['location'] = 'File location';
$string['mailadmin'] = 'Notify admin by email';
$string['mailstudents'] = 'Notify students by email';
$string['mailteachers'] = 'Notify teachers by email';
$string['location_desc'] = 'Specify full path to the enrolment file. The file is automatically deleted after processing.';
$string['notifyadmin'] = 'Notify administrator';
$string['notifyenrolled'] = 'Notify enrolled users';
$string['notifyenroller'] = 'Notify user responsible for enrolments';
$string['messageprovider:flatfile_enrolment'] = 'Flat file enrolment messages';
$string['mapping'] = 'Flat file mapping';
$string['mapping'] = 'Flat file role mapping';
$string['pluginname'] = 'Flat file (CSV)';
$string['pluginname_desc'] = 'This method will repeatedly check for and process a specially-formatted text file in the location that you specify.
The file is a comma separated file assumed to have four or six fields per line:
<pre class="informationbox">
* operation, role, idnumber(user), idnumber(course) [, starttime, endtime]
operation, role, user idnumber, course idnumber [, starttime [, endtime]]
where:
* operation = add | del
* role = student | teacher | teacheredit
* idnumber(user) = idnumber in the user table NB not id
* idnumber(course) = idnumber in the course table NB not id
* starttime = start time (in seconds since epoch) - optional
* endtime = end time (in seconds since epoch) - optional
</pre>
* operation - add | del
* role - student | teacher | teacheredit
* user idnumber - idnumber in the user table NB not id
* course idnumber - idnumber in the course table NB not id
* starttime - start time (in seconds since epoch) - optional
* endtime - end time (in seconds since epoch) - optional
It could look something like this:
<pre class="informationbox">
add, student, 5, CF101
+730 -269
View File
File diff suppressed because it is too large Load Diff
+23 -7
View File
@@ -1,5 +1,4 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
@@ -18,8 +17,7 @@
/**
* Flatfile enrolments plugin settings and presets.
*
* @package enrol
* @subpackage flatfile
* @package enrol_flatfile
* @copyright 2010 Eugene Venter
* @author Eugene Venter - based on code by Petr Skoda and others
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -32,13 +30,30 @@ if ($ADMIN->fulltree) {
//--- general settings -----------------------------------------------------------------------------------
$settings->add(new admin_setting_heading('enrol_flatfile_settings', '', get_string('pluginname_desc', 'enrol_flatfile')));
$settings->add(new admin_setting_configtext('enrol_flatfile/location', get_string('location', 'enrol_flatfile'), '', ''));
$settings->add(new admin_setting_configfile('enrol_flatfile/location', get_string('location', 'enrol_flatfile'), get_string('location_desc', 'enrol_flatfile'), ''));
$settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailstudents', get_string('mailstudents', 'enrol_flatfile'), '', 0));
$options = textlib::get_encodings();
$settings->add(new admin_setting_configselect('enrol_flatfile/encoding', get_string('encoding', 'enrol_flatfile'), '', 'UTF-8', $options));
$settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailteachers', get_string('mailteachers', 'enrol_flatfile'), '', 0));
$settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailstudents', get_string('notifyenrolled', 'enrol_flatfile'), '', 0));
$settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailadmins', get_string('mailadmin', 'enrol_flatfile'), '', 0));
$settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailteachers', get_string('notifyenroller', 'enrol_flatfile'), '', 0));
$settings->add(new admin_setting_configcheckbox('enrol_flatfile/mailadmins', get_string('notifyadmin', 'enrol_flatfile'), '', 0));
$options = array(ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'),
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'));
$settings->add(new admin_setting_configselect('enrol_flatfile/unenrolaction', get_string('extremovedaction', 'enrol'), get_string('extremovedaction_help', 'enrol'), ENROL_EXT_REMOVED_SUSPENDNOROLES, $options));
// Note: let's reuse the ext sync constants and strings here, internally it is very similar,
// it describes what should happen when users are not supposed to be enrolled any more.
$options = array(
ENROL_EXT_REMOVED_KEEP => get_string('extremovedkeep', 'enrol'),
ENROL_EXT_REMOVED_SUSPENDNOROLES => get_string('extremovedsuspendnoroles', 'enrol'),
ENROL_EXT_REMOVED_UNENROL => get_string('extremovedunenrol', 'enrol'),
);
$settings->add(new admin_setting_configselect('enrol_flatfile/expiredaction', get_string('expiredaction', 'enrol_flatfile'), get_string('expiredaction_help', 'enrol_flatfile'), ENROL_EXT_REMOVED_SUSPENDNOROLES, $options));
//--- mapping -------------------------------------------------------------------------------------------
if (!during_initial_install()) {
@@ -49,5 +64,6 @@ if ($ADMIN->fulltree) {
foreach ($roles as $id => $role) {
$settings->add(new admin_setting_configtext('enrol_flatfile/map_'.$id, $role->localname, '', $role->shortname));
}
unset($roles);
}
}
+468
View File
@@ -0,0 +1,468 @@
<?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/>.
/**
* flatfile enrolment sync tests.
*
* @package enrol_flatfile
* @category phpunit
* @copyright 2012 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
class enrol_flatfile_testcase extends advanced_testcase {
protected function enable_plugin() {
$enabled = enrol_get_plugins(true);
$enabled['flatfile'] = true;
$enabled = array_keys($enabled);
set_config('enrol_plugins_enabled', implode(',', $enabled));
}
protected function disable_plugin() {
$enabled = enrol_get_plugins(true);
unset($enabled['flatfile']);
$enabled = array_keys($enabled);
set_config('enrol_plugins_enabled', implode(',', $enabled));
}
public function test_basics() {
$this->assertFalse(enrol_is_enabled('flatfile'));
$plugin = enrol_get_plugin('flatfile');
$this->assertInstanceOf('enrol_flatfile_plugin', $plugin);
$this->assertEquals(ENROL_EXT_REMOVED_SUSPENDNOROLES, get_config('enrol_flatfile', 'expiredaction'));
}
public function test_sync_nothing() {
$this->resetAfterTest();
$this->disable_plugin();
$flatfileplugin = enrol_get_plugin('flatfile');
// Just make sure the sync does not throw any errors when nothing to do.
$flatfileplugin->sync(new null_progress_trace());
$this->enable_plugin();
$flatfileplugin->sync(new null_progress_trace());
}
public function test_sync() {
global $CFG, $DB;
$this->resetAfterTest();
/** @var enrol_flatfile_plugin $flatfileplugin */
$flatfileplugin = enrol_get_plugin('flatfile');
/** @var enrol_manual_plugin $manualplugin */
$manualplugin = enrol_get_plugin('manual');
$this->assertNotEmpty($manualplugin);
$trace = new null_progress_trace();
$this->enable_plugin();
$file = "$CFG->dataroot/enrol.txt";
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
$this->assertNotEmpty($studentrole);
$teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
$this->assertNotEmpty($teacherrole);
$managerrole = $DB->get_record('role', array('shortname'=>'manager'));
$this->assertNotEmpty($managerrole);
$user1 = $this->getDataGenerator()->create_user(array('idnumber'=>'u1'));
$user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'u2'));
$user3 = $this->getDataGenerator()->create_user(array('idnumber'=>'u3'));
$user4 = $this->getDataGenerator()->create_user(array('idnumber'=>'čtvrtý'));
$user5 = $this->getDataGenerator()->create_user(array('idnumber'=>'u5'));
$user6 = $this->getDataGenerator()->create_user(array('idnumber'=>'u6'));
$course1 = $this->getDataGenerator()->create_course(array('idnumber'=>'c1'));
$course2 = $this->getDataGenerator()->create_course(array('idnumber'=>'c2'));
$course3 = $this->getDataGenerator()->create_course(array('idnumber'=>'c3'));
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$context3 = context_course::instance($course3->id);
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$maninstance2 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$maninstance3 = $DB->get_record('enrol', array('courseid'=>$course3->id, 'enrol'=>'manual'), '*', MUST_EXIST);
// Rename teacher role.
$flatfileplugin->set_config('map_'.$teacherrole->id, 'ucitel');
// Disable manager role.
$flatfileplugin->set_config('map_'.$managerrole->id, '');
// Set file location.
$flatfileplugin->set_config('location', $file);
$now = time();
$before = $now - 60;
$future = $now + 60*60*5;
$farfuture = $now + 60*60*24*5;
// Test add action.
$data ="'add','student','u1','c1'
\"add\" , \"ucitel\", u2 , c2
add,manager,u3,c1
add,student,čtvrtý,c2,$before
add,student,u5,c1,0,0,1
add,student,u5,c2,20,10
add,student,u6,c1,0,$future
add,student,u6,c2,$future,0
add,student,u6,c3,$future,$farfuture";
file_put_contents($file, $data);
$this->assertEquals(0, $DB->count_records('user_enrolments'));
$this->assertEquals(0, $DB->count_records('role_assignments'));
$this->assertEquals(0, $DB->count_records('enrol_flatfile'));
$this->assertTrue(file_exists($file));
$flatfileplugin->sync($trace);
$this->assertFalse(file_exists($file));
$this->assertEquals(4, $DB->count_records('user_enrolments'));
$this->assertEquals(4, $DB->count_records('role_assignments'));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user2->id, 'roleid'=>$teacherrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
// Test buffer.
$this->assertEquals(2, $DB->count_records('enrol_flatfile'));
$flatfileplugin->sync($trace);
$this->assertEquals(2, $DB->count_records('enrol_flatfile'));
$this->assertEquals(4, $DB->count_records('user_enrolments'));
$this->assertEquals(4, $DB->count_records('role_assignments'));
$DB->set_field('enrol_flatfile', 'timestart', time()-60, array('timestart'=>$future, 'timeend'=>0));
$flatfileplugin->sync($trace);
$this->assertEquals(1, $DB->count_records('enrol_flatfile'));
$this->assertEquals(5, $DB->count_records('user_enrolments'));
$this->assertEquals(5, $DB->count_records('role_assignments'));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('enrol_flatfile', array('userid'=>$user6->id, 'roleid'=>$studentrole->id, 'timeend'=>$farfuture)));
// Test encoding.
$data = "add;student;čtvrtý;c3";
$data = textlib::convert($data, 'utf-8', 'iso-8859-2');
file_put_contents($file, $data);
$flatfileplugin->set_config('encoding', 'iso-8859-2');
$flatfileplugin->sync($trace);
$this->assertEquals(6, $DB->count_records('user_enrolments'));
$this->assertEquals(6, $DB->count_records('role_assignments'));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user4->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$flatfileplugin->set_config('encoding', 'UTF-8');
// Test unenrolling purges buffer.
$manualplugin->enrol_user($maninstance1, $user1->id, $teacherrole->id);
$manualplugin->enrol_user($maninstance3, $user5->id, $teacherrole->id);
$this->assertEquals(8, $DB->count_records('user_enrolments'));
$this->assertEquals(8, $DB->count_records('role_assignments'));
$this->assertEquals(1, $DB->count_records('enrol_flatfile'));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
$flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
$data = "del,student,u1,c1\ndel,teacher,u6,c3";
file_put_contents($file, $data);
$flatfileplugin->sync($trace);
$this->assertEquals(8, $DB->count_records('user_enrolments'));
$this->assertEquals(8, $DB->count_records('role_assignments'));
$this->assertEquals(1, $DB->count_records('enrol_flatfile'));
$data = "del,student,u6,c3";
file_put_contents($file, $data);
$flatfileplugin->sync($trace);
$this->assertEquals(8, $DB->count_records('user_enrolments'));
$this->assertEquals(8, $DB->count_records('role_assignments'));
$this->assertEquals(0, $DB->count_records('enrol_flatfile'));
$flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$data = "
del,student,u1,c1
del,grrr,u5,c1
del,guest,u5,c2
del,student,u6,c2
del,ucitel,u5,c3";
file_put_contents($file, $data);
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
$flatfileplugin->sync($trace);
$this->assertEquals(8, $DB->count_records('user_enrolments'));
$this->assertEquals(5, $DB->count_records('role_assignments'));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
$flatfileplugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$manualplugin->enrol_user($maninstance3, $user5->id, $teacherrole->id);
$data = "
add,student,u1,c1
add,student,u6,c2";
file_put_contents($file, $data);
$flatfileplugin->sync($trace);
$this->assertEquals(8, $DB->count_records('user_enrolments'));
$this->assertEquals(8, $DB->count_records('role_assignments'));
$this->assertEquals(0, $DB->count_records('enrol_flatfile'));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
$this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user5->id, 'enrolid'=>$maninstance3->id)));
$this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user1->id, 'enrolid'=>$maninstance1->id)));
$data = "
del,student,u1,c1
del,grrr,u5,c1
del,guest,u5,c2
del,student,u6,c2
del,ucitel,u5,c3";
file_put_contents($file, $data);
$flatfileplugin->sync($trace);
$this->assertEquals(5, $DB->count_records('user_enrolments'));
$this->assertEquals(5, $DB->count_records('role_assignments'));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id, 'component'=>'enrol_flatfile')));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user6->id, 'roleid'=>$studentrole->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context3->id, 'userid'=>$user5->id, 'roleid'=>$teacherrole->id)));
$this->assertFalse($DB->record_exists('user_enrolments', array('userid'=>$user5->id, 'enrolid'=>$maninstance3->id)));
$this->assertTrue($DB->record_exists('user_enrolments', array('userid'=>$user1->id, 'enrolid'=>$maninstance1->id)));
}
public function test_notification() {
global $CFG, $DB;
$this->resetAfterTest();
$this->preventResetByRollback();
/** @var enrol_flatfile_plugin $flatfileplugin */
$flatfileplugin = enrol_get_plugin('flatfile');
/** @var enrol_manual_plugin $manualplugin */
$manualplugin = enrol_get_plugin('manual');
$this->assertNotEmpty($manualplugin);
$this->enable_plugin();
$trace = new progress_trace_buffer(new text_progress_trace(), false);
$file = "$CFG->dataroot/enrol.txt";
$flatfileplugin->set_config('location', $file);
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
$this->assertNotEmpty($studentrole);
$teacherrole = $DB->get_record('role', array('shortname'=>'editingteacher'));
$this->assertNotEmpty($teacherrole);
$user1 = $this->getDataGenerator()->create_user(array('idnumber'=>'u1'));
$user2 = $this->getDataGenerator()->create_user(array('idnumber'=>'u2'));
$user3 = $this->getDataGenerator()->create_user(array('idnumber'=>'u3'));
$admin = get_admin();
$course1 = $this->getDataGenerator()->create_course(array('idnumber'=>'c1'));
$course2 = $this->getDataGenerator()->create_course(array('idnumber'=>'c2'));
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course1->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$now = time();
$future = $now + 60*60*5;
$farfuture = $now + 60*60*24*5;
$manualplugin->enrol_user($maninstance1, $user3->id, $teacherrole->id);
$data =
"add,student,u1,c1
add,student,u2,c2
add,student,u2,c1,$future,$farfuture";
file_put_contents($file, $data);
$this->assertEquals(1, $DB->count_records('user_enrolments'));
$this->assertEquals(1, $DB->count_records('role_assignments'));
$this->assertEquals(0, $DB->count_records('enrol_flatfile'));
$flatfileplugin->set_config('mailadmins', 1);
$flatfileplugin->set_config('mailteachers', 1);
$flatfileplugin->set_config('mailstudents', 1);
$sink = $this->redirectMessages();
$flatfileplugin->sync($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments'));
$this->assertEquals(3, $DB->count_records('role_assignments'));
$this->assertEquals(1, $DB->count_records('enrol_flatfile'));
$messages = $sink->get_messages();
$this->assertCount(5, $messages);
// Notify student from teacher.
$this->assertEquals($user1->id, $messages[0]->useridto);
$this->assertEquals($user3->id, $messages[0]->useridfrom);
// Notify teacher.
$this->assertEquals($user3->id, $messages[1]->useridto);
$this->assertEquals($admin->id, $messages[1]->useridfrom);
// Notify student when teacher not present.
$this->assertEquals($user2->id, $messages[2]->useridto);
$this->assertEquals($admin->id, $messages[2]->useridfrom);
// Notify admin when teacher not present.
$this->assertEquals($admin->id, $messages[3]->useridto);
$this->assertEquals($admin->id, $messages[3]->useridfrom);
// Sent report to admin from self.
$this->assertEquals($admin->id, $messages[4]->useridto);
$this->assertEquals($admin->id, $messages[4]->useridfrom);
}
public function test_expired() {
global $DB;
$this->resetAfterTest();
/** @var enrol_flatfile_plugin $flatfileplugin */
$flatfileplugin = enrol_get_plugin('flatfile');
/** @var enrol_manual_plugin $manualplugin */
$manualplugin = enrol_get_plugin('manual');
$this->assertNotEmpty($manualplugin);
$now = time();
$trace = new null_progress_trace();
$this->enable_plugin();
// Prepare some data.
$studentrole = $DB->get_record('role', array('shortname'=>'student'));
$this->assertNotEmpty($studentrole);
$teacherrole = $DB->get_record('role', array('shortname'=>'teacher'));
$this->assertNotEmpty($teacherrole);
$managerrole = $DB->get_record('role', array('shortname'=>'manager'));
$this->assertNotEmpty($managerrole);
$user1 = $this->getDataGenerator()->create_user();
$user2 = $this->getDataGenerator()->create_user();
$user3 = $this->getDataGenerator()->create_user();
$user4 = $this->getDataGenerator()->create_user();
$course1 = $this->getDataGenerator()->create_course();
$course2 = $this->getDataGenerator()->create_course();
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$data = array('roleid'=>$studentrole->id, 'courseid'=>$course1->id);
$id = $flatfileplugin->add_instance($course1, $data);
$instance1 = $DB->get_record('enrol', array('id'=>$id));
$data = array('roleid'=>$studentrole->id, 'courseid'=>$course2->id);
$id = $flatfileplugin->add_instance($course2, $data);
$instance2 = $DB->get_record('enrol', array('id'=>$id));
$data = array('roleid'=>$teacherrole->id, 'courseid'=>$course2->id);
$id = $flatfileplugin->add_instance($course2, $data);
$instance3 = $DB->get_record('enrol', array('id'=>$id));
$maninstance1 = $DB->get_record('enrol', array('courseid'=>$course2->id, 'enrol'=>'manual'), '*', MUST_EXIST);
$manualplugin->enrol_user($maninstance1, $user3->id, $studentrole->id);
$this->assertEquals(1, $DB->count_records('user_enrolments'));
$this->assertEquals(1, $DB->count_records('role_assignments'));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
$flatfileplugin->enrol_user($instance1, $user1->id, $studentrole->id);
$flatfileplugin->enrol_user($instance1, $user2->id, $studentrole->id);
$flatfileplugin->enrol_user($instance1, $user3->id, $studentrole->id, 0, $now-60);
$flatfileplugin->enrol_user($instance2, $user1->id, $studentrole->id, 0, 0);
$flatfileplugin->enrol_user($instance2, $user2->id, $studentrole->id, 0, $now-60*60);
$flatfileplugin->enrol_user($instance2, $user3->id, $studentrole->id, 0, $now+60*60);
$flatfileplugin->enrol_user($instance3, $user1->id, $teacherrole->id, $now-60*60*24*7, $now-60);
$flatfileplugin->enrol_user($instance3, $user4->id, $teacherrole->id);
role_assign($managerrole->id, $user3->id, $context1->id);
$this->assertEquals(9, $DB->count_records('user_enrolments'));
$this->assertEquals(10, $DB->count_records('role_assignments'));
$this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
// Execute tests.
$flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_KEEP);
$code = $flatfileplugin->sync($trace);
$this->assertSame(0, $code);
$this->assertEquals(9, $DB->count_records('user_enrolments'));
$this->assertEquals(10, $DB->count_records('role_assignments'));
$flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$flatfileplugin->sync($trace);
$this->assertEquals(9, $DB->count_records('user_enrolments'));
$this->assertEquals(7, $DB->count_records('role_assignments'));
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context1->id, 'userid'=>$user3->id, 'roleid'=>$studentrole->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user2->id, 'roleid'=>$studentrole->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$teacherrole->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('contextid'=>$context2->id, 'userid'=>$user1->id, 'roleid'=>$studentrole->id)));
$flatfileplugin->set_config('expiredaction', ENROL_EXT_REMOVED_UNENROL);
role_assign($studentrole->id, $user3->id, $context1->id, 'enrol_flatfile', $instance1->id);
role_assign($studentrole->id, $user2->id, $context2->id, 'enrol_flatfile', $instance2->id);
role_assign($teacherrole->id, $user1->id, $context2->id, 'enrol_flatfile', $instance3->id);
$this->assertEquals(9, $DB->count_records('user_enrolments'));
$this->assertEquals(10, $DB->count_records('role_assignments'));
$this->assertEquals(7, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
$this->assertEquals(2, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
$flatfileplugin->sync($trace);
$this->assertEquals(6, $DB->count_records('user_enrolments'));
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance1->id, 'userid'=>$user3->id)));
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance2->id, 'userid'=>$user2->id)));
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$instance3->id, 'userid'=>$user1->id)));
$this->assertEquals(6, $DB->count_records('role_assignments'));
$this->assertEquals(5, $DB->count_records('role_assignments', array('roleid'=>$studentrole->id)));
$this->assertEquals(1, $DB->count_records('role_assignments', array('roleid'=>$teacherrole->id)));
$this->assertEquals(0, $DB->count_records('role_assignments', array('roleid'=>$managerrole->id)));
}
}
+2 -3
View File
@@ -17,8 +17,7 @@
/**
* Flat file enrolment plugin version specification.
*
* @package enrol
* @subpackage flatfile
* @package enrol_flatfile
* @copyright 2010 Eugene Venter
* @author Eugene Venter
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
@@ -26,7 +25,7 @@
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2012112900; // The current plugin version (Date: YYYYMMDDXX)
$plugin->version = 2012121800; // The current plugin version (Date: YYYYMMDDXX)
$plugin->requires = 2012112900; // Requires this Moodle version
$plugin->component = 'enrol_flatfile'; // Full name of the plugin (used for diagnostics)
$plugin->cron = 60;