$process_url = $home . 'process.php';
$PAGE->set_title($CFG->pageheading . ': ' . get_string('apply', 'local_obu_application'));
// HTTPS is required in this page when $CFG->loginhttps enabled
$PAGE->https_required();
$PAGE->set_url($url);
$message = '';
$record = read_applicant($USER->id, false);
if ($record === false || $record->birthdate == 0) {
    // Must have completed the profile
    $message = get_string('complete_profile', 'local_obu_application');
} else {
    if (!isset($record->course_code) || $record->course_code === '') {
        // They must complete the course
        $message = get_string('complete_course', 'local_obu_application');
    } else {
        $course = read_course_record($record->course_code);
        if ($course->supplement != '') {
            $supplement = get_supplement_form($course->supplement, is_siteadmin());
            if (!$supplement) {
                $message = get_string('invalid_data', 'local_obu_application');
                // Shouldn't be here
            } else {
                unpack_supplement_data($record->supplement_data, $fields);
                if ($fields['supplement'] != $supplement->ref || $fields['version'] != $supplement->version) {
                    $message = get_string('complete_course', 'local_obu_application');
                    // Shouldn't be here
                }
            }
        }
    }
}
$program = $home . 'local/obu_application/mdl_amend_course.php?id=' . $application->id;
$process = $home . 'local/obu_application/mdl_process.php?id=' . $application->id;
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$PAGE->set_title(get_string('plugintitle', 'local_obu_application') . ': ' . get_string('process', 'local_obu_application'));
// Part of application processing
$PAGE->set_url($program);
$PAGE->navbar->add(get_string('application', 'local_obu_application', $application->id));
$message = '';
$parameters = ['courses' => get_course_names(), 'application' => $application];
$mform = new mdl_amend_course_form(null, $parameters);
if ($mform->is_cancelled()) {
    redirect($process);
}
if ($mform_data = $mform->get_data()) {
    // Update the applications's course fields
    $course = read_course_record($mform_data->course_code);
    $application->course_code = $course->code;
    $application->course_name = $course->name;
    $application->course_date = $mform_data->course_date;
    update_application($application);
    redirect($process);
}
echo $OUTPUT->header();
echo $OUTPUT->heading(get_string('application', 'local_obu_application', $application->id));
if ($message) {
    notice($message, $process);
} else {
    $mform->display();
}
echo $OUTPUT->footer();
function write_application($user_id, $form_data)
{
    global $DB;
    $user = read_user($user_id);
    // Contact details
    $applicant = read_applicant($user_id, true);
    // Profile & course must exist
    // Initialise the new record
    $record = new stdClass();
    $record->id = 0;
    $record->userid = $user_id;
    // Contact details
    $record->title = $applicant->title;
    $record->firstname = $user->firstname;
    $record->lastname = $user->lastname;
    $record->address_1 = $applicant->address_1;
    $record->address_2 = $applicant->address_2;
    $record->address_3 = $applicant->address_3;
    $record->town = $applicant->town;
    $record->domicile_code = $applicant->domicile_code;
    $record->county = $applicant->county;
    $record->postcode = $applicant->postcode;
    $record->phone = $user->phone1;
    $record->email = $user->email;
    // Profile
    $record->birthdate = $applicant->birthdate;
    $record->nationality_code = $applicant->nationality_code;
    $record->nationality = $applicant->nationality;
    $record->p16school = $applicant->p16school;
    $record->p16schoolperiod = $applicant->p16schoolperiod;
    $record->p16fe = $applicant->p16fe;
    $record->p16feperiod = $applicant->p16feperiod;
    $record->training = $applicant->training;
    $record->trainingperiod = $applicant->trainingperiod;
    $record->prof_level = $applicant->prof_level;
    $record->prof_award = $applicant->prof_award;
    $record->prof_date = $applicant->prof_date;
    $record->emp_place = $applicant->emp_place;
    $record->emp_area = $applicant->emp_area;
    $record->emp_title = $applicant->emp_title;
    $record->emp_prof = $applicant->emp_prof;
    $record->prof_reg_no = $applicant->prof_reg_no;
    if ($applicant->criminal_record == '1') {
        // '1' = yes, '2' = no
        $record->criminal_record = '1';
        // Yes
    } else {
        $record->criminal_record = '0';
        // No
    }
    // Course
    $record->course_code = $applicant->course_code;
    $record->course_name = $applicant->course_name;
    $record->course_date = $applicant->course_date;
    $record->statement = $applicant->statement;
    $course = read_course_record($applicant->course_code);
    if ($course->supplement != '') {
        // There should be supplementary data
        $record->supplement_data = $applicant->supplement_data;
    }
    // Final details
    $record->self_funding = $form_data->self_funding;
    //	$record->manager_email = $form_data->email;
    if (isset($form_data->declaration)) {
        // Only set if checked
        $record->declaration = 1;
    } else {
        $record->declaration = 0;
    }
    $record->application_date = time();
    return $DB->insert_record('local_obu_application', $record);
    // The remaining fields will have default values
}