Index: lib/datalib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/datalib.php,v
retrieving revision 1.371
diff -u -r1.371 datalib.php
--- lib/datalib.php	13 Apr 2007 05:40:11 -0000	1.371
+++ lib/datalib.php	17 Apr 2007 04:36:06 -0000
@@ -944,19 +944,24 @@
  * @return array of category ids.
  */
 function get_all_subcategories($catid) {
-
-    $subcats = array();
-
-    if ($categories = get_records('course_categories', 'parent', $catid)) {
-        foreach ($categories as $cat) {
-            array_push($subcats, $cat->id);
-            $subcats = array_merge($subcats, get_all_subcategories($cat->id));
-        }
+    global $CFG;
+    $concat_string = sql_concat('c1.path', "'/%'");
+    $qry = '
+            SELECT DISTINCT
+                c2.*
+            FROM
+                '.$CFG->prefix.'course_categories c1,
+                '.$CFG->prefix.'course_categories c2
+            WHERE
+                c2.path like '.$concat_string.' AND
+                c1.id='.$catid;
+    $subcats = get_records_sql($qry);
+    if (empty($records)) {
+        $subcats = array();
     }
     return $subcats;
 }
 
-
 /**
 * This recursive function makes sure that the courseorder is consecutive
 *
Index: lib/accesslib.php
===================================================================
RCS file: /cvsroot/moodle/moodle/lib/accesslib.php,v
retrieving revision 1.253
diff -u -r1.253 accesslib.php
--- lib/accesslib.php	9 Apr 2007 11:11:31 -0000	1.253
+++ lib/accesslib.php	17 Apr 2007 04:36:05 -0000
@@ -1685,9 +1685,9 @@
         $context->instanceid = $instanceid;
         if ($id = insert_record('context',$context)) {
             // we need to populate context_rel for every new context inserted
-            $c = get_record('context','id',$id);
-            insert_context_rel ($c);           
-            return $c;
+            $context->id = $id;
+            insert_context_rel ($context);
+            return $context;
         } else {
             debugging('Error: could not insert new context level "'.s($contextlevel).'", instance "'.s($instanceid).'".');
             return NULL;
@@ -2881,6 +2881,7 @@
  * @return array()
  */
 function get_parent_contexts($context) {
+    global $context_cache, $context_cache_id;
 
     static $pcontexts; // cache
     if (isset($pcontexts[$context->id])) {
@@ -2914,20 +2915,57 @@
         break;
 
         case CONTEXT_COURSECAT: // Coursecat -> coursecat or site
+            /*
+             * This behaviour is strange... if the course category doesn't exist
+             * we return an empty array... not an error? What should happen?
+
             if (!$coursecat = get_record('course_categories','id',$context->instanceid)) {
                 return array();
             }
-            if (!empty($coursecat->parent)) { // return parent value if exist
-                $parent = get_context_instance(CONTEXT_COURSECAT, $coursecat->parent);
-                $res = array_merge(array($parent->id), get_parent_contexts($parent));
-                $pcontexts[$context->id] = $res;
-                return $res;
-            } else { // else return site value
-                $parent = get_context_instance(CONTEXT_SYSTEM);
-                $res = array($parent->id);
-                $pcontexts[$context->id] = $res;
-                return $res;
+             */
+            $concat_string = sql_concat('c2.path', "'/%'");
+
+            $qry = '
+                    SELECT DISTINCT
+                        c2.*,
+                        x.id as contextid,
+                        x.contextlevel,
+                        x.instanceid 
+                    FROM
+                        '.$CFG->prefix.'course_categories c1,
+                        '.$CFG->prefix.'course_categories c2
+                    LEFT JOIN
+                        '.$CFG->prefix.'context x
+                    ON
+                        x.instanceid = c2.id AND
+                        x.contextlevel = \''.CONTEXT_COURSECAT.'\'
+                    WHERE
+                        c1.path like '.$concat_string.' AND
+                        c1.id='.$context->instanceid;
+
+            $records = get_records_sql($qry);
+
+            if (empty($records)) {
+                $records = array();
+            }
+            foreach($records as $category) {
+                if(empty($category->contextid)) {
+                    $context = create_context(CONTEXT_COURSECAT, $category->id);
+                } else {
+                    $context = new stdClass();
+                    $context->id           = $category->contextid;
+                    $context->contextlevel = $category->contextlevel;
+                    $context->instanceid   = $category->instanceid;
+                }
+                /// Only add to cache if context isn't empty.
+                if (!empty($context)) {
+                    $context_cache[CONTEXT_COURSECAT][$category->id] = $context;    // Cache it for later
+                    $context_cache_id[$context->id] = $context;      // Cache it for later
+                }
             }
+            $res = array_merge(get_context_instance(CONTEXT_SYSTEM), $records);
+            $pcontexts[$context->id] = $res;
+            return $res;
         break;
 
         case CONTEXT_COURSE: // 1 to 1 to course cat
