File "index.php"

Full Path: /home/scheduling/public_html/faculty/index.php
File size: 8.17 KB
MIME-type: text/x-php
Charset: utf-8

<?php
include "../include/conn.php";
include "out.php";

// Get Faculty ID (required)
$faculty_id = $_SESSION['faculty'] ?? 1;
if (!$faculty_id) {
    die("Access denied. Please log in as faculty.");
}

// Get selected semester
$semester = $_GET['semester'] ?? '';

// Fetch faculty info
$faculty = $conn->query("SELECT * FROM faculty WHERE id='$faculty_id'")->fetch_assoc();

// Build query with optional semester filter
$where = "WHERE s.faculty_id = '$faculty_id'";
if ($semester) {
    $where .= " AND s.semester = '" . $conn->real_escape_string($semester) . "'";
}

// Fetch schedule
$sql = "
SELECT 
    s.days, s.start_time, s.end_time, 
    s.year_level, s.semester, 
    s.section_id, sec.section_name,
    s.curriculum_id, cur.code AS curriculum_name, cur.track,
    sub.subject_code, sub.subject_description,
    r.room_name, r.type
FROM schedule s
LEFT JOIN subject sub ON s.subject_id = sub.id
LEFT JOIN curriculum cur ON s.curriculum_id = cur.id
LEFT JOIN section sec ON s.section_id = sec.id
LEFT JOIN room r ON s.room_id = r.id
$where
ORDER BY 
    FIELD(s.semester, '1st','2nd','Summer/MidYear'),
    FIELD(s.days,'Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'),
    s.start_time
";

$result = $conn->query($sql);
?>
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Faculty Schedule</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.10.5/font/bootstrap-icons.css">
    <link rel="stylesheet" href="style.css">
</head>

<body>
    <div class="d-flex">
        <?php include("sidebar.php"); ?>
        <div class="w-100">
            <?php include("nav.php"); ?>

            <div class="content mt-5">
                <div class="container-fluid">
                    <h2 class="mb-4 text-center">
                        Faculty Schedule<br>
                        <small class="text-muted"><?= htmlspecialchars($faculty['faculty_name']) ?></small>
                    </h2>

                    <!-- Filter Form -->
                    <form method="get" class="row g-3 mb-4 justify-left-center">
                        <input type="hidden" name="faculty_id" value="<?= htmlspecialchars($faculty_id) ?>">
                        <div class="col-md-4">
                            <label class="form-label fw-bold">Semester</label>
                            <select name="semester" class="form-select" onchange="this.form.submit()">
                                <option value="1st" <?= $semester == "1st" ? 'selected' : '' ?>>1st Semester</option>
                                <option value="2nd" <?= $semester == "2nd" ? 'selected' : '' ?>>2nd Semester</option>
                                <option value="Summer/MidYear" <?= $semester == "Summer/MidYear" ? 'selected' : '' ?>>Summer / MidYear</option>
                            </select>
                        </div>
                    </form>

                    <?php if ($result->num_rows > 0): ?>
                        <div class="table-responsive shadow p-3 bg-white rounded">
                            <table class="table table-hover table-bordered align-middle">
                                <thead class="bg-primary text-white text-center">
                                    <tr>
                                        <th>Day</th>
                                        <th>Time</th>
                                        <th>Room</th>
                                        <th>Code</th>
                                        <th>Description</th>
                                        <th>Class</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php while ($row = $result->fetch_assoc()): ?>
                                        <?php
                                        $start = date("g:i A", strtotime($row['start_time']));
                                        $end = date("g:i A", strtotime($row['end_time']));
                                        ?>
                                        <tr>
                                            <td><?= htmlspecialchars($row['days']) ?></td>
                                            <td><?= "$start - $end" ?></td>
                                            <td><?= htmlspecialchars($row['room_name']) ?> (<?= htmlspecialchars($row['type']) ?>)</td>
                                            <td><?= htmlspecialchars($row['subject_code']) ?></td>
                                            <td><?= htmlspecialchars($row['subject_description']) ?></td>
                                            <td><?= htmlspecialchars($row['section_name']) ?></td> <!-- ✅ Added section -->
                                        </tr>
                                    <?php endwhile; ?>
                                </tbody>
                            </table>
                        </div>
                    <?php else: ?>
                        <div class="alert alert-warning text-center mt-3">No schedules found for the selected semester.</div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>

    <!-- Curriculum Selection Modal -->
    <div class="modal fade" id="curriculumModal" tabindex="-1" aria-labelledby="curriculumModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content">
                <div class="modal-header bg-primary text-white">
                    <h5 class="modal-title" id="curriculumModalLabel">Select Curriculum</h5>
                    <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body">
                    <label for="curriculumSelect" class="form-label fw-bold">Curriculum</label>
                    <select id="curriculumSelect" class="form-select">
                        <option value="">-- Select Curriculum --</option>
                        <?php
                        $curResult = $conn->query("SELECT id, code, name FROM curriculum ORDER BY code ASC");
                        while ($cur = $curResult->fetch_assoc()):
                        ?>
                            <option value="<?= $cur['id'] ?>">
                                <?= htmlspecialchars($cur['code']) ?> - <?= htmlspecialchars($cur['name']) ?>
                            </option>
                        <?php endwhile; ?>
                    </select>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="button" class="btn btn-success" id="confirmDownload">
                        <i class="bi bi-download"></i> Download
                    </button>
                </div>
            </div>
        </div>
    </div>
</body>

<!-- ✅ Scripts -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
    document.getElementById('confirmDownload').addEventListener('click', function() {
        const curriculumId = document.getElementById('curriculumSelect').value;
        const facultyId = <?= json_encode($faculty_id) ?>;
        const semester = <?= json_encode($_GET['semester'] ?? '1st') ?>; // ✅ safe default to "1st"

        if (!curriculumId) {
            alert('Please select a curriculum before downloading.');
            return;
        }

        // Redirect to download file with all parameters
        const url = `download/faculty.php?faculty_id=${facultyId}&semester=${encodeURIComponent(semester)}&curriculum_id=${curriculumId}`;
        window.location.href = url;
    });
</script>
<script src="javascript.js"></script>

</html>