]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-11521 fixed problems when creating contexts and aprents does not exist yet
authorskodak <skodak>
Fri, 5 Oct 2007 15:06:38 +0000 (15:06 +0000)
committerskodak <skodak>
Fri, 5 Oct 2007 15:06:38 +0000 (15:06 +0000)
MDL-11527 fixed warning from $CFG->defaultuserroleid when installing and upgrading
+ removed reusing of previous contexts in has_capability() and get_context_instance()  because it was hiding critical errors when context was false, not specified, etc.
+ tweaked roles defaults - faster and defaults are applied when roles exist
+ SYSCONTEXTID may be specified in config.php to eliminate 1 db query
+ static context cache is now reset after rebuilding of context paths
+ path field in context table is now nullable - pg compatibility requirement
+ other minor fixes

admin/settings/users.php
lib/accesslib.php
lib/db/install.xml
lib/db/mysql.php
lib/db/postgres7.php
lib/db/upgrade.php
lib/setup.php
lib/weblib.php
version.php

index 1ae5e0951deb51e0603cc67636f9c8bb88bc76e2..4d1802d9ef4ccaa0e275509d7ec3271c6f440cba 100644 (file)
@@ -2,6 +2,8 @@
 
 // This file defines settingpages and externalpages under the "users" category
 
+if (has_capability('moodle/site:config',$systemcontext)) { // speedup for non-admins
+
 
 $ADMIN->add('users', new admin_externalpage('userauthentication', get_string('authentication','admin'), "$CFG->wwwroot/$CFG->admin/auth.php"));
 
@@ -30,56 +32,62 @@ $ADMIN->add('roles', new admin_externalpage('assignroles', get_string('assignglo
 // "userpolicies" settingpage
 $temp = new admin_settingpage('userpolicies', get_string('userpolicies', 'admin'));
 
-$context = get_context_instance(CONTEXT_SYSTEM);
-if (!$guestrole = get_guest_role()) {
-    $guestrole->id = 0;
-}
-if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
-    $studentrole = array_shift($studentroles);   /// Take the first one
-} else {
-    $studentrole->id = 0;
-}
-if ($userroles = get_roles_with_capability('moodle/legacy:user', CAP_ALLOW)) {
-    $userrole = array_shift($userroles);   /// Take the first one
-} else {
-    $userrole->id = 0;
-}
-if (empty($CFG->creatornewroleid)) {
-    if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, $context)) {
-        $teachereditrole = array_shift($teacherroles);
-        set_config('creatornewroleid', $teachereditrole->id);
+if (!empty($CFG->rolesactive)) {
+    $context = get_context_instance(CONTEXT_SYSTEM);
+    if (!$guestrole = get_guest_role()) {
+        $guestrole->id = 0;
+    }
+    if ($studentroles = get_roles_with_capability('moodle/legacy:student', CAP_ALLOW)) {
+        $studentrole = array_shift($studentroles);   /// Take the first one
     } else {
-        set_config('creatornewroleid', 0);
+        $studentrole->id = 0;
     }
-}
-// we must not use assignable roles here:
-//   1/ unsetting roles as assignable for admin might bork the settings!
-//   2/ default user role should not be assignable anyway
-$allroles = array();
-if ($roles = get_all_roles()) {
-    foreach ($roles as $role) {
-        $allroles[$role->id] = strip_tags(format_string($role->name, true));
+    if ($userroles = get_roles_with_capability('moodle/legacy:user', CAP_ALLOW)) {
+        $userrole = array_shift($userroles);   /// Take the first one
+    } else {
+        $userrole->id = 0;
+    }
+    if (empty($CFG->creatornewroleid)) {
+        if ($teacherroles = get_roles_with_capability('moodle/legacy:editingteacher', CAP_ALLOW, $context)) {
+            $teachereditrole = array_shift($teacherroles);
+            set_config('creatornewroleid', $teachereditrole->id);
+        } else {
+            set_config('creatornewroleid', 0);
+        }
+    }
+    // we must not use assignable roles here:
+    //   1/ unsetting roles as assignable for admin might bork the settings!
+    //   2/ default user role should not be assignable anyway
+    $allroles = array();
+    if ($roles = get_all_roles()) {
+        foreach ($roles as $role) {
+            $allroles[$role->id] = strip_tags(format_string($role->name, true));
+        }
     }
-}
 
-$temp->add(new admin_setting_configselect('notloggedinroleid', get_string('notloggedinroleid', 'admin'),
-              get_string('confignotloggedinroleid', 'admin'), $guestrole->id, $allroles ));
-$temp->add(new admin_setting_configselect('guestroleid', get_string('guestroleid', 'admin'),
-              get_string('configguestroleid', 'admin'), $guestrole->id, $allroles));
-$temp->add(new admin_setting_configselect('defaultuserroleid', get_string('defaultuserroleid', 'admin'),
-              get_string('configdefaultuserroleid', 'admin'), $userrole->id, $allroles));
+    $temp->add(new admin_setting_configselect('notloggedinroleid', get_string('notloggedinroleid', 'admin'),
+                  get_string('confignotloggedinroleid', 'admin'), $guestrole->id, $allroles ));
+    $temp->add(new admin_setting_configselect('guestroleid', get_string('guestroleid', 'admin'),
+                  get_string('configguestroleid', 'admin'), $guestrole->id, $allroles));
+    $temp->add(new admin_setting_configselect('defaultuserroleid', get_string('defaultuserroleid', 'admin'),
+                  get_string('configdefaultuserroleid', 'admin'), $userrole->id, $allroles));
+}
 
 $temp->add(new admin_setting_configcheckbox('nodefaultuserrolelists', get_string('nodefaultuserrolelists', 'admin'), get_string('confignodefaultuserrolelists', 'admin'), 0));
 
-$temp->add(new admin_setting_configselect('defaultcourseroleid', get_string('defaultcourseroleid', 'admin'),
-              get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $allroles));
-$temp->add(new admin_setting_configselect('creatornewroleid', get_string('creatornewroleid', 'admin'),
-              get_string('configcreatornewroleid', 'admin'), $CFG->creatornewroleid, $allroles));
+if (!empty($CFG->rolesactive)) {
+    $temp->add(new admin_setting_configselect('defaultcourseroleid', get_string('defaultcourseroleid', 'admin'),
+                  get_string('configdefaultcourseroleid', 'admin'), $studentrole->id, $allroles));
+    $temp->add(new admin_setting_configselect('creatornewroleid', get_string('creatornewroleid', 'admin'),
+                  get_string('configcreatornewroleid', 'admin'), $CFG->creatornewroleid, $allroles));
+}
 
 $temp->add(new admin_setting_configcheckbox('autologinguests', get_string('autologinguests', 'admin'), get_string('configautologinguests', 'admin'), 0));
 
-$temp->add(new admin_setting_configmultiselect('nonmetacoursesyncroleids', get_string('nonmetacoursesyncroleids', 'admin'),
+if (!empty($CFG->rolesactive)) {
+    $temp->add(new admin_setting_configmultiselect('nonmetacoursesyncroleids', get_string('nonmetacoursesyncroleids', 'admin'),
               get_string('confignonmetacoursesyncroleids', 'admin'), array(), $allroles));
+}
 
 //$temp->add(new admin_setting_configcheckbox('allusersaresitestudents', get_string('allusersaresitestudents', 'admin'), get_string('configallusersaresitestudents','admin'), 1));
 $temp->add(new admin_setting_configmultiselect('hiddenuserfields', get_string('hiddenuserfields', 'admin'),
@@ -100,5 +108,6 @@ $temp->add(new admin_setting_configmultiselect('hiddenuserfields', get_string('h
 
 $ADMIN->add('roles', $temp);
 
+} // end of admin tree capability if
 
 ?>
index 470b47201f8ef59a42682673a4dd377ae5a199b0..7854a00758695f225444f434cec65af99fe047e1 100755 (executable)
@@ -283,27 +283,6 @@ function get_default_frontpage_role_access($roleid, $accessdata=NULL) {
 }
 
 
-/**
- * Get the id for the not-logged-in role - or set it up if needed
- * @return bool
- */
-function get_notloggedin_roleid($return=false) {
-    global $CFG, $USER;
-
-    if (empty($CFG->notloggedinroleid)) {    // Let's set the default to the guest role
-        if ($role = get_guest_role()) {
-            set_config('notloggedinroleid', $role->id);
-            return $role->id;
-        } else {
-            return false;
-        }
-    } else {
-        return $CFG->notloggedinroleid;
-    }
-
-    return (get_record('role','id', $CFG->notloggedinas));
-}
-
 /**
  * Get the default guest role
  * @return object role
@@ -331,19 +310,14 @@ function get_guest_role() {
     }
 }
 
-function has_capability($capability, $context=NULL, $userid=NULL, $doanything=true) {
-    global $USER, $CONTEXT, $ACCESS, $CFG, $DIRTYCONTEXTS;
+function has_capability($capability, $context, $userid=NULL, $doanything=true) {
+    global $USER, $ACCESS, $CFG, $DIRTYCONTEXTS;
 
-    /// Make sure we know the current context
-    if (empty($context)) {              // Use default CONTEXT if none specified
-        if (empty($CONTEXT)) {
-            return false;
-        } else {
-            $context = $CONTEXT;
-        }
-    }
-    if (empty($CONTEXT)) {
-        $CONTEXT = $context;
+    // the original $CONTEXT here was hiding serious errors
+    // for security reasons do notreuse previous context
+    if (empty($context)) {
+        debugging('Incorrect context specified');
+        return false;
     }
 
     if (is_null($userid) || $userid===0) {
@@ -365,10 +339,10 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
     }
 
     if ($USER->id === 0 && !isset($USER->access)) {
+        // not-logged-in user first time here
         load_all_capabilities();
-    }
 
-    if (defined('FULLME') && FULLME === 'cron' && !isset($USER->access)) {
+    } else if (defined('FULLME') && FULLME === 'cron' && !isset($USER->access)) {
         //
         // In cron, some modules setup a 'fake' $USER,
         // ensure we load the appropriate accessdata.
@@ -382,6 +356,11 @@ function has_capability($capability, $context=NULL, $userid=NULL, $doanything=tr
         }
         $USER->access = $ACCESS[$userid];
         $DIRTYCONTEXTS = array();
+
+    } else if ($USER->id === $userid && !isset($USER->access)) {
+        // caps not loaded yet - better to load them to keep BC with 1.8
+        // probably $USER object set up manually
+        load_all_capabilities();
     }
 
     // Careful check for staleness...
@@ -1214,7 +1193,7 @@ function get_user_access_sitewide($userid) {
     //
     $raparents = array();
     $lastseen  = '';
-    if ($rs->RecordCount()) {
+    if ($rs and $rs->RecordCount()) {
         while ($ra = rs_fetch_next_record($rs)) {
             // RAs leafs are arrays to support multi
             // role assignments...
@@ -1547,13 +1526,15 @@ function load_user_accessdata($userid) {
     //
     // provide "default role" & set 'dr'
     //
-    $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
-    if (!isset($accessdata['ra'][$base])) {
-        $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
-    } else {
-        array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+    if (!empty($CFG->defaultuserroleid)) {
+        $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
+        if (!isset($accessdata['ra'][$base])) {
+            $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
+        } else {
+            array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+        }
+        $accessdata['dr'] = $CFG->defaultuserroleid;
     }
-    $accessdata['dr'] = $CFG->defaultuserroleid;
 
     //
     // provide "default frontpage role"
@@ -1598,13 +1579,15 @@ function load_all_capabilities() {
         //
         // provide "default role" & set 'dr'
         //
-        $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
-        if (!isset($accessdata['ra'][$base])) {
-            $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
-        } else {
-            array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+        if (!empty($CFG->defaultuserroleid)) {
+            $accessdata = get_role_access($CFG->defaultuserroleid, $accessdata);
+            if (!isset($accessdata['ra'][$base])) {
+                $accessdata['ra'][$base] = array($CFG->defaultuserroleid);
+            } else {
+                array_push($accessdata['ra'][$base], $CFG->defaultuserroleid);
+            }
+            $accessdata['dr'] = $CFG->defaultuserroleid;
         }
-        $accessdata['dr'] = $CFG->defaultuserroleid;
 
         $frontpagecontext = get_context_instance(CONTEXT_COURSE, SITEID);
 
@@ -1623,11 +1606,9 @@ function load_all_capabilities() {
         } 
         $USER->access = $accessdata;
     
-    } else {
-        if ($roleid = get_notloggedin_roleid()) {
-            $USER->access = get_role_access($roleid);
-            $USER->access['ra'][$base] = array($roleid);
-        }
+    } else if (!empty($CFG->notloggedinroleid)) {
+        $USER->access = get_role_access($CFG->notloggedinroleid);
+        $USER->access['ra'][$base] = array($CFG->notloggedinroleid);
     }
 
     // Timestamp to read 
@@ -1991,11 +1972,13 @@ function moodle_install_roles() {
 
 /// Delete the old user tables when we are done
 
-    drop_table(new XMLDBTable('user_students'));
-    drop_table(new XMLDBTable('user_teachers'));
-    drop_table(new XMLDBTable('user_coursecreators'));
-    drop_table(new XMLDBTable('user_admins'));
-
+    $tables = array('user_students', 'user_teachers', 'user_coursecreators', 'user_admins');  
+    foreach ($tables as $tablename) {
+        $table = new XMLDBTable($tablename);
+        if (table_exists($table)) {
+            drop_table($table);
+        }
+    }
 }
 
 /**
@@ -2115,6 +2098,8 @@ function create_context($contextlevel, $instanceid) {
     $basepath  = '/' . SYSCONTEXTID;
     $basedepth = 1;
 
+    $result = true;
+
     switch ($contextlevel) {
         case CONTEXT_COURSECAT:
             $sql = "SELECT ctx.path, ctx.depth 
@@ -2125,6 +2110,20 @@ function create_context($contextlevel, $instanceid) {
             if ($p = get_record_sql($sql)) {
                 $basepath  = $p->path;
                 $basedepth = $p->depth;
+            } else if ($category = get_record('course_categories', 'id', $instanceid)) {
+                if (empty($category->parent)) {
+                    // ok - this is a top category
+                } else if ($parent = get_context_instance(CONTEXT_COURSECAT, $category->parent)) {
+                    $basepath  = $parent->path;
+                    $basedepth = $parent->depth;
+                } else {
+                    // wrong parent category - no big deal, this can be fixed later
+                    $basepath  = null;
+                    $basedepth = 0;
+                }
+            } else {
+                // incorrect category id
+                $result = false;
             }
             break;
 
@@ -2137,6 +2136,23 @@ function create_context($contextlevel, $instanceid) {
             if ($p = get_record_sql($sql)) {
                 $basepath  = $p->path;
                 $basedepth = $p->depth;
+            } else if ($course = get_record('course', 'id', $instanceid)) {
+                if ($course->id == SITEID) {
+                    //ok - no parent category
+                } else if ($parent = get_context_instance(CONTEXT_COURSECAT, $course->category)) {
+                    $basepath  = $parent->path;
+                    $basedepth = $parent->depth;
+                } else {
+                    // wrong parent category of course - no big deal, this can be fixed later
+                    $basepath  = null;
+                    $basedepth = 0;
+                }
+            } else if ($instanceid == SITEID) {
+                // no errors for missing site course during installation
+                return false;
+            } else {
+                // incorrect course id
+                $result = false;
             }
             break;
 
@@ -2146,9 +2162,21 @@ function create_context($contextlevel, $instanceid) {
                     JOIN {$CFG->prefix}course_modules    cm
                       ON (cm.course=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.")
                     WHERE cm.id={$instanceid}";
-            $p = get_record_sql($sql);
-            $basepath  = $p->path;
-            $basedepth = $p->depth;
+            if ($p = get_record_sql($sql)) {
+                $basepath  = $p->path;
+                $basedepth = $p->depth;
+            } else if ($cm = get_record('course_modules', 'id', $instanceid)) {
+                if ($parent = get_context_instance(CONTEXT_COURSE, $cm->course)) {
+                    $basepath  = $parent->path;
+                    $basedepth = $parent->depth;
+                } else {
+                    // course does not exist - modules can not exist without a course
+                    $result = false;
+                }
+            } else {
+                // cm does not exist
+                $result = false;
+            }
             break;
 
         case CONTEXT_BLOCK:
@@ -2161,6 +2189,19 @@ function create_context($contextlevel, $instanceid) {
             if ($p = get_record_sql($sql)) {
                 $basepath  = $p->path;
                 $basedepth = $p->depth;
+            } else if ($bi = get_record('block_instance', 'id', $instanceid)) {
+                if ($bi->pagetype != 'course-view') {
+                    // ok - not a course block
+                } else if ($parent = get_context_instance(CONTEXT_COURSE, $bi->pageid)) {
+                    $basepath  = $parent->path;
+                    $basedepth = $parent->depth;
+                } else {
+                    // parent course does not exist - course blocks can not exist without a course
+                    $result = false;
+                }
+            } else {
+                // block does not exist
+                $result = false;
             }
             break;
         case CONTEXT_USER:
@@ -2171,44 +2212,75 @@ function create_context($contextlevel, $instanceid) {
             break;
     }
 
-    $context->depth = $basedepth+1;
+    // if grandparents unknown, maybe rebuild_context_path() will solve it later
+    if ($basedepth != 0) {
+        $context->depth = $basedepth+1;
+    }
+
+    if ($result and $id = insert_record('context', $context)) {
+        // can't set the full path till we know the id!
+        if ($basedepth != 0 and !empty($basepath)) {
+            set_field('context', 'path', $basepath.'/'. $id, 'id', $id);
+        }
+        return get_context_instance_by_id($id);
 
-    if ($id = insert_record('context',$context)) {
-        // can't set the path till we know the id!
-        set_field('context', 'path', $basepath . '/' . $id,
-                  'id', $id);
-        $c = get_context_instance_by_id($id);
-        return $c;
     } else {
         debugging('Error: could not insert new context level "'.
                   s($contextlevel).'", instance "'.
                   s($instanceid).'".');
-        return NULL;
+        return false;
     }
 }
 
 /**
  * This hacky function is needed because we can not change system context instanceid using normal upgrade routine.
  */
-function create_system_context() {
-    if ($context = get_record('context', 'contextlevel', CONTEXT_SYSTEM, 'instanceid', SITEID)) {
-        // we are going to change instanceid of system context to 0 now
-        $context->instanceid = 0;
-        update_record('context', $context);
-        //context rel not affected
-        return $context;
+function get_system_context($cache=true) {
+    static $cached = null;
+    if ($cache and defined('SYSCONTEXTID')) {
+        if (is_null($cached)) {
+            $cached = new object();
+            $cached->id           = SYSCONTEXTID;
+            $cached->contextlevel = CONTEXT_SYSTEM;
+            $cached->instanceid   = 0;
+            $cached->path         = '/'.SYSCONTEXTID;
+            $cached->depth        = 1;
+        }
+        return $cached;
+    }
 
-    } else {
+    if (!$context = get_record('context', 'contextlevel', CONTEXT_SYSTEM)) {
         $context = new object();
         $context->contextlevel = CONTEXT_SYSTEM;
-        $context->instanceid = 0;
-        if ($context->id = insert_record('context',$context)) {
-            return $context;
-        } else {
+        $context->instanceid   = 0;
+        $context->depth        = 1;
+        $context->path         = NULL; //not known before insert
+
+        if (!$context->id = insert_record('context', $context)) {
+            // better something than nothing - let's hope it will work somehow
+            if (!defined('SYSCONTEXTID')) {
+                define('SYSCONTEXTID', 1);
+            }
             debugging('Can not create system context');
-            return NULL;
+            $context->id   = SYSCONTEXTID;
+            $context->path = '/'.SYSCONTEXTID;
+            return $context;
         }
     }
+
+    if (!isset($context->depth) or $context->depth != 1 or $context->instanceid != 0 or $context->path != '/'.$context->id) {
+        $context->instanceid   = 0;
+        $context->path         = '/'.$context->id;
+        $context->depth        = 1;
+        update_record('context', $context);
+    }
+
+    if (!defined('SYSCONTEXTID')) {
+        define('SYSCONTEXTID', $context->id);
+    }
+
+    $cached = $context;
+    return $cached;
 }
 /**
  * Remove a context record and any dependent entries
@@ -2303,9 +2375,9 @@ function cleanup_contexts() {
  *      for $level = CONTEXT_MODULE, this would be $cm->id. And so on.
  * @return object The context object.
  */
-function get_context_instance($contextlevel=NULL, $instance=0) {
+function get_context_instance($contextlevel, $instance=0) {
 
-    global $context_cache, $context_cache_id, $CONTEXT;
+    global $context_cache, $context_cache_id;
     static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_PERSONAL, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK);
 
     if ($contextlevel === 'clearcache') {
@@ -2317,19 +2389,9 @@ function get_context_instance($contextlevel=NULL, $instance=0) {
         return false;
     }
 
-/// If no level is supplied then return the current global context if there is one
-    if (empty($contextlevel)) {
-        if (empty($CONTEXT)) {
-            //fatal error, code must be fixed
-            error("Error: get_context_instance() called without a context");
-        } else {
-            return $CONTEXT;
-        }
-    }
-
-/// Backwards compatibility with obsoleted (CONTEXT_SYSTEM, SITEID)
+/// System context has special cache
     if ($contextlevel == CONTEXT_SYSTEM) {
-        $instance = 0;
+        return get_system_context();
     }
 
 /// check allowed context levels
@@ -2345,8 +2407,7 @@ function get_context_instance($contextlevel=NULL, $instance=0) {
 
 /// Get it from the database, or create it
     if (!$context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance)) {
-        create_context($contextlevel, $instance);
-        $context = get_record('context', 'contextlevel', $contextlevel, 'instanceid', $instance);
+        $context = create_context($contextlevel, $instance);
     }
 
 /// Only add to cache if context isn't empty.
@@ -2368,6 +2429,10 @@ function get_context_instance_by_id($id) {
 
     global $context_cache, $context_cache_id;
 
+    if ($id == SYSCONTEXTID) {
+        return get_system_context();
+    }
+
     if (isset($context_cache_id[$id])) {  // Already cached
         return $context_cache_id[$id];
     }
@@ -4504,9 +4569,9 @@ function build_context_path($force=false) {
                                     'instanceid', SITEID);
     }
 
-    $ctxemptyclause = " AND (ctx.depth IS NULL
+    $ctxemptyclause = " AND (ctx.path IS NULL
                               OR ctx.depth=0) ";
-    $emptyclause    = " AND ({$CFG->prefix}context.depth IS NULL
+    $emptyclause    = " AND ({$CFG->prefix}context.path IS NULL
                               OR {$CFG->prefix}context.depth=0) ";
     if ($force) {
         $ctxemptyclause = $emptyclause = '';
@@ -4655,6 +4720,12 @@ function build_context_path($force=false) {
     // Personal TODO
 
     //TODO: fix group contexts
+
+    // reset static course cache - it might have incorrect cached data
+    global $context_cache, $context_cache_id;
+    $context_cache    = array();
+    $context_cache_id = array();
+
 }
 
 /**
index 38995d47a009f30ceea89e3e4a6718ee8854875b..813cb996ce5ce60245939b408e5bec8b2e2f79bd 100644 (file)
         <FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" SEQUENCE="true" ENUM="false" NEXT="contextlevel"/>
         <FIELD NAME="contextlevel" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="id" NEXT="instanceid"/>
         <FIELD NAME="instanceid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="contextlevel" NEXT="path"/>
-        <FIELD NAME="path" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" ENUM="false" PREVIOUS="instanceid" NEXT="depth"/>
+        <FIELD NAME="path" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" ENUM="false" PREVIOUS="instanceid" NEXT="depth"/>
         <FIELD NAME="depth" TYPE="int" LENGTH="2" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" ENUM="false" PREVIOUS="path"/>
       </FIELDS>
       <KEYS>
index 756ada266421766ef990d203eb2f6fde81ad606d..ea2dcf4eae71868e82a698fe3cecaceee01ba36b 100644 (file)
@@ -2195,7 +2195,7 @@ function main_upgrade($oldversion=0) {
                     )TYPE=MYISAM COMMENT ='time user last accessed any page in a course';", true);
     }
 
-    if ($oldversion < 2006091212) {   // Reload the guest roles completely with new defaults
+    if (!empty($CFG->rolesactive) and $oldversion < 2006091212) {   // Reload the guest roles completely with new defaults
         if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
             delete_records('capabilities');
             $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
@@ -2218,7 +2218,7 @@ function main_upgrade($oldversion=0) {
         delete_records('config', 'name', 'requestedstudentsname');
     }
 
-    if ($oldversion < 2006091901) {
+    if (!empty($CFG->rolesactive) and $oldversion < 2006091901) {
         if ($roles = get_records('role')) {
             $first = array_shift($roles);
             if (!empty($first->shortname)) {
index 2884a86c329b061cdde3ca066e7f67105e0c8670..fdec2bcde1ccb46481219ba66219ad759f241e9f 100644 (file)
@@ -1794,7 +1794,7 @@ function main_upgrade($oldversion=0) {
     
     }
 
-    if ($oldversion < 2006091212) {   // Reload the guest roles completely with new defaults
+    if (!empty($CFG->rolesactive) and $oldversion < 2006091212) {   // Reload the guest roles completely with new defaults
         if ($guestroles = get_roles_with_capability('moodle/legacy:guest', CAP_ALLOW)) {
             delete_records('capabilities');
             $sitecontext = get_context_instance(CONTEXT_SYSTEM, SITEID);
@@ -1817,7 +1817,7 @@ function main_upgrade($oldversion=0) {
         delete_records('config', 'name', 'requestedstudentsname');
     }
 
-    if ($oldversion < 2006091901) {
+    if (!empty($CFG->rolesactive) and $oldversion < 2006091901) {
         if ($roles = get_records('role')) {
             $first = array_shift($roles);
             if (!empty($first->shortname)) {
index 7caf209a75d14dd6cd9bdcbce1c121214f000f86..18232947fb219e24eba695d32d03dea5e20203f3 100644 (file)
@@ -1509,12 +1509,7 @@ function xmldb_main_upgrade($oldversion=0) {
         $result = $result && add_key($table, $key);
     }
 
-    if ($result && $oldversion < 2007080101) {
-        if ($firstadmin = get_admin()) {  // the person currently used for support emails
-            set_config('supportname',  s(fullname($firstadmin)));   // New settings same as old
-            set_config('supportemail',  s($firstadmin->email));
-        }
-    }
+    /// originally there was supportname and supportemail upgrade code - this is handled in upgradesettings.php instead
 
     /// MDL-10679, context_rel clean up
     if ($result && $oldversion < 2007080200) {
@@ -1580,7 +1575,7 @@ function xmldb_main_upgrade($oldversion=0) {
     /// Add context.path & index
         $table = new XMLDBTable('context');
         $field = new XMLDBField('path');
-        $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, XMLDB_NOTNULL, null, null, null, null, 'instanceid');
+        $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
         $result = $result && add_field($table, $field);
         $table = new XMLDBTable('context');
         $index = new XMLDBIndex('path');
@@ -1592,6 +1587,9 @@ function xmldb_main_upgrade($oldversion=0) {
         $field = new XMLDBField('depth');
         $field->setAttributes(XMLDB_TYPE_INTEGER, '2', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, null, null, '0', 'path');
         $result = $result && add_field($table, $field);
+
+    /// make sure the system context has proper data
+        get_system_context(false);
     }
 
     if ($result && $oldversion < 2007080903) {
@@ -2039,9 +2037,11 @@ function xmldb_main_upgrade($oldversion=0) {
         $result = $result && create_table($table);
 
     /// Recalculate depths, paths and so on
-        cleanup_contexts();
-        build_context_path(true);
-        load_all_capabilities();
+        if (!empty($CFG->rolesactive)) {
+            cleanup_contexts();
+            build_context_path(true);
+            load_all_capabilities();
+        }
     }
 
     /**
@@ -2342,6 +2342,18 @@ function xmldb_main_upgrade($oldversion=0) {
         $result = $result && change_field_notnull($table, $field);
     }
 
+    if ($result && $oldversion < 2007100500) {
+    /// for dev sites - it is ok to do this repeatedly
+
+    /// Changing nullability of field path on table context to null
+        $table = new XMLDBTable('context');
+        $field = new XMLDBField('path');
+        $field->setAttributes(XMLDB_TYPE_CHAR, '255', null, null, null, null, null, null, 'instanceid');
+
+    /// Launch change of nullability for field path
+        $result = $result && change_field_notnull($table, $field);
+    }
+
 
 /* NOTE: please keep this at the end of upgrade file for now ;-)
     /// drop old gradebook tables
index 53ce9e4b59f4770eee8ecc6acd80c9b43d9ada27..d1fcc9dd502726f21808e5625189de7c2cad80b1 100644 (file)
@@ -249,10 +249,9 @@ global $HTTPSPAGEREQUIRED;
         $COURSE->id = 1;
     }
 
-    if ($sysctxid = get_field('context', 'id', 'contextlevel', CONTEXT_SYSTEM)) {
-        define('SYSCONTEXTID', $sysctxid);
-    } else {
-        define('SYSCONTEXTID', 1);
+    // define SYSCONTEXTID in config.php if you want to save some queries (after install or upgrade!)
+    if (!defined('SYSCONTEXTID')) {
+        get_system_context();
     }
 
 /// Set error reporting back to normal
index 9f5abfc763f2f676ac8e062f018042d417ce0a6c..5510d2296fc2fc3f29acbc92de5ade0f0e3ff6e2 100644 (file)
@@ -6294,7 +6294,7 @@ function convert_tabrows_to_tree($tabrows, $selected, $inactive, $activated) {
 function page_doc_link($text='', $iconpath='') {
     global $ME, $COURSE, $CFG;
 
-    if (empty($CFG->docroot)) {
+    if (empty($CFG->docroot) or empty($CFG->rolesactive)) {
         return '';
     }
 
index 7e801aafdd6d45d071d59a47747c62f52873400f..fb5e3a5273a2c79013412adb7120be4d5137c686 100644 (file)
@@ -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 = 2007100303;  // YYYYMMDD = date
+    $version = 2007100500;  // YYYYMMDD = date
                             //       XY = increments within a single day
 
     $release = '1.9 Beta +';   // Human-friendly version name