public static function get_effective_contextlevel_value($contextlevel, $element) {
        if ($element !== 'purpose' && $element !== 'category') {
            throw new coding_exception('Only \'purpose\' and \'category\' are supported.');
        }
        $fieldname = $element . 'id';

        if ($contextlevel != CONTEXT_SYSTEM && $contextlevel != CONTEXT_USER) {
            throw new \coding_exception('Only context_system and context_user values can be retrieved, no other context levels ' .
                'have a purpose or a category.');
        }

        list($purposeid, $categoryid) = self::get_effective_default_contextlevel_purpose_and_category($contextlevel);

        // Note: The $$fieldname points to either $purposeid, or $categoryid.
        if (context_instance::NOTSET != $$fieldname && context_instance::INHERIT != $$fieldname) {
            // There is a specific value set.
            return self::get_element_instance($element, $$fieldname);
        }

        throw new coding_exception('Something went wrong, system defaults should be set and we should already have a value.');
    }