public function get_contextid($level, $instance = 0) {

        $stashname = 'context' . $level;

        if ($level == CONTEXT_SYSTEM or $level == CONTEXT_COURSE) {
            $instance = 0;
        }

        try {
            // try the previously stashed id
            return $this->get_stash($stashname, $instance);

        } catch (moodle1_convert_empty_storage_exception $e) {
            // this context level + instance is required for the first time
            $newid = $this->get_nextid();
            $this->set_stash($stashname, $newid, $instance);
            return $newid;
        }
    }