function update_workflow($authorised = true, $comment = null)
{
    global $form, $record, $fields;
    // Update the form data record
    $authoriser_id = 0;
    if ($record->authorisation_level == 0) {
        // Being submitted
        $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_1_role, $fields);
        $record->auth_1_id = $authoriser_id;
        if ($authoriser_id != 0) {
            $record->authorisation_level = 1;
        } else {
            // Skip the level (OK for some roles)
            $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_2_role, $fields);
            $record->auth_2_id = $authoriser_id;
            $record->authorisation_level = 2;
        }
    } else {
        if ($record->authorisation_level == 1) {
            $record->auth_1_notes = $comment;
            $record->auth_1_date = time();
            if (!$authorised) {
                $record->authorisation_state = 1;
                // Rejected
            } else {
                if ($form->auth_2_role == 0) {
                    $record->authorisation_state = 2;
                    // It ends here
                } else {
                    $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_2_role, $fields);
                    $record->auth_2_id = $authoriser_id;
                    if ($authoriser_id != 0) {
                        $record->authorisation_level = 2;
                    } else {
                        // Skip the level (OK for some roles)
                        $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_3_role, $fields);
                        $record->auth_3_id = $authoriser_id;
                        $record->authorisation_level = 3;
                    }
                }
            }
        } else {
            if ($record->authorisation_level == 2) {
                $record->auth_2_notes = $comment;
                $record->auth_2_date = time();
                if (!$authorised) {
                    $record->authorisation_state = 1;
                    // Rejected
                } else {
                    if ($form->auth_3_role == 0) {
                        $record->authorisation_state = 2;
                        // It ends here
                    } else {
                        $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_3_role, $fields);
                        $record->auth_3_id = $authoriser_id;
                        if ($authoriser_id != 0) {
                            $record->authorisation_level = 3;
                        } else {
                            // Skip the level (OK for some roles)
                            $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_4_role, $fields);
                            $record->auth_4_id = $authoriser_id;
                            $record->authorisation_level = 4;
                        }
                    }
                }
            } else {
                if ($record->authorisation_level == 3) {
                    $record->auth_3_notes = $comment;
                    $record->auth_3_date = time();
                    if (!$authorised) {
                        $record->authorisation_state = 1;
                        // Rejected
                    } else {
                        if ($form->auth_4_role == 0) {
                            $record->authorisation_state = 2;
                            // It ends here
                        } else {
                            $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_4_role, $fields);
                            $record->auth_4_id = $authoriser_id;
                            if ($authoriser_id != 0) {
                                $record->authorisation_level = 4;
                            } else {
                                // Skip the level (OK for some roles)
                                $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_5_role, $fields);
                                $record->auth_5_id = $authoriser_id;
                                $record->authorisation_level = 5;
                            }
                        }
                    }
                } else {
                    if ($record->authorisation_level == 4) {
                        $record->auth_4_notes = $comment;
                        $record->auth_4_date = time();
                        if (!$authorised) {
                            $record->authorisation_state = 1;
                            // Rejected
                        } else {
                            if ($form->auth_5_role == 0) {
                                $record->authorisation_state = 2;
                                // It ends here
                            } else {
                                $authoriser_id = get_authoriser($record->author, $form->modular, $form->auth_5_role, $fields);
                                $record->auth_5_id = $authoriser_id;
                                $record->authorisation_level = 5;
                            }
                        }
                    } else {
                        $record->auth_5_notes = $comment;
                        $record->auth_5_date = time();
                        if (!$authorised) {
                            $record->authorisation_state = 1;
                            // Rejected
                        } else {
                            $record->authorisation_state = 2;
                            // It ends here
                        }
                    }
                }
            }
        }
    }
    save_form_data($record, $fields);
    // Update the stored authorisation requests and send notification emails
    update_authoriser($form, $record, $authoriser_id);
}
        if ($mform_data->submitbutton == get_string('save', 'local_obu_forms')) {
            if ($data->authorisation_level == 1) {
                $data->auth_1_id = $authoriser_id;
            } else {
                if ($data->authorisation_level == 2) {
                    $data->auth_2_id = $authoriser_id;
                } else {
                    if ($data->authorisation_level == 3) {
                        $data->auth_3_id = $authoriser_id;
                    } else {
                        echo get_string('invalid_data', 'local_obu_forms');
                        die;
                    }
                }
            }
            write_form_data($data);
            // Update the form data record
            update_authoriser($form, $data, $authoriser_id);
            // Update the authorisations and send notification emails
            redirect($home);
        }
    }
}
echo $OUTPUT->header();
echo $OUTPUT->heading($heading);
if ($message) {
    notice($message, $home);
} else {
    $mform->display();
}
echo $OUTPUT->footer();