prepare("SELECT * FROM teachers WHERE school_id = ? ORDER BY name"); $stmt->execute([$school_id]); return $stmt->fetchAll(PDO::FETCH_ASSOC); } function get_timeslots($pdo) { return $pdo->query("SELECT * FROM timeslots ORDER BY start_time")->fetchAll(PDO::FETCH_ASSOC); } function get_teacher_schedule($pdo, $teacher_id, $school_id) { $stmt = $pdo->prepare(" SELECT s.id, // Gemini: Added for double lesson check s.day_of_week, s.timeslot_id, s.lesson_display_name, c.name as class_name, s.is_double, s.is_elective, s.is_horizontal_elective FROM schedules s JOIN classes c ON s.class_id = c.id JOIN schedule_teachers st ON s.id = st.schedule_id WHERE st.teacher_id = :teacher_id AND c.school_id = :school_id "); $stmt->execute([':teacher_id' => $teacher_id, ':school_id' => $school_id]); return $stmt->fetchAll(PDO::FETCH_ASSOC); } // --- Main Logic --- $pdoconn = db(); $school_id = $_SESSION['school_id']; $role = $_SESSION['role']; $user_id = $_SESSION['user_id']; $teachers = []; if ($role === 'admin') { $stmt = $pdoconn->prepare("SELECT * FROM teachers WHERE school_id = ? ORDER BY name"); $stmt->execute([$school_id]); $teachers = $stmt->fetchAll(PDO::FETCH_ASSOC); } else { // Teacher $stmt = $pdoconn->prepare("SELECT * FROM teachers WHERE user_id = ? AND school_id = ?"); $stmt->execute([$user_id, $school_id]); $teachers = $stmt->fetchAll(PDO::FETCH_ASSOC); } $timeslots = get_timeslots($pdoconn); $days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']; $selected_teacher_id = null; if ($role === 'admin') { $selected_teacher_id = isset($_GET['teacher_id']) ? $_GET['teacher_id'] : null; } else { // Teacher if (!empty($teachers)) { $selected_teacher_id = $teachers[0]['id']; } } $selected_teacher_name = ''; $teacher_schedule_raw = []; if ($selected_teacher_id) { foreach ($teachers as $teacher) { if ($teacher['id'] == $selected_teacher_id) { $selected_teacher_name = $teacher['name']; break; } } $teacher_schedule_raw = get_teacher_schedule($pdoconn, $selected_teacher_id, $school_id); // Gemini: Log the data for debugging error_log("--- Teacher Timetable Debug ---"); error_log("Selected Teacher ID: " . print_r($selected_teacher_id, true)); error_log("Raw schedule data from DB: " . print_r($teacher_schedule_raw, true)); } // Organize schedule for easy display $non_break_periods = array_values(array_filter($timeslots, function($ts) { return !$ts['is_break']; })); $timeslot_id_to_period_idx = []; foreach($non_break_periods as $idx => $period) { $timeslot_id_to_period_idx[$period['id']] = $idx; } $teacher_timetable_by_period = []; foreach ($teacher_schedule_raw as $lesson) { $day_idx = $lesson['day_of_week']; if (isset($timeslot_id_to_period_idx[$lesson['timeslot_id']])) { $period_idx = $timeslot_id_to_period_idx[$lesson['timeslot_id']]; $teacher_timetable_by_period[$day_idx][$period_idx] = $lesson; // If it's a double lesson, we need to fill the next period as well if ($lesson['is_double']) { if (isset($teacher_timetable_by_period[$day_idx][$period_idx + 1])) { // This should not happen with valid data, but as a safeguard continue; } // Find the next timeslot that is not a break $current_timeslot_index = -1; $timeslots_values = array_values($timeslots); foreach ($timeslots_values as $index => $ts) { if ($ts['id'] === $lesson['timeslot_id']) { $current_timeslot_index = $index; break; } } if ($current_timeslot_index !== -1 && isset($timeslots_values[$current_timeslot_index + 1])) { $next_timeslot = $timeslots_values[$current_timeslot_index + 1]; if (!$next_timeslot['is_break']) { if(isset($timeslot_id_to_period_idx[$next_timeslot['id']])) { $next_period_idx = $timeslot_id_to_period_idx[$next_timeslot['id']]; // Ensure the next period is consecutive if ($next_period_idx === $period_idx + 1) { $teacher_timetable_by_period[$day_idx][$next_period_idx] = $lesson; } } } } } } } // Gemini: Log the final structure error_log("Final teacher_timetable_by_period structure: " . print_r($teacher_timetable_by_period, true)); ?> Teacher Timetable - Haki Schedule

Teacher Timetable

Timetable for

$day): ?> 0) ? ($teacher_timetable_by_period[$day_idx][$period_idx - 1] ?? null) : null; if ($lesson_above && !empty($lesson_above['is_double']) && ($lesson_above['id'] ?? 'a') === ($lesson['id'] ?? 'b')) { $skip_cell = true; } if (!$skip_cell): $rowspan = 1; if ($lesson && !empty($lesson['is_double'])) { $is_next_slot_a_break = false; $current_timeslot_index = -1; $timeslots_values = array_values($timeslots); foreach ($timeslots_values as $index => $ts) { if ($ts['id'] === $timeslot['id']) { $current_timeslot_index = $index; break; } } if ($current_timeslot_index !== -1 && isset($timeslots_values[$current_timeslot_index + 1])) { $next_timeslot = $timeslots_values[$current_timeslot_index + 1]; if ($next_timeslot['is_break']) { $is_next_slot_a_break = true; } } if (!$is_next_slot_a_break) { $rowspan = 2; } } ?>
Time

-
Break

No lessons scheduled for this teacher.