From 74da3c2c895c89df977a98e970d13a2add72fcb6 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Thu, 9 Mar 2006 03:15:42 +0000 Subject: [PATCH] [multienrol]Improved get_student/teacher_courses() functions to cache enrolment type that saves lots of recursive database query --- enrol/database/enrol.php | 6 +++--- enrol/internal/enrol.php | 4 ++-- enrol/ldap/enrol.php | 11 ++++------- 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/enrol/database/enrol.php b/enrol/database/enrol.php index 0b8f3bc9ad..c4415802b4 100644 --- a/enrol/database/enrol.php +++ b/enrol/database/enrol.php @@ -37,7 +37,7 @@ function get_student_courses(&$user) { 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 { @@ -51,11 +51,11 @@ function get_student_courses(&$user) { 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; } } } diff --git a/enrol/internal/enrol.php b/enrol/internal/enrol.php index 0800229539..9ebbfbb28c 100644 --- a/enrol/internal/enrol.php +++ b/enrol/internal/enrol.php @@ -59,7 +59,7 @@ function get_student_courses(&$user) { 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; } } @@ -89,7 +89,7 @@ function get_teacher_courses(&$user) { 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; diff --git a/enrol/ldap/enrol.php b/enrol/ldap/enrol.php index ea37bc7c46..0664da8f57 100755 --- a/enrol/ldap/enrol.php +++ b/enrol/ldap/enrol.php @@ -73,7 +73,7 @@ function get_user_courses(&$user, $type) { 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]); @@ -83,7 +83,7 @@ function get_user_courses(&$user, $type) { // 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; @@ -104,11 +104,8 @@ function get_user_courses(&$user, $type) { // 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') { -- 2.39.5