die('Direct access to this script is forbidden.');
    ///  It must be included from view.php in mod/scheduler
}
/************************************************ Saving choice ************************************************/
if ($action == 'savechoice') {
    // get parameters
    $slotid = optional_param('slotid', '', PARAM_INT);
    $appointgroup = optional_param('appointgroup', 0, PARAM_INT);
    // $notes = optional_param('notes', '', PARAM_TEXT);
    if (!$slotid) {
        notice(get_string('notselected', 'scheduler'), "view.php?id={$cm->id}");
    }
    if (!($slot = get_record('scheduler_slots', 'id', $slotid))) {
        error('Invalid slot ID');
    }
    $available = scheduler_get_appointments($slotid);
    $consumed = $available ? count($available) : 0;
    // if slot is already overcrowded
    if ($slot->exclusivity > 0 && $slot->exclusivity <= $consumed) {
        print_simple_box_start('center');
        echo '<h2 style="color:red;">' . get_string('slot_is_just_in_use', 'scheduler') . '</h2>';
        print_simple_box_end();
        print_continue("{$CFG->wwwroot}/mod/scheduler/view.php?id={$cm->id}");
        print_footer($course);
        exit;
    }
    /// If we are scheduling a full group we must discard all pending appointments of other participants of the scheduled group
    /// just add the list of other students for searching slots to delete
    if ($appointgroup) {
        if (!function_exists('build_navigation')) {
            // we are still in 1.8
            }
        } else {
            $app->appointmentnote = addslashes(required_param('appointmentnote', PARAM_CLEANHTML));
            update_record('scheduler_appointment', $app);
        }
    }
}
/******************************* Update grades when concerned teacher ************************/
if ($subaction == 'updategrades' and (has_capability('mod/scheduler:manage', $context) or has_capability('mod/scheduler:manageallappointments', $context))) {
    $keys = preg_grep("/^gr(.*)/", array_keys($_POST));
    foreach ($keys as $key) {
        preg_match("/^gr(.*)/", $key, $matches);
        $app->id = $matches[1];
        $app->grade = required_param($key, PARAM_INT);
        $app->timemodified = time();
        $distribute = optional_param('distribute' . $app->id, 0, PARAM_INT);
        if ($distribute) {
            // distribute to all members
            $slotid = get_field('scheduler_appointment', 'slotid', 'id', $app->id);
            $allapps = scheduler_get_appointments($slotid);
            foreach ($allapps as $anapp) {
                $anapp->grade = $app->grade;
                $anapp->timemodified = $app->timemodified;
                update_record('scheduler_appointment', $anapp);
            }
        } else {
            // set to current members
            update_record('scheduler_appointment', $app);
        }
    }
}
/**
 * Backup scheduler_slots appointments (executed from scheduler_backup_mods)
 */
function backup_scheduler_appointments($bf, $preferences, $slots)
{
    global $CFG;
    $status = true;
    //Write start tag
    $status = $status && fwrite($bf, start_tag('APPOINTMENTS', 4, true));
    foreach ($slots as $slot) {
        $appointments = scheduler_get_appointments($slot->id);
        //If there is slots
        if ($appointments) {
            //Iterate over each slots
            foreach ($appointments as $appointment) {
                //Start slot
                $status = $status && fwrite($bf, start_tag('APPOINTMENT', 5, true));
                //Print appointment data
                fwrite($bf, full_tag('ID', 6, false, $appointment->id));
                fwrite($bf, full_tag('SLOTID', 6, false, $appointment->slotid));
                fwrite($bf, full_tag('STUDENTID', 6, false, $appointment->studentid));
                fwrite($bf, full_tag('ATTENDED', 6, false, $appointment->attended));
                fwrite($bf, full_tag('GRADE', 6, false, $appointment->grade));
                fwrite($bf, full_tag('APPOINTMENTNOTE', 6, false, $appointment->appointmentnote));
                fwrite($bf, full_tag('TIMECREATED', 6, false, $appointment->timecreated));
                fwrite($bf, full_tag('TIMEMODIFIED', 6, false, $appointment->timemodified));
                //End appointment
                $status = $status && fwrite($bf, end_tag('APPOINTMENT', 5, true));
            }
        }
    }
    //Write end tag
    $status = $status && fwrite($bf, end_tag('APPOINTMENTS', 4, true));
    return $status;
}
 $previoustime = 0;
 $previousendtime = 0;
 $canappoint = false;
 foreach ($studentSlots as $key => $aSlot) {
     $startdate = scheduler_userdate($aSlot->starttime, 1);
     $starttime = scheduler_usertime($aSlot->starttime, 1);
     $endtime = scheduler_usertime($aSlot->starttime + $aSlot->duration * 60, 1);
     $startdatestr = $startdate == $previousdate ? '' : $startdate;
     $starttimestr = $starttime == $previoustime ? '' : $starttime;
     $endtimestr = $endtime == $previousendtime ? '' : $endtime;
     if ($aSlot->appointedbyme and !$aSlot->attended) {
         $radio = "<input type=\"radio\" name=\"slotid\" value=\"{$aSlot->id}\" checked=\"checked\" />\n";
         $table->data[] = array("<b>{$startdatestr}</b>", "<b>{$starttime}</b>", "<b>{$endtime}</b>", $radio, "<b>" . "<a href=\"../../user/view.php?id={$aSlot->teacherid}&amp;course={$scheduler->course}\">" . fullname(get_record('user', 'id', $aSlot->teacherid)) . '</a></b>', '<b>' . $aSlot->groupsession . '</b>');
     } else {
         if ($aSlot->appointed and has_capability('mod/scheduler:seeotherstudentsbooking', $context)) {
             $appointments = scheduler_get_appointments($aSlot->id);
             $collegues = "<div style=\"visibility:hidden; display:none\" id=\"collegues{$aSlot->id}\"><br/>";
             foreach ($appointments as $appstudent) {
                 $student = get_record('user', 'id', $appstudent->studentid);
                 $picture = print_user_picture($appstudent->studentid, $course->id, $student->picture, 0, true, true);
                 $name = "<a href=\"view.php?what=viewstudent&amp;id={$cm->id}&amp;studentid={$student->id}&amp;course={$scheduler->course}&amp;order=DESC\">" . fullname($student) . '</a>';
                 $collegues .= " {$picture} {$name}<br/>";
             }
             $collegues .= '</div>';
             $aSlot->groupsession .= " <a href=\"javascript:toggleVisibility('{$aSlot->id}')\"><img name=\"group<?php p({$aSlot->id}) ?>\" src=\"{$CFG->pixpath}/t/switch_plus.gif\" border=\"0\" title=\"" . get_string('whosthere', 'scheduler') . "\"></a> {$collegues}";
         }
         $canappoint = true;
         $canusegroup = $aSlot->appointed ? 0 : 1;
         $radio = "<input type=\"radio\" name=\"slotid\" value=\"{$aSlot->id}\" onclick=\"checkGroupAppointment({$canusegroup})\" />\n";
         $table->data[] = array($startdatestr, $starttimestr, $endtimestr, $radio, "<a href=\"../../user/view.php?id={$aSlot->teacherid}&amp;course={$scheduler->course}\">" . fullname(get_record('user', 'id', $aSlot->teacherid)) . '</a>', $aSlot->groupsession);
     }