foreach ($courselist as $coursefield) {
if ($course = get_record('course', $CFG->enrol_localcoursefield, $coursefield)) {
- $newstudent[$course->id] = true; /// Add it to new list
+ $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 {
foreach ($user->student as $courseid => $value) {
// unenrol only if it's a record pulled from external db
- if (get_record('user_students', 'userid', $user->id, 'course', $courseid, 'enrol', 'database')) {
+ if ($value == 'database') {
unenrol_student($user->id, $courseid); /// Unenrol the student
unset($user->student[$course->id]); /// Remove from old list
} else {
- $newstudent[$courseid] = true;
+ $newstudent[$courseid] = $value;
}
}
}
if ( ( $student->timestart == 0 or ( $currenttime > $student->timestart )) and
( $student->timeend == 0 or ( $currenttime < $student->timeend )) ) {
- $user->student[$student->course] = true;
+ $user->student[$student->course] = $student->enrol;
$user->timeaccess[$student->course] = $student->timeaccess;
}
}
if ( ( $teacher->timestart == 0 or ( $currenttime > $teacher->timestart )) and
( $teacher->timeend == 0 or ( $currenttime < $teacher->timeend )) ) {
- $user->teacher[$teacher->course] = true;
+ $user->teacher[$teacher->course] = $teacher->enrol;
if ($teacher->editall) {
$user->teacheredit[$teacher->course] = true;
error_log("User $user->username enrolled to a nonexistant course $course_ext_id \n");
}
} else { // the course object exists before we call...
- if ($course_obj->visible==0) {
+ if ($course_obj->visible==0 && $user->{$type}[$course_obj->id] == 'ldap') {
// non-visible courses don't show up in the enrolled
// array, so we should skip them --
unset($user->{$type}[$course_obj->id]);
// deal with enrolment in the moodle db
if (!empty($course_obj)) { // does course exist now?
- if(isset($user->{$type}[$course_obj->id])){
+ if(isset($user->{$type}[$course_obj->id]) && $user->{$type}[$course_obj->id] == 'ldap'){
unset($user->{$type}[$course_obj->id]); // remove from old list
} else {
$CFG->debug=10;
// ok, if there's any thing still left in the $user->student or $user->teacher
// array, those are old enrolments that we want to remove (or are they?)
if(!empty($user->{$type})){
- $courses = array_keys($user->{$type});
- foreach ($courses as $courseid){
- // get the record if it's ldap
- $rec = get_record('user_' . $type . 's', 'userid', $user->id, 'course', $courseid, 'enrol', 'ldap');
- if(!empty($rec)){ // this was a legacy
+ foreach ($user->{$type} as $courseid => $value){
+ if($value == 'ldap'){ // this was a legacy
if ($type === 'student') { // enrol
unenrol_student($user->id, $courseid);
} else if ($type === 'teacher') {