$teachers = get_records_sql($sql);
 $stream = '';
 $slots = get_records('scheduler_slots', 'schedulerid', $scheduler->id, 'starttime', 'id, starttime, duration, exclusivity, teacherid, hideuntil');
 if ($subaction == 'slots') {
     /// Making title line
     $stream .= get_string('date', 'scheduler') . $csvfieldseparator;
     $stream .= get_string('starttime', 'scheduler') . $csvfieldseparator;
     $stream .= get_string('endtime', 'scheduler') . $csvfieldseparator;
     $stream .= get_string('slottype', 'scheduler') . $csvfieldseparator;
     $stream .= get_string('students', 'scheduler') . $csvrecordseparator;
     /// Print all the lines of data.
     if (!empty($slots)) {
         foreach ($slots as $slot) {
             $appointments = get_records('scheduler_appointment', 'slotid', $slot->id);
             /// fill slot data
             $datestart = scheduler_userdate($slot->starttime, 1);
             $timestart = scheduler_usertime($slot->starttime, 1);
             $timeend = scheduler_usertime($slot->starttime + $slot->duration * 60, 1);
             $stream .= $datestart . $csvfieldseparator;
             $stream .= $timestart . $csvfieldseparator;
             $stream .= $timeend . $csvfieldseparator;
             switch ($slot->exclusivity) {
                 case 0:
                     $stream .= get_string('unlimited', 'scheduler') . $csvfieldseparator;
                     break;
                 case 1:
                     $stream .= get_string('exclusive', 'scheduler') . $csvfieldseparator;
                     break;
                 default:
                     $stream .= get_string('limited', 'scheduler') . ' ' . ($slot->exclusivity - count($appointments)) . $csvfieldseparator;
             }
     $orderlink = "<a href=\"view.php?what=viewstudent&amp;id={$cm->id}&amp;studentid=" . $studentid . "&amp;course={$scheduler->course}&amp;order=DESC&amp;page={$page}\">";
 }
 /// print page header and prepare table headers
 if ($page == 'appointments') {
     print_heading(get_string('slots', 'scheduler'));
     $table->head = array($strdate, $strstart, $strend, $strseen, $strnote, $strgrade, format_string($scheduler->staffrolename));
     $table->align = array('LEFT', 'LEFT', 'CENTER', 'CENTER', 'LEFT', 'CENTER', 'CENTER');
     $table->width = '80%';
 } else {
     print_heading(get_string('comments', 'scheduler'));
     $table->head = array(get_string('studentcomments', 'scheduler'), get_string('comments', 'scheduler'), $straction);
     $table->align = array('LEFT', 'LEFT');
     $table->width = '80%';
 }
 foreach ($slots as $slot) {
     $startdate = scheduler_userdate($slot->starttime, 1);
     $starttime = scheduler_usertime($slot->starttime, 1);
     $endtime = scheduler_usertime($slot->starttime + $slot->duration * 60, 1);
     $distributecheck = '';
     if ($page == 'appointments') {
         if (count_records('scheduler_appointment', 'slotid', $slot->id) > 1) {
             $distributecheck = "<br/><input type=\"checkbox\" name=\"distribute{$slot->appid}\" value=\"1\" /> " . get_string('distributetoslot', 'scheduler') . "\n";
         }
         //display appointments
         if ($slot->attended == 0) {
             $table->data[] = array($startdate, $starttime, $endtime, "<img src=\"pix/unticked.gif\" border=\"0\" />", $slot->appointmentnote, $slot->grade, fullname(get_record('user', 'id', $slot->teacherid)));
         } else {
             $slot->appointmentnote .= "<br/><span class=\"timelabel\">[" . userdate($slot->apptimemodified) . "]</span>";
             if ($slot->teacherid == $USER->id || $CFG->scheduler_allteachersgrading) {
                 $grade = scheduler_make_grading_menu($scheduler, 'gr' . $slot->appid, $slot->grade, true);
             } else {