]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-8352 Fully separate CONTEXT_COURSE, SITEID from COURSE_SYSTEM - now instanceid...
authorskodak <skodak>
Tue, 30 Jan 2007 10:10:25 +0000 (10:10 +0000)
committerskodak <skodak>
Tue, 30 Jan 2007 10:10:25 +0000 (10:10 +0000)
lib/accesslib.php

index 8001729f0b5d212c953d0ad10164b0ac40d1a628..da9654e0a07bed99d4b1aa518d4e515dd43d1013 100755 (executable)
@@ -225,7 +225,7 @@ function get_parent_cats($context, $type) {
         break;
         
         // a course always fall into a category, unless it's a site course
-        // this happens when SITEID = $course->id
+        // this happens when SITEID == $course->id
         // in this case the parent of the course is site context
         case CONTEXT_COURSE:
             if (!$course = get_record('course', 'id', $context->instanceid)) {
@@ -953,13 +953,13 @@ function load_user_capability($capability='', $context = NULL, $userid='') {
 */
     /* so up to this point we should have somethign like this
      * $capabilities[1]    ->contextlevel = 1000
-                           ->module = SITEID
+                           ->module = 0 // changed from SITEID in 1.8 (??)
                            ->capability = do_anything
                            ->id = 1 (id is the context id)
                            ->sum = 0
 
      * $capabilities[2]     ->contextlevel = 1000
-                            ->module = SITEID
+                            ->module = 0 // changed from SITEID in 1.8 (??)
                             ->capability = post_messages
                             ->id = 1
                             ->sum = -9000
@@ -1562,6 +1562,10 @@ function create_context($contextlevel, $instanceid) {
             debugging('Error: Invalid context creation request for level "'.s($contextlevel).'", instance "'.s($instanceid).'".');
             return NULL;
         }
+        if ($contextlevel == CONTEXT_SYSTEM) {
+            return create_system_context();
+            
+        }
         $context = new object();
         $context->contextlevel = $contextlevel;
         $context->instanceid = $instanceid;
@@ -1580,6 +1584,30 @@ function create_context($contextlevel, $instanceid) {
     }
 }
 
+/*
+ * 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;
+
+    } else {
+        $context = new object();
+        $context->contextlevel = CONTEXT_SYSTEM;
+        $context->instanceid = 0;
+        if ($context->id = insert_record('context',$context)) {
+            // we need not to populate context_rel for system context
+            return $context;
+        } else {
+            debugging('Can not create system context');
+            return NULL;
+        }
+    }
+}
 /**
  * Create a new context record for use by all roles-related stuff
  * @param $level
@@ -1607,7 +1635,7 @@ function validate_context($contextlevel, $instanceid) {
     switch ($contextlevel) {
 
         case CONTEXT_SYSTEM:
-            return ($instanceid == SITEID);
+            return ($instanceid == 0);
 
         case CONTEXT_PERSONAL:
             return (boolean)count_records('user', 'id', $instanceid);
@@ -1645,17 +1673,12 @@ function validate_context($contextlevel, $instanceid) {
  * @param $level
  * @param $instance
  */
-function get_context_instance($contextlevel=NULL, $instance=SITEID) {
+function get_context_instance($contextlevel=NULL, $instance=0) {
 
     global $context_cache, $context_cache_id, $CONTEXT;
     static $allowed_contexts = array(CONTEXT_SYSTEM, CONTEXT_PERSONAL, CONTEXT_USER, CONTEXT_COURSECAT, CONTEXT_COURSE, CONTEXT_GROUP, CONTEXT_MODULE, CONTEXT_BLOCK);
 
-    // This is really a systen context
-    // Yu: Separating site and site course context
-    /*
-    if ($contextlevel == CONTEXT_COURSE && $instance == SITEID) {
-        $contextlevel = CONTEXT_SYSTEM;
-    }*/
+    // Yu: Separating site and site course context - removed CONTEXT_COURSE override when SITEID
 
 /// If no level is supplied then return the current global context if there is one
     if (empty($contextlevel)) {
@@ -1667,6 +1690,11 @@ function get_context_instance($contextlevel=NULL, $instance=SITEID) {
         }
     }
 
+/// Backwards compatibility with obsoleted (CONTEXT_SYSTEM, SITEID)
+    if ($contextlevel == CONTEXT_SYSTEM) {
+        $instance = 0;
+    }
+
 /// check allowed context levels
     if (!in_array($contextlevel, $allowed_contexts)) {
         // fatal error, code must be fixed - probably typo or switched parameters