From 4881f2d329a78576aad6cbf5c2b54a862c3cda54 Mon Sep 17 00:00:00 2001 From: martinlanghoff Date: Wed, 19 Sep 2007 07:12:56 +0000 Subject: [PATCH] accesslib: simplify and speedup create_context() Callers _must_ do their homework before calling create_context(). This allows us to save 2/3 queries per call (!!!). As it stands, callers are all in accesslib anyway. --- lib/accesslib.php | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/accesslib.php b/lib/accesslib.php index 5cd35f19a6..12cc0c3332 100755 --- a/lib/accesslib.php +++ b/lib/accesslib.php @@ -1964,34 +1964,29 @@ function islegacy($capabilityname) { /** * Create a new context record for use by all roles-related stuff + * assumes that the caller has done the homework. + * * @param $level * @param $instanceid * * @return object newly created context (or existing one with a debug warning) */ function create_context($contextlevel, $instanceid) { - if (!$context = get_record('context','contextlevel',$contextlevel,'instanceid',$instanceid)) { - if (!validate_context($contextlevel, $instanceid)) { - debugging('Error: Invalid context creation request for level "'.s($contextlevel).'", instance "'.s($instanceid).'".'); - return NULL; - } - if ($contextlevel == CONTEXT_SYSTEM) { - return create_system_context(); + if ($contextlevel == CONTEXT_SYSTEM) { + return create_system_context(); + } - } - $context = new object(); - $context->contextlevel = $contextlevel; - $context->instanceid = $instanceid; - if ($id = insert_record('context',$context)) { - $c = get_record('context','id',$id); - return $c; - } else { - debugging('Error: could not insert new context level "'.s($contextlevel).'", instance "'.s($instanceid).'".'); - return NULL; - } + $context = new object(); + $context->contextlevel = $contextlevel; + $context->instanceid = $instanceid; + if ($id = insert_record('context',$context)) { + $c = get_record('context','id',$id); + return $c; } else { - debugging('Warning: Context id "'.s($context->id).'" not created, because it already exists.'); - return $context; + debugging('Error: could not insert new context level "'. + s($contextlevel).'", instance "'. + s($instanceid).'".'); + return NULL; } } -- 2.39.5