Index: course/lib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/course/lib.php,v
retrieving revision 1.541
diff -u -r1.541 lib.php
--- course/lib.php	27 Nov 2007 01:56:21 -0000	1.541
+++ course/lib.php	6 Dec 2007 17:16:03 -0000
@@ -1580,6 +1580,32 @@
 }
 
 
+function get_child_categories($parent) {
+/// Returns an array of the children categories for the given category
+/// ID by caching all of the categories in a static hash
+
+    static $allcategories = null;
+
+    // only fill in this variable the first time
+    if (null == $allcategories) {
+        $allcategories = array();
+
+        $categories = get_categories();
+        foreach ($categories as $category) {
+            if (empty($allcategories[$category->parent])) {
+                $allcategories[$category->parent] = array();
+            }
+            $allcategories[$category->parent][] = $category;
+        }
+    }
+
+    if (empty($allcategories[$parent])) {
+        return array();
+    } else {
+        return $allcategories[$parent];
+    }
+}
+
 
 function make_categories_list(&$list, &$parents, $category=NULL, $path="") {
 /// Given an empty array, this function recursively travels the
@@ -1605,7 +1631,7 @@
         $category->id = 0;
     }
 
-    if ($categories = get_categories($category->id)) {   // Print all the children recursively
+    if ($categories = get_child_categories($category->id)) {   // Print all the children recursively
         foreach ($categories as $cat) {
             if (!empty($category->id)) {
                 if (isset($parents[$category->id])) {
@@ -1643,7 +1669,7 @@
         $category->id = "0";
     }
 
-    if ($categories = get_categories($category->id)) {   // Print all the children recursively
+    if ($categories = get_child_categories($category->id)) {   // Print all the children recursively
         $countcats = count($categories);
         $count = 0;
         $first = true;
@@ -1782,7 +1808,7 @@
     global $CFG;
 
     if (!is_object($category) && $category==0) {
-        $categories = get_categories(0);  // Parent = 0   ie top-level categories only
+        $categories = get_child_categories(0);  // Parent = 0   ie top-level categories only
         if (is_array($categories) && count($categories) == 1) {
             $category   = array_shift($categories);
             $courses    = get_courses_wmanagers($category->id, 
