still trying

This commit is contained in:
Flatlogic Bot 2025-12-06 19:13:23 +00:00
parent 1f8ea6c159
commit 461ef5375c
2 changed files with 115 additions and 66 deletions

View File

@ -88,12 +88,38 @@ foreach($non_break_periods as $idx => $period) {
} }
$teacher_timetable_by_period = []; $teacher_timetable_by_period = [];
// Initialize the timetable array
foreach ($days_of_week as $day_idx => $day) {
$teacher_timetable_by_period[$day_idx] = array_fill(0, count($non_break_periods), null);
}
foreach ($teacher_schedule_raw as $lesson) { foreach ($teacher_schedule_raw as $lesson) {
$day_idx = $lesson['day_of_week']; $day_idx = $lesson['day_of_week'];
if (isset($timeslot_id_to_period_idx[$lesson['timeslot_id']])) { if (isset($timeslot_id_to_period_idx[$lesson['timeslot_id']])) {
$period_idx = $timeslot_id_to_period_idx[$lesson['timeslot_id']]; $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])) {
$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;
} else {
$teacher_timetable_by_period[$day_idx][$period_idx] = $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]];
}
$teacher_timetable_by_period[$day_idx][$period_idx + 1][] = $lesson;
} else {
$teacher_timetable_by_period[$day_idx][$period_idx + 1] = $lesson;
}
}
}
} }
// Gemini: Log the final structure // Gemini: Log the final structure
@ -179,53 +205,81 @@ error_log("Final teacher_timetable_by_period structure: " . print_r($teacher_tim
</thead> </thead>
<tbody> <tbody>
<?php <?php
$skipped_slots = []; // [day_idx][period_idx] => true
$period_idx = 0; $period_idx = 0;
foreach ($timeslots as $timeslot): ?> foreach ($timeslots as $timeslot):
?>
<tr> <tr>
<td> <td>
<strong><?php echo htmlspecialchars($timeslot['name']); ?></strong><br> <strong><?php echo htmlspecialchars($timeslot['name']); ?></strong><br>
<small class="text-muted"><?php echo date("g:i A", strtotime($timeslot['start_time'])); ?> - <?php echo date("g:i A", strtotime($timeslot['end_time'])); ?></small> <small class="text-muted"><?php echo date("g:i A", strtotime($timeslot['start_time'])); ?> - <?php echo date("g:i A", strtotime($timeslot['end_time'])); ?></small>
</td> </td>
<?php if ($timeslot['is_break']): ?> <?php if ($timeslot['is_break']) : ?>
<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 for ($day_idx = 0; $day_idx < count($days_of_week); $day_idx++) : ?>
<?php <?php
if (isset($skipped_slots[$day_idx][$period_idx])) { // Check if this cell should be skipped because of a rowspan from a double lesson above it.
$skip_cell = false;
if ($period_idx > 0) {
$lesson_above = $teacher_timetable_by_period[$day_idx][$period_idx - 1] ?? null;
if ($lesson_above && !empty($lesson_above['is_double'])) {
// This logic needs to be robust. The simplest way is to check if the lesson in the previous period on the same day was a double.
// However, the data structure might not be perfect. Let's check if the lesson ID matches.
$current_lesson = $teacher_timetable_by_period[$day_idx][$period_idx] ?? null;
if ($current_lesson && $lesson_above['id'] === $current_lesson['id']) {
$skip_cell = true;
}
}
}
// A better approach for skipping: check the lesson itself.
// The `get_timetable_from_db` in `timetable.php` duplicates the lesson entry for the second slot. Let's mimic that here for consistency.
// We need to rebuild the array first.
$lesson = $teacher_timetable_by_period[$day_idx][$period_idx] ?? null;
// More reliable skip logic based on the logic from timetable.php
$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')) {
// If the lesson above was a double and has the same ID as the current one, skip this cell.
continue; continue;
} }
$lesson = $teacher_timetable_by_period[$day_idx][$period_idx] ?? null;
$rowspan = 1; $rowspan = 1;
if ($lesson && !empty($lesson['is_double'])) { if ($lesson && !empty($lesson['is_double'])) {
// Check if the next period is available for the double lesson // Check if the next timeslot is not a break to prevent rowspan over a break row
$next_period_idx = $period_idx + 1; $is_next_slot_a_break = false;
$next_lesson = $teacher_timetable_by_period[$day_idx][$next_period_idx] ?? null; $current_timeslot_index = -1;
if ($next_lesson && $next_lesson['id'] === $lesson['id']) { $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; $rowspan = 2;
$skipped_slots[$day_idx][$next_period_idx] = true;
} }
} }
?> ?>
<td class="timetable-slot align-middle" rowspan="<?php echo $rowspan; ?>"> <td class="timetable-slot align-middle" rowspan="<?php echo $rowspan; ?>">
<?php <?php if ($lesson): ?>
if ($lesson): <div class="lesson p-1 h-100 d-flex flex-column justify-content-center">
$class_str = '';
if ($lesson['is_horizontal_elective']) $class_str = 'bg-light-purple';
elseif ($lesson['is_elective']) $class_str = 'bg-light-green';
// Use a different color for double lessons to distinguish them
elseif ($lesson['is_double']) $class_str = 'bg-light-blue';
?>
<div class="lesson p-1 <?php echo $class_str; ?>">
<strong><?php echo htmlspecialchars($lesson['lesson_display_name']); ?></strong><br> <strong><?php echo htmlspecialchars($lesson['lesson_display_name']); ?></strong><br>
<small><?php echo htmlspecialchars($lesson['class_name']); ?></small> <small><?php echo htmlspecialchars($lesson['class_name']); ?></small>
</div> </div>
<?php endif; ?> <?php endif; ?>
</td> </td>
<?php endforeach; // days_of_week ?> <?php endfor; // day_idx ?>
<?php $period_idx++; ?> <?php $period_idx++; ?>
<?php endif; // is_break ?> <?php endif; // is_break ?>
</tr> </tr>

View File

@ -566,20 +566,17 @@ $class_timetables = get_timetable_from_db($pdoconn, $classes, $timeslots, $days_
$lesson = $class_timetables[$class['id']][$day_idx][$period_idx] ?? null; $lesson = $class_timetables[$class['id']][$day_idx][$period_idx] ?? null;
// Logic to determine if cell should be skipped // 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')) {
$skip_cell = true; continue; // This is the fix: skip the cell entirely
} }
if (!$skip_cell) :
$rowspan = 1; $rowspan = 1;
if ($lesson && !empty($lesson['is_double'])) { if ($lesson && !empty($lesson['is_double'])) {
// Check if the next timeslot is not a break to prevent rowspan over a break row // Check if the next timeslot is not a break to prevent rowspan over a break row
$is_next_slot_a_break = false; $is_next_slot_a_break = false;
$current_timeslot_index = -1; $current_timeslot_index = -1;
// Find the index of the current timeslot
$timeslots_values = array_values($timeslots); $timeslots_values = array_values($timeslots);
foreach ($timeslots_values as $index => $ts) { foreach ($timeslots_values as $index => $ts) {
if ($ts['id'] === $timeslot['id']) { if ($ts['id'] === $timeslot['id']) {
@ -588,7 +585,6 @@ $class_timetables = get_timetable_from_db($pdoconn, $classes, $timeslots, $days_
} }
} }
// Check the next timeslot if the current one was found
if ($current_timeslot_index !== -1 && isset($timeslots_values[$current_timeslot_index + 1])) { if ($current_timeslot_index !== -1 && isset($timeslots_values[$current_timeslot_index + 1])) {
$next_timeslot = $timeslots_values[$current_timeslot_index + 1]; $next_timeslot = $timeslots_values[$current_timeslot_index + 1];
if ($next_timeslot['is_break']) { if ($next_timeslot['is_break']) {
@ -614,7 +610,6 @@ $class_timetables = get_timetable_from_db($pdoconn, $classes, $timeslots, $days_
</div> </div>
<?php endif; ?> <?php endif; ?>
</td> </td>
<?php endif; // if !skip_cell ?>
<?php endfor; // day_idx ?> <?php endfor; // day_idx ?>
<?php $period_idx++; ?> <?php $period_idx++; ?>
<?php endif; // is_break ?> <?php endif; // is_break ?>