Skip to content

Commit

Permalink
MDL-25659 fixed borked creation of enrol instances enrol/database, fi…
Browse files Browse the repository at this point in the history
…xed detection of misconfigured sync
  • Loading branch information
skodak committed Dec 13, 2010
1 parent 12c3ba9 commit 883deb3
Showing 1 changed file with 52 additions and 33 deletions.
85 changes: 52 additions & 33 deletions enrol/database/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,43 +271,19 @@ public function sync_enrolments() {
$roles[$role->$localrolefield] = $role->id;
}

// first find all existing courses with enrol instance
$sql = "SELECT c.id, c.visible, c.$localcoursefiled AS mapping, e.id AS enrolid
FROM {course} c
JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'database')";
$existing = array();
$rs = $DB->get_recordset_sql($sql); // watch out for idnumber duplicates
foreach ($rs as $course) {
if (empty($course->mapping)) {
continue;
}
$existing[$course->mapping] = $course;
}
$rs->close();

// add necessary enrol instances that are not present yet;
// get a list of courses to be synced that are in external table
$externalcourses = array();
$sql = $this->db_get_sql($table, array(), array($coursefield), true);
if ($rs = $extdb->Execute($sql)) {
if (!$rs->EOF) {
$sql = "SELECT c.id, c.visible
FROM {course} c
JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'database')
WHERE c.$localcoursefiled = :mapping";
$params = array();
while ($mapping = $rs->FetchRow()) {
$mapping = reset($mapping);
$mapping = $this->db_decode($mapping);
if (!empty($mapping) and !isset($existing[$mapping])) {
$params['mapping'] = $mapping;
if ($course = $DB->get_record_sql($sql, $params, IGNORE_MULTIPLE)) {
$new = new stdClass();
$new->id = $course->id;
$new->visible = $course->visible;
$new->mapping = $mapping;
$new->enrolid = $this->add_instance($course);
$existing[$mapping] = $new;
}
if (empty($mapping)) {
// invalid mapping
continue;
}
$externalcourses[$mapping] = true;
}
}
$rs->Close();
Expand All @@ -316,6 +292,50 @@ public function sync_enrolments() {
$extdb->Close();
return;
}
$preventfullunenrol = empty($externalcourses);
if ($preventfullunenrol and $unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
debugging('Preventing unenrolment of all current users, because it might result in major data loss, there has to be at least one record in external enrol table, sorry.');
}

// first find all existing courses with enrol instance
$existing = array();
$sql = "SELECT c.id, c.visible, c.$localcoursefiled AS mapping, e.id AS enrolid
FROM {course} c
JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'database')";
$rs = $DB->get_recordset_sql($sql); // watch out for idnumber duplicates
foreach ($rs as $course) {
if (empty($course->mapping)) {
continue;
}
$existing[$course->mapping] = $course;
}
$rs->close();

// add necessary enrol instances that are not present yet
$sql = "SELECT c.id, c.visible, c.$localcoursefiled AS mapping
FROM {course} c
LEFT JOIN {enrol} e ON (e.courseid = c.id AND e.enrol = 'database')
WHERE e.id IS NULL AND c.$localcoursefiled <> ?";
$rs = $DB->get_recordset_sql($sql, array($DB->sql_empty()));
foreach ($rs as $course) {
if (empty($course->mapping)) {
continue;
}
if (!isset($externalcourses[$course->mapping])) {
// course not synced
continue;
}
if (isset($existing[$course->mapping])) {
// some duplicate, sorry
continue;
}
$course->enrolid = $this->add_instance($course);
$existing[$course->mapping] = $course;
}
$rs->close();

// free memory
unset($externalcourses);

// sync enrolments
$ignorehidden = $this->get_config('ignorehiddencourses');
Expand Down Expand Up @@ -426,9 +446,8 @@ public function sync_enrolments() {

// deal with enrolments removed from external table
if ($unenrolaction == ENROL_EXT_REMOVED_UNENROL) {
// unenrol
if (!empty($requested_roles)) {
// we might get some error or connection problem, better not unenrol everybody
if (!$preventfullunenrol) {
// unenrol
foreach ($current_status as $userid=>$status) {
if (isset($requested_roles[$userid])) {
continue;
Expand Down

0 comments on commit 883deb3

Please sign in to comment.