From 8a4d32d8a32038c27ef10c93ff18be7471084025 Mon Sep 17 00:00:00 2001 From: skodak Date: Wed, 10 Oct 2007 08:33:06 +0000 Subject: [PATCH] MDL-11688 makes sure all course and user contexts exist during upgrade (all contexts when going from in 1.6) - this is needed for grade_letter upgrade; other minor tweaks - the USER->access is now loaded automatically, feedback parameters added --- admin/index.php | 2 +- admin/upgradesettings.php | 6 ----- lib/accesslib.php | 50 ++++++++++++++++++++------------------- lib/db/upgrade.php | 16 +++++++++---- 4 files changed, 38 insertions(+), 36 deletions(-) diff --git a/admin/index.php b/admin/index.php index c597fbe992..6d97e525a2 100644 --- a/admin/index.php +++ b/admin/index.php @@ -517,7 +517,7 @@ /// Set up the admin user if (empty($CFG->rolesactive)) { - build_context_path(); + build_context_path(); // just in case - should not be needed create_admin_user(); } diff --git a/admin/upgradesettings.php b/admin/upgradesettings.php index 96e1265bcc..c011d20332 100644 --- a/admin/upgradesettings.php +++ b/admin/upgradesettings.php @@ -8,12 +8,6 @@ require_once($CFG->libdir.'/adminlib.php'); $return = optional_param('return', '', PARAM_ALPHA); -// Technically, we shouldn't need it, but during the -// v1.9 accesslib upgrade _sometimes_ USER->access -// isn't set. -if (!isset($USER->access)) { - load_all_capabilities(); -} admin_externalpage_setup('upgradesettings'); // now hidden page // a caveat: we're depending on only having one admin access this page at once. why? the following line diff --git a/lib/accesslib.php b/lib/accesslib.php index 6c06fb31ac..765851eea5 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -2319,9 +2319,10 @@ function delete_context($contextlevel, $instanceid) { * Precreates all contexts including all parents * @param int $contextlevel, empty means all * @param bool $buildpaths update paths and depths + * @param bool $feedback show sql feedback * @return void */ -function create_contexts($contextlevel=null, $buildpaths=true) { +function create_contexts($contextlevel=null, $buildpaths=true, $feedback=false) { global $CFG; //make sure system context exists @@ -2337,7 +2338,7 @@ function create_contexts($contextlevel=null, $buildpaths=true) { WHERE NOT EXISTS (SELECT 'x' FROM {$CFG->prefix}context cx WHERE cc.id = cx.instanceid AND cx.contextlevel=".CONTEXT_COURSECAT.")"; - execute_sql($sql, false); + execute_sql($sql, $feedback); } @@ -2350,7 +2351,7 @@ function create_contexts($contextlevel=null, $buildpaths=true) { WHERE NOT EXISTS (SELECT 'x' FROM {$CFG->prefix}context cx WHERE c.id = cx.instanceid AND cx.contextlevel=".CONTEXT_COURSE.")"; - execute_sql($sql, false); + execute_sql($sql, $feedback); } @@ -2361,7 +2362,7 @@ function create_contexts($contextlevel=null, $buildpaths=true) { WHERE NOT EXISTS (SELECT 'x' FROM {$CFG->prefix}context cx WHERE cm.id = cx.instanceid AND cx.contextlevel=".CONTEXT_MODULE.")"; - execute_sql($sql, false); + execute_sql($sql, $feedback); } if (empty($contextlevel) or $contextlevel == CONTEXT_BLOCK) { @@ -2371,7 +2372,7 @@ function create_contexts($contextlevel=null, $buildpaths=true) { WHERE NOT EXISTS (SELECT 'x' FROM {$CFG->prefix}context cx WHERE bi.id = cx.instanceid AND cx.contextlevel=".CONTEXT_BLOCK.")"; - execute_sql($sql, false); + execute_sql($sql, $feedback); } if (empty($contextlevel) or $contextlevel == CONTEXT_USER) { @@ -2382,12 +2383,12 @@ function create_contexts($contextlevel=null, $buildpaths=true) { AND NOT EXISTS (SELECT 'x' FROM {$CFG->prefix}context cx WHERE u.id = cx.instanceid AND cx.contextlevel=".CONTEXT_USER.")"; - execute_sql($sql, false); + execute_sql($sql, $feedback); } if ($buildpaths) { - build_context_path(false); + build_context_path(false, $feedback); } } @@ -4644,9 +4645,10 @@ function component_level_changed($cap, $comp, $contextlevel) { /** * Populate context.path and context.depth where missing. * @param bool $force force a complete rebuild of the path and depth fields. + * @param bool $feedback display feedback (during upgrade usually) * @return void */ -function build_context_path($force=false) { +function build_context_path($force=false, $feedback=false) { global $CFG; require_once($CFG->libdir.'/ddllib.php'); @@ -4722,9 +4724,9 @@ function build_context_path($force=false) { AND cc.depth=1) $emptyclause"; - execute_sql($sql, $force); + execute_sql($sql, $feedback); - execute_sql($udelsql, $force); + execute_sql($udelsql, $feedback); // Deeper categories - one query per depthlevel $maxdepth = get_field_sql("SELECT MAX(depth) @@ -4742,11 +4744,11 @@ function build_context_path($force=false) { FROM {$CFG->prefix}context_temp temp WHERE temp.id = ctx.id) $ctxemptyclause"; - execute_sql($sql, $force); + execute_sql($sql, $feedback); } - execute_sql($updatesql, $force); - execute_sql($udelsql, $force); + execute_sql($updatesql, $feedback); + execute_sql($udelsql, $feedback); // Courses -- except sitecourse $sql = "INSERT INTO {$CFG->prefix}context_temp (id, path, depth) @@ -4761,10 +4763,10 @@ function build_context_path($force=false) { FROM {$CFG->prefix}context_temp temp WHERE temp.id = ctx.id) $ctxemptyclause"; - execute_sql($sql, $force); + execute_sql($sql, $feedback); - execute_sql($updatesql, $force); - execute_sql($udelsql, $force); + execute_sql($updatesql, $feedback); + execute_sql($udelsql, $feedback); // Module instances $sql = "INSERT INTO {$CFG->prefix}context_temp (id, path, depth) @@ -4778,10 +4780,10 @@ function build_context_path($force=false) { FROM {$CFG->prefix}context_temp temp WHERE temp.id = ctx.id) $ctxemptyclause"; - execute_sql($sql, $force); + execute_sql($sql, $feedback); - execute_sql($updatesql, $force); - execute_sql($udelsql, $force); + execute_sql($updatesql, $feedback); + execute_sql($udelsql, $feedback); // Blocks - non-pinned course-view only $sql = "INSERT INTO {$CFG->prefix}context_temp (id, path, depth) @@ -4796,10 +4798,10 @@ function build_context_path($force=false) { FROM {$CFG->prefix}context_temp temp WHERE temp.id = ctx.id) $ctxemptyclause"; - execute_sql($sql, $force); + execute_sql($sql, $feedback); - execute_sql($updatesql, $force); - execute_sql($udelsql, $force); + execute_sql($updatesql, $feedback); + execute_sql($udelsql, $feedback); // Blocks - others $sql = "UPDATE {$CFG->prefix}context @@ -4810,7 +4812,7 @@ function build_context_path($force=false) { WHERE bi.id = {$CFG->prefix}context.instanceid AND bi.pagetype!='course-view') $emptyclause "; - execute_sql($sql, $force); + execute_sql($sql, $feedback); // User $sql = "UPDATE {$CFG->prefix}context @@ -4820,7 +4822,7 @@ function build_context_path($force=false) { FROM {$CFG->prefix}user u WHERE u.id = {$CFG->prefix}context.instanceid) $emptyclause "; - execute_sql($sql, $force); + execute_sql($sql, $feedback); // Personal TODO diff --git a/lib/db/upgrade.php b/lib/db/upgrade.php index a31e18652d..7eb056cc4d 100644 --- a/lib/db/upgrade.php +++ b/lib/db/upgrade.php @@ -1301,9 +1301,6 @@ function xmldb_main_upgrade($oldversion=0) { rs_close($rs); } } - - /// migrate grade letter table - $result = $result && upgrade_18_letters(); } if ($result && $oldversion < 2007072400) { @@ -1973,8 +1970,16 @@ function xmldb_main_upgrade($oldversion=0) { /// Recalculate depths, paths and so on if (!empty($CFG->rolesactive)) { cleanup_contexts(); - build_context_path(true); + // make sure all course, category and user contexts exist - we need it for grade letter upgrade, etc. + create_contexts(CONTEXT_COURSE, false, true); + create_contexts(CONTEXT_USER, false, true); + // we need all contexts path/depths filled properly + build_context_path(true, true); load_all_capabilities(); + + } else { + // upgrade from 1.6 - build all contexts + create_contexts(null, true, true); } } @@ -2175,10 +2180,11 @@ function xmldb_main_upgrade($oldversion=0) { execute_sql("UPDATE {$CFG->prefix}grade_items SET display=0 WHERE display=-1"); } +/// migrade grade letters - we can not do this in normal grades upgrade becuase we need all course contexts if ($result && $oldversion < 2007092806) { require_once($CFG->libdir.'/db/upgradelib.php'); - $result = upgrade_18_letters(); // executes on dev sites only + $result = upgrade_18_letters(); /// Define index contextidlowerboundary (not unique) to be added to grade_letters $table = new XMLDBTable('grade_letters'); -- 2.39.5