]> git.mjollnir.org Git - moodle.git/commitdiff
Fixed up get_my_courses().
authormoodler <moodler>
Thu, 21 Aug 2003 17:24:40 +0000 (17:24 +0000)
committermoodler <moodler>
Thu, 21 Aug 2003 17:24:40 +0000 (17:24 +0000)
It's not a tidy SQL statement any more but it's MUCH MUCH faster
with large data sets.

lib/datalib.php

index 569ba3bf9398a85ad417fded782dcae8c3eeb3e5..9bc9732066796a2e46da16c7a2ee93a1149c1274 100644 (file)
@@ -1185,17 +1185,40 @@ function get_courses_page($categoryid="all", $sort="c.sortorder ASC", $fields="c
 }
 
 
-function get_my_courses($userid, $sort="c.fullname ASC") {
+function get_my_courses($userid, $sort="fullname ASC") {
+
     global $CFG;
 
-    return get_records_sql("SELECT c.* 
-                              FROM {$CFG->prefix}course c, 
-                                   {$CFG->prefix}user_students s, 
-                                   {$CFG->prefix}user_teachers t 
-                             WHERE (s.userid = '$userid' AND s.course = c.id)
-                                OR (t.userid = '$userid' AND t.course = c.id)
-                             GROUP BY c.id 
-                             ORDER BY $sort");
+    $course = array();
+
+    if ($students = get_records("user_students", "userid", $userid, "", "id, course")) {
+        foreach ($students as $student) {
+            $course[$student->course] = $student->course;
+        }
+    }
+    if ($teachers = get_records("user_teachers", "userid", $userid, "", "id, course")) {
+        foreach ($teachers as $teacher) {
+            $course[$teacher->course] = $teacher->course;
+        }
+    }
+    if (empty($course)) {
+        return $course;
+    }
+
+    $courseids = implode(',', $course);
+
+    return get_records_list("course", "id", $courseids, $sort);
+
+//  The following is correct but VERY slow with large datasets
+//
+//    return get_records_sql("SELECT c.* 
+//                              FROM {$CFG->prefix}course c, 
+//                                   {$CFG->prefix}user_students s, 
+//                                   {$CFG->prefix}user_teachers t 
+//                             WHERE (s.userid = '$userid' AND s.course = c.id)
+//                                OR (t.userid = '$userid' AND t.course = c.id)
+//                             GROUP BY c.id 
+//                             ORDER BY $sort");
 }