From 6051c68d4b56fb0e1aef10562ede35bc040d6152 Mon Sep 17 00:00:00 2001 From: moodler Date: Tue, 19 Sep 2006 16:29:43 +0000 Subject: [PATCH] Deleted the old students-based function and created the new-style setup_enrolments() function that uses roles to do the tasks required when a user logs in. Other enrolment plugins should use this as an example/guide The sync parts of this plugin are not yet fixed. --- enrol/database/config.html | 24 +++++++++- enrol/database/enrol.php | 96 +++++++++++++++++++++++++------------- 2 files changed, 86 insertions(+), 34 deletions(-) diff --git a/enrol/database/config.html b/enrol/database/config.html index 1de83de103..8d27945849 100644 --- a/enrol/database/config.html +++ b/enrol/database/config.html @@ -123,7 +123,29 @@ - + + + + + enrol_db_defaultcourseroleid: + + get_string('default')) + $assignableroles; + + choose_from_menu($assignableroles, 'enrol_db_defaultcourseroleid', $frm->enrol_db_defaultcourseroleid, ''); + if (isset($err['enrol_db_defaultcourseroleid'])) formerr($err['enrol_db_defaultcourseroleid']); + ?> + + + + + + + + + diff --git a/enrol/database/enrol.php b/enrol/database/enrol.php index c4415802b4..55d4b54a31 100644 --- a/enrol/database/enrol.php +++ b/enrol/database/enrol.php @@ -1,66 +1,91 @@ dirroot/enrol/enrol.class.php"); -require_once("$CFG->dirroot/course/lib.php"); -require_once("$CFG->dirroot/lib/blocklib.php"); -require_once("$CFG->dirroot/lib/pagelib.php"); +require_once($CFG->dirroot.'/enrol/enrol.class.php'); class enrolment_plugin_database { var $log; -/// Overide the base get_student_courses() function -function get_student_courses(&$user) { +/* + * For the given user, let's go out and look in an external database + * for an authoritative list of enrolments, and then adjust the + * local Moodle assignments to match. + */ +function setup_enrolments(&$user) { global $CFG; // NOTE: if $this->enrol_connect() succeeds you MUST remember to call // $this->enrol_disconnect() as it is doing some nasty vodoo with $CFG->prefix if ($enroldb = $this->enrol_connect()) { - $courselist = array(); /// Initialise new array - $newstudent = array(); - - /// Get the authoritative list of enrolments from the database - - $useridnumber = $user->{$CFG->enrol_localuserfield}; + /// Get the authoritative list of enrolments from the external database table + /// We're using the ADOdb functions natively here and not our datalib functions + /// because we didn't want to mess with the $db global + $useridfield = $user->{$CFG->enrol_localuserfield}; if ($rs = $enroldb->Execute("SELECT $CFG->enrol_remotecoursefield FROM $CFG->enrol_dbtable - WHERE $CFG->enrol_remoteuserfield = '$useridnumber' ")) { + WHERE $CFG->enrol_remoteuserfield = '$useridfield' ")) { + + $existing = get_my_courses($user->id, '', 'id'); // We'll use this to see what to add and remove - if ($rs->RecordCount() > 0) { - while (!$rs->EOF) { + if ($rs->RecordCount() > 0) { // We found some courses + + $courselist = array(); + while (!$rs->EOF) { // Make a nice little array of courses to process $courselist[] = $rs->fields[0]; $rs->MoveNext(); } - foreach ($courselist as $coursefield) { + foreach ($courselist as $coursefield) { /// Check the list of courses against existing if ($course = get_record('course', $CFG->enrol_localcoursefield, $coursefield)) { - $newstudent[$course->id] = 'database'; /// 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, 0, 0, 'database'); /// Enrol the student + + if (isset($existing[$course->id])) { // Already enrolled so remove from checklist + unset($existing[$course->id]); + + } else { /// Not enrolled yet so let's do enrol them + + if ($context = get_context_instance(CONTEXT_COURSE, $course->id)) { // Get the context + $role = NULL; + + /// Check if a particular role has been forced by the plugin site-wide + if ($CFG->enrol_db_defaultcourseroleid) { + $role = get_record('role', 'id', $CFG->enrol_db_defaultcourseroleid); + } + + /// Otherwise, we get the default course role (usually student) + if (empty($role)) { + $role = get_default_course_role($course); + } + + /// If we have a role now then assign it + if ($role) { + role_assign($role->id, $user->id, 0, $context->id, 0, 0, 0, 'database'); + } + } } } } - } + } // We've processed all external courses found + + if (!empty($existing)) { - if (!empty($user->student)) { /// We have some courses left that we need to unenrol from - foreach ($user->student as $courseid => $value) { + /// We have some courses left that we might need to unenrol from + /// Note: we only process enrolments that we (ie 'database' plugin) made - // unenrol only if it's a record pulled from external db - if ($value == 'database') { - unenrol_student($user->id, $courseid); /// Unenrol the student - unset($user->student[$course->id]); /// Remove from old list - } else { - $newstudent[$courseid] = $value; + foreach ($existing as $course) { + if ($context = get_context_instance(CONTEXT_COURSE, $course->id)) { // Get the context + if ($roles = get_user_roles($context, $user->id, false)) { // User has some roles here + foreach ($roles as $role) { + if ($role->enrol == 'database') { // Yes! It's one of ours + role_unassign($role->id, $user->id, '', $context->id); + } + } + } } } } - - $user->student = $newstudent; /// Overwrite the array with the new one } $this->enrol_disconnect($enroldb); } // end if (enroldb=connect) @@ -363,8 +388,13 @@ function process_config($config) { $config->enrol_db_template = ''; } set_config('enrol_db_template', $config->enrol_db_template); - return true; + if (!isset($config->enrol_db_defaultcourseroleid)) { + $config->enrol_db_defaultcourseroleid = ''; + } + set_config('enrol_db_defaultcourseroleid', $config->enrol_db_defaultcourseroleid); + + return true; } // will create the moodle course from the template -- 2.39.5