still working

This commit is contained in:
Flatlogic Bot 2025-12-06 19:01:17 +00:00
parent 77833a9ac9
commit a254737287
2 changed files with 133 additions and 56 deletions

View File

@ -17,6 +17,7 @@ function get_timeslots($pdo) {
function get_teacher_schedule($pdo, $teacher_id, $school_id) { function get_teacher_schedule($pdo, $teacher_id, $school_id) {
$stmt = $pdo->prepare(" $stmt = $pdo->prepare("
SELECT SELECT
s.id, // Gemini: Added for double lesson check
s.day_of_week, s.day_of_week,
s.timeslot_id, s.timeslot_id,
s.lesson_display_name, s.lesson_display_name,
@ -80,17 +81,53 @@ if ($selected_teacher_id) {
} }
// Organize schedule for easy display // Organize schedule for easy display
$teacher_timetable = []; $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) { foreach ($teacher_schedule_raw as $lesson) {
$day_idx = $lesson['day_of_week']; // Use the 0-indexed value directly $day_idx = $lesson['day_of_week'];
if (!isset($teacher_timetable[$day_idx])) { if (isset($timeslot_id_to_period_idx[$lesson['timeslot_id']])) {
$teacher_timetable[$day_idx] = []; $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;
}
}
}
}
}
} }
$teacher_timetable[$day_idx][$lesson['timeslot_id']] = $lesson;
} }
// Gemini: Log the final structure // Gemini: Log the final structure
error_log("Final teacher_timetable structure: " . print_r($teacher_timetable, true)); error_log("Final teacher_timetable_by_period structure: " . print_r($teacher_timetable_by_period, true));
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
@ -171,7 +208,9 @@ error_log("Final teacher_timetable structure: " . print_r($teacher_timetable, tr
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<?php foreach ($timeslots as $timeslot): ?> <?php
$period_idx = 0;
foreach ($timeslots as $timeslot): ?>
<tr> <tr>
<td> <td>
<strong><?php echo htmlspecialchars($timeslot['name']); ?></strong><br> <strong><?php echo htmlspecialchars($timeslot['name']); ?></strong><br>
@ -181,9 +220,41 @@ error_log("Final teacher_timetable structure: " . print_r($teacher_timetable, tr
<td colspan="<?php echo count($days_of_week); ?>" class="text-center table-secondary"><strong>Break</strong></td> <td colspan="<?php echo count($days_of_week); ?>" class="text-center table-secondary"><strong>Break</strong></td>
<?php else: ?> <?php else: ?>
<?php foreach ($days_of_week as $day_idx => $day): ?> <?php foreach ($days_of_week as $day_idx => $day): ?>
<td class="timetable-slot"> <?php
$lesson = $teacher_timetable_by_period[$day_idx][$period_idx] ?? null;
// Logic to skip rendering the cell if it's the second part of a double lesson
$skip_cell = false;
$lesson_above = ($period_idx > 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;
}
}
?>
<td class="timetable-slot align-middle" rowspan="<?php echo $rowspan; ?>">
<?php <?php
$lesson = $teacher_timetable[$day_idx][$timeslot['id']] ?? null;
if ($lesson): if ($lesson):
$class_str = ''; $class_str = '';
if ($lesson['is_horizontal_elective']) $class_str = 'bg-light-purple'; if ($lesson['is_horizontal_elective']) $class_str = 'bg-light-purple';
@ -196,10 +267,12 @@ error_log("Final teacher_timetable structure: " . print_r($teacher_timetable, tr
</div> </div>
<?php endif; ?> <?php endif; ?>
</td> </td>
<?php endforeach; ?> <?php endif; // if !skip_cell ?>
<?php endif; ?> <?php endforeach; // days_of_week ?>
<?php $period_idx++; ?>
<?php endif; // is_break ?>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; // timeslots ?>
</tbody> </tbody>
</table> </table>
</div> </div>

View File

@ -565,57 +565,61 @@ $class_timetables = get_timetable_from_db($pdoconn, $classes, $timeslots, $days_
<?php <?php
$lesson = $class_timetables[$class['id']][$day_idx][$period_idx] ?? null; $lesson = $class_timetables[$class['id']][$day_idx][$period_idx] ?? null;
// Logic to skip rendering the cell if it's the second part of a double lesson // Logic to determine if cell should be skipped
$skip_cell = false;
$lesson_above = ($period_idx > 0) ? ($class_timetables[$class['id']][$day_idx][$period_idx - 1] ?? null) : null; $lesson_above = ($period_idx > 0) ? ($class_timetables[$class['id']][$day_idx][$period_idx - 1] ?? null) : null;
if ($lesson_above && !empty($lesson_above['is_double']) && ($lesson_above['id'] ?? 'a') === ($lesson['id'] ?? 'b')) { if ($lesson_above && !empty($lesson_above['is_double']) && ($lesson_above['id'] ?? 'a') === ($lesson['id'] ?? 'b')) {
continue; // This cell is covered by a rowspan from the lesson above. $skip_cell = true;
} }
$rowspan = 1; if (!$skip_cell) :
if ($lesson && !empty($lesson['is_double'])) { $rowspan = 1;
// Check if the next timeslot is not a break to prevent rowspan over a break row if ($lesson && !empty($lesson['is_double'])) {
$is_next_slot_a_break = false; // Check if the next timeslot is not a break to prevent rowspan over a break row
$current_timeslot_index = -1; $is_next_slot_a_break = false;
$current_timeslot_index = -1;
// Find the index of the current timeslot // Find the index of the current timeslot
foreach (array_values($timeslots) as $index => $ts) { $timeslots_values = array_values($timeslots);
if ($ts['id'] === $timeslot['id']) { foreach ($timeslots_values as $index => $ts) {
$current_timeslot_index = $index; if ($ts['id'] === $timeslot['id']) {
break; $current_timeslot_index = $index;
break;
}
}
// Check the next timeslot if the current one was found
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;
} }
} }
// Check the next timeslot if the current one was found
if ($current_timeslot_index !== -1 && isset(array_values($timeslots)[$current_timeslot_index + 1])) {
$next_timeslot = array_values($timeslots)[$current_timeslot_index + 1];
if ($next_timeslot['is_break']) {
$is_next_slot_a_break = true;
}
}
if (!$is_next_slot_a_break) {
$rowspan = 2;
}
}
?>
<td class="timetable-slot align-middle" rowspan="<?php echo $rowspan; ?>">
<?php
if ($lesson) :
$css_class = 'lesson p-1 h-100 d-flex flex-column justify-content-center';
if (!empty($lesson['is_elective'])) $css_class .= ' is-elective';
if (!empty($lesson['is_double'])) $css_class .= ' is-double';
?> ?>
<div class="<?php echo $css_class; ?>" data-lesson-id="<?php echo $lesson['id'] ?? ''; ?>"> <td class="timetable-slot align-middle" rowspan="<?php echo $rowspan; ?>">
<strong><?php echo htmlspecialchars($lesson['subject_name']); ?></strong><br> <?php
<small><?php echo htmlspecialchars($lesson['teacher_name']); ?></small> if ($lesson) :
</div> $css_class = 'lesson p-1 h-100 d-flex flex-column justify-content-center';
<?php endif; ?> if (!empty($lesson['is_elective'])) $css_class .= ' is-elective';
</td> if (!empty($lesson['is_double'])) $css_class .= ' is-double';
<?php endfor; ?> ?>
<div class="<?php echo $css_class; ?>" data-lesson-id="<?php echo $lesson['id'] ?? ''; ?>">
<strong><?php echo htmlspecialchars($lesson['subject_name']); ?></strong><br>
<small><?php echo htmlspecialchars($lesson['teacher_name']); ?></small>
</div>
<?php endif; ?>
</td>
<?php endif; // if !skip_cell ?>
<?php endfor; // day_idx ?>
<?php $period_idx++; ?> <?php $period_idx++; ?>
<?php endif; ?> <?php endif; // is_break ?>
</tr> </tr>
<?php endforeach; ?> <?php endforeach; // timeslots ?>
</tbody> </tbody>
</table> </table>
</div> </div>