<!-- This page defines the form to move an appointment slot -->
<!-- it is used from view.php -->

<?php $usehtmleditor = can_use_html_editor(); ?>

<form name="add" method="post" action="view.php">
<input type="hidden" name="what" value="<?php p($form->what) ?>" />
<input type="hidden" name="id" value="<?php p($cm->id) ?>" />
<input type="hidden" name="page" value="<?php p($page) ?>" />
<input type="hidden" name="subaction" value="" />
<input type="hidden" name="offset" value="<?php p($offset) ?>" />
<input type="hidden" name="studentid" value="" /><!-- for appointments managment, and scheduling only -->
<input type="hidden" name="groupid" value="" /><!-- for group scheduling only -->
<input type="hidden" name="appointments" value="<?php p(serialize($form->appointments)) ?>" />
<?php 
if (!has_capability('mod/scheduler:canscheduletootherteachers', $context)){
?>
<input type="hidden" name="teacherid" value="<?php p($form->teacherid) ?>" />
<?php
}
if ($action == 'updateslot'){
?>
<input type="hidden" name="slotid" value="<?php p($slotid) ?>" />
<?php
}
elseif ($action == 'schedule'){
?>
<input type="hidden" name="seen" value="<?php p($form->seen) ?>" />
<?php
}
?>
<center>
<table cellpadding="5">
<?php
if($action == 'schedule' or $action == 'schedulegroup'){
    if ($form->availableslots){
?>
<tr valign="top">
    <td align="right"><b><?php print_string('chooseexisting', 'scheduler') ?>:</b></td>
    <td align="left">
        <script type="text/javascript">
            function reloadandchoose(){
                document.forms['add'].what.value = '<?php p($action) ?>';
                document.forms['add'].subaction.value = 'dochooseslot';
                document.forms['add'].studentid.value = '<?php p(@$form->studentid) ?>';
                document.forms['add'].groupid.value = '<?php p(@$form->groupid) ?>';
                document.forms['add'].submit();
            }
        </script>
<?php 
    $startdatemem = '';
    $starttimemem = '';
    foreach($form->availableslots as $aSlot){
        $startdatecnv = scheduler_userdate($aSlot->starttime,1);
        $starttimecnv = scheduler_usertime($aSlot->starttime,1);        

        $startdatestr = ($startdatemem != '' and $startdatemem == $startdatecnv) ? "-----------------" : $startdatecnv ;
        $starttimestr = ($starttimemem != '' and $starttimemem == $starttimecnv) ? '' : $starttimecnv ;

        $startdatemem = $startdatecnv;
        $starttimemem = $starttimecnv;

        $form->availableslotsmenu[$aSlot->id] = "$startdatestr $starttimestr";
    }
    choose_from_menu($form->availableslotsmenu, 'slotid', $form->slotid, 'choose', 'reloadandchoose();');
?>
    </td>
</tr>
<?php 
    }
}
?>
<tr valign="top">
    <td align="right"><b><?php print_string('date', 'scheduler') ?>:</b></td>
    <td align="left" <?php print_error_class(@$errors, 'rangestart') ?> >
        <?php print_date_selector('day', 'month', 'year', $form->starttime); ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php print_string('starttime', 'scheduler') ?>:</b></td>
    <td align="left">
        <?php
           print_time_selector('hour', 'minute', $form->starttime, 5);
           helpbutton('choosingslotstart', get_string('moveslot', 'scheduler'), 'scheduler');
        ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php print_string('duration', 'scheduler') ?>:</b></td>
    <td align="left">
        <input type="text" size="3" maxlength="3" name="duration" value="<?php echo $form->duration?>" />
        <?php print_string('minutes', 'scheduler') ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php print_string('multiplestudents', 'scheduler') ?>:</b></td>
    <td align="left" <?php print_error_class(@$errors, 'exclusivity') ?> >
        <script type="text/javascript">
           function allowmoreappointments(selectobj){
                var addlink = document.getElementById('addappointment');
                if (selectobj.options[selectobj.selectedIndex].value > <?php echo count($form->appointments) ?> ){
                    addlink.style.visibility = 'visible';
                } else {
                    addlink.style.visibility = 'hidden';
                }
            }
        </script>
        <?php
            $exclusivemenu['0'] = get_string('unlimited', 'scheduler');
            $knownslots = count($form->appointments);
            if ($knownslots == 0) $knownslots++;            
            for($i = $knownslots ; $i < 10 ; $i++){
                $exclusivemenu[(string)$i] = $i;
            }
            choose_from_menu($exclusivemenu, 'exclusivity', $form->exclusivity, '', 'allowmoreappointments(this);');
            helpbutton('exclusivity', get_string('groupsession', 'scheduler'), 'scheduler');
        ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php print_string('reuse', 'scheduler') ?>:</b></td>
    <td align="left">
        <?php
            $reusemenu[0] = get_string('no');
            $reusemenu[1] = get_string('yes');
            choose_from_menu($reusemenu, 'reuse', $form->reuse, '');
            helpbutton('reuse', get_string('reuse', 'scheduler'), 'scheduler');
        ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php print_string('location', 'scheduler') ?>:</b></td>
    <td align="left">
        <input type="text" size="30" maxlength="50" name="appointmentlocation" value="<?php echo $form->appointmentlocation?>" />
        <?php helpbutton('location', get_string('location', 'scheduler'), 'scheduler'); ?>
    </td>
</tr>
<?php
if (has_capability('mod/scheduler:canscheduletootherteachers', $context)){
?>
<tr valign="top">
    <td align="right"><b><?php echo $scheduler->staffrolename ?>:</b></td>
    <td align="left" <?php print_error_class(@$errors, 'teacherid') ?> >
        <?php
            $teachers = get_users_by_capability($context, 'mod/scheduler:attend', 'u.id,lastname,firstname,email,picture', 'lastname');
            $teachersmenu = array();
            if ($teachers){
                foreach($teachers as $aTeacher){
                    $teachersmenu[$aTeacher->id] = fullname($aTeacher);
                }
                choose_from_menu($teachersmenu, 'teacherid', $form->teacherid);
            } else {
               print_string('noteachershere', 'scheduler', $scheduler->staffrolename);
            }
            helpbutton('bookwithteacher', get_string('bookwithteacher', 'scheduler'), 'scheduler'); 
        ?>
    </td>
</tr>
<?php
}
?>
<tr valign="top">
    <td align="right"><b><?php print_string('displayfrom', 'scheduler') ?>:</b></td>
    <td align="left">
        <?php print_date_selector('displayday', 'displaymonth', 'displayyear', $form->hideuntil); ?>
    </td>
</tr>
<tr valign="top">
    <td align="right"><b><?php print_string('comments', 'scheduler') ?>:</b></td>
    <td align="left">
        <?php
            if (!isset($form->notes)) {
                $form->notes = '';
            }
            print_textarea($usehtmleditor, 20, 60, 680, 400, 'notes', $form->notes);
            if ($usehtmleditor) {
                echo '<input type="hidden" name="format" value="'.FORMAT_HTML.'" />';
            } else {
                echo '<p align="right">';
                helpbutton('textformat', get_string('formattexttype'));
                print_string('formattexttype');
                echo ':&nbsp;';
                if (!$form->format) {
                    $form->format = 'MOODLE';
                }
                choose_from_menu(format_text_menu(), 'format', $form->format, ''); 
                echo '</p>';
            }
        ?>
    </td>
</tr>
<tr>
    <td colspan="2">
<?php 
print_simple_box_start(get_string('appointments', 'scheduler'), 'center');
if (!empty($form->appointments) && count($form->appointments)){
    echo "
        <script type=\"text/javascript\">
            function deleteAppointment(studentid){
                document.forms['add'].subaction.value = 'doremoveappointed';
                document.forms['add'].studentid.value = studentid;
                document.forms['add'].what.value = '{$action}';
                document.forms['add'].submit();
            }

            function updateAppointment(studentid){
                document.forms['add'].subaction.value = 'updateappointed';
                document.forms['add'].studentid.value = studentid;
                document.forms['add'].what.value = '{$action}';
                document.forms['add'].submit();
            }
        </script>
    ";
    $table->head  = array ('', get_string('students'), $strseen, $strnote, $strgrade, $straction);
    $table->align = array ('LEFT', 'LEFT', 'CENTER', 'LEFT', 'CENTER', 'RIGHT');
    $table->width = '';
    foreach($form->appointments as $anAppointment){
        $user = get_record('user', 'id', $anAppointment->studentid);
        $anAppointment->grade = ($scheduler->scale == 0) ? get_string('nograde') : $anAppointment->grade ; 
        $attended = $anAppointment->attended ? '<img src="pix/ticked.gif" border="0">' : '<img src="pix/unticked.gif" border="0">' ;
        $cmds = "<a href=\"javascript:updateAppointment('{$anAppointment->studentid}')\" title=\"".get_string('edit')."\"><img src=\"{$CFG->pixpath}/t/edit.gif\"></a> ";
        $cmds .= "<a href=\"javascript:deleteAppointment('{$anAppointment->studentid}')\" title=\"".get_string('remove')."\"><img src=\"{$CFG->pixpath}/t/delete.gif\"></a>";
        $table->data[] = array('', fullname($user), $attended, stripslashes($anAppointment->appointmentnote), $anAppointment->grade, $cmds);
    }
    print_table($table);
} else {
    print_string('noappointments', 'scheduler');
}
echo '<br />';
$hiddenstate = ((count($form->appointments) < $form->exclusivity and $form->exclusivity > 0) || !$form->exclusivity) ? "style=\"visibility:visible\"" : "style=\"visibility:hidden\"" ;
echo "<div id=\"addappointment\" $hiddenstate ><a href=\"javascript:document.forms['add'].what.value = '{$action}' ;document.forms['add'].subaction.value = 'addappointed' ;document.forms['add'].submit();\">".get_string('appointsomeone','scheduler').'</a></div>';
print_simple_box_end();
?>
        </td>
    </tr>
</table>
<input type="submit" value="<?php  print_string('save', 'scheduler') ?>" />
<input type="button" value="<?php  print_string('cancel') ?>" onclick="self.location.href='view.php?id=<?php echo $cm->id?>'" />
</center>
</form>
