private static function complete($node, $currentcontextlevel = false, $currentcontextid = false) {
        if (!isset($node['active'])) {
            if ($currentcontextlevel && !empty($node['contextlevel']) &&
                    $currentcontextlevel == $node['contextlevel'] &&
                    empty($currentcontextid)) {
                // This is the active context level, we also checked that there
                // is no default contextid set.
                $node['active'] = true;
            } else if ($currentcontextid && !empty($node['contextid']) &&
                    $currentcontextid == $node['contextid']) {
                $node['active'] = true;
            } else {
                $node['active'] = null;
            }
        }

        if (!isset($node['branches'])) {
            $node['branches'] = [];
        } else {
            foreach ($node['branches'] as $key => $childnode) {
                $node['branches'][$key] = self::complete($childnode, $currentcontextlevel, $currentcontextid);
            }
        }

        if (!isset($node['expandelement'])) {
            $node['expandelement'] = null;
        }

        if (!isset($node['expandcontextid'])) {
            $node['expandcontextid'] = null;
        }

        if (!isset($node['contextid'])) {
            $node['contextid'] = null;
        }

        if (!isset($node['contextlevel'])) {
            $node['contextlevel'] = null;
        }

        if (!isset($node['expanded'])) {
            if (!empty($node['branches'])) {
                $node['expanded'] = 1;
            } else {
                $node['expanded'] = 0;
            }
        }
        return $node;
    }