$formref = strtoupper($_REQUEST['formref']);
 $settings = read_form_settings_by_ref($formref);
 if ($settings === false) {
     $message = get_string('invalid_data', 'local_obu_forms');
 } else {
     if (!is_manager($settings)) {
         // Not a manager of this type of form
         $message = get_string('form_unavailable', 'local_obu_forms');
     } else {
         $form_id = $settings->id;
         $formname = $settings->name;
         if (isset($_REQUEST['version'])) {
             $version = strtoupper($_REQUEST['version']);
         } else {
             if (!isset($_REQUEST['versions']) || isset($_REQUEST['versions']) && $_REQUEST['versions'] != 0) {
                 $templates = read_form_templates($form_id);
                 if ($templates) {
                     $versions[0] = get_string('new_version', 'local_obu_forms');
                     // The 'New Version' option
                     foreach ($templates as $template) {
                         $versions[] = $template->version;
                     }
                     if (isset($_REQUEST['versions'])) {
                         $version = $versions[$_REQUEST['versions']];
                         $record = read_form_template($form_id, $version);
                     }
                 }
             }
         }
     }
 }
function get_form_template($form_id, $include_unpublished = false)
{
    // return the latest version of the template for the given form
    global $DB;
    // return the latest version
    $template = null;
    $templates = read_form_templates($form_id);
    foreach ($templates as $t) {
        if ($t->published || $include_unpublished) {
            $template = $t;
        }
    }
    if ($template) {
        return $template;
    }
    return false;
}