$PAGE->set_context($context);
$PAGE->set_heading($SITE->fullname);
$PAGE->set_title(get_string('supplements', 'local_obu_application'));
$message = '';
$ref = '';
$version = '';
$versions = array();
$record = null;
$is_published = 0;
if (isset($_REQUEST['ref'])) {
    $ref = strtoupper($_REQUEST['ref']);
    if (isset($_REQUEST['version'])) {
        $version = strtoupper($_REQUEST['version']);
    } else {
        if (!isset($_REQUEST['versions']) || isset($_REQUEST['versions']) && $_REQUEST['versions'] != 0) {
            $supplements = read_supplement_forms($ref);
            if ($supplements) {
                $versions[0] = get_string('new_version', 'local_obu_application');
                // The 'New Version' option
                foreach ($supplements as $supplement) {
                    $versions[] = $supplement->version;
                }
                if (isset($_REQUEST['versions'])) {
                    $version = $versions[$_REQUEST['versions']];
                    $record = read_supplement_form($ref, $version);
                }
            }
        }
    }
}
$parameters = ['ref' => $ref, 'version' => $version, 'versions' => $versions, 'record' => $record];
function get_supplement_form($ref, $include_unpublished = false)
{
    // Return the latest version of the supplement form
    global $DB;
    // Return the latest version
    $supplement = null;
    $supplements = read_supplement_forms($ref);
    foreach ($supplements as $s) {
        if ($s->published || $include_unpublished) {
            $supplement = $s;
        }
    }
    if ($supplement) {
        return $supplement;
    }
    return false;
}