require_once($CFG->dirroot.'/course/lib.php');
}
+/**
+ * The name that will be used to seperate the navigation cache within SESSION
+ */
+define('NAVIGATION_CACHE_NAME', 'navigation');
+
/**
* This class is used to represent a node in a navigation tree
*
$this->text = get_string('home');
$this->forceopen = true;
$this->action = new moodle_url($CFG->wwwroot);
- $this->cache = new navigation_cache('navigation');
+ $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME);
$PAGE->requires->string_for_js('moveallsidetabstoblock','moodle');
$regenerate = optional_param('regenerate', null, PARAM_TEXT);
if ($regenerate==='navigation') {
$depth = 0;
switch ($contextlevel) {
- case CONTEXT_SYSTEM:
+ case CONTEXT_SYSTEM:
+ $this->cache->volatile();
$depth = $this->load_for_category(false);
break;
case CONTEXT_COURSECAT:
}
return 0;
}
+
+ /**
+ * This function marks the cache as volatile so it is cleared during shutdown
+ */
+ public function clear_cache() {
+ $this->cache->volatile();
+ }
}
/**
* @param bool $firstnode
* @return string HTML
*/
- protected function parse_branch_to_html($navarray, $firstnode=true, $moreafterthis) {
+ protected function parse_branch_to_html($navarray, $firstnode=true, $moreafterthis=false) {
$separator = get_separator();
$output = '';
if ($firstnode===true) {
// before we try anything
$this->page->navigation->initialise();
// Initialise the navigation cache
- $this->cache = new navigation_cache('navigation');
+ $this->cache = new navigation_cache(NAVIGATION_CACHE_NAME);
}
/**
* Initialise the settings navigation based on the current context
$this->context = $this->page->context;
switch ($this->context->contextlevel) {
case CONTEXT_SYSTEM:
+ $this->cache->volatile();
$adminkey = $this->load_administration_settings();
$settingskey = $this->load_user_settings(SITEID);
break;
if (!function_exists('admin_get_root')) {
require_once($CFG->dirroot.'/lib/adminlib.php');
}
- $adminroot = admin_get_root();
+ $adminroot = admin_get_root(false, false);
$branchkey = $this->add(get_string('administrationsite'),new moodle_url($CFG->wwwroot.'/admin/'), self::TYPE_SETTING);
$referencebranch = $this->get($branchkey);
foreach ($adminroot->children as $adminbranch) {
}
}
}
+
+ /**
+ * This function marks the cache as volatile so it is cleared during shutdown
+ */
+ public function clear_cache() {
+ $this->cache->volatile();
+ }
}
/**
const CACHEUSERID = 1;
/** @var int */
const CACHEVALUE = 2;
+ /** @var null|array An array of navigation cache areas to expire on shutdown */
+ public static $volatilecaches;
/**
* Contructor for the cache. Requires two arguments
public function __set($key, $information) {
$this->set($key, $information);
}
-
+
/**
* Sets some information against the cache (session) for later retrieval
*
}
}
}
+
+ /**
+ * Marks the cache as being volatile (likely to change)
+ *
+ * Any caches marked as volatile will be destroyed at the on shutdown by
+ * {@link navigation_node::destroy_volatile_caches()} which is registered
+ * as a shutdown function if any caches are marked as volatile.
+ *
+ * @param bool $setting True to destroy the cache false not too
+ */
+ public function volatile($setting = true) {
+ if (self::$volatilecaches===null) {
+ self::$volatilecaches = array();
+ register_shutdown_function(array('navigation_cache','destroy_volatile_caches'));
+ }
+
+ if ($setting) {
+ self::$volatilecaches[$this->area] = $this->area;
+ } else if (array_key_exists($this->area, self::$volatilecaches)) {
+ unset(self::$volatilecaches[$this->area]);
+ }
+ }
+
+ /**
+ * Destroys all caches marked as volatile
+ *
+ * This function is static and works in conjunction with the static volatilecaches
+ * property of navigation cache.
+ * Because this function is static it manually resets the cached areas back to an
+ * empty array.
+ */
+ public static function destroy_volatile_caches() {
+ global $SESSION;
+ if (is_array(self::$volatilecaches) && count(self::$volatilecaches)>0) {
+ foreach (self::$volatilecaches as $area) {
+ $SESSION->navcache->{$area} = array();
+ }
+ }
+ }
}
\ No newline at end of file