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.
This commit is contained in:
martinlanghoff
2007-09-19 07:13:08 +00:00
parent 4881f2d329
commit e40413befa
+64 -2
View File
@@ -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 "'.