diff --git a/teacher_timetable.php b/teacher_timetable.php index d86497a..53e2965 100644 --- a/teacher_timetable.php +++ b/teacher_timetable.php @@ -16,21 +16,20 @@ function get_timeslots($pdo) { function get_teacher_schedule($pdo, $teacher_id) { $sql = " + -- Get all lessons (elective and non-elective) for a teacher SELECT s.id, s.day_of_week, s.timeslot_id, s.lesson_display_name, c.name as class_name, - sub.name as subject_name, -- The specific subject for the teacher s.is_double, - s.is_elective + s.is_elective, + eg.name as elective_group_name FROM schedules s JOIN schedule_teachers st ON s.id = st.schedule_id LEFT JOIN classes c ON s.class_id = c.id - -- Find the specific subject this teacher teaches to this class from workloads - LEFT JOIN workloads w ON st.teacher_id = w.teacher_id AND s.class_id = w.class_id - LEFT JOIN subjects sub ON w.subject_id = sub.id + LEFT JOIN elective_groups eg ON s.elective_group_id = eg.id WHERE st.teacher_id = :teacher_id ORDER BY s.day_of_week, s.timeslot_id "; @@ -40,6 +39,7 @@ function get_teacher_schedule($pdo, $teacher_id) { return $stmt->fetchAll(PDO::FETCH_ASSOC); } + // --- Main Logic --- $pdoconn = db(); $school_id = $_SESSION['school_id']; @@ -79,11 +79,6 @@ if ($selected_teacher_id) { } } $teacher_schedule_raw = get_teacher_schedule($pdoconn, $selected_teacher_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 @@ -105,9 +100,7 @@ foreach ($teacher_schedule_raw as $lesson) { $period_idx = $timeslot_id_to_period_idx[$lesson['timeslot_id']]; if (isset($teacher_timetable_by_period[$day_idx][$period_idx])) { - // This slot is already filled, potentially by a multi-class elective. - // Create an array if it's not already one. - if (!is_array($teacher_timetable_by_period[$day_idx][$period_idx])) { + if (!is_array($teacher_timetable_by_period[$day_idx][$period_idx]) || !isset($teacher_timetable_by_period[$day_idx][$period_idx][0])) { $teacher_timetable_by_period[$day_idx][$period_idx] = [$teacher_timetable_by_period[$day_idx][$period_idx]]; } $teacher_timetable_by_period[$day_idx][$period_idx][] = $lesson; @@ -115,22 +108,20 @@ foreach ($teacher_schedule_raw as $lesson) { $teacher_timetable_by_period[$day_idx][$period_idx] = $lesson; } - if (!empty($lesson['is_double']) && isset($teacher_timetable_by_period[$day_idx][$period_idx + 1])) { - if (isset($teacher_timetable_by_period[$day_idx][$period_idx + 1])) { - if (!is_array($teacher_timetable_by_period[$day_idx][$period_idx + 1])) { - $teacher_timetable_by_period[$day_idx][$period_idx + 1] = [$teacher_timetable_by_period[$day_idx][$period_idx + 1]]; + if (!empty($lesson['is_double']) && isset($non_break_periods[$period_idx + 1])) { + $next_period_idx = $period_idx + 1; + if (isset($teacher_timetable_by_period[$day_idx][$next_period_idx])) { + if (!is_array($teacher_timetable_by_period[$day_idx][$next_period_idx]) || !isset($teacher_timetable_by_period[$day_idx][$next_period_idx][0])) { + $teacher_timetable_by_period[$day_idx][$next_period_idx] = [$teacher_timetable_by_period[$day_idx][$next_period_idx]]; } - $teacher_timetable_by_period[$day_idx][$period_idx + 1][] = $lesson; + $teacher_timetable_by_period[$day_idx][$next_period_idx][] = $lesson; } else { - $teacher_timetable_by_period[$day_idx][$period_idx + 1] = $lesson; + $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)); - ?> @@ -196,7 +187,6 @@ error_log("Final teacher_timetable_by_period structure: " . print_r($teacher_tim $period_indices = array_keys($non_break_periods); foreach ($period_indices as $period_idx) { $current_timeslot_id = $non_break_periods[$period_idx]['id']; - // Find the full timeslot info, including breaks $timeslot_info = null; foreach ($timeslots as $ts) { if ($ts['id'] === $current_timeslot_id) { @@ -205,9 +195,8 @@ error_log("Final teacher_timetable_by_period structure: " . print_r($teacher_tim } } - // Find if there is a break before this timeslot $break_html = ''; - $last_period_end_time = null; + $last_period_end_time = '00:00:00'; if ($period_idx > 0) { $prev_period_id = $non_break_periods[$period_idx - 1]['id']; foreach($timeslots as $ts) { @@ -236,23 +225,55 @@ error_log("Final teacher_timetable_by_period structure: " . print_r($teacher_tim $day): ?>