From f41ef63ee6cebfbc89ff63953d6f9cc3e01383a1 Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Tue, 16 Aug 2005 23:15:58 +0000 Subject: [PATCH] materialized path & depth for course categories. --- lib/datalib.php | 30 ++++++++++++++++++++++++------ lib/db/mysql.php | 5 +++++ lib/db/mysql.sql | 2 ++ lib/db/postgres7.php | 6 +++++- lib/db/postgres7.sql | 4 +++- version.php | 2 +- 6 files changed, 40 insertions(+), 9 deletions(-) diff --git a/lib/datalib.php b/lib/datalib.php index bf4b1548f0a..ddf60c35978 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -2330,20 +2330,38 @@ function get_categories($parent='none', $sort='sortorder ASC') { * $safe (bool) prevents it from assuming category-sortorder is unique, used to upgrade * safely from 1.4 to 1.5 */ -function fix_course_sortorder($categoryid=0, $n=0, $safe=0) { - +function fix_course_sortorder($categoryid=0, $n=0, $safe=0, $depth=0, $path='') { + global $CFG; $count = 0; - $catgap = 1000; # "standard" category gap - $tolerance = 200; # how "close" categories can get + $catgap = 1000; // "standard" category gap + $tolerance = 200; // how "close" categories can get + + if ($categoryid > 0){ + // update depth and path + $cat = get_record('course_categories', 'id', $categoryid); + if ($cat->parent == 0) { + $depth = 0; + $path = ''; + } else if ($depth == 0 ) { // doesn't make sense; get from DB + // this is only called if the $depth parameter looks dodgy + $parent = get_record('course_categories', 'id', $cat->parent); + $path = $parent->path; + $depth = $parent->depth; + } + $path = $path . '/' . $categoryid; + $depth = $depth + 1; + set_field('course_categories', 'path', addslashes($path), 'id', $categoryid); + set_field('course_categories', 'depth', $depth, 'id', $categoryid); + } // get some basic info about courses in the category $info = get_record_sql('SELECT MIN(sortorder) AS min, MAX(sortorder) AS max, - COUNT(sortorder) AS count + COUNT(sortorder) AS count FROM ' . $CFG->prefix . 'course WHERE category=' . $categoryid); if (is_object($info)) { // no courses? @@ -2417,7 +2435,7 @@ function fix_course_sortorder($categoryid=0, $n=0, $safe=0) { if ($categories = get_categories($categoryid)) { foreach ($categories as $category) { - $n = fix_course_sortorder($category->id, $n, $safe); + $n = fix_course_sortorder($category->id, $n, $safe, $depth, $path); } } diff --git a/lib/db/mysql.php b/lib/db/mysql.php index a6786d9185b..c8daab58ce1 100644 --- a/lib/db/mysql.php +++ b/lib/db/mysql.php @@ -1520,6 +1520,11 @@ function main_upgrade($oldversion=0) { table_column('course','','restrictmodules','int','1','','0','not null'); } + if ($oldversion < 2005081700) { + table_column('course_categories','','depth','integer'); + table_column('course_categories','','path','varchar','255'); + } + return $result; } diff --git a/lib/db/mysql.sql b/lib/db/mysql.sql index 5066243218d..dc54181cbd0 100644 --- a/lib/db/mysql.sql +++ b/lib/db/mysql.sql @@ -99,6 +99,8 @@ CREATE TABLE `prefix_course_categories` ( `coursecount` int(10) unsigned NOT NULL default '0', `visible` tinyint(1) NOT NULL default '1', `timemodified` int(10) unsigned NOT NULL default '0', + `depth` int(10) unsigned NOT NULL default '0', + `path` varchar(255) NOT NULL default '', PRIMARY KEY (`id`), UNIQUE KEY `id` (`id`) ) TYPE=MyISAM COMMENT='Course categories'; diff --git a/lib/db/postgres7.php b/lib/db/postgres7.php index a853e93f356..08c75f84ad3 100644 --- a/lib/db/postgres7.php +++ b/lib/db/postgres7.php @@ -1261,8 +1261,12 @@ function main_upgrade($oldversion=0) { modify_database('','CREATE INDEX prefix_course_allowed_modules_course_idx ON prefix_course_allowed_modules (course);'); modify_database('','CREATE INDEX prefix_course_allowed_modules_module_idx ON prefix_course_allowed_modules (module);'); table_column('course','','restrictmodules','int','1','','0','not null'); - } + } + if ($oldversion < 2005081700) { + table_column('course_categories','','depth','integer'); + table_column('course_categories','','path','varchar','255'); + } return $result; } diff --git a/lib/db/postgres7.sql b/lib/db/postgres7.sql index efc6014a01b..53f31cfca44 100644 --- a/lib/db/postgres7.sql +++ b/lib/db/postgres7.sql @@ -64,7 +64,9 @@ CREATE TABLE prefix_course_categories ( sortorder integer NOT NULL default '0', coursecount integer NOT NULL default '0', visible integer NOT NULL default '1', - timemodified integer NOT NULL default '0' + timemodified integer NOT NULL default '0', + depth integer NOT NULL default '0', + path varchar(255) NOT NULL default '' ); CREATE TABLE prefix_course_display ( diff --git a/version.php b/version.php index 6da314ce209..509fb3e4a2f 100644 --- a/version.php +++ b/version.php @@ -6,7 +6,7 @@ // This is compared against the values stored in the database to determine // whether upgrades should be performed (see lib/db/*.php) - $version = 2005081601; // YYYYMMDD = date + $version = 2005081700; // YYYYMMDD = date // XY = increments within a single day $release = '1.6 development'; // Human-friendly version name