Skip to content

Commit

Permalink
Merged improved database plugin for enrolment
Browse files Browse the repository at this point in the history
  • Loading branch information
moodler committed Sep 23, 2004
1 parent 4ebae17 commit a3224cc
Showing 1 changed file with 45 additions and 24 deletions.
69 changes: 45 additions & 24 deletions enrol/database/enrol.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,66 @@ class enrolment_plugin extends enrolment_base {
function get_student_courses(&$user) {
global $CFG;

parent::get_student_courses($user);

parent::get_student_courses($user); /// Start with the list of known enrolments
/// If the database fails we can at least use this

// This is a hack to workaround what seems to be a bug in ADOdb with accessing
// two databases of the same kind ... it seems to get confused when trying to access
// the first database again, after having accessed the second.
// The following hack will make the database explicit which keeps it happy

if (strpos($CFG->prefix, $CFG->dbname) === false) {
$CFG->prefix = "$CFG->dbname.$CFG->prefix";
}


// Connect to the external database
// Try to connect to the external database

$enroldb = &ADONewConnection($CFG->enrol_dbtype);

if ($enroldb->PConnect($CFG->enrol_dbhost,$CFG->enrol_dbuser,$CFG->enrol_dbpass,$CFG->enrol_dbname)) {

foreach ($user->student as $courseid=>$value) {

/// Get the value of the local course field
$localcoursevalue = get_field("course", $CFG->enrol_localcoursefield, "id", $courseid);

/// Find a record in the external database that matches the local course field and local user field
/// to the respective remote fields
$rs = $enroldb->Execute("SELECT * FROM $CFG->enrol_dbtable
WHERE $CFG->enrol_remotecoursefield = '$localcoursevalue'
AND $CFG->enrol_remoteuserfield = '{$user->$CFG->enrol_localuserfield}' ");

/// If no records existed then student has been unenrolled externally.
/// Unenrol locally and remove entry from the $user->student array
if (! ($rs->RecordCount()) ) {
unenrol_student($user->id, $courseid);
unset ($user->student[$courseid]);
$courselist = array(); /// Initialise new array
$newstudent = array();

/// Get the authoritative list of enrolments from the database

$useridnumber = $user->{$CFG->enrol_localuserfield};


if ($rs = $enroldb->Execute("SELECT $CFG->enrol_remotecoursefield
FROM $CFG->enrol_dbtable
WHERE $CFG->enrol_remoteuserfield = '$useridnumber' ")) {

if ($rs->RecordCount() > 0) {
while (!$rs->EOF) {
$courselist[] = $rs->fields[0];
$rs->MoveNext();
}

foreach ($courselist as $coursefield) {
if ($course = get_record('course', $CFG->enrol_localcoursefield, $coursefield)) {
$newstudent[$course->id] = true; /// Add it to new list
if (isset($user->student[$course->id])) { /// We have it already
unset($user->student[$course->id]); /// Remove from old list
} else {
enrol_student($user->id, $course->id); /// Enrol the student
}
}
}
}
}

$enroldb->Close();
if (!empty($user->student)) { /// We have some courses left that we need to unenrol from
foreach ($user->student as $courseid => $value) {
unenrol_student($user->id, $courseid); /// Unenrol the student
unset($user->student[$course->id]); /// Remove from old list
}
}

$user->student = $newstudent; /// Overwrite the array with the new one
}

$enroldb->Close();
}

}


Expand Down Expand Up @@ -87,7 +108,7 @@ function get_access_icons($course) {
}


/// Overrise the base config_form() function
/// Overide the base config_form() function
function config_form($frm) {
global $CFG;
include("$CFG->dirroot/enrol/database/config.html");
Expand Down

0 comments on commit a3224cc

Please sign in to comment.