if ($mform->is_cancelled()) {
    $redirecturl = new moodle_url('view.php', array('id' => $cm->id));
    redirect($redirecturl, '', 0);
} else {
    if ($fromform = $mform->get_data()) {
        //validated data.
        if (confirm_sesskey() && has_capability('mod/booking:updatebooking', $context)) {
            if (!isset($fromform->limitanswers)) {
                $fromform->limitanswers = 0;
            }
            if (!isset($fromform->daystonotify)) {
                $fromform->daystonotify = 0;
            }
            $nBooking = booking_update_options($fromform);
            $bookingData = new booking_option($cm->id, $nBooking);
            $bookingData->sync_waiting_list();
            if (isset($fromform->submittandaddnew)) {
                $redirecturl = new moodle_url('editoptions.php', array('id' => $cm->id, 'optionid' => 'add'));
                redirect($redirecturl, get_string('changessaved'), 0);
            } else {
                $redirecturl = new moodle_url('report.php', array('id' => $cm->id, 'optionid' => $nBooking));
                redirect($redirecturl, get_string('changessaved'), 0);
            }
        }
    } else {
        $PAGE->set_title(format_string($booking->name));
        $PAGE->set_heading($course->fullname);
        echo $OUTPUT->header();
        // this branch is executed if the form is submitted but the data doesn't validate and the form should be redisplayed
        // or on the first display of the form.
        $mform->set_data($default_values);