Example #1
0
$formparams = array('course' => $course, 'cm' => $cm, 'modcontext' => $PAGE->context);
switch ($att->pageparams->action) {
    case att_sessions_page_params::ACTION_ADD:
        $url = $att->url_sessions(array('action' => att_sessions_page_params::ACTION_ADD));
        $mform = new mod_attforblock_add_form($url, $formparams);
        if ($formdata = $mform->get_data()) {
            $sessions = construct_sessions_data_for_add($formdata);
            $att->add_sessions($sessions);
            redirect($url, get_string('sessionsgenerated', 'attforblock'));
        }
        break;
    case att_sessions_page_params::ACTION_UPDATE:
        $sessionid = required_param('sessionid', PARAM_INT);
        $url = $att->url_sessions(array('action' => att_sessions_page_params::ACTION_UPDATE, 'sessionid' => $sessionid));
        $formparams['sessionid'] = $sessionid;
        $mform = new mod_attforblock_update_form($url, $formparams);
        if ($mform->is_cancelled()) {
            redirect($att->url_manage());
        }
        if ($formdata = $mform->get_data()) {
            $att->update_session_from_form_data($formdata, $sessionid);
            redirect($att->url_manage(), get_string('sessionupdated', 'attforblock'));
        }
        break;
    case att_sessions_page_params::ACTION_DELETE:
        $sessionid = required_param('sessionid', PARAM_INT);
        $confirm = optional_param('confirm', NULL, PARAM_INT);
        if (isset($confirm)) {
            $att->delete_sessions(array($sessionid));
            att_update_all_users_grades($att->id, $att->course, $att->context);
            redirect($att->url_manage(), get_string('sessiondeleted', 'attforblock'));
            if (insert_record('attendance_sessions', $rec)) {
                add_to_log($course->id, 'attforblock', 'one session added', 'manage.php?id=' . $id, $user->lastname . ' ' . $user->firstname);
                notice(get_string('sessionadded', 'attforblock'));
            } else {
                error(get_string('errorinaddingsession', 'attforblock'), "sessions.php?id={$id}&action=add");
            }
        }
    }
    $mform_add->display();
}
//////////////////////////////////////////////////////////
// Updating sessions
//////////////////////////////////////////////////////////
if ($action === 'update') {
    $sessionid = required_param('sessionid');
    $mform_update = new mod_attforblock_update_form('sessions.php', array('course' => $course, 'cm' => $cm, 'modcontext' => $context, 'sessionid' => $sessionid));
    if ($mform_update->is_cancelled()) {
        redirect('manage.php?id=' . $id);
    }
    if ($fromform = $mform_update->get_data()) {
        if (!($att = get_record('attendance_sessions', 'id', $sessionid))) {
            error('No such session in this course');
        }
        //update session
        $att->sessdate = $fromform->sessiondate;
        $att->duration = $fromform->durtime['hours'] * HOURSECS + $fromform->durtime['minutes'] * MINSECS;
        $startdate = $fromform->sessiondate;
        $duration = $fromform->durtime['hours'] * HOURSECS + $fromform->durtime['minutes'] * MINSECS;
        $att->sessionend = $startdate + $duration;
        $att->description = $fromform->sdescription;
        // check for new session title in text box and choose whether to use from dropdown or text box