[multienrol]Improved get_student/teacher_courses() functions to cache enrolment type that saves lots of recursive database query

This commit is contained in:
martinlanghoff
2006-03-09 03:15:42 +00:00
parent 1919f9926e
commit 74da3c2c89
3 changed files with 9 additions and 12 deletions
+3 -3
View File
@@ -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;
}
}
}
+2 -2
View File
@@ -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;
+4 -7
View File
@@ -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') {