Mini Kabibi Habibi

Current Path : C:/xampp/htdocs/cid/system/
Upload File :
Current File : C:/xampp/htdocs/cid/system/create_qcais_report.php

<?php
session_start();
@include 'include/config.php';

if (!isset($_SESSION['username']) || !isset($_SESSION['role']) || $_SESSION['role'] !== 'admin') {
    // Access denied or redirection logic here if needed
}

$username = $_SESSION['username']; // admin name

// Fetch division, section, and learning area from the database
$sql = "SELECT division, section, learning_area FROM information WHERE username = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $username);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $rows = $result->fetch_assoc();
} else {
    die("Error: User information not found.");
}
$stmt->close();

// Handle form submission
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $division = $_POST['division'] ?? null;
    $section = $_POST['section'] ?? null;
    $district = $_POST['learning_area'] ?? null;
    $school_year = $_POST['school_year'] ?? null;

    $period_type = $_POST['period_type'] ?? null;
    $month = $_POST['month'] ?? null;
    $quarter = $_POST['quarter'] ?? null;
    $time_period = ($period_type === 'month') ? $month : $quarter;

    $stmt = $conn->prepare("INSERT INTO admin_mqcais_reports (division, section, district, month, school_year, username) VALUES (?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssss", $division, $section, $district, $time_period, $school_year, $username);

    if (!$stmt->execute()) {
        die("Error saving MQCAIS report: " . $stmt->error);
    }

    $report_id = $conn->insert_id;
    $stmt->close();

    $date = $_POST['date'] ?? [];
    $no_of_schools = $_POST['no_of_schools_monitored_and_provided_ta'] ?? [];
    $activities = $_POST['activities_undertaken'] ?? [];
    $findings = $_POST['findings'] ?? [];
    $assistances = $_POST['technical_assistance_provided'] ?? [];
    $agreements = $_POST['agreement'] ?? [];

    for ($i = 0; $i < count($date); $i++) {
        if (empty($date[$i]) && empty($activities[$i])) {
            continue;
        }

        $stmt_report = $conn->prepare("INSERT INTO admin_mqcais_monitoring_reports 
            (report_id, date, no_of_schools_monitored_and_provided_ta, activities_undertaken, findings, technical_assistance_provided, agreement) 
            VALUES (?, ?, ?, ?, ?, ?, ?)");

        $stmt_report->bind_param(
            "issssss",
            $report_id,
            $date[$i],
            $no_of_schools[$i],
            $activities[$i],
            $findings[$i],
            $assistances[$i],
            $agreements[$i]
        );
        $stmt_report->execute();
        $stmt_report->close();
    }

    if (isset($_POST['generate_report'])) {
        header("Location: generate_mqcais_word.php?report_id=" . $report_id);
        exit();
    } elseif (isset($_POST['save_only'])) {
        header("Location: view_qcais_reports.php");
        exit();
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>MQCAIS Report</title>
    <link href="../vendors/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .form-control-sm { font-size: 0.875rem; padding: 0.25rem 0.5rem; }
        th, td { vertical-align: middle !important; }
        @media (max-width: 768px) {
            th, td { font-size: 0.75rem; white-space: nowrap; }
        }
    </style>
</head>
<body class="mt-4">
<div class="container-fluid px-3">
    <h3 class="text-center mb-4">Create Quarterly Consolidated and Analyzed Instructional Supervisory Report</h3>

    <form action="" method="POST" onsubmit="return confirm('Are you sure you want to create this MQCAIS report?');">
        <div class="row g-2 mb-3">
            <div class="col-12 col-sm-6 col-md-3 col-lg-2">
                <label>Division</label>
                <input type="text" name="division" class="form-control form-control-sm" value="<?php echo htmlspecialchars($rows['division']); ?>" readonly required>
            </div>
            <div class="col-12 col-sm-6 col-md-3 col-lg-2">
                <label>Section/Unit</label>
                <input type="text" name="section" class="form-control form-control-sm" value="<?php echo htmlspecialchars($rows['section']); ?>" readonly required>
            </div>
            <div class="col-12 col-md-4 col-lg-3">
                <label>District/Learning Area</label>
                <input type="text" name="learning_area" class="form-control form-control-sm" value="<?php echo htmlspecialchars($rows['learning_area']); ?>" readonly required>
            </div>
            <div class="col-6 col-md-3 col-lg-2">
                <label>Period Type</label>
                <select id="periodType" name="period_type" class="form-control form-control-sm" required onchange="togglePeriodSelectors()">
                    <option value="">Select Type</option>
                    <option value="quarter">Quarter</option>
                    <option value="month">Month</option>
                </select>
            </div>
            <div class="col-6 col-md-3 col-lg-2" id="monthSelector" style="display:none;">
                <label>Month</label>
                <select id="monthDropdown" name="month" class="form-control form-control-sm">
                    <option value="">Select Month</option>
                    <option>January</option><option>February</option><option>March</option>
                    <option>April</option><option>May</option><option>June</option>
                    <option>July</option><option>August</option><option>September</option>
                    <option>October</option><option>November</option><option>December</option>
                </select>
            </div>
            <div class="col-6 col-md-3 col-lg-2" id="quarterSelector" style="display:none;">
                <label>Quarter</label>
                <select id="quarterDropdown" name="quarter" class="form-control form-control-sm">
                    <option value="">Select Quarter</option>
                    <option value="Q1">Q1 (Jan-Mar)</option>
                    <option value="Q2">Q2 (Apr-Jun)</option>
                    <option value="Q3">Q3 (Jul-Sep)</option>
                    <option value="Q4">Q4 (Oct-Dec)</option>
                </select>
            </div>
            <div class="col-12 col-md-4 col-lg-3">
                <label>School Year</label>
                <input type="text" name="school_year" class="form-control form-control-sm" value="<?php echo date('Y'); ?>" readonly required>
            </div>
        </div>

        <div class="form-group text-center mb-3">
            <h4 class="text-center mb-2 text-white bg-primary p-2 rounded d-inline-block">
                Your Activities
            </h4>
        </div>

        <div class="table-responsive mb-3">
            <table class="table table-bordered table-sm text-center" id="activityTable">
                <div class="text-center mb-3">
    <button type="button" class="btn btn-sm btn-success" id="addRowBtn">+ Add Activity</button>
</div>

                <thead class="thead-light">
                    <tr>
                        <th>Date</th>
                        <th>No. of Schools Monitored and Provided TA</th>
                        <th>Activities Undertaken</th>
                        <th>Findings</th>
                        <th>Technical Assistance</th>
                        <th>Agreement</th>
                    </tr>
                </thead>
                <tbody id="activityRows">
    <?php for ($i = 0; $i < 3; $i++): ?>
    <tr>
        <td><input type="date" name="date[]" class="form-control form-control-sm" required></td>
        <td><input type="text" name="no_of_schools_monitored_and_provided_ta[]" class="form-control form-control-sm" required></td>
        <td><input type="text" name="activities_undertaken[]" class="form-control form-control-sm" required></td>
        <td><input type="text" name="findings[]" class="form-control form-control-sm"></td>
        <td><input type="text" name="technical_assistance_provided[]" class="form-control form-control-sm"></td>
        <td><input type="text" name="agreement[]" class="form-control form-control-sm"></td>
        <td><button type="button" class="btn btn-sm btn-danger remove-row">&times;</button></td>
    </tr>
    <?php endfor; ?>
    <script>
function togglePeriodSelectors() {
    const periodType = document.getElementById("periodType").value;
    document.getElementById("monthSelector").style.display = (periodType === "month") ? "block" : "none";
    document.getElementById("quarterSelector").style.display = (periodType === "quarter") ? "block" : "none";
}

// Add new row
document.getElementById("addRowBtn").addEventListener("click", function () {
    const rowHTML = `
        <tr>
            <td><input type="date" name="date[]" class="form-control form-control-sm" required></td>
            <td><input type="text" name="no_of_schools_monitored_and_provided_ta[]" class="form-control form-control-sm" required></td>
            <td><input type="text" name="activities_undertaken[]" class="form-control form-control-sm" required></td>
            <td><input type="text" name="findings[]" class="form-control form-control-sm"></td>
            <td><input type="text" name="technical_assistance_provided[]" class="form-control form-control-sm"></td>
            <td><input type="text" name="agreement[]" class="form-control form-control-sm"></td>
            <td><button type="button" class="btn btn-sm btn-danger remove-row">&times;</button></td>
        </tr>
    `;
    document.getElementById("activityRows").insertAdjacentHTML('beforeend', rowHTML);
});

// Remove row
document.addEventListener("click", function (e) {
    if (e.target && e.target.classList.contains("remove-row")) {
        const row = e.target.closest("tr");
        if (row) row.remove();
    }
});
</script>

</tbody>

            </table>
        </div>

        <div class="form-group text-center mt-3">
            <button type="submit" name="save_only" class="btn btn-warning">Save</button>
        </div>
    </form>
</div>

<script>
function togglePeriodSelectors() {
    const periodType = document.getElementById("periodType").value;
    document.getElementById("monthSelector").style.display = (periodType === "month") ? "block" : "none";
    document.getElementById("quarterSelector").style.display = (periodType === "quarter") ? "block" : "none";
}
</script>
</body>
</html>