From 824f1c40fccc5bedf4b519fffc91f7396ac970b0 Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Tue, 17 Jan 2006 23:46:42 +0000 Subject: [PATCH] cache category_parent_visible so as to avoid doing millions of database queries --- lib/datalib.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/datalib.php b/lib/datalib.php index 6b55715882f..e10b4564844 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -3129,19 +3129,33 @@ function course_parent_visible($course = null) { } function category_parent_visible($parent = 0) { + + static $visible; + if (!$parent) { return true; } + + if (empty($visible)) { + $visible = array(); // initialize + } + + if (array_key_exists($parent,$visible)) { + return $visible[$parent]; + } + $category = get_record('course_categories', 'id', $parent); $list = explode('/', preg_replace('/^\/(.*)$/', '$1', $category->path)); $list[] = $parent; $parents = get_records_list('course_categories', 'id', implode(',', $list), 'depth DESC'); - foreach ($parents as $parent) { - if (!$parent->visible) { - return false; + $v = true; + foreach ($parents as $p) { + if (!$p->visible) { + $v = false; } } - return true; + $visible[$parent] = $v; // now cache it + return $v; } // vim:autoindent:expandtab:shiftwidth=4:tabstop=4:tw=140: