From e4fec273408390ef0744407d6f7d11203caa5004 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 07:13:33 +0000 Subject: [PATCH] accesslib: Introducing context_moved() - call after moving courses/coursecats 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 | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/lib/accesslib.php b/lib/accesslib.php index 763d9429c6..918fde9223 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -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 -- 2.39.5