still trying to fix

This commit is contained in:
Flatlogic Bot 2025-12-06 17:01:21 +00:00
parent 14a23a9f38
commit be54ee0bce

View File

@ -316,15 +316,12 @@ function save_timetable($pdo, $class_timetables, $timeslots) {
} }
function get_timetable_from_db($pdo, $classes, $timeslots) { function get_timetable_from_db($pdo, $classes, $timeslots) {
error_log("get_timetable_from_db: Fetching saved lessons...");
$stmt = $pdo->query('SELECT * FROM schedules ORDER BY id'); $stmt = $pdo->query('SELECT * FROM schedules ORDER BY id');
$saved_lessons = $stmt->fetchAll(PDO::FETCH_ASSOC); $saved_lessons = $stmt->fetchAll(PDO::FETCH_ASSOC);
if (empty($saved_lessons)) { if (empty($saved_lessons)) {
error_log("get_timetable_from_db: No saved lessons found in the database.");
return []; return [];
} }
error_log("get_timetable_from_db: Found " . count($saved_lessons) . " saved lesson records.");
$days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']; $days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
$periods = array_values(array_filter($timeslots, function($ts) { return !$ts['is_break']; })); $periods = array_values(array_filter($timeslots, function($ts) { return !$ts['is_break']; }));
@ -339,8 +336,6 @@ function get_timetable_from_db($pdo, $classes, $timeslots) {
foreach($periods as $idx => $period) { foreach($periods as $idx => $period) {
$timeslot_id_to_period_idx[$period['id']] = $idx; $timeslot_id_to_period_idx[$period['id']] = $idx;
} }
error_log("get_timetable_from_db: Timeslot to Period Index Map: " . print_r($timeslot_id_to_period_idx, true));
foreach ($saved_lessons as $lesson) { foreach ($saved_lessons as $lesson) {
$class_id = $lesson['class_id']; $class_id = $lesson['class_id'];
@ -348,11 +343,9 @@ function get_timetable_from_db($pdo, $classes, $timeslots) {
$timeslot_id = $lesson['timeslot_id']; $timeslot_id = $lesson['timeslot_id'];
if (!isset($timeslot_id_to_period_idx[$timeslot_id])) { if (!isset($timeslot_id_to_period_idx[$timeslot_id])) {
error_log("get_timetable_from_db: SKIPPING lesson ID {$lesson['id']} - could not find period index for timeslot ID {$timeslot_id}.");
continue; continue;
} }
if (!isset($class_timetables[$class_id])) { if (!isset($class_timetables[$class_id])) {
error_log("get_timetable_from_db: SKIPPING lesson ID {$lesson['id']} - could not find class timetable for class ID {$class_id}.");
continue; continue;
} }
@ -372,7 +365,6 @@ function get_timetable_from_db($pdo, $classes, $timeslots) {
} }
} }
error_log("get_timetable_from_db: Final processed timetables structure: " . print_r($class_timetables, true));
return $class_timetables; return $class_timetables;
} }
@ -390,10 +382,14 @@ if (isset($_POST['generate'])) {
if (!empty($workloads)) { if (!empty($workloads)) {
$class_timetables = generate_timetable($all_data, $days_of_week); $class_timetables = generate_timetable($all_data, $days_of_week);
save_timetable($pdoconn, $class_timetables, $timeslots); save_timetable($pdoconn, $class_timetables, $timeslots);
// Redirect to the same page to prevent form resubmission and show the new timetable
header("Location: timetable.php");
exit;
} }
} else {
$class_timetables = get_timetable_from_db($pdoconn, $classes, $timeslots);
} }
// Always fetch the latest timetable from the database for display
$class_timetables = get_timetable_from_db($pdoconn, $classes, $timeslots);
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">