} else {
    $approver_email = '';
    $approver_name = '';
}
$home = new moodle_url('/');
$dir = $home . 'local/obu_application/';
$program = $dir . 'mdl_redirect.php?id=' . $application_id;
$heading = get_string('redirect_application', 'local_obu_application');
$PAGE->set_context($context);
$PAGE->set_pagelayout('standard');
$PAGE->set_url($program);
$PAGE->set_heading($SITE->fullname);
$PAGE->set_title($heading);
$application = read_application($application_id);
$application_title = $application->course_code . ' ' . $application->course_name . ' (Application Ref HLS/' . $application->id . ')';
get_application_status($USER->id, $application, $text, $button);
// get the approval trail and the next action (from the user's perspective)
$application_status = '<h4>' . $application_title . '</h4>' . $text;
$parameters = ['application_id' => $application_id, 'application_status' => $application_status, 'approver_email' => $approver_email, 'approver_name' => $approver_name];
// Check that it is possible to redirect the form
if ($application->approval_state > 0) {
    // Already finally approved or rejected?
    $message = get_string('application_unavailable', 'local_obu_application');
} else {
    $message = '';
}
$mform = new mdl_redirect_form(null, $parameters);
if ($mform->is_cancelled()) {
    redirect($home);
}
if ($mform_data = $mform->get_data()) {
function update_approver($application, $approver_email)
{
    // Update the stored approval requests
    read_approval($application->id, $approval);
    if ($approver_email == '') {
        delete_approval($approval);
    } else {
        $approval->approver = strtolower($approver_email);
        $approval->request_date = time();
        write_approval($approval);
    }
    // Determine the URL to use to link to the application
    $process = new moodle_url('/local/obu_application/process.php') . '?id=' . $application->id;
    $mdl_process = new moodle_url('/local/obu_application/mdl_process.php') . '?id=' . $application->id;
    // 'Mainstream' Moodle
    // Email the new status to the applicant and to the HLS Team (if not the next approver)
    $applicant = get_complete_user_data('id', $application->userid);
    $hls = get_complete_user_data('username', 'hls');
    $applicant->customheaders = array('Sender: ' . $hls->email, 'Precedence: Bulk', 'X-Auto-Response-Suppress: All', 'Auto-Submitted: auto-generated');
    $hls->customheaders = array('Sender: ' . $hls->email, 'Precedence: Bulk', 'X-Auto-Response-Suppress: All', 'Auto-Submitted: auto-generated');
    get_application_status($applicant->id, $application, $text, $button_text);
    // Get the status from the applicant's perspective
    $html = '<h4><a href="' . $process . '">HLS Application (Ref HLS/' . $application->id . ')</a></h4>' . $text;
    email_to_user($applicant, $hls, 'The Status of Your HLS Application (Ref HLS/' . $application->id . ')', html_to_text($html), $html);
    if ($approver_email != $hls->email) {
        // Update HLS unless they are the next approver
        get_application_status($hls->id, $application, $text, $button_text);
        // get the status from the HLS's perspective
        $html = '<h4><a href="' . $mdl_process . '">' . $application->course_code . ' ' . $application->course_name . ' (Application Ref HLS/' . $application->id . ')</a></h4>' . $text;
        //		email_to_user($hls, $applicant, 'Status Update: ' . $application->course_code . ' ' . $application->course_name . ' (' . $applicant->firstname . ' ' . $applicant->lastname . ')', html_to_text($html), $html);
    }
    // Notify the next approver (if there is one)
    if ($approver_email != '') {
        $approver = get_complete_user_data('email', strtolower($approver_email));
        if ($approver === false) {
            // Approver hasn't yet registered
            // Moodle requires a user to send emails to, not just an email address
            $approver = new Object();
            $approver->email = $approver_email;
            $approver->firstname = '';
            $approver->lastname = '';
            $approver->maildisplay = true;
            $approver->mailformat = 1;
            // 0 (zero) text-only emails, 1 (one) for HTML/Text emails.
            $approver->id = -99;
            // Moodle User ID. If it is for someone who is not a Moodle user, use an invalid ID like -99.
            $approver->firstnamephonetic = '';
            $approver->lastnamephonetic = '';
            $approver->middlename = '';
            $approver->alternatename = '';
        }
        if ($approver->email == $hls->email) {
            // HLS require the course name and will use 'mainstream' Moodle for their approvals
            /*			$link = '<a href="' . $mdl_process . '">' . $application->course_code . ' ' . $application->course_name . ' (Application Ref HLS/'. $application->id . ')</a>';
            			$html = get_string('request_approval', 'local_obu_application', $link);
            			email_to_user($approver, $applicant, 'Approval Required: ' . $application->course_code . ' ' . $application->course_name
            				. ' (' . $applicant->firstname . ' ' . $applicant->lastname . ')', html_to_text($html), $html);
            */
        } else {
            $link = '<a href="' . $process . '">HLS Application (Application Ref HLS/' . $application->id . ')</a>';
            $html = get_string('request_approval', 'local_obu_application', $link);
            email_to_user($approver, $applicant, 'Request for HLS Application Approval (' . $applicant->firstname . ' ' . $applicant->lastname . ')', html_to_text($html), $html);
        }
    }
}