/************************************ Revoking all appointments to a slot ***************************************/
 case 'revokeall':
     $slotid = required_param('slotid', PARAM_INT);
     $slot = $scheduler->get_slot($slotid);
     $oldstudents = array();
     foreach ($slot->get_appointments() as $app) {
         $oldstudents[] = $app->studentid;
         $slot->remove_appointment($app);
     }
     // notify student
     if ($scheduler->allownotifications) {
         foreach ($oldstudents as $oldstudent) {
             include_once $CFG->dirroot . '/mod/scheduler/mailtemplatelib.php';
             $student = $DB->get_record('user', array('id' => $oldstudent));
             $teacher = $DB->get_record('user', array('id' => $slot->teacherid));
             $vars = scheduler_get_mail_variables($scheduler, $slot, $teacher, $student, $COURSE, $student);
             scheduler_send_email_from_template($student, $teacher, $COURSE, 'cancelledbyteacher', 'teachercancelled', $vars, 'scheduler');
         }
     }
     $slot->save();
     break;
     /************************************ Toggling to unlimited group ***************************************/
 /************************************ Toggling to unlimited group ***************************************/
 case 'allowgroup':
     $slotid = required_param('slotid', PARAM_INT);
     $slot = new stdClass();
     $slot->id = $slotid;
     $slot->exclusivity = 0;
     $DB->update_record('scheduler_slots', $slot);
     break;
     /************************************ Toggling to single student ******************************************/
/**
 * Function to be run periodically according to the moodle
 * This function searches for things that need to be done, such
 * as sending out mail, toggling flags etc ...
 * @return boolean always true
 * @uses $CFG
 * @uses $DB
 */
function scheduler_cron()
{
    global $CFG, $DB;
    $date = make_timestamp(date('Y'), date('m'), date('d'), date('H'), date('i'));
    // for every appointment in all schedulers
    $select = 'emaildate > 0 AND emaildate <= ? AND starttime > ?';
    $slots = $DB->get_records_select('scheduler_slots', $select, array($date, $date), 'starttime');
    foreach ($slots as $slot) {
        // get teacher
        $teacher = $DB->get_record('user', array('id' => $slot->teacherid));
        // get scheduler, slot and course
        $scheduler = scheduler_instance::load_by_id($slot->schedulerid);
        $slotm = $scheduler->get_slot($slot->id);
        $course = $DB->get_record('course', array('id' => $scheduler->course));
        // get appointed student list
        $appointments = $DB->get_records('scheduler_appointment', array('slotid' => $slot->id), '', 'id, studentid');
        //if no email previously sent and one is required
        foreach ($appointments as $appointment) {
            $student = $DB->get_record('user', array('id' => $appointment->studentid));
            cron_setup_user($student, $course);
            $vars = scheduler_get_mail_variables($scheduler, $slotm, $teacher, $student, $course, $student);
            scheduler_send_email_from_template($student, $teacher, $course, 'remindtitle', 'reminder', $vars, 'scheduler');
        }
        // mark as sent
        $slot->emaildate = -1;
        $DB->update_record('scheduler_slots', $slot);
    }
    cron_setup_user();
    return true;
}
if ($action == 'cancelbooking') {
    require_sesskey();
    // Get the request parameters.
    $slotid = required_param('slotid', PARAM_INT);
    $slot = $scheduler->get_slot($slotid);
    if (!$slot) {
        throw new moodle_exception('error');
    }
    if (!$slot->is_in_bookable_period()) {
        throw new moodle_exception('nopermissions');
    }
    require_capability('mod/scheduler:appoint', $context);
    $userstocancel = array($USER->id);
    if ($appointgroup) {
        $userstocancel = array_keys($scheduler->get_possible_attendees(array($appointgroup)));
    }
    foreach ($userstocancel as $userid) {
        if ($appointment = $slot->get_student_appointment($userid)) {
            $scheduler->delete_appointment($appointment->id);
            // Notify the teacher.
            if ($scheduler->allownotifications) {
                $student = $DB->get_record('user', array('id' => $USER->id));
                $teacher = $DB->get_record('user', array('id' => $slot->teacherid));
                $vars = scheduler_get_mail_variables($scheduler, $slot, $teacher, $student, $course, $teacher);
                scheduler_send_email_from_template($teacher, $student, $COURSE, 'cancelledbystudent', 'cancelled', $vars, 'scheduler');
            }
            \mod_scheduler\event\booking_removed::create_from_slot($slot)->trigger();
        }
    }
    redirect($returnurl);
}