]> git.mjollnir.org Git - moodle.git/commitdiff
MDL-10679, improvement to context_rel table and load_user_capability
authortoyomoyo <toyomoyo>
Thu, 2 Aug 2007 08:28:29 +0000 (08:28 +0000)
committertoyomoyo <toyomoyo>
Thu, 2 Aug 2007 08:28:29 +0000 (08:28 +0000)
admin/roles/override.php
backup/restorelib.php
course/index.php
course/lib.php
lib/accesslib.php
lib/db/upgrade.php
version.php

index 2c250618247e0ee1173e41e85f665ec8a97cbded..d5de485abb4c238161f8ab94eb8bf96298f82a56 100755 (executable)
                  continue;
             }
 
-            if (isset($localoverrides[$capname])) {    // Something exists, so update it
-                if ($value == CAP_INHERIT) {       // inherit = delete
-                    delete_records('role_capabilities', 'roleid', $roleid, 'contextid', $context->id,
-                                                        'capability', $capname);
-                } else {
-                    $localoverride = new object();
-                    $localoverride->id = $localoverrides[$capname]->id;
-                    $localoverride->permission = $value;
-                    $localoverride->timemodified = time();
-                    $localoverride->modifierid = $USER->id;
-                    if (!update_record('role_capabilities', $localoverride)) {
-                        error('Could not update a capability!');
-                    }
-                }
-
+            if (isset($localoverrides[$capname])) {    
+                // Something exists, so update it               
+                assign_capability($capname, $value, $roleid, $context->id, true);
             } else { // insert a record
                 if ($value != CAP_INHERIT) {    // Ignore inherits
-                    $localoverride = new object();
-                    $localoverride->capability = $capname;
-                    $localoverride->contextid = $context->id;
-                    $localoverride->roleid = $roleid;
-                    $localoverride->permission = $value;
-                    $localoverride->timemodified = time();
-                    $localoverride->modifierid = $USER->id;
-                    if (!insert_record('role_capabilities', $localoverride)) {
-                        error('Could not insert a capability!');
-                    }
+                    assign_capability($capname, $value, $roleid, $context->id);
                 }
             }
         }
index 886d82ced779fc5bab04d4c4684d9cd6a9a4a3ad..5017abf60ee35a8ca2710161a2d16f52a3479be4 100644 (file)
             }
 
             $newcontext = get_context_instance($contextlevel, $oldinstance->new_id);
-            $override->contextid = $newcontext->id; // new context id
-            // might already have same override
-            if (!get_record('role_capabilities', 'capability', $override->capability, 'roleid', $override->roleid, 'contextid', $override->contextid)) {
-                insert_record('role_capabilities', $override);
-            }
+            $override->contextid = $newcontext->id; // new context id                
+            // use assign capability instead so we can add context to context_rel
+            assign_capability($override->capability, $override->permission, $override->roleid, $override->contextid);
         }
     }
     //write activity date changes to the html log file, and update date values in the the xml array
index 2862163d54d515298784b7a22b9877d460b37eaf..04af05d3fe1da9dd938783082fc5ee826618d5a8 100644 (file)
             if ($tempcat->parent != $moveto) {
                 if (! set_field('course_categories', 'parent', $moveto, 'id', $tempcat->id)) {
                     notify('Could not update that category!');
-                }
+                } else {
+                    rebuild_context_rel(get_context_instance(CONTEXT_COURSECAT, $move)); 
+                }            
             }
         }
     }
index 10ae2a8f2ae722ed2ebfece4d2f1094bf2c279a6..7b0ba2fa98591d7f135d7eacebc4ce8ad87c03f9 100644 (file)
@@ -2518,8 +2518,9 @@ function move_courses ($courseids, $categoryid) {
                     if (!update_record('course', $course)) {
                         notify("An error occurred - course not moved!");
                     }
-                    // parents changed (course category), do not delete child context relations
-                    insert_context_rel(get_context_instance(CONTEXT_COURSE, $course->id), false);
+                    // parents changed (course category)
+                    // rebuild this context and all children
+                    rebuild_context_rel(get_context_instance(CONTEXT_COURSE, $course->id));
                 }
             }
             fix_course_sortorder();
index 0fa6664edb0fa58cd9dd1a8a2c1c9646b8a115e6..695221ff7c077126872427144f8050e117b79a2a 100755 (executable)
@@ -1691,9 +1691,7 @@ function create_context($contextlevel, $instanceid) {
         $context->contextlevel = $contextlevel;
         $context->instanceid = $instanceid;
         if ($id = insert_record('context',$context)) {
-            // we need to populate context_rel for every new context inserted
-            $c = get_record('context','id',$id);
-            insert_context_rel ($c);           
+            $c = get_record('context','id',$id);          
             return $c;
         } else {
             debugging('Error: could not insert new context level "'.s($contextlevel).'", instance "'.s($instanceid).'".');
@@ -1958,6 +1956,7 @@ function create_role($name, $shortname, $description, $legacy='') {
  * @return success
  */
 function delete_role($roleid) {
+    global $CFG;
     $success = true;
 
 // mdl 10149, check if this is the last active admin role
@@ -1995,12 +1994,28 @@ function delete_role($roleid) {
 
 // cleanup all references to this role, ignore errors
     if ($success) {
+      
+        // MDL-10679 find all contexts where this role has an override
+        $contexts = get_records_sql("SELECT contextid, contextid 
+                                     FROM {$CFG->prefix}role_capabilities
+                                     WHERE roleid = $roleid");
+      
         delete_records('role_capabilities', 'roleid', $roleid);
+        
+        // MDL-10679, delete from context_rel if this role holds the last override in these contexts
+        if ($contexts) {
+            foreach ($contexts as $context) {
+                if (!record_exists('role_capabilities', 'contextid', $context->contextid)) {
+                    delete_records('context_rel', 'c1', $context->contextid);  
+                }   
+            }
+        }
+
         delete_records('role_allow_assign', 'roleid', $roleid);
         delete_records('role_allow_assign', 'allowassign', $roleid);
         delete_records('role_allow_override', 'roleid', $roleid);
         delete_records('role_allow_override', 'allowoverride', $roleid);
-        delete_records('role_names', 'roleid', $roleid);
+        delete_records('role_names', 'roleid', $roleid);    
     }
 
 // finally delete the role itself
@@ -2048,6 +2063,9 @@ function assign_capability($capability, $permission, $roleid, $contextid, $overw
         $cap->id = $existing->id;
         return update_record('role_capabilities', $cap);
     } else {
+        $c = get_record('context', 'id', $contextid);
+        /// MDL-10679 insert context rel here
+        insert_context_rel ($c);
         return insert_record('role_capabilities', $cap);
     }
 }
@@ -2062,9 +2080,19 @@ function assign_capability($capability, $permission, $roleid, $contextid, $overw
 function unassign_capability($capability, $roleid, $contextid=NULL) {
 
     if (isset($contextid)) {
+        // delete from context rel, if this is the last override in this context
         $status = delete_records('role_capabilities', 'capability', $capability,
                 'roleid', $roleid, 'contextid', $contextid);
+        
+        // MDL-10679, if this is no more overrides for this context
+        // delete entries from context where this context is a child
+        if (!record_exists('role_capabilities', 'contextid', $contextid)) {
+            delete_records('context_rel', 'c1', $contextid);  
+        }             
+                
     } else {
+        // There is no need to delete from context_rel here because
+        // this is only used for legacy, for now
         $status = delete_records('role_capabilities', 'capability', $capability,
                 'roleid', $roleid);
     }
@@ -4044,24 +4072,30 @@ function insert_context_rel($context, $deletechild=true, $deleteparent=true) {
  */
 function build_context_rel() {
   
-    global $db;
+    global $CFG, $db;
     $savedb = $db->debug;
-  
+
+    // MDL-10679, only identify contexts with overrides in them
+    $contexts = get_records_sql("SELECT c.* FROM {$CFG->prefix}context c, 
+                                                 {$CFG->prefix}role_capabilities rc
+                                            WHERE c.id = rc.contextid");
     // total number of records
-    $total = count_records('context');
+    // subtract one because the site context should not be calculated, will not be processed
+    $total = count($contexts) - 1;
+    
     // processed records
     $done = 0;
     print_progress($done, $total, 10, 0, 'Processing context relations');
-    $db->debug = false;
-    if ($contexts = get_records('context')) {
-        foreach ($contexts as $context) {
-            // no need to delete because it's all empty
-            insert_context_rel($context, false, false);
-            $db->debug = true;
-            print_progress(++$done, $total, 10, 0, 'Processing context relations');
-            $db->debug = false;
-        }
-    }
+    $db->debug = false;    
+      
+    //if ($contexts = get_records('context')) {
+    foreach ($contexts as $context) {
+        // no need to delete because it's all empty
+        insert_context_rel($context, false, false);
+        $db->debug = true;
+        print_progress(++$done, $total, 10, 0, 'Processing context relations');
+        $db->debug = false;
+    }    
     
     $db->debug = $savedb;
 }
@@ -4077,4 +4111,37 @@ function role_get_name($role, $context) {
         return format_string($role->name);
     }
 }
+
+/* 
+ * @param int object - context object (node), from which we find all it's children
+ * and rebuild all associated context_rel info
+ * this is needed when a course or course category is moved
+ * as the children's relationship to grandparents needs to be fixed
+ * @return int number of contexts rebuilt
+ */
+function rebuild_context_rel($context) {
+
+    $contextlist = array();
+    
+    if (record_exists('role_capabilities', 'contextid', $context->id)) {
+        $contextlist[] = $context;    
+    }
+
+    // find all children used in context_rel
+    if ($childcontexts = get_records('context_rel', 'c2', $context->id)) {
+        foreach ($childcontexts as $childcontext) {
+            $contextlist[$childcontext->c1] = get_record('context', 'id', $childcontext->c1);      
+        }
+    }
+    
+    $i = 0;
+    // rebuild all the contexts of this list
+    foreach ($contextlist as $c) {
+        insert_context_rel($c);
+        $i++;
+    }
+    
+    return $i;
+}
+
 ?>
index cebffb07483644fb3aa746f7d8de9d69649e7d2a..c1cdc593882a2a9d9f428fe1991787046372ef03 100644 (file)
@@ -1551,7 +1551,6 @@ function xmldb_main_upgrade($oldversion=0) {
         /// Launch add field description
             $result = $result && add_field($table, $field);
         }
-
     }
     
     // adding unique contraint on (courseid,shortname) of an outcome
@@ -1572,6 +1571,12 @@ function xmldb_main_upgrade($oldversion=0) {
             set_config('supportemail',  s($firstadmin->email));
         }
     }
+    
+    /// MDL-10679, context_rel clean up
+    if ($result && $oldversion < 2007080200) {
+        delete_records('context_rel');
+        build_context_rel();
+    }
 
 /*
     /// drop old gradebook tables
index 43ee93180ff9003c5afb997cd7cf559b48c1aa72..8959b2defb76a62a7103ea27f6f53858c521a0f6 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 = 2007080103;  // YYYYMMDD = date
+    $version = 2007080200;  // YYYYMMDD = date
                             //       XY = increments within a single day
 
     $release = '1.9 dev';   // Human-friendly version name