Mini Kabibi Habibi

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

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

if (!isset($_SESSION['username_school'])) {
    die("Error: User not logged in.");
}

$username = $_SESSION['username_school'];
$report_id = $_GET['report_id'] ?? null;  // Assuming report_id is passed as a GET parameter

if (!$report_id) {
    die("Error: No report ID provided.");
}

// Fetch the report data for the given report_id and username
$stmt = $conn->prepare("SELECT * FROM mqcais_reports WHERE id = ? AND username = ?");
$stmt->bind_param("is", $report_id, $username);
$stmt->execute();
$report_data = $stmt->get_result()->fetch_assoc();
$stmt->close();

if (empty($report_data)) {
    die("Error: Report not found.");
}

// Fetch the monitoring activity data related to the given report_id
$stmt = $conn->prepare("SELECT * FROM mqcais_monitoring_reports WHERE report_id = ?");
$stmt->bind_param("i", $report_id);
$stmt->execute();
$monitoring_data = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
$stmt->close();

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    // Update report details
    $division = $_POST['division'] ?? '';
    $section = $_POST['section'] ?? '';
    $district = $_POST['district'] ?? '';  // Changed from 'learning_area' to 'district'
    $month = $_POST['month'] ?? '';
    $school_year = $_POST['school_year'] ?? '';

    // Update the MQCAIS report
    $stmt = $conn->prepare("UPDATE mqcais_reports SET division = ?, section = ?, district = ?, month = ?, school_year = ? WHERE id = ?");
    $stmt->bind_param("sssssi", $division, $section, $district, $month, $school_year, $report_id);

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

    // Update each row of monitoring activities
    $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++) {
        // Skip empty rows
        if (empty($date[$i]) && empty($activities[$i])) {
            continue;
        }

        $stmt_activity = $conn->prepare("UPDATE mqcais_monitoring_reports SET 
            date = ?, no_of_schools_monitored_and_provided_ta = ?, activities_undertaken = ?, findings = ?, technical_assistance_provided = ?, agreement = ?
            WHERE report_id = ? AND id = ?");
        $stmt_activity->bind_param("ssssssii", 
            $date[$i], $no_of_schools[$i], $activities[$i], $findings[$i], $assistances[$i], $agreements[$i], $report_id, $monitoring_data[$i]['id']
        );

        if (!$stmt_activity->execute()) {
            die("Error updating activity: " . $stmt_activity->error);
        }

        $stmt_activity->close();
    }

    // Redirect to a different page after saving
    header("Location: view_mqcais_reports.php");
    exit();
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit 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;
        }
    </style>
</head>
<body class="container mt-5">

    <h3 class="text-center mb-4">Edit Monthly/Quarterly Consolidated and Analyzed Instructional Supervisory Report</h3>

    <form action="edit_mqcais_report.php?report_id=<?php echo $report_id; ?>" method="POST" onsubmit="return confirmSubmission();">

        <!-- General Info -->
        <div class="form-row mb-3">
            <div class="col-md-2">
                <label>Division</label>
                <input type="text" name="division" class="form-control form-control-sm" 
                       value="<?php echo htmlspecialchars($report_data['division'] ?? ''); ?>" required>
            </div>
            <div class="col-md-2">
                <label>Section/Unit</label>
                <input type="text" name="section" class="form-control form-control-sm" 
                       value="<?php echo htmlspecialchars($report_data['section'] ?? ''); ?>" required>
            </div>
            <div class="col-md-3">
                <label>District</label>
                <input type="text" name="district" class="form-control form-control-sm" 
                       value="<?php echo htmlspecialchars($report_data['district'] ?? ''); ?>" required>
            </div>
            <div class="col-md-2">
                <label>Month</label>
                <select id="monthDropdown" name="month" class="form-control form-control-sm" required>
                    <option value="">Select Month</option>
                    <option value="January" <?php echo $report_data['month'] == 'January' ? 'selected' : ''; ?>>January</option>
                    <option value="February" <?php echo $report_data['month'] == 'February' ? 'selected' : ''; ?>>February</option>
                    <option value="March" <?php echo $report_data['month'] == 'March' ? 'selected' : ''; ?>>March</option>
                    <option value="April" <?php echo $report_data['month'] == 'April' ? 'selected' : ''; ?>>April</option>
                    <option value="May" <?php echo $report_data['month'] == 'May' ? 'selected' : ''; ?>>May</option>
                    <option value="June" <?php echo $report_data['month'] == 'June' ? 'selected' : ''; ?>>June</option>
                    <option value="July" <?php echo $report_data['month'] == 'July' ? 'selected' : ''; ?>>July</option>
                    <option value="August" <?php echo $report_data['month'] == 'August' ? 'selected' : ''; ?>>August</option>
                    <option value="September" <?php echo $report_data['month'] == 'September' ? 'selected' : ''; ?>>September</option>
                    <option value="October" <?php echo $report_data['month'] == 'October' ? 'selected' : ''; ?>>October</option>
                    <option value="November" <?php echo $report_data['month'] == 'November' ? 'selected' : ''; ?>>November</option>
                    <option value="December" <?php echo $report_data['month'] == 'December' ? 'selected' : ''; ?>>December</option>
                </select>
            </div>
            <div class="col-md-3">
                <label>School Year</label>
                <input type="text" name="school_year" class="form-control form-control-sm"
                       value="<?php echo htmlspecialchars($report_data['school_year']); ?>" required>
            </div>
        </div>

        <!-- Activity Table -->
        <div class="table-responsive">
            <table class="table table-bordered table-sm text-center" id="activityTable">
                <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>
                    <?php if (!empty($monitoring_data)): ?>
                        <?php foreach ($monitoring_data as $activity): ?>
                            <tr>
                                <td><input type="date" name="date[]" class="form-control form-control-sm" value="<?php echo htmlspecialchars($activity['date']); ?>" required></td>
                                <td><input type="text" name="no_of_schools_monitored_and_provided_ta[]" class="form-control form-control-sm" value="<?php echo htmlspecialchars($activity['no_of_schools_monitored_and_provided_ta']); ?>" required></td>
                                <td><input type="text" name="activities_undertaken[]" class="form-control form-control-sm" value="<?php echo htmlspecialchars($activity['activities_undertaken']); ?>" required></td>
                                <td><input type="text" name="findings[]" class="form-control form-control-sm" value="<?php echo htmlspecialchars($activity['findings']); ?>"></td>
                                <td><input type="text" name="technical_assistance_provided[]" class="form-control form-control-sm" value="<?php echo htmlspecialchars($activity['technical_assistance_provided']); ?>"></td>
                                <td><input type="text" name="agreement[]" class="form-control form-control-sm" value="<?php echo htmlspecialchars($activity['agreement']); ?>"></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <!-- Submit -->
        <div class="form-group text-center mt-3">
            <button type="submit" class="btn btn-success">Update Report</button>
        </div>
    </form>

    <!-- Scripts -->
    <script>
        function confirmSubmission() {
            return confirm("Are you sure you want to save these changes?");
        }
    </script>

</body>
</html>