}
+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
$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])) {
$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;
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,