timetables should be complete now
This commit is contained in:
parent
4687d48132
commit
851b3613d1
@ -70,9 +70,11 @@ try {
|
||||
<li><a class="dropdown-item" href="/admin_subjects.php">Subjects</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_teachers.php">Teachers</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_timeslots.php">Timeslots</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Class Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/teacher_timetable.php">Teacher Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item"><a class="nav-link" href="/demo.php">Demo</a></li>
|
||||
|
||||
@ -104,9 +104,11 @@ try {
|
||||
<li><a class="dropdown-item active" href="/admin_subjects.php">Subjects</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_teachers.php">Teachers</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_timeslots.php">Timeslots</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Class Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/teacher_timetable.php">Teacher Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item"><a class="nav-link" href="/demo.php">Demo</a></li>
|
||||
|
||||
@ -74,9 +74,11 @@ try {
|
||||
<li><a class="dropdown-item" href="/admin_subjects.php">Subjects</a></li>
|
||||
<li><a class="dropdown-item active" href="/admin_teachers.php">Teachers</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_timeslots.php">Timeslots</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Class Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/teacher_timetable.php">Teacher Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item"><a class="nav-link" href="/demo.php">Demo</a></li>
|
||||
|
||||
219
admin_timeslots.php
Normal file
219
admin_timeslots.php
Normal file
@ -0,0 +1,219 @@
|
||||
<?php
|
||||
require_once __DIR__ . '/includes/auth_check.php';
|
||||
require_once __DIR__ . '/db/config.php';
|
||||
|
||||
$message = '';
|
||||
$error = '';
|
||||
$editing_timeslot = null;
|
||||
|
||||
$pdo = db();
|
||||
|
||||
// Handle Edit request
|
||||
if (isset($_GET['edit_id'])) {
|
||||
try {
|
||||
$edit_id = $_GET['edit_id'];
|
||||
$stmt = $pdo->prepare("SELECT * FROM timeslots WHERE id = ?");
|
||||
$stmt->execute([$edit_id]);
|
||||
$editing_timeslot = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
$error = "Error fetching timeslot: " . $e->getMessage();
|
||||
}
|
||||
}
|
||||
|
||||
// Handle POST request
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
if (isset($_POST['delete_id'])) {
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->prepare("DELETE FROM timeslots WHERE id = ?");
|
||||
if ($stmt->execute([$_POST['delete_id']])) {
|
||||
$message = 'Timeslot deleted successfully!';
|
||||
} else {
|
||||
$error = 'Failed to delete timeslot.';
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$error = 'Database error: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
if (isset($_POST['add_timeslot'])) {
|
||||
$name = trim($_POST['name']);
|
||||
$start_time = $_POST['start_time'];
|
||||
$end_time = $_POST['end_time'];
|
||||
$is_break = isset($_POST['is_break']) ? 1 : 0;
|
||||
$timeslot_id = $_POST['timeslot_id'] ?? null;
|
||||
|
||||
if (empty($name) || empty($start_time) || empty($end_time)) {
|
||||
$error = 'All fields are required.';
|
||||
} else {
|
||||
try {
|
||||
if ($timeslot_id) {
|
||||
// Update existing timeslot
|
||||
$stmt = $pdo->prepare("UPDATE timeslots SET name = ?, start_time = ?, end_time = ?, is_break = ? WHERE id = ?");
|
||||
$stmt->execute([$name, $start_time, $end_time, $is_break, $timeslot_id]);
|
||||
$message = "Timeslot updated successfully!";
|
||||
} else {
|
||||
// Insert new timeslot
|
||||
$stmt = $pdo->prepare("INSERT INTO timeslots (name, start_time, end_time, is_break) VALUES (?, ?, ?, ?)");
|
||||
$stmt->execute([$name, $start_time, $end_time, $is_break]);
|
||||
$message = "Timeslot created successfully!";
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
$error = 'Database error: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Fetch all timeslots
|
||||
$timeslots = [];
|
||||
try {
|
||||
$pdo = db();
|
||||
$stmt = $pdo->query("SELECT * FROM timeslots ORDER BY start_time");
|
||||
$timeslots = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
} catch (PDOException $e) {
|
||||
$error = 'Database error: ' . $e->getMessage();
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Admin: Manage Timeslots - Haki Schedule</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-white sticky-top shadow-sm">
|
||||
<div class="container">
|
||||
<a class="navbar-brand fw-bold" href="/">Haki Schedule</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
|
||||
<?php if (isset($_SESSION['user_id'])): ?>
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle active" href="#" id="manageDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
Manage
|
||||
</a>
|
||||
<ul class="dropdown-menu" aria-labelledby="manageDropdown">
|
||||
<li><a class="dropdown-item" href="/admin_classes.php">Classes</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_subjects.php">Subjects</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_teachers.php">Teachers</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item active" href="/admin_timeslots.php">Timeslots</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Class Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/teacher_timetable.php">Teacher Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item"><a class="nav-link" href="/demo.php">Demo</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/login.php">Login</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/register.php">Register</a></li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<main class="container py-5">
|
||||
<div class="row justify-content-center">
|
||||
<div class="col-lg-8">
|
||||
<h1 class="h2 fw-bold mb-4">Manage Timeslots</h1>
|
||||
|
||||
<?php if ($message): ?>
|
||||
<div class="alert alert-success"><?php echo $message; ?></div>
|
||||
<?php endif; ?>
|
||||
<?php if ($error): ?>
|
||||
<div class="alert alert-danger"><?php echo $error; ?></div>
|
||||
<?php endif; ?>
|
||||
|
||||
<!-- Create Timeslot Form -->
|
||||
<div class="card mb-4">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title"><?php echo $editing_timeslot ? 'Edit Timeslot' : 'Create a New Timeslot'; ?></h5>
|
||||
<form action="admin_timeslots.php" method="POST">
|
||||
<input type="hidden" name="timeslot_id" value="<?php echo $editing_timeslot['id'] ?? ''; ?>">
|
||||
<div class="row">
|
||||
<div class="col-md-6 mb-3">
|
||||
<label for="name" class="form-label">Period Name</label>
|
||||
<input type="text" class="form-control" id="name" name="name" placeholder="e.g., Period 1, Lunch" value="<?php echo htmlspecialchars($editing_timeslot['name'] ?? ''); ?>" required>
|
||||
</div>
|
||||
<div class="col-md-3 mb-3">
|
||||
<label for="start_time" class="form-label">Start Time</label>
|
||||
<input type="time" class="form-control" id="start_time" name="start_time" value="<?php echo htmlspecialchars($editing_timeslot['start_time'] ?? ''); ?>" required>
|
||||
</div>
|
||||
<div class="col-md-3 mb-3">
|
||||
<label for="end_time" class="form-label">End Time</label>
|
||||
<input type="time" class_="form-control" id="end_time" name="end_time" value="<?php echo htmlspecialchars($editing_timeslot['end_time'] ?? ''); ?>" required>
|
||||
</div>
|
||||
</div>
|
||||
<div class="mb-3 form-check">
|
||||
<input type="checkbox" class="form-check-input" id="is_break" name="is_break" value="1" <?php echo !empty($editing_timeslot['is_break']) ? 'checked' : ''; ?>>
|
||||
<label class="form-check-label" for="is_break">This is a break</label>
|
||||
</div>
|
||||
<button type="submit" name="add_timeslot" class="btn btn-primary"><?php echo $editing_timeslot ? 'Update Timeslot' : 'Create Timeslot'; ?></button>
|
||||
<?php if ($editing_timeslot): ?>
|
||||
<a href="admin_timeslots.php" class="btn btn-secondary">Cancel Edit</a>
|
||||
<?php endif; ?>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Existing Timeslots List -->
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<h5 class="card-title">Existing Timeslots</h5>
|
||||
<?php if (empty($timeslots)): ?>
|
||||
<p class="text-muted">No timeslots have been created yet.</p>
|
||||
<?php else: ?>
|
||||
<table class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Name</th>
|
||||
<th>Start Time</th>
|
||||
<th>End Time</th>
|
||||
<th>Type</th>
|
||||
<th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($timeslots as $timeslot): ?>
|
||||
<tr>
|
||||
<td><?php echo htmlspecialchars($timeslot['name']); ?></td>
|
||||
<td><?php echo date("g:i A", strtotime($timeslot['start_time'])); ?></td>
|
||||
<td><?php echo date("g:i A", strtotime($timeslot['end_time'])); ?></td>
|
||||
<td><?php echo $timeslot['is_break'] ? 'Break' : 'Lesson'; ?></td>
|
||||
<td>
|
||||
<a href="?edit_id=<?php echo $timeslot['id']; ?>" class="btn btn-sm btn-outline-secondary">Edit</a>
|
||||
<form action="admin_timeslots.php" method="POST" class="d-inline" onsubmit="return confirm('Are you sure you want to delete this timeslot?');">
|
||||
<input type="hidden" name="delete_id" value="<?php echo $timeslot['id']; ?>">
|
||||
<button type="submit" class="btn btn-sm btn-outline-danger">Delete</button>
|
||||
</form>
|
||||
</td>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
</main>
|
||||
|
||||
<footer class="bg-dark text-white py-4 mt-5">
|
||||
<div class="container text-center">
|
||||
<p>© <?php echo date("Y"); ?> Haki Schedule. All Rights Reserved.</p>
|
||||
</div>
|
||||
</footer>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@ -126,10 +126,12 @@ try {
|
||||
<li><a class="dropdown-item" href="/admin_classes.php">Classes</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_subjects.php">Subjects</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_teachers.php">Teachers</a></li>
|
||||
<li><a class="dropdown-item active" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_timeslots.php">Timeslots</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Class Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/teacher_timetable.php">Teacher Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item"><a class="nav-link" href="/demo.php">Demo</a></li>
|
||||
|
||||
52
api/move_lesson.php
Normal file
52
api/move_lesson.php
Normal file
@ -0,0 +1,52 @@
|
||||
<?php
|
||||
header('Content-Type: application/json');
|
||||
require_once '../db/config.php';
|
||||
require_once '../includes/auth_check.php';
|
||||
|
||||
$response = ['success' => false, 'error' => 'Invalid request'];
|
||||
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$data = json_decode(file_get_contents('php://input'), true);
|
||||
|
||||
$lesson_id = $data['lesson_id'] ?? null;
|
||||
$to_class_id = $data['to_class_id'] ?? null;
|
||||
$to_day = $data['to_day'] ?? null;
|
||||
$to_timeslot_id = $data['to_timeslot_id'] ?? null;
|
||||
|
||||
if ($lesson_id && $to_class_id && isset($to_day) && $to_timeslot_id) {
|
||||
try {
|
||||
$pdo = db();
|
||||
|
||||
// TODO: Add validation logic here to check for conflicts
|
||||
|
||||
$stmt = $pdo->prepare(
|
||||
'UPDATE schedules SET class_id = :class_id, day_of_week = :day, timeslot_id = :timeslot_id WHERE id = :lesson_id'
|
||||
);
|
||||
|
||||
$stmt->execute([
|
||||
':class_id' => $to_class_id,
|
||||
':day' => $to_day,
|
||||
':timeslot_id' => $to_timeslot_id,
|
||||
':lesson_id' => $lesson_id
|
||||
]);
|
||||
|
||||
if ($stmt->rowCount() > 0) {
|
||||
$response = ['success' => true];
|
||||
} else {
|
||||
$response['error'] = 'Failed to move lesson. No rows were updated.';
|
||||
}
|
||||
|
||||
} catch (PDOException $e) {
|
||||
// Check for unique constraint violation
|
||||
if ($e->getCode() == 23000) { // SQLSTATE for integrity constraint violation
|
||||
$response['error'] = 'This time slot is already occupied.';
|
||||
} else {
|
||||
$response['error'] = 'Database error: ' . $e->getMessage();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
$response['error'] = 'Missing required data.';
|
||||
}
|
||||
}
|
||||
|
||||
echo json_encode($response);
|
||||
7
db/migrations/007_create_timeslots_table.sql
Normal file
7
db/migrations/007_create_timeslots_table.sql
Normal file
@ -0,0 +1,7 @@
|
||||
CREATE TABLE IF NOT EXISTS timeslots (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
name VARCHAR(255) NOT NULL,
|
||||
start_time TIME NOT NULL,
|
||||
end_time TIME NOT NULL,
|
||||
is_break BOOLEAN NOT NULL DEFAULT 0
|
||||
);
|
||||
18
db/migrations/008_create_schedules_table.sql
Normal file
18
db/migrations/008_create_schedules_table.sql
Normal file
@ -0,0 +1,18 @@
|
||||
CREATE TABLE IF NOT EXISTS `schedules` (
|
||||
`id` INT AUTO_INCREMENT PRIMARY KEY,
|
||||
`class_id` INT NOT NULL,
|
||||
`day_of_week` INT NOT NULL,
|
||||
`timeslot_id` INT NOT NULL,
|
||||
`subject_id` INT,
|
||||
`teacher_id` INT,
|
||||
`lesson_display_name` VARCHAR(255) NOT NULL,
|
||||
`teacher_display_name` VARCHAR(255) NOT NULL,
|
||||
`is_double` BOOLEAN DEFAULT FALSE,
|
||||
`is_elective` BOOLEAN DEFAULT FALSE,
|
||||
`is_horizontal_elective` BOOLEAN DEFAULT FALSE,
|
||||
UNIQUE KEY `unique_schedule_entry` (`class_id`, `day_of_week`, `timeslot_id`),
|
||||
FOREIGN KEY (`class_id`) REFERENCES `classes`(`id`) ON DELETE CASCADE,
|
||||
FOREIGN KEY (`timeslot_id`) REFERENCES `timeslots`(`id`) ON DELETE CASCADE,
|
||||
FOREIGN KEY (`subject_id`) REFERENCES `subjects`(`id`) ON DELETE SET NULL,
|
||||
FOREIGN KEY (`teacher_id`) REFERENCES `teachers`(`id`) ON DELETE SET NULL
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
||||
251
teacher_timetable.php
Normal file
251
teacher_timetable.php
Normal file
@ -0,0 +1,251 @@
|
||||
<?php
|
||||
session_start();
|
||||
require_once 'includes/auth_check.php';
|
||||
require_once 'db/config.php';
|
||||
|
||||
// --- Database Fetch Functions ---
|
||||
function get_teachers($pdo) {
|
||||
return $pdo->query("SELECT * FROM teachers ORDER BY name")->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) {
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT
|
||||
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
|
||||
WHERE s.teacher_id = :teacher_id
|
||||
");
|
||||
$stmt->execute([':teacher_id' => $teacher_id]);
|
||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
// --- Main Logic ---
|
||||
$pdoconn = db();
|
||||
$teachers = get_teachers($pdoconn);
|
||||
$timeslots = get_timeslots($pdoconn);
|
||||
$days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
|
||||
|
||||
$selected_teacher_id = isset($_GET['teacher_id']) ? $_GET['teacher_id'] : null;
|
||||
$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);
|
||||
}
|
||||
|
||||
// Organize schedule for easy display
|
||||
$teacher_timetable = array_fill(0, count($days_of_week), []);
|
||||
foreach ($timeslots as $timeslot) {
|
||||
if (!$timeslot['is_break']) {
|
||||
foreach ($days_of_week as $day_idx => $day) {
|
||||
$teacher_timetable[$day_idx][$timeslot['id']] = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($teacher_schedule_raw as $lesson) {
|
||||
$day_idx = $lesson['day_of_week'];
|
||||
$timeslot_id = $lesson['timeslot_id'];
|
||||
if (isset($teacher_timetable[$day_idx][$timeslot_id])) {
|
||||
$teacher_timetable[$day_idx][$timeslot_id] = $lesson;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Teacher Timetable - Haki Schedule</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
|
||||
<style>
|
||||
@media print {
|
||||
body * {
|
||||
visibility: hidden;
|
||||
}
|
||||
#timetable-container, #timetable-container * {
|
||||
visibility: visible;
|
||||
}
|
||||
#timetable-container {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
}
|
||||
.card {
|
||||
border: 1px solid #dee2e6 !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-white sticky-top shadow-sm">
|
||||
<div class="container">
|
||||
<a class="navbar-brand fw-bold" href="/">Haki Schedule</a>
|
||||
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
|
||||
<span class="navbar-toggler-icon"></span>
|
||||
</button>
|
||||
<div class="collapse navbar-collapse" id="navbarNav">
|
||||
<ul class="navbar-nav ms-auto">
|
||||
<li class="nav-item"><a class="nav-link" href="/">Home</a></li>
|
||||
<?php if (isset($_SESSION['user_id'])): ?>
|
||||
<li class="nav-item dropdown">
|
||||
<a class="nav-link dropdown-toggle" href="#" id="manageDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
|
||||
Manage
|
||||
</a>
|
||||
<ul class="dropdown-menu" aria-labelledby="manageDropdown">
|
||||
<li><a class="dropdown-item" href="/admin_classes.php">Classes</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_subjects.php">Subjects</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_teachers.php">Teachers</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_timeslots.php">Timeslots</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link" href="/timetable.php">Class Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link active" href="/teacher_timetable.php">Teacher Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item"><a class="nav-link" href="/demo.php">Demo</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/login.php">Login</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/register.php">Register</a></li>
|
||||
<?php endif; ?>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div class="container mt-4">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h1>Teacher Timetable</h1>
|
||||
<?php if ($selected_teacher_id): ?>
|
||||
<div class="d-flex gap-2">
|
||||
<button id="print-btn" class="btn btn-secondary">Print</button>
|
||||
<button id="download-btn" class="btn btn-secondary">Download as PDF</button>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<form method="GET" action="" class="mb-4">
|
||||
<div class="row">
|
||||
<div class="col-md-4">
|
||||
<label for="teacher_id" class="form-label">Select Teacher</label>
|
||||
<select name="teacher_id" id="teacher_id" class="form-select" onchange="this.form.submit()">
|
||||
<option value="">-- Select a Teacher --</option>
|
||||
<?php foreach ($teachers as $teacher): ?>
|
||||
<option value="<?php echo $teacher['id']; ?>" <?php echo ($selected_teacher_id == $teacher['id']) ? 'selected' : ''; ?>>
|
||||
<?php echo htmlspecialchars($teacher['name']); ?>
|
||||
</option>
|
||||
<?php endforeach; ?>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<div id="timetable-container">
|
||||
<?php if ($selected_teacher_id && !empty($teacher_schedule_raw)): ?>
|
||||
<h3 class="mt-4">Timetable for <?php echo htmlspecialchars($selected_teacher_name); ?></h3>
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<table class="table table-bordered text-center">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 12%;">Time</th>
|
||||
<?php foreach ($days_of_week as $day): ?>
|
||||
<th><?php echo $day; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php foreach ($timeslots as $timeslot): ?>
|
||||
<tr>
|
||||
<td>
|
||||
<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>
|
||||
</td>
|
||||
<?php if ($timeslot['is_break']): ?>
|
||||
<td colspan="<?php echo count($days_of_week); ?>" class="text-center table-secondary"><strong>Break</strong></td>
|
||||
<?php else: ?>
|
||||
<?php foreach ($days_of_week as $day_idx => $day): ?>
|
||||
<td class="timetable-slot">
|
||||
<?php
|
||||
$lesson = $teacher_timetable[$day_idx][$timeslot['id']] ?? null;
|
||||
if ($lesson):
|
||||
$class_str = '';
|
||||
if ($lesson['is_horizontal_elective']) $class_str = 'bg-light-purple';
|
||||
elseif ($lesson['is_elective']) $class_str = 'bg-light-green';
|
||||
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>
|
||||
<small><?php echo htmlspecialchars($lesson['class_name']); ?></small>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php endforeach; ?>
|
||||
<?php endif; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
<?php elseif ($selected_teacher_id): ?>
|
||||
<div class="alert alert-info">No lessons scheduled for this teacher.</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
<?php if ($selected_teacher_id): ?>
|
||||
const { jsPDF } = window.jspdf;
|
||||
|
||||
document.getElementById('print-btn').addEventListener('click', function () {
|
||||
window.print();
|
||||
});
|
||||
|
||||
document.getElementById('download-btn').addEventListener('click', function () {
|
||||
const element = document.getElementById('timetable-container');
|
||||
const teacherName = "<?php echo htmlspecialchars($selected_teacher_name, ENT_QUOTES, 'UTF-8'); ?>";
|
||||
const fileName = `timetable-${teacherName.replace(/\s+/g, '-').toLowerCase()}.pdf`;
|
||||
|
||||
html2canvas(element, { scale: 2 }).then(canvas => {
|
||||
const imgData = canvas.toDataURL('image/png');
|
||||
const doc = new jsPDF({
|
||||
orientation: 'l',
|
||||
unit: 'pt',
|
||||
format: 'a4'
|
||||
});
|
||||
const imgWidth = doc.internal.pageSize.getWidth() - 40;
|
||||
const imgHeight = canvas.height * imgWidth / canvas.width;
|
||||
doc.addImage(imgData, 'PNG', 20, 20, imgWidth, imgHeight);
|
||||
doc.save(fileName);
|
||||
});
|
||||
});
|
||||
<?php endif; ?>
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
489
timetable.php
489
timetable.php
@ -3,10 +3,6 @@ session_start();
|
||||
require_once 'includes/auth_check.php';
|
||||
require_once 'db/config.php';
|
||||
|
||||
// --- Configuration ---
|
||||
define('DAYS_OF_WEEK', ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']);
|
||||
define('PERIODS_PER_DAY', 8);
|
||||
|
||||
// --- Database Fetch ---
|
||||
function get_workloads($pdo) {
|
||||
$stmt = $pdo->query("
|
||||
@ -34,6 +30,10 @@ function get_classes($pdo) {
|
||||
return $pdo->query("SELECT * FROM classes ORDER BY name")->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
function get_timeslots($pdo) {
|
||||
return $pdo->query("SELECT * FROM timeslots ORDER BY start_time")->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
// --- Helper Functions ---
|
||||
function get_grade_from_class_name($class_name) {
|
||||
if (preg_match('/^(Grade\s+\d+)/i', $class_name, $matches)) {
|
||||
@ -43,7 +43,7 @@ function get_grade_from_class_name($class_name) {
|
||||
}
|
||||
|
||||
// --- Scoring and Placement Logic ---
|
||||
function find_best_slot_for_lesson($lesson, $is_double, &$class_timetables, &$teacher_timetables, $all_class_ids) {
|
||||
function find_best_slot_for_lesson($lesson, $is_double, &$class_timetables, &$teacher_timetables, $all_class_ids, $days_of_week, $periods_per_day) {
|
||||
$best_slot = null;
|
||||
$highest_score = -1;
|
||||
|
||||
@ -51,14 +51,14 @@ function find_best_slot_for_lesson($lesson, $is_double, &$class_timetables, &$te
|
||||
$teachers_in_lesson = array_unique(array_column($lesson['component_lessons'], 'teacher_id'));
|
||||
$class_ids_in_lesson = $lesson['type'] === 'horizontal_elective' ? $lesson['participating_class_ids'] : [$class_id];
|
||||
|
||||
for ($day = 0; $day < count(DAYS_OF_WEEK); $day++) {
|
||||
for ($period = 0; $period < PERIODS_PER_DAY; $period++) {
|
||||
$current_score = 100; // Base score for a valid slot
|
||||
for ($day = 0; $day < count($days_of_week); $day++) {
|
||||
for ($period = 0; $period < $periods_per_day; $period++) {
|
||||
$current_score = 100; // Base score
|
||||
|
||||
// 1. Check basic availability
|
||||
$slot_available = true;
|
||||
if ($is_double) {
|
||||
if ($period + 1 >= PERIODS_PER_DAY) continue; // Not enough space for a double
|
||||
if ($period + 1 >= $periods_per_day) continue;
|
||||
foreach ($class_ids_in_lesson as $cid) {
|
||||
if (isset($class_timetables[$cid][$day][$period]) || isset($class_timetables[$cid][$day][$period + 1])) {
|
||||
$slot_available = false; break;
|
||||
@ -85,50 +85,32 @@ function find_best_slot_for_lesson($lesson, $is_double, &$class_timetables, &$te
|
||||
}
|
||||
if (!$slot_available) continue;
|
||||
|
||||
// 2. Apply scoring rules for even distribution
|
||||
// 2. Apply scoring rules
|
||||
foreach ($class_ids_in_lesson as $cid) {
|
||||
// Penalty for same subject on the same day
|
||||
for ($p = 0; $p < PERIODS_PER_DAY; $p++) {
|
||||
for ($p = 0; $p < $periods_per_day; $p++) {
|
||||
if (isset($class_timetables[$cid][$day][$p]) && $class_timetables[$cid][$day][$p]['subject_name'] === $lesson['display_name']) {
|
||||
$current_score -= 50;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Penalty for teacher back-to-back lessons
|
||||
foreach ($teachers_in_lesson as $teacher_id) {
|
||||
// Check period before
|
||||
if ($period > 0 && isset($teacher_timetables[$teacher_id][$day][$period - 1])) {
|
||||
$current_score -= 15;
|
||||
}
|
||||
// Check period after
|
||||
if ($period > 0 && isset($teacher_timetables[$teacher_id][$day][$period - 1])) $current_score -= 15;
|
||||
$after_period = $is_double ? $period + 2 : $period + 1;
|
||||
if ($after_period < PERIODS_PER_DAY && isset($teacher_timetables[$teacher_id][$day][$after_period])) {
|
||||
$current_score -= 15;
|
||||
}
|
||||
if ($after_period < $periods_per_day && isset($teacher_timetables[$teacher_id][$day][$after_period])) $current_score -= 15;
|
||||
}
|
||||
|
||||
// 3. New Penalty: Avoid scheduling double lessons of the same subject at the same time (resource conflict)
|
||||
if ($is_double) {
|
||||
$subject_name_to_check = $lesson['display_name'];
|
||||
foreach ($all_class_ids as $cid) {
|
||||
// Skip the classes that are part of the current lesson being placed
|
||||
if (in_array($cid, $class_ids_in_lesson)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isset($class_timetables[$cid][$day][$period])) {
|
||||
$conflicting_lesson = $class_timetables[$cid][$day][$period];
|
||||
// Check if it's a double lesson of the same subject.
|
||||
if ($conflicting_lesson['is_double'] && $conflicting_lesson['subject_name'] === $subject_name_to_check) {
|
||||
$current_score -= 500; // High penalty for resource conflict
|
||||
break; // Found a conflict, no need to check other classes
|
||||
}
|
||||
if (in_array($cid, $class_ids_in_lesson)) continue;
|
||||
if (isset($class_timetables[$cid][$day][$period]) && $class_timetables[$cid][$day][$period]['is_double'] && $class_timetables[$cid][$day][$period]['subject_name'] === $subject_name_to_check) {
|
||||
$current_score -= 500;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 4. Compare with the highest score
|
||||
if ($current_score > $highest_score) {
|
||||
$highest_score = $current_score;
|
||||
$best_slot = ['day' => $day, 'period' => $period];
|
||||
@ -138,23 +120,15 @@ function find_best_slot_for_lesson($lesson, $is_double, &$class_timetables, &$te
|
||||
return $best_slot;
|
||||
}
|
||||
|
||||
|
||||
// --- Main Scheduling Engine ---
|
||||
function generate_timetable($workloads, $classes) {
|
||||
function generate_timetable($workloads, $classes, $days_of_week, $periods_per_day) {
|
||||
$class_timetables = [];
|
||||
foreach ($classes as $class) {
|
||||
$class_timetables[$class['id']] = array_fill(0, count(DAYS_OF_WEEK), array_fill(0, PERIODS_PER_DAY, null));
|
||||
$class_timetables[$class['id']] = array_fill(0, count($days_of_week), array_fill(0, $periods_per_day, null));
|
||||
}
|
||||
$teacher_timetables = [];
|
||||
|
||||
// --- Lesson Preparation ---
|
||||
$classes_by_grade = [];
|
||||
foreach ($classes as $class) {
|
||||
$grade_name = get_grade_from_class_name($class['name']);
|
||||
$classes_by_grade[$grade_name][] = $class;
|
||||
}
|
||||
|
||||
// Step 1: Identify horizontal electives and separate them from other workloads
|
||||
$horizontal_elective_doubles = [];
|
||||
$horizontal_elective_singles = [];
|
||||
$other_workloads = [];
|
||||
@ -172,7 +146,6 @@ function generate_timetable($workloads, $classes) {
|
||||
foreach ($workloads_by_grade_and_elective_group as $grade_name => $elective_groups) {
|
||||
foreach ($elective_groups as $elective_group_name => $group_workloads) {
|
||||
$participating_class_ids = array_unique(array_column($group_workloads, 'class_id'));
|
||||
// A horizontal elective involves more than one class
|
||||
if (count($participating_class_ids) > 1) {
|
||||
$first = $group_workloads[0];
|
||||
$block = [
|
||||
@ -185,27 +158,17 @@ function generate_timetable($workloads, $classes) {
|
||||
];
|
||||
|
||||
if ($block['has_double_lesson'] && $block['lessons_per_week'] >= 2) {
|
||||
$horizontal_elective_doubles[] = $block; // Create one double lesson
|
||||
// Create remaining as single lessons
|
||||
for ($i = 0; $i < $block['lessons_per_week'] - 2; $i++) {
|
||||
$horizontal_elective_singles[] = $block;
|
||||
}
|
||||
$horizontal_elective_doubles[] = $block;
|
||||
for ($i = 0; $i < $block['lessons_per_week'] - 2; $i++) $horizontal_elective_singles[] = $block;
|
||||
} else {
|
||||
// Create all as single lessons
|
||||
for ($i = 0; $i < $block['lessons_per_week']; $i++) {
|
||||
$horizontal_elective_singles[] = $block;
|
||||
}
|
||||
for ($i = 0; $i < $block['lessons_per_week']; $i++) $horizontal_elective_singles[] = $block;
|
||||
}
|
||||
} else {
|
||||
// Not a horizontal elective, add back to the pool of other workloads
|
||||
foreach($group_workloads as $workload) {
|
||||
$other_workloads[] = $workload;
|
||||
}
|
||||
foreach($group_workloads as $workload) $other_workloads[] = $workload;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Process remaining workloads (regular subjects and single-class electives)
|
||||
$double_lessons = [];
|
||||
$single_lessons = [];
|
||||
$workloads_by_class = [];
|
||||
@ -216,7 +179,6 @@ function generate_timetable($workloads, $classes) {
|
||||
foreach ($workloads_by_class as $class_id => $class_workloads) {
|
||||
$elective_groups = [];
|
||||
$individual_lessons = [];
|
||||
// Separate single-class electives from individual subjects
|
||||
foreach ($class_workloads as $workload) {
|
||||
if (!empty($workload['elective_group'])) {
|
||||
$elective_groups[$workload['elective_group']][] = $workload;
|
||||
@ -225,75 +187,48 @@ function generate_timetable($workloads, $classes) {
|
||||
}
|
||||
}
|
||||
|
||||
// Process single-class elective groups
|
||||
foreach ($elective_groups as $group_name => $group_workloads) {
|
||||
$first = $group_workloads[0];
|
||||
$block = [
|
||||
'type' => 'elective',
|
||||
'class_id' => $class_id,
|
||||
'display_name' => $group_name,
|
||||
'lessons_per_week' => $first['lessons_per_week'],
|
||||
'has_double_lesson' => $first['has_double_lesson'],
|
||||
'type' => 'elective', 'class_id' => $class_id, 'display_name' => $group_name,
|
||||
'lessons_per_week' => $first['lessons_per_week'], 'has_double_lesson' => $first['has_double_lesson'],
|
||||
'component_lessons' => $group_workloads
|
||||
];
|
||||
|
||||
if ($block['has_double_lesson'] && $block['lessons_per_week'] >= 2) {
|
||||
$double_lessons[] = $block; // One double lesson
|
||||
// Remaining are single lessons
|
||||
for ($i = 0; $i < $block['lessons_per_week'] - 2; $i++) {
|
||||
$single_lessons[] = $block;
|
||||
}
|
||||
$double_lessons[] = $block;
|
||||
for ($i = 0; $i < $block['lessons_per_week'] - 2; $i++) $single_lessons[] = $block;
|
||||
} else {
|
||||
// All are single lessons
|
||||
for ($i = 0; $i < $block['lessons_per_week']; $i++) {
|
||||
$single_lessons[] = $block;
|
||||
}
|
||||
for ($i = 0; $i < $block['lessons_per_week']; $i++) $single_lessons[] = $block;
|
||||
}
|
||||
}
|
||||
|
||||
// Process individual subjects
|
||||
foreach ($individual_lessons as $workload) {
|
||||
$lesson = [
|
||||
'type' => 'single',
|
||||
'class_id' => $workload['class_id'],
|
||||
'display_name' => $workload['subject_name'],
|
||||
'lessons_per_week' => $workload['lessons_per_week'],
|
||||
'has_double_lesson' => $workload['has_double_lesson'],
|
||||
'type' => 'single', 'class_id' => $workload['class_id'], 'display_name' => $workload['subject_name'],
|
||||
'lessons_per_week' => $workload['lessons_per_week'], 'has_double_lesson' => $workload['has_double_lesson'],
|
||||
'component_lessons' => [$workload]
|
||||
];
|
||||
|
||||
if ($lesson['has_double_lesson'] && $lesson['lessons_per_week'] >= 2) {
|
||||
$double_lessons[] = $lesson; // One double lesson
|
||||
// Remaining are single lessons
|
||||
for ($i = 0; $i < $lesson['lessons_per_week'] - 2; $i++) {
|
||||
$single_lessons[] = $lesson;
|
||||
}
|
||||
$double_lessons[] = $lesson;
|
||||
for ($i = 0; $i < $lesson['lessons_per_week'] - 2; $i++) $single_lessons[] = $lesson;
|
||||
} else {
|
||||
// All are single lessons
|
||||
for ($i = 0; $i < $lesson['lessons_per_week']; $i++) {
|
||||
$single_lessons[] = $lesson;
|
||||
}
|
||||
for ($i = 0; $i < $lesson['lessons_per_week']; $i++) $single_lessons[] = $lesson;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// --- Placement using Scoring ---
|
||||
$all_class_ids = array_column($classes, 'id'); // Get all class IDs for the conflict check
|
||||
|
||||
// The order determines priority: most constrained lessons are scheduled first.
|
||||
$all_class_ids = array_column($classes, 'id');
|
||||
$all_lessons_in_order = [
|
||||
'horizontal_doubles' => $horizontal_elective_doubles,
|
||||
'doubles' => $double_lessons,
|
||||
'horizontal_singles' => $horizontal_elective_singles,
|
||||
'singles' => $single_lessons
|
||||
'horizontal_doubles' => $horizontal_elective_doubles, 'doubles' => $double_lessons,
|
||||
'horizontal_singles' => $horizontal_elective_singles, 'singles' => $single_lessons
|
||||
];
|
||||
|
||||
foreach ($all_lessons_in_order as $type => $lessons) {
|
||||
shuffle($lessons); // Randomize order within the same type to avoid bias
|
||||
shuffle($lessons);
|
||||
foreach ($lessons as $lesson) {
|
||||
$is_double = ($type === 'doubles' || $type === 'horizontal_doubles');
|
||||
|
||||
$best_slot = find_best_slot_for_lesson($lesson, $is_double, $class_timetables, $teacher_timetables, $all_class_ids);
|
||||
$best_slot = find_best_slot_for_lesson($lesson, $is_double, $class_timetables, $teacher_timetables, $all_class_ids, $days_of_week, $periods_per_day);
|
||||
|
||||
if ($best_slot) {
|
||||
$day = $best_slot['day'];
|
||||
@ -301,7 +236,16 @@ function generate_timetable($workloads, $classes) {
|
||||
$class_ids_to_place = ($lesson['type'] === 'horizontal_elective') ? $lesson['participating_class_ids'] : [$lesson['class_id']];
|
||||
$teachers_to_place = array_unique(array_column($lesson['component_lessons'], 'teacher_id'));
|
||||
|
||||
$subject_id = null;
|
||||
$teacher_id = null;
|
||||
if ($lesson['type'] === 'single' && count($lesson['component_lessons']) === 1) {
|
||||
$subject_id = $lesson['component_lessons'][0]['subject_id'];
|
||||
$teacher_id = $lesson['component_lessons'][0]['teacher_id'];
|
||||
}
|
||||
|
||||
$lesson_info = [
|
||||
'subject_id' => $subject_id,
|
||||
'teacher_id' => $teacher_id,
|
||||
'subject_name' => $lesson['display_name'],
|
||||
'teacher_name' => count($teachers_to_place) > 1 ? 'Multiple' : $lesson['component_lessons'][0]['teacher_name'],
|
||||
'is_double' => $is_double,
|
||||
@ -319,29 +263,107 @@ function generate_timetable($workloads, $classes) {
|
||||
$teacher_timetables[$tid][$day][$period + 1] = true;
|
||||
}
|
||||
} else {
|
||||
foreach ($class_ids_to_place as $cid) {
|
||||
$class_timetables[$cid][$day][$period] = $lesson_info;
|
||||
}
|
||||
foreach ($teachers_to_place as $tid) {
|
||||
$teacher_timetables[$tid][$day][$period] = true;
|
||||
}
|
||||
foreach ($class_ids_to_place as $cid) $class_timetables[$cid][$day][$period] = $lesson_info;
|
||||
foreach ($teachers_to_place as $tid) $teacher_timetables[$tid][$day][$period] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $class_timetables;
|
||||
}
|
||||
|
||||
// --- Timetable Persistence ---
|
||||
function save_timetable($pdo, $class_timetables, $timeslots) {
|
||||
$pdo->exec('TRUNCATE TABLE schedules');
|
||||
$stmt = $pdo->prepare(
|
||||
'INSERT INTO schedules (class_id, day_of_week, timeslot_id, subject_id, teacher_id, lesson_display_name, teacher_display_name, is_double, is_elective, is_horizontal_elective) ' .
|
||||
'VALUES (:class_id, :day_of_week, :timeslot_id, :subject_id, :teacher_id, :lesson_display_name, :teacher_display_name, :is_double, :is_elective, :is_horizontal_elective)'
|
||||
);
|
||||
|
||||
$lesson_periods = array_values(array_filter($timeslots, function($ts) { return !$ts['is_break']; }));
|
||||
|
||||
foreach ($class_timetables as $class_id => $day_schedule) {
|
||||
foreach ($day_schedule as $day_idx => $period_schedule) {
|
||||
foreach ($period_schedule as $period_idx => $lesson) {
|
||||
if ($lesson) {
|
||||
if (!isset($lesson_periods[$period_idx])) continue;
|
||||
$timeslot_id = $lesson_periods[$period_idx]['id'];
|
||||
$stmt->execute([
|
||||
':class_id' => $class_id,
|
||||
':day_of_week' => $day_idx,
|
||||
':timeslot_id' => $timeslot_id,
|
||||
':subject_id' => $lesson['subject_id'],
|
||||
':teacher_id' => $lesson['teacher_id'],
|
||||
':lesson_display_name' => $lesson['subject_name'],
|
||||
':teacher_display_name' => $lesson['teacher_name'],
|
||||
':is_double' => $lesson['is_double'],
|
||||
':is_elective' => $lesson['is_elective'],
|
||||
':is_horizontal_elective' => $lesson['is_horizontal_elective']
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function get_timetable_from_db($pdo, $classes, $timeslots) {
|
||||
$stmt = $pdo->query('SELECT * FROM schedules ORDER BY id');
|
||||
$saved_lessons = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
if (empty($saved_lessons)) return [];
|
||||
|
||||
$days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
|
||||
$periods = array_filter($timeslots, function($ts) { return !$ts['is_break']; });
|
||||
$periods_per_day = count($periods);
|
||||
|
||||
$class_timetables = [];
|
||||
foreach ($classes as $class) {
|
||||
$class_timetables[$class['id']] = array_fill(0, count($days_of_week), array_fill(0, $periods_per_day, null));
|
||||
}
|
||||
|
||||
$lesson_periods = array_values($periods);
|
||||
$timeslot_id_to_period_idx = [];
|
||||
foreach($lesson_periods as $idx => $period) {
|
||||
$timeslot_id_to_period_idx[$period['id']] = $idx;
|
||||
}
|
||||
|
||||
foreach ($saved_lessons as $lesson) {
|
||||
$class_id = $lesson['class_id'];
|
||||
$day_idx = $lesson['day_of_week'];
|
||||
$timeslot_id = $lesson['timeslot_id'];
|
||||
|
||||
if (!isset($timeslot_id_to_period_idx[$timeslot_id]) || !isset($class_timetables[$class_id])) continue;
|
||||
$period_idx = $timeslot_id_to_period_idx[$timeslot_id];
|
||||
|
||||
$class_timetables[$class_id][$day_idx][$period_idx] = [
|
||||
'id' => $lesson['id'],
|
||||
'subject_id' => $lesson['subject_id'],
|
||||
'teacher_id' => $lesson['teacher_id'],
|
||||
'subject_name' => $lesson['lesson_display_name'],
|
||||
'teacher_name' => $lesson['teacher_display_name'],
|
||||
'is_double' => (bool)$lesson['is_double'],
|
||||
'is_elective' => (bool)$lesson['is_elective'],
|
||||
'is_horizontal_elective' => (bool)$lesson['is_horizontal_elective']
|
||||
];
|
||||
}
|
||||
return $class_timetables;
|
||||
}
|
||||
|
||||
// --- Main Logic ---
|
||||
$pdoconn = db();
|
||||
$workloads = get_workloads($pdoconn);
|
||||
$classes = get_classes($pdoconn);
|
||||
$class_timetables = [];
|
||||
$timeslots = get_timeslots($pdoconn);
|
||||
|
||||
$days_of_week = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
|
||||
$periods = array_filter($timeslots, function($timeslot) { return !$timeslot['is_break']; });
|
||||
$periods_per_day = count($periods);
|
||||
|
||||
if (isset($_POST['generate'])) {
|
||||
$class_timetables = generate_timetable($workloads, $classes);
|
||||
$class_timetables = generate_timetable($workloads, $classes, $days_of_week, $periods_per_day);
|
||||
save_timetable($pdoconn, $class_timetables, $timeslots);
|
||||
} else {
|
||||
$class_timetables = get_timetable_from_db($pdoconn, $classes, $timeslots);
|
||||
}
|
||||
|
||||
?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
@ -351,6 +373,29 @@ if (isset($_POST['generate'])) {
|
||||
<title>Timetable - Haki Schedule</title>
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
||||
<script src="https://cdn.jsdelivr.net/npm/sortablejs@latest/Sortable.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
|
||||
<style>
|
||||
@media print {
|
||||
body * {
|
||||
visibility: hidden;
|
||||
}
|
||||
#timetables-container, #timetables-container * {
|
||||
visibility: visible;
|
||||
}
|
||||
#timetables-container {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
width: 100%;
|
||||
}
|
||||
.card {
|
||||
border: 1px solid #dee2e6 !important;
|
||||
box-shadow: none !important;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<nav class="navbar navbar-expand-lg navbar-light bg-white sticky-top shadow-sm">
|
||||
@ -372,9 +417,11 @@ if (isset($_POST['generate'])) {
|
||||
<li><a class="dropdown-item" href="/admin_subjects.php">Subjects</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_teachers.php">Teachers</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_workloads.php">Workloads</a></li>
|
||||
<li><a class="dropdown-item" href="/admin_timeslots.php">Timeslots</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li class="nav-item"><a class="nav-link active" href="/timetable.php">Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link active" href="/timetable.php">Class Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/teacher_timetable.php">Teacher Timetable</a></li>
|
||||
<li class="nav-item"><a class="nav-link" href="/logout.php">Logout</a></li>
|
||||
<?php else: ?>
|
||||
<li class="nav-item"><a class="nav-link" href="/demo.php">Demo</a></li>
|
||||
@ -388,74 +435,174 @@ if (isset($_POST['generate'])) {
|
||||
|
||||
<div class="container mt-4">
|
||||
<div class="d-flex justify-content-between align-items-center mb-4">
|
||||
<h1>Timetable</h1>
|
||||
<form method="POST" action="">
|
||||
<button type="submit" name="generate" class="btn btn-primary">Generate Timetable</button>
|
||||
</form>
|
||||
<h1>Class Timetable</h1>
|
||||
<div class="d-flex gap-2">
|
||||
<form method="POST" action="">
|
||||
<button type="submit" name="generate" class="btn btn-primary">Generate Timetable</button>
|
||||
</form>
|
||||
<button id="print-btn" class="btn btn-secondary">Print</button>
|
||||
<button id="download-btn" class="btn btn-secondary">Download as PDF</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<?php if (isset($_POST['generate'])): ?>
|
||||
<?php if (empty($workloads)): ?>
|
||||
<div class="alert alert-warning">
|
||||
No workloads found. Please add classes, subjects, teachers, and workloads in the "Manage" section first.
|
||||
</div>
|
||||
<?php else: ?>
|
||||
<div id="timetables-container">
|
||||
<?php if (!empty($class_timetables)): ?>
|
||||
<?php foreach ($classes as $class): ?>
|
||||
<h3 class="mt-5"><?php echo htmlspecialchars($class['name']); ?></h3>
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<table class="table table-bordered text-center">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 12%;">Time</th>
|
||||
<?php foreach (DAYS_OF_WEEK as $day):
|
||||
?><th><?php echo $day; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php for ($period = 0; $period < PERIODS_PER_DAY; $period++):
|
||||
?><tr>
|
||||
<td>Period <?php echo $period + 1; ?></td>
|
||||
<?php for ($day_idx = 0; $day_idx < count(DAYS_OF_WEEK); $day_idx++):
|
||||
?><td>
|
||||
<?php
|
||||
$lesson = $class_timetables[$class['id']][$day_idx][$period];
|
||||
if ($lesson):
|
||||
$class_str = '';
|
||||
if ($lesson['is_horizontal_elective']) {
|
||||
$class_str = 'bg-light-purple';
|
||||
} elseif ($lesson['is_elective']) {
|
||||
$class_str = 'bg-light-green';
|
||||
} elseif ($lesson['is_double']) {
|
||||
$class_str = 'bg-light-blue';
|
||||
}
|
||||
?>
|
||||
<div class="p-1 <?php echo $class_str; ?>">
|
||||
<strong><?php echo htmlspecialchars($lesson['subject_name']); ?></strong><br>
|
||||
<small><?php echo htmlspecialchars($lesson['teacher_name']); ?></small>
|
||||
</div>
|
||||
<?php else:
|
||||
?>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php endfor; ?>
|
||||
<?php if (!isset($class_timetables[$class['id']])) continue; ?>
|
||||
<div class="timetable-wrapper mb-5">
|
||||
<h3 class="mt-5"><?php echo htmlspecialchars($class['name']); ?></h3>
|
||||
<div class="card">
|
||||
<div class="card-body">
|
||||
<table class="table table-bordered text-center">
|
||||
<thead>
|
||||
<tr>
|
||||
<th style="width: 12%;">Time</th>
|
||||
<?php foreach ($days_of_week as $day): ?>
|
||||
<th><?php echo $day; ?></th>
|
||||
<?php endforeach; ?>
|
||||
</tr>
|
||||
<?php endfor; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</thead>
|
||||
<tbody>
|
||||
<?php
|
||||
$period_idx = 0;
|
||||
foreach ($timeslots as $timeslot):
|
||||
?>
|
||||
<tr>
|
||||
<td>
|
||||
<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>
|
||||
</td>
|
||||
<?php if ($timeslot['is_break']): ?>
|
||||
<td colspan="<?php echo count($days_of_week); ?>" class="text-center table-secondary"><strong>Break</strong></td>
|
||||
<?php else: ?>
|
||||
<?php for ($day_idx = 0; $day_idx < count($days_of_week); $day_idx++): ?>
|
||||
<td class="timetable-slot" data-class-id="<?php echo $class['id']; ?>" data-day="<?php echo $day_idx; ?>" data-timeslot-id="<?php echo $timeslot['id']; ?>">
|
||||
<?php
|
||||
$lesson = $class_timetables[$class['id']][$day_idx][$period_idx] ?? null;
|
||||
if ($lesson):
|
||||
$class_str = '';
|
||||
if ($lesson['is_horizontal_elective']) $class_str = 'bg-light-purple';
|
||||
elseif ($lesson['is_elective']) $class_str = 'bg-light-green';
|
||||
elseif ($lesson['is_double']) $class_str = 'bg-light-blue';
|
||||
?>
|
||||
<div class="lesson p-1 <?php echo $class_str; ?>" data-lesson-id="<?php echo $lesson['id']; ?>" draggable="true">
|
||||
<strong><?php echo htmlspecialchars($lesson['subject_name']); ?></strong><br>
|
||||
<small><?php echo htmlspecialchars($lesson['teacher_name']); ?></small>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</td>
|
||||
<?php endfor; ?>
|
||||
<?php $period_idx++; ?>
|
||||
<?php endif; ?>
|
||||
</tr>
|
||||
<?php endforeach; ?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<?php endforeach; ?>
|
||||
<?php else: ?>
|
||||
<div class="alert alert-info">
|
||||
<?php if (empty($workloads)): ?>
|
||||
No workloads found. Please add classes, subjects, teachers, and workloads in the "Manage" section first.
|
||||
<?php else: ?>
|
||||
Click the "Generate Timetable" button to create a schedule.
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
<?php else:
|
||||
?><div class="alert alert-info">
|
||||
Click the "Generate Timetable" button to create a schedule based on the current workloads.
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<script>
|
||||
document.addEventListener('DOMContentLoaded', function () {
|
||||
// Drag and drop
|
||||
const slots = document.querySelectorAll('.timetable-slot');
|
||||
slots.forEach(function (slot) {
|
||||
new Sortable(slot, {
|
||||
group: 'lessons',
|
||||
animation: 150,
|
||||
onEnd: function (evt) {
|
||||
const itemEl = evt.item;
|
||||
const to = evt.to;
|
||||
|
||||
const lessonId = itemEl.dataset.lessonId;
|
||||
const toClassId = to.dataset.classId;
|
||||
const toDay = to.dataset.day;
|
||||
const toTimeslotId = to.dataset.timeslotId;
|
||||
|
||||
fetch('/api/move_lesson.php', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
lesson_id: lessonId,
|
||||
to_class_id: toClassId,
|
||||
to_day: toDay,
|
||||
to_timeslot_id: toTimeslotId,
|
||||
}),
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (!data.success) {
|
||||
console.error('Error moving lesson:', data.error);
|
||||
alert('Error moving lesson: ' + data.error);
|
||||
location.reload();
|
||||
}
|
||||
})
|
||||
.catch((error) => {
|
||||
console.error('Error:', error);
|
||||
alert('An unexpected error occurred.');
|
||||
location.reload();
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Print and Download
|
||||
const { jsPDF } = window.jspdf;
|
||||
|
||||
document.getElementById('print-btn').addEventListener('click', function () {
|
||||
window.print();
|
||||
});
|
||||
|
||||
document.getElementById('download-btn').addEventListener('click', function () {
|
||||
const container = document.getElementById('timetables-container');
|
||||
const doc = new jsPDF({
|
||||
orientation: 'l',
|
||||
unit: 'pt',
|
||||
format: 'a4'
|
||||
});
|
||||
const margin = 20;
|
||||
const pageHeight = doc.internal.pageSize.getHeight() - (margin * 2);
|
||||
const timetableWrappers = container.querySelectorAll('.timetable-wrapper');
|
||||
let yPos = margin;
|
||||
let pageNum = 1;
|
||||
|
||||
function addPageContent(element, isFirstPage) {
|
||||
return html2canvas(element, { scale: 2 }).then(canvas => {
|
||||
const imgData = canvas.toDataURL('image/png');
|
||||
const imgWidth = doc.internal.pageSize.getWidth() - (margin * 2);
|
||||
const imgHeight = canvas.height * imgWidth / canvas.width;
|
||||
|
||||
if (!isFirstPage) {
|
||||
doc.addPage();
|
||||
}
|
||||
doc.addImage(imgData, 'PNG', margin, margin, imgWidth, imgHeight);
|
||||
});
|
||||
}
|
||||
|
||||
let promise = Promise.resolve();
|
||||
timetableWrappers.forEach((wrapper, index) => {
|
||||
promise = promise.then(() => addPageContent(wrapper, index === 0));
|
||||
});
|
||||
|
||||
promise.then(() => {
|
||||
doc.save('class-timetables.pdf');
|
||||
});
|
||||
});
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user