]> git.mjollnir.org Git - moodle.git/commitdiff
accesslib: Introducing context_moved() - call after moving courses/coursecats
authormartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:13:33 +0000 (07:13 +0000)
committermartinlanghoff <martinlanghoff>
Wed, 19 Sep 2007 07:13:33 +0000 (07:13 +0000)
With the new accesslib, moving courses and categories has a major
impact on enrolments and unenrolments.

At _least_ we need to signal accesslib that it has happened. So here
is context_moved() for exactly that.

Open to refactoring later into something along the lines of

 - move_course()
 - move_category()

However, at this stage the most important of those two: move_course()
does not fit very well with the code in course/edit. So keep it simple
for now.

lib/accesslib.php

index 763d9429c6654b92f219ec15fdeac683670502af..918fde9223decf0cdaeb71beaf564101ac4649f4 100755 (executable)
@@ -4731,6 +4731,49 @@ function build_context_path() {
 
 }
 
+/**
+ * Update the path field of the context and
+ * all the dependent subcontexts that follow
+ * the move. 
+ *
+ * The most important thing here is to be as
+ * DB efficient as possible. This op can have a
+ * massive impact in the DB.
+ *
+ * @param obj current   context obj
+ * @param obj newparent new parent obj
+ *
+ */
+function context_moved($context, $newparent) {
+    global $CFG;
+
+    $frompath = $context->path;
+    $newpath  = $newparent->path . '/' . $context->id;
+
+    $setdepth = '';
+    if ($newparent->depth +1 != $context->depth) {
+        $setdepth = ", depth= depth + " . ($context->depth - $newparent->depth +1);
+    }
+    $sql = "UPDATE {$CFG->prefix}context 
+            SET path='$newpath'
+                $setdepth
+            WHERE path='$frompath'";
+    error_log($sql);
+    execute_sql($sql,false);
+
+    $len = strlen($frompath);
+    $sql = "UPDATE {$CFG->prefix}context
+            SET path = '$newpath' || SUBSTR(path, {$len} +1)
+                $setdepth
+            WHERE path LIKE '{$frompath}/%'";
+    error_log($sql);
+    execute_sql($sql,false);
+
+    mark_context_dirty($frompath);
+    mark_context_dirty($newpath);
+}
+
+
 /**
  * Turn the ctx* fields in an objectlike record
  * into a context subobject. This allows