From e40413befa08e2b2b5669bff2e11bd25dfa4b2c0 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 07:13:08 +0000 Subject: [PATCH] accesslib: create_context() now populates the path of new contexts ... and it populates the context cache too. Unfortunately, it needs an INSERT followed by an UPDATE. Other than a stored procedure, I don't know how to deal with this better. (We could save the SELECT though! that's a thought...) OTOH, we are getting so much mileage out of the path field that it's probably a hit we have to take in the chin and move on. --- lib/accesslib.php | 66 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 64 insertions(+), 2 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 12cc0c3332..763d9429c6 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1969,9 +1969,12 @@ function islegacy($capabilityname) { * @param $level * @param $instanceid * - * @return object newly created context (or existing one with a debug warning) + * @return object newly created context */ function create_context($contextlevel, $instanceid) { + + global $CFG; + if ($contextlevel == CONTEXT_SYSTEM) { return create_system_context(); } @@ -1979,8 +1982,67 @@ function create_context($contextlevel, $instanceid) { $context = new object(); $context->contextlevel = $contextlevel; $context->instanceid = $instanceid; + + // Define $context->path based on the parent + // context. In other words... Who is your daddy? + $basepath = '/' . SYSCONTEXTID; + + switch ($contextlevel) { + case CONTEXT_COURSECAT: + $sql = "SELECT ctx.path + FROM {$CFG->prefix}context ctx + JOIN {$CFG->prefix}course_categories cc + ON (cc.parent=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT.") + WHERE cc.id={$instanceid}"; + if ($path = get_field_sql($sql)) { + $basepath = $path; + } + break; + + case CONTEXT_COURSE: + $sql = "SELECT ctx.path + FROM {$CFG->prefix}context ctx + JOIN {$CFG->prefix}course c + ON (c.category=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSECAT.") + WHERE c.id={$instanceid} AND c.id !=" . SITEID; + if ($path = get_field_sql($sql)) { + $basepath = $path; + } + break; + + case CONTEXT_MODULE: + $sql = "SELECT ctx.path + FROM {$CFG->prefix}context ctx + JOIN {$CFG->prefix}course_modules cm + ON (cm.course=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.") + WHERE cm.id={$instanceid}"; + $path = get_field_sql($sql); + $basepath = $path; + break; + + case CONTEXT_BLOCK: + // Only non-pinned & course-page based + $sql = "SELECT ctx.path + FROM {$CFG->prefix}context ctx + JOIN {$CFG->prefix}block_instance bi + ON (bi.pageid=ctx.instanceid AND ctx.contextlevel=".CONTEXT_COURSE.") + WHERE bi.id={$instanceid} AND bi.pagetype='course-view'"; + $path = get_field_sql($sql); + $basepath = $path; + break; + case CONTEXT_USER: + // default to basepath + break; + case CONTEXT_PERSONAL: + // default to basepath + break; + } + if ($id = insert_record('context',$context)) { - $c = get_record('context','id',$id); + // 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 "'. -- 2.39.5