Mini Kabibi Habibi

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

<?php
@include 'include/config.php';
header('Content-Type: application/json');

$period = $_GET['period'] ?? null;

if (!$period) {
    echo json_encode(["error" => "Period parameter is missing."]);
    exit;
}

// Map month names to numbers
$months_map = [
    'January' => 1, 'February' => 2, 'March' => 3, 'April' => 4,
    'May' => 5, 'June' => 6, 'July' => 7, 'August' => 8,
    'September' => 9, 'October' => 10, 'November' => 11, 'December' => 12
];

// Handle if it's a month (e.g., January)
if (isset($months_map[$period])) {
    $month_num = $months_map[$period];

    $sql = "
        SELECT 
            DATE_FORMAT(m.dates, '%Y-%m-%d') AS date,
            m.no_of_schools_monitored_and_provided_ta,
            m.activities_undertaken,
            m.findings,
            m.technical_assistance_provided,
            m.agreement
        FROM mista_monitoring_reports m
        WHERE MONTH(m.dates) = ?
        ORDER BY m.dates ASC
    ";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        echo json_encode(["error" => "Failed to prepare SQL for month."]);
        exit;
    }

    $stmt->bind_param("i", $month_num);

// Handle if it's a quarter (e.g., Q1, Q2...)
} elseif (in_array($period, ['Q1', 'Q2', 'Q3', 'Q4'])) {
    $quarters = [
        'Q1' => [1, 3],
        'Q2' => [4, 6],
        'Q3' => [7, 9],
        'Q4' => [10, 12],
    ];

    [$startMonth, $endMonth] = $quarters[$period];

    $sql = "
        SELECT 
            DATE_FORMAT(m.dates, '%Y-%m-%d') AS date,
            m.no_of_schools_monitored_and_provided_ta,
            m.activities_undertaken,
            m.findings,
            m.technical_assistance_provided,
            m.agreement
        FROM mista_monitoring_reports m
        WHERE MONTH(m.dates) BETWEEN ? AND ?
        ORDER BY m.dates ASC
    ";

    $stmt = $conn->prepare($sql);
    if (!$stmt) {
        echo json_encode(["error" => "Failed to prepare SQL for quarter."]);
        exit;
    }

    $stmt->bind_param("ii", $startMonth, $endMonth);

} else {
    echo json_encode(["error" => "Invalid period value."]);
    exit;
}

// Execute the query
if (!$stmt->execute()) {
    echo json_encode(["error" => "Failed to execute SQL query."]);
    exit;
}

$result = $stmt->get_result();
$data = [];

while ($row = $result->fetch_assoc()) {
    $data[] = [
        'date' => $row['date'],
        'no_of_schools_monitored_and_provided_ta' => $row['no_of_schools_monitored_and_provided_ta'],
        'activities_undertaken' => $row['activities_undertaken'],
        'findings' => $row['findings'],
        'technical_assistance_provided' => $row['technical_assistance_provided'],
        'agreement' => $row['agreement']
    ];
}

echo empty($data) ? json_encode(["message" => "No data found for the selected period"]) : json_encode($data);

$stmt->close();
$conn->close();
?>