Merge branch 'MDL-28420_master' of https://github.com/jrchamp/moodle

This commit is contained in:
Eloy Lafuente (stronk7) 2014-06-25 02:07:34 +02:00
commit 0518b6a26a
4 changed files with 285 additions and 190 deletions

View File

@ -60,6 +60,8 @@ $string['remotecoursefield'] = 'Remote course field';
$string['remotecoursefield_desc'] = 'The name of the field in the remote table that we are using to match entries in the course table.';
$string['remoteenroltable'] = 'Remote user enrolment table';
$string['remoteenroltable_desc'] = 'Specify the name of the table that contains list of user enrolments. Empty means no user enrolment sync.';
$string['remoteotheruserfield'] = 'Remote Other User field';
$string['remoteotheruserfield_desc'] = 'The name of the field in the remote table that we are using to flag "Other User" role assignments.';
$string['remoterolefield'] = 'Remote role field';
$string['remoterolefield_desc'] = 'The name of the field in the remote table that we are using to match entries in the roles table.';
$string['remoteuserfield'] = 'Remote user field';

View File

@ -106,11 +106,13 @@ class enrol_database_plugin extends enrol_plugin {
$coursefield = trim($this->get_config('remotecoursefield'));
$userfield = trim($this->get_config('remoteuserfield'));
$rolefield = trim($this->get_config('remoterolefield'));
$otheruserfield = trim($this->get_config('remoteotheruserfield'));
// Lowercased versions - necessary because we normalise the resultset with array_change_key_case().
$coursefield_l = strtolower($coursefield);
$userfield_l = strtolower($userfield);
$rolefield_l = strtolower($rolefield);
$otheruserfieldlower = strtolower($otheruserfield);
$localrolefield = $this->get_config('localrolefield');
$localuserfield = $this->get_config('localuserfield');
@ -140,6 +142,7 @@ class enrol_database_plugin extends enrol_plugin {
$roles[$role->$localrolefield] = $role->id;
}
$roleassigns = array();
$enrols = array();
$instances = array();
@ -178,10 +181,10 @@ class enrol_database_plugin extends enrol_plugin {
$roleid = $roles[$fields[$rolefield_l]];
}
if (empty($enrols[$course->id])) {
$enrols[$course->id] = array();
$roleassigns[$course->id][$roleid] = $roleid;
if (empty($fields[$otheruserfieldlower])) {
$enrols[$course->id][$roleid] = $roleid;
}
$enrols[$course->id][] = $roleid;
if ($instance = $DB->get_record('enrol', array('courseid'=>$course->id, 'enrol'=>'database'), '*', IGNORE_MULTIPLE)) {
$instances[$course->id] = $instance;
@ -201,21 +204,23 @@ class enrol_database_plugin extends enrol_plugin {
}
// Enrol user into courses and sync roles.
foreach ($enrols as $courseid => $roles) {
foreach ($roleassigns as $courseid => $roles) {
if (!isset($instances[$courseid])) {
// Ignored.
continue;
}
$instance = $instances[$courseid];
if ($e = $DB->get_record('user_enrolments', array('userid'=>$user->id, 'enrolid'=>$instance->id))) {
// Reenable enrolment when previously disable enrolment refreshed.
if ($e->status == ENROL_USER_SUSPENDED) {
$this->update_user_enrol($instance, $user->id, ENROL_USER_ACTIVE);
if (isset($enrols[$courseid])) {
if ($e = $DB->get_record('user_enrolments', array('userid' => $user->id, 'enrolid' => $instance->id))) {
// Reenable enrolment when previously disable enrolment refreshed.
if ($e->status == ENROL_USER_SUSPENDED) {
$this->update_user_enrol($instance, $user->id, ENROL_USER_ACTIVE);
}
} else {
$roleid = reset($enrols[$courseid]);
$this->enrol_user($instance, $user->id, $roleid, 0, 0, ENROL_USER_ACTIVE);
}
} else {
$roleid = reset($roles);
$this->enrol_user($instance, $user->id, $roleid, 0, 0, ENROL_USER_ACTIVE);
}
if (!$context = context_course::instance($instance->courseid, IGNORE_MISSING)) {
@ -226,7 +231,7 @@ class enrol_database_plugin extends enrol_plugin {
$existing = array();
foreach ($current as $r) {
if (in_array($r->roleid, $roles)) {
if (isset($roles[$r->roleid])) {
$existing[$r->roleid] = $r->roleid;
} else {
role_unassign($r->roleid, $user->id, $context->id, 'enrol_database', $instance->id);
@ -242,9 +247,10 @@ class enrol_database_plugin extends enrol_plugin {
// Unenrol as necessary.
$sql = "SELECT e.*, c.visible AS cvisible, ue.status AS ustatus
FROM {enrol} e
JOIN {user_enrolments} ue ON ue.enrolid = e.id
JOIN {course} c ON c.id = e.courseid
WHERE ue.userid = :userid AND e.enrol = 'database'";
JOIN {role_assignments} ra ON ra.itemid = e.id
LEFT JOIN {user_enrolments} ue ON ue.enrolid = e.id
WHERE ra.userid = :userid AND e.enrol = 'database'";
$rs = $DB->get_recordset_sql($sql, array('userid'=>$user->id));
foreach ($rs as $instance) {
if (!$instance->cvisible and $ignorehidden) {
@ -274,6 +280,10 @@ class enrol_database_plugin extends enrol_plugin {
$this->update_user_enrol($instance, $user->id, ENROL_USER_SUSPENDED);
}
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
if (!empty($roleassigns[$instance->courseid])) {
// We want this "other user" to keep their roles.
continue;
}
role_unassign_all(array('contextid'=>$context->id, 'userid'=>$user->id, 'component'=>'enrol_database', 'itemid'=>$instance->id));
}
}
@ -314,11 +324,13 @@ class enrol_database_plugin extends enrol_plugin {
$coursefield = trim($this->get_config('remotecoursefield'));
$userfield = trim($this->get_config('remoteuserfield'));
$rolefield = trim($this->get_config('remoterolefield'));
$otheruserfield = trim($this->get_config('remoteotheruserfield'));
// Lowercased versions - necessary because we normalise the resultset with array_change_key_case().
$coursefield_l = strtolower($coursefield);
$userfield_l = strtolower($userfield);
$rolefield_l = strtolower($rolefield);
$otheruserfieldlower = strtolower($otheruserfield);
$localrolefield = $this->get_config('localrolefield');
$localuserfield = $this->get_config('localuserfield');
@ -446,6 +458,9 @@ class enrol_database_plugin extends enrol_plugin {
if ($rolefield) {
$sqlfields[] = $rolefield;
}
if ($otheruserfield) {
$sqlfields[] = $otheruserfield;
}
foreach ($existing as $course) {
if ($ignorehidden and !$course->visible) {
continue;
@ -456,13 +471,14 @@ class enrol_database_plugin extends enrol_plugin {
$context = context_course::instance($course->id);
// Get current list of enrolled users with their roles.
$current_roles = array();
$current_status = array();
$user_mapping = array();
$sql = "SELECT u.$localuserfield AS mapping, u.id, ue.status, ue.userid, ra.roleid
$currentroles = array();
$currentenrols = array();
$currentstatus = array();
$usermapping = array();
$sql = "SELECT u.$localuserfield AS mapping, u.id AS userid, ue.status, ra.roleid
FROM {user} u
JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = :enrolid)
JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.itemid = ue.enrolid AND ra.component = 'enrol_database')
JOIN {role_assignments} ra ON (ra.userid = u.id AND ra.component = 'enrol_database' AND ra.itemid = :enrolid)
LEFT JOIN {user_enrolments} ue ON (ue.userid = u.id AND ue.enrolid = ra.itemid)
WHERE u.deleted = 0";
$params = array('enrolid'=>$instance->id);
if ($localuserfield === 'username') {
@ -471,14 +487,19 @@ class enrol_database_plugin extends enrol_plugin {
}
$rs = $DB->get_recordset_sql($sql, $params);
foreach ($rs as $ue) {
$current_roles[$ue->userid][$ue->roleid] = $ue->roleid;
$current_status[$ue->userid] = $ue->status;
$user_mapping[$ue->mapping] = $ue->userid;
$currentroles[$ue->userid][$ue->roleid] = $ue->roleid;
$usermapping[$ue->mapping] = $ue->userid;
if (isset($ue->status)) {
$currentenrols[$ue->userid][$ue->roleid] = $ue->roleid;
$currentstatus[$ue->userid] = $ue->status;
}
}
$rs->close();
// Get list of users that need to be enrolled and their roles.
$requested_roles = array();
$requestedroles = array();
$requestedenrols = array();
$sql = $this->db_get_sql($table, array($coursefield=>$course->mapping), $sqlfields);
if ($rs = $extdb->Execute($sql)) {
if (!$rs->EOF) {
@ -493,16 +514,16 @@ class enrol_database_plugin extends enrol_plugin {
continue;
}
$mapping = $fields[$userfield_l];
if (!isset($user_mapping[$mapping])) {
if (!isset($usermapping[$mapping])) {
$usersearch[$localuserfield] = $mapping;
if (!$user = $DB->get_record('user', $usersearch, 'id', IGNORE_MULTIPLE)) {
$trace->output("error: skipping unknown user $localuserfield '$mapping' in course '$course->mapping'", 1);
continue;
}
$user_mapping[$mapping] = $user->id;
$usermapping[$mapping] = $user->id;
$userid = $user->id;
} else {
$userid = $user_mapping[$mapping];
$userid = $usermapping[$mapping];
}
if (empty($fields[$rolefield_l]) or !isset($roles[$fields[$rolefield_l]])) {
if (!$defaultrole) {
@ -514,7 +535,10 @@ class enrol_database_plugin extends enrol_plugin {
$roleid = $roles[$fields[$rolefield_l]];
}
$requested_roles[$userid][$roleid] = $roleid;
$requestedroles[$userid][$roleid] = $roleid;
if (empty($fields[$otheruserfieldlower])) {
$requestedenrols[$userid][$roleid] = $roleid;
}
}
}
$rs->Close();
@ -522,50 +546,61 @@ class enrol_database_plugin extends enrol_plugin {
$trace->output("error: skipping course '$course->mapping' - could not match with external database", 1);
continue;
}
unset($user_mapping);
unset($usermapping);
// Enrol all users and sync roles.
foreach ($requested_roles as $userid=>$userroles) {
foreach ($requestedenrols as $userid => $userroles) {
foreach ($userroles as $roleid) {
if (empty($current_roles[$userid])) {
if (empty($currentenrols[$userid])) {
$this->enrol_user($instance, $userid, $roleid, 0, 0, ENROL_USER_ACTIVE);
$current_roles[$userid][$roleid] = $roleid;
$current_status[$userid] = ENROL_USER_ACTIVE;
$currentroles[$userid][$roleid] = $roleid;
$currentenrols[$userid][$roleid] = $roleid;
$currentstatus[$userid] = ENROL_USER_ACTIVE;
$trace->output("enrolling: $userid ==> $course->shortname as ".$allroles[$roleid]->shortname, 1);
}
}
// Reenable enrolment when previously disable enrolment refreshed.
if ($currentstatus[$userid] == ENROL_USER_SUSPENDED) {
$this->update_user_enrol($instance, $userid, ENROL_USER_ACTIVE);
$trace->output("unsuspending: $userid ==> $course->shortname", 1);
}
}
foreach ($requestedroles as $userid => $userroles) {
// Assign extra roles.
foreach ($userroles as $roleid) {
if (empty($current_roles[$userid][$roleid])) {
if (empty($currentroles[$userid][$roleid])) {
role_assign($roleid, $userid, $context->id, 'enrol_database', $instance->id);
$current_roles[$userid][$roleid] = $roleid;
$currentroles[$userid][$roleid] = $roleid;
$trace->output("assigning roles: $userid ==> $course->shortname as ".$allroles[$roleid]->shortname, 1);
}
}
// Unassign removed roles.
foreach($current_roles[$userid] as $cr) {
foreach ($currentroles[$userid] as $cr) {
if (empty($userroles[$cr])) {
role_unassign($cr, $userid, $context->id, 'enrol_database', $instance->id);
unset($current_roles[$userid][$cr]);
unset($currentroles[$userid][$cr]);
$trace->output("unsassigning roles: $userid ==> $course->shortname", 1);
}
}
// Reenable enrolment when previously disable enrolment refreshed.
if ($current_status[$userid] == ENROL_USER_SUSPENDED) {
$this->update_user_enrol($instance, $userid, ENROL_USER_ACTIVE);
$trace->output("unsuspending: $userid ==> $course->shortname", 1);
}
unset($currentroles[$userid]);
}
foreach ($currentroles as $userid => $userroles) {
// These are roles that exist only in Moodle, not the external database
// so make sure the unenrol actions will handle them by setting status.
$currentstatus += array($userid => ENROL_USER_ACTIVE);
}
// Deal with enrolments removed from external table.
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
if (!$preventfullunenrol) {
// Unenrol.
foreach ($current_status as $userid=>$status) {
if (isset($requested_roles[$userid])) {
foreach ($currentstatus as $userid => $status) {
if (isset($requestedenrols[$userid])) {
continue;
}
$this->unenrol_user($instance, $userid);
@ -578,8 +613,8 @@ class enrol_database_plugin extends enrol_plugin {
} else if ($unenrolaction == ENROL_EXT_REMOVED_SUSPEND or $unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
// Suspend enrolments.
foreach ($current_status as $userid=>$status) {
if (isset($requested_roles[$userid])) {
foreach ($currentstatus as $userid => $status) {
if (isset($requestedenrols[$userid])) {
continue;
}
if ($status != ENROL_USER_SUSPENDED) {
@ -587,7 +622,12 @@ class enrol_database_plugin extends enrol_plugin {
$trace->output("suspending: $userid ==> $course->shortname", 1);
}
if ($unenrolaction == ENROL_EXT_REMOVED_SUSPENDNOROLES) {
if (isset($requestedroles[$userid])) {
// We want this "other user" to keep their roles.
continue;
}
role_unassign_all(array('contextid'=>$context->id, 'userid'=>$userid, 'component'=>'enrol_database', 'itemid'=>$instance->id));
$trace->output("unsassigning all roles: $userid ==> $course->shortname", 1);
}
}

View File

@ -78,6 +78,10 @@ if ($ADMIN->fulltree) {
$settings->add(new admin_setting_configtext('enrol_database/remoterolefield', get_string('remoterolefield', 'enrol_database'), get_string('remoterolefield_desc', 'enrol_database'), ''));
$otheruserfieldlabel = get_string('remoteotheruserfield', 'enrol_database');
$otheruserfielddesc = get_string('remoteotheruserfield_desc', 'enrol_database');
$settings->add(new admin_setting_configtext('enrol_database/remoteotheruserfield', $otheruserfieldlabel, $otheruserfielddesc, ''));
if (!during_initial_install()) {
$options = get_default_enrol_roles(context_system::instance());
$student = get_archetype_roles('student');

View File

@ -118,6 +118,7 @@ class enrol_database_testcase extends advanced_testcase {
$table->add_field('courseid', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('userid', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('roleid', XMLDB_TYPE_CHAR, '255', null, null, null);
$table->add_field('otheruser', XMLDB_TYPE_CHAR, '1', null, XMLDB_NOTNULL, null, '0');
$table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
if ($dbman->table_exists($table)) {
$dbman->drop_table($table);
@ -127,6 +128,7 @@ class enrol_database_testcase extends advanced_testcase {
set_config('remotecoursefield', 'courseid', 'enrol_database');
set_config('remoteuserfield', 'userid', 'enrol_database');
set_config('remoterolefield', 'roleid', 'enrol_database');
set_config('remoteotheruserfield', 'otheruser', 'enrol_database');
$table = new xmldb_table('enrol_database_test_courses');
$table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
@ -146,12 +148,12 @@ class enrol_database_testcase extends advanced_testcase {
set_config('newcoursecategory', 'category', 'enrol_database');
// Create some test users and courses.
for($i=1;$i<=3;$i++) {
self::$courses[$i] = $this->getDataGenerator()->create_course(array('fullname'=>'Test course '.$i, 'shortname'=>'tc'.$i, 'idnumber'=>'courseid'.$i));
for ($i = 1; $i <= 4; $i++) {
self::$courses[$i] = $this->getDataGenerator()->create_course(array('fullname' => 'Test course '.$i, 'shortname' => 'tc'.$i, 'idnumber' => 'courseid'.$i));
}
for($i=1;$i<=10;$i++) {
self::$users[$i] = $this->getDataGenerator()->create_user(array('username'=>'username'.$i, 'idnumber'=>'userid'.$i, 'email'=>'user'.$i.'@example.com'));
for ($i = 1; $i <= 10; $i++) {
self::$users[$i] = $this->getDataGenerator()->create_user(array('username' => 'username'.$i, 'idnumber' => 'userid'.$i, 'email' => 'user'.$i.'@example.com'));
}
foreach (get_all_roles() as $role) {
@ -182,36 +184,43 @@ class enrol_database_testcase extends advanced_testcase {
$DB->delete_records('enrol_database_test_courses', array());
$plugin = enrol_get_plugin('database');
$instances = $DB->get_records('enrol', array('enrol'=>'database'));
$instances = $DB->get_records('enrol', array('enrol' => 'database'));
foreach($instances as $instance) {
$plugin->delete_instance($instance);
}
}
public function assertIsEnrolled($userindex, $courseindex, $status=null, $rolename = null) {
protected function assertIsEnrolled($userindex, $courseindex, $status=null, $rolename = null) {
global $DB;
$dbinstance = $DB->get_record('enrol', array('courseid'=>self::$courses[$courseindex]->id, 'enrol'=>'database'), '*', MUST_EXIST);
$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST);
$conditions = array('enrolid'=>$dbinstance->id, 'userid'=>self::$users[$userindex]->id);
$conditions = array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id);
if ($status !== null) {
$conditions['status'] = $status;
}
$this->assertTrue($DB->record_exists('user_enrolments', $conditions));
$this->assertHasRoleAssignment($userindex, $courseindex, $rolename);
}
protected function assertHasRoleAssignment($userindex, $courseindex, $rolename = null) {
global $DB;
$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'), '*', MUST_EXIST);
$coursecontext = context_course::instance(self::$courses[$courseindex]->id);
if ($rolename === false) {
$this->assertFalse($DB->record_exists('role_assignments', array('component'=>'enrol_database', 'itemid'=>$dbinstance->id, 'userid'=>self::$users[$userindex]->id, 'contextid'=>$coursecontext->id)));
$this->assertFalse($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id)));
} else if ($rolename !== null) {
$this->assertTrue($DB->record_exists('role_assignments', array('component'=>'enrol_database', 'itemid'=>$dbinstance->id, 'userid'=>self::$users[$userindex]->id, 'contextid'=>$coursecontext->id, 'roleid'=>self::$roles[$rolename]->id)));
$this->assertTrue($DB->record_exists('role_assignments', array('component' => 'enrol_database', 'itemid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id, 'contextid' => $coursecontext->id, 'roleid' => self::$roles[$rolename]->id)));
}
}
public function assertIsNotEnrolled($userindex, $courseindex) {
protected function assertIsNotEnrolled($userindex, $courseindex) {
global $DB;
if (!$dbinstance = $DB->get_record('enrol', array('courseid'=>self::$courses[$courseindex]->id, 'enrol'=>'database'))) {
if (!$dbinstance = $DB->get_record('enrol', array('courseid' => self::$courses[$courseindex]->id, 'enrol' => 'database'))) {
return;
}
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid'=>$dbinstance->id, 'userid'=>self::$users[$userindex]->id)));
$this->assertFalse($DB->record_exists('user_enrolments', array('enrolid' => $dbinstance->id, 'userid' => self::$users[$userindex]->id)));
}
public function test_sync_user_enrolments() {
@ -233,20 +242,21 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('defaultrole', self::$roles['student']->id);
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid2', 'roleid'=>'teacher'));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid2', 'courseid'=>'courseid1', 'roleid'=>null));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'xxxxxxx', 'courseid'=>'courseid1', 'roleid'=>'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'xxxxxxxxx', 'roleid'=>'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'teacher'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => null));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored.
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
@ -254,27 +264,37 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$plugin->sync_user_enrolments(self::$users[2]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$plugin->sync_user_enrolments(self::$users[4]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
// Enrolment removals.
$DB->delete_records('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
@ -282,68 +302,76 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsNotEnrolled(1, 1);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$plugin->sync_user_enrolments(self::$users[4]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, false);
// Test all other mapping options.
$this->reset_enrol_database();
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->set_config('localcoursefield', 'id');
$plugin->set_config('localuserfield', 'id');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->id, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->id, 'courseid'=>self::$courses[2]->id, 'roleid'=>self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[2]->id, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
@ -353,14 +381,14 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('localuserfield', 'email');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->email, 'courseid'=>self::$courses[1]->shortname, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->email, 'courseid'=>self::$courses[2]->shortname, 'roleid'=>self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[2]->email, 'courseid'=>self::$courses[1]->shortname, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
@ -370,14 +398,14 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('localuserfield', 'username');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->username, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->username, 'courseid'=>self::$courses[2]->id, 'roleid'=>self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[2]->username, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_user_enrolments(self::$users[1]);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
}
@ -403,123 +431,144 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('localuserfield', 'idnumber');
$plugin->set_config('localrolefield', 'shortname');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid2', 'roleid'=>'editingteacher'));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid2', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'xxxxxxx', 'courseid'=>'courseid1', 'roleid'=>'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'xxxxxxxxx', 'roleid'=>'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid2', 'roleid' => 'editingteacher'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid2', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid4', 'courseid' => 'courseid4', 'roleid' => 'editingteacher', 'otheruser' => '1'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'xxxxxxx', 'courseid' => 'courseid1', 'roleid' => 'student')); // Bogus record to be ignored.
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'xxxxxxxxx', 'roleid' => 'student')); // Bogus record to be ignored.
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$plugin->set_config('defaultrole', self::$roles['teacher']->id);
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid3', 'courseid'=>'courseid3'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid3', 'courseid' => 'courseid3'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
// Test different unenrolment options.
$DB->delete_records('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_KEEP);
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPEND);
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_SUSPENDNOROLES);
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_SUSPENDED, false);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$plugin->set_config('unenrolaction', ENROL_EXT_REMOVED_UNENROL);
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsNotEnrolled(1, 1);
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'teacher'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'student'));
$DB->insert_record('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(6, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
$DB->delete_records('enrol_database_test_enrols', array('userid'=>'userid1', 'courseid'=>'courseid1', 'roleid'=>'teacher'));
$DB->delete_records('enrol_database_test_enrols', array('userid' => 'userid1', 'courseid' => 'courseid1', 'roleid' => 'teacher'));
$plugin->sync_enrolments($trace);
$this->assertEquals(4, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(4, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(4, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(5, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'editingteacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsNotEnrolled(4, 4);
$this->assertHasRoleAssignment(4, 4, 'editingteacher');
$this->assertIsEnrolled(3, 3, ENROL_USER_ACTIVE, 'teacher');
@ -528,21 +577,21 @@ class enrol_database_testcase extends advanced_testcase {
$this->reset_enrol_database();
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->set_config('localcoursefield', 'id');
$plugin->set_config('localuserfield', 'id');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->id, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->id, 'courseid'=>self::$courses[2]->id, 'roleid'=>self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[2]->id, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->id, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->id, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
@ -553,14 +602,14 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('localuserfield', 'email');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->email, 'courseid'=>self::$courses[1]->shortname, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->email, 'courseid'=>self::$courses[2]->shortname, 'roleid'=>self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[2]->email, 'courseid'=>self::$courses[1]->shortname, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->email, 'courseid' => self::$courses[2]->shortname, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->email, 'courseid' => self::$courses[1]->shortname, 'roleid' => self::$roles['student']->id));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
@ -571,14 +620,14 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('localuserfield', 'username');
$plugin->set_config('localrolefield', 'id');
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->username, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->username, 'courseid'=>self::$courses[2]->id, 'roleid'=>self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[2]->username, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$plugin->sync_enrolments($trace);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
@ -588,30 +637,30 @@ class enrol_database_testcase extends advanced_testcase {
$this->reset_enrol_database();
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->username, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[1]->username, 'courseid'=>self::$courses[2]->id, 'roleid'=>self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid'=>self::$users[2]->username, 'courseid'=>self::$courses[1]->id, 'roleid'=>self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[1]->username, 'courseid' => self::$courses[2]->id, 'roleid' => self::$roles['teacher']->id));
$DB->insert_record('enrol_database_test_enrols', array('userid' => self::$users[2]->username, 'courseid' => self::$courses[1]->id, 'roleid' => self::$roles['student']->id));
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(0, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_enrolments($trace, self::$courses[3]->id);
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(1, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(1, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$plugin->sync_enrolments($trace, self::$courses[1]->id);
$this->assertEquals(2, $DB->count_records('user_enrolments', array()));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(2, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(2, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
$plugin->sync_enrolments($trace, self::$courses[2]->id);
$this->assertEquals(3, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(3, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 1, ENROL_USER_ACTIVE, 'student');
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$this->assertIsEnrolled(2, 1, ENROL_USER_ACTIVE, 'student');
@ -623,14 +672,14 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->sync_enrolments($trace, self::$courses[1]->id);
$this->assertEquals(1, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(1, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(1, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
$this->assertIsEnrolled(1, 2, ENROL_USER_ACTIVE, 'teacher');
$plugin->sync_enrolments($trace, self::$courses[2]->id);
$this->assertEquals(0, $DB->count_records('user_enrolments', array()));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol'=>'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component'=>'enrol_database')));
$this->assertEquals(3, $DB->count_records('enrol', array('enrol' => 'database')));
$this->assertEquals(0, $DB->count_records('role_assignments', array('component' => 'enrol_database')));
}
/**
@ -648,16 +697,16 @@ class enrol_database_testcase extends advanced_testcase {
$trace = new null_progress_trace();
$plugin->set_config('localcategoryfield', 'id');
$coursecat = $this->getDataGenerator()->create_category(array('name'=>'Test category 1', 'idnumber'=>'tcid1'));
$defcat = $DB->get_record('course_categories', array('id'=>$plugin->get_config('defaultcategory')));
$coursecat = $this->getDataGenerator()->create_category(array('name' => 'Test category 1', 'idnumber' => 'tcid1'));
$defcat = $DB->get_record('course_categories', array('id' => $plugin->get_config('defaultcategory')));
$course1 = array('fullname'=>'New course 1', 'shortname'=>'nc1', 'idnumber'=>'ncid1', 'category'=>$coursecat->id);
$course2 = array('fullname'=>'New course 2', 'shortname'=>'nc2', 'idnumber'=>'ncid2', 'category'=>null);
$course1 = array('fullname' => 'New course 1', 'shortname' => 'nc1', 'idnumber' => 'ncid1', 'category' => $coursecat->id);
$course2 = array('fullname' => 'New course 2', 'shortname' => 'nc2', 'idnumber' => 'ncid2', 'category' => null);
// Duplicate records are to be ignored.
$course3 = array('fullname'=>'New course 3', 'shortname'=>'xx', 'idnumber'=>'yy2', 'category'=>$defcat->id);
$course4 = array('fullname'=>'New course 4', 'shortname'=>'xx', 'idnumber'=>'yy3', 'category'=>$defcat->id);
$course5 = array('fullname'=>'New course 5', 'shortname'=>'xx1', 'idnumber'=>'yy', 'category'=>$defcat->id);
$course6 = array('fullname'=>'New course 6', 'shortname'=>'xx2', 'idnumber'=>'yy', 'category'=>$defcat->id);
$course3 = array('fullname' => 'New course 3', 'shortname' => 'xx', 'idnumber' => 'yy2', 'category' => $defcat->id);
$course4 = array('fullname' => 'New course 4', 'shortname' => 'xx', 'idnumber' => 'yy3', 'category' => $defcat->id);
$course5 = array('fullname' => 'New course 5', 'shortname' => 'xx1', 'idnumber' => 'yy', 'category' => $defcat->id);
$course6 = array('fullname' => 'New course 6', 'shortname' => 'xx2', 'idnumber' => 'yy', 'category' => $defcat->id);
$DB->insert_record('enrol_database_test_courses', $course1);
$DB->insert_record('enrol_database_test_courses', $course2);
@ -679,14 +728,14 @@ class enrol_database_testcase extends advanced_testcase {
// People should NOT push duplicates there because the results are UNDEFINED! But anyway skip the duplicates.
$this->assertEquals(1, $DB->count_records('course', array('idnumber'=>'yy')));
$this->assertEquals(1, $DB->count_records('course', array('shortname'=>'xx')));
$this->assertEquals(1, $DB->count_records('course', array('idnumber' => 'yy')));
$this->assertEquals(1, $DB->count_records('course', array('shortname' => 'xx')));
// Test category mapping via idnumber.
$plugin->set_config('localcategoryfield', 'idnumber');
$course7 = array('fullname'=>'New course 7', 'shortname'=>'nc7', 'idnumber'=>'ncid7', 'category'=>'tcid1');
$course7 = array('fullname' => 'New course 7', 'shortname' => 'nc7', 'idnumber' => 'ncid7', 'category' => 'tcid1');
$DB->insert_record('enrol_database_test_courses', $course7);
$plugin->sync_courses($trace);
@ -699,10 +748,10 @@ class enrol_database_testcase extends advanced_testcase {
// Test course template.
$template = $this->getDataGenerator()->create_course(array('numsections'=>666, 'shortname'=>'crstempl'));
$template = $this->getDataGenerator()->create_course(array('numsections' => 666, 'shortname' => 'crstempl'));
$plugin->set_config('templatecourse', 'crstempl');
$course8 = array('fullname'=>'New course 8', 'shortname'=>'nc8', 'idnumber'=>'ncid8', 'category'=>null);
$course8 = array('fullname' => 'New course 8', 'shortname' => 'nc8', 'idnumber' => 'ncid8', 'category' => null);
$DB->insert_record('enrol_database_test_courses', $course8);
$plugin->sync_courses($trace);
@ -715,11 +764,11 @@ class enrol_database_testcase extends advanced_testcase {
// Test invalid category.
$course9 = array('fullname'=>'New course 9', 'shortname'=>'nc9', 'idnumber'=>'ncid9', 'category'=>'xxxxxxx');
$course9 = array('fullname' => 'New course 9', 'shortname' => 'nc9', 'idnumber' => 'ncid9', 'category' => 'xxxxxxx');
$DB->insert_record('enrol_database_test_courses', $course9);
$plugin->sync_courses($trace);
$this->assertEquals(2+1+4+1+count(self::$courses), $DB->count_records('course'));
$this->assertFalse($DB->record_exists('course', array('idnumber'=>'ncid9')));
$this->assertFalse($DB->record_exists('course', array('idnumber' => 'ncid9')));
// Test when categories not specified.
@ -727,7 +776,7 @@ class enrol_database_testcase extends advanced_testcase {
$plugin->set_config('newcoursecategory', '');
$plugin->sync_courses($trace);
$this->assertEquals(1+2+1+4+1+count(self::$courses), $DB->count_records('course'));
$this->assertTrue($DB->record_exists('course', array('idnumber'=>'ncid9')));
$this->assertTrue($DB->record_exists('course', array('idnumber' => 'ncid9')));
// Final cleanup - remove extra tables, fixtures and caches.