public static function setup_for_template($pagecontextid, moodle_url $url, $template = null, $subtitle = '',
                                              $returntype = null) {
        global $PAGE, $SITE;

        $pagecontext = context::instance_by_id($pagecontextid);
        $context = $pagecontext;
        if (!empty($template)) {
            $context = $template->get_context();
        }

        $templatesurl = new moodle_url('/admin/tool/lp/learningplans.php', array('pagecontextid' => $pagecontextid));
        $templateurl = null;
        if ($template) {
            $templateurl = new moodle_url('/admin/tool/lp/templatecompetencies.php', [
                'templateid' => $template->get('id'),
                'pagecontextid' => $pagecontextid
            ]);
        }

        $returnurl = $templatesurl;
        if ($returntype != 'templates' && $templateurl) {
            $returnurl = $templateurl;
        }

        $PAGE->navigation->override_active_url($templatesurl);
        $PAGE->set_context($pagecontext);

        if (!empty($template)) {
            $title = format_string($template->get('shortname'), true, array('context' => $context));
        } else {
            $title = get_string('templates', 'tool_lp');
        }

        if ($pagecontext->contextlevel == CONTEXT_SYSTEM) {
            $heading = $SITE->fullname;
        } else if ($pagecontext->contextlevel == CONTEXT_COURSECAT) {
            $heading = $pagecontext->get_context_name();
        } else {
            throw new coding_exception('Unexpected context!');
        }

        $PAGE->set_pagelayout('admin');
        $PAGE->set_url($url);
        $PAGE->set_title($title);
        $PAGE->set_heading($heading);

        if (!empty($template)) {
            $PAGE->navbar->add($title, $templateurl);
            if (!empty($subtitle)) {
                $PAGE->navbar->add($subtitle, $url);
            }

        } else if (!empty($subtitle)) {
            // We're in a sub page without a specific template.
            $PAGE->navbar->add($subtitle, $url);
        }

        return array($title, $subtitle, $returnurl);
    }