Skip to content

Commit

Permalink
added DHIS2 Reports API multi orgUnits
Browse files Browse the repository at this point in the history
  • Loading branch information
MIKEINTOSHSYSTEMS committed Jul 28, 2024
1 parent 950019b commit 4d18a46
Show file tree
Hide file tree
Showing 18 changed files with 1,659 additions and 913 deletions.
90 changes: 61 additions & 29 deletions api/dhis2/report/dhis2report.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,66 +18,98 @@ function fetchData($url, $username, $password) {
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($ch);

// Check for cURL errors
if (curl_errno($ch)) {
http_response_code(500);
echo json_encode(['error' => curl_error($ch)]);
curl_close($ch);
exit;
}

// Check for HTTP status code
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode != 200) {
http_response_code($httpCode);
echo json_encode(['error' => 'Unexpected HTTP response code: ' . $httpCode]);
curl_close($ch);
exit;
}

curl_close($ch);
return json_decode($result, true);
}

// Fetch Analytics Data
function fetchAnalyticsData($baseUrl, $username, $password, $dataSet, $orgUnits, $reportPeriod) {
$indicators = [
"{$dataSet}.ACTUAL_REPORTS",
"{$dataSet}.EXPECTED_REPORTS",
"{$dataSet}.REPORTING_RATE",
"{$dataSet}.ACTUAL_REPORTS_ON_TIME",
"{$dataSet}.REPORTING_RATE_ON_TIME"
];
function fetchAnalyticsData($baseUrl, $username, $password, $dataSets, $orgUnits, $reportPeriods) {
$indicators = [];
foreach ($dataSets as $dataSet) {
$indicators[] = "{$dataSet}.ACTUAL_REPORTS";
$indicators[] = "{$dataSet}.EXPECTED_REPORTS";
$indicators[] = "{$dataSet}.REPORTING_RATE";
$indicators[] = "{$dataSet}.ACTUAL_REPORTS_ON_TIME";
$indicators[] = "{$dataSet}.REPORTING_RATE_ON_TIME";
}
$dimensionDx = implode(';', $indicators);
$dimensionOu = implode(';', $orgUnits);
$url = $baseUrl . "/api/analytics.json?dimension=dx:$dimensionDx&dimension=ou:$dimensionOu&filter=pe:$reportPeriod&columns=dx&rows=ou&tableLayout=true&hideEmptyRows=true&displayProperty=SHORTNAME&includeNumDen=false";
$dimensionPe = implode(';', $reportPeriods);

$url = $baseUrl . "/api/analytics.json?dimension=dx:$dimensionDx&dimension=ou:$dimensionOu&filter=pe:$dimensionPe&columns=dx&rows=ou&tableLayout=true&hideEmptyRows=true&displayProperty=SHORTNAME&includeNumDen=false";
return fetchData($url, $username, $password);
}

// Handle API requests
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$action = isset($_GET['action']) ? $_GET['action'] : '';

// Log the action parameter
error_log("Action: " . $action);

switch ($action) {
case 'fetchAnalyticsData':
$dataSet = isset($_GET['dataSet']) ? $_GET['dataSet'] : '';
$dataSets = isset($_GET['dataSet']) ? (array) $_GET['dataSet'] : [];
$orgUnits = isset($_GET['organisationUnit']) ? (array) $_GET['organisationUnit'] : [];
$reportPeriod = isset($_GET['reportPeriod']) ? $_GET['reportPeriod'] : '';
if ($dataSet && !empty($orgUnits) && $reportPeriod) {
$analyticsData = fetchAnalyticsData($baseUrl, $username, $password, $dataSet, $orgUnits, $reportPeriod);
$reportPeriods = isset($_GET['reportPeriod']) ? (array) $_GET['reportPeriod'] : [];

// Log parameters
error_log("DataSets: " . print_r($dataSets, true));
error_log("OrgUnits: " . print_r($orgUnits, true));
error_log("ReportPeriods: " . print_r($reportPeriods, true));

if (!empty($dataSets) && !empty($orgUnits) && !empty($reportPeriods)) {
$analyticsData = fetchAnalyticsData($baseUrl, $username, $password, $dataSets, $orgUnits, $reportPeriods);
if ($analyticsData) {
// Extract title from response
$title = isset($analyticsData['title']) ? $analyticsData['title'] : '';

// Handle the rows safely
$rows = isset($analyticsData['rows']) && !empty($analyticsData['rows']) ? $analyticsData['rows'][0] : [];
$rows = isset($analyticsData['rows']) ? $analyticsData['rows'] : [];

// Prepare the output
$output = [
$formattedData = [];
foreach ($rows as $row) {
$formattedData[] = [
"Title" => $title,
"Data Set" => implode(', ', $dataSets),
"Report Period" => implode(', ', $reportPeriods),
"Organisation Unit ID" => isset($row[0]) ? $row[0] : '',
"Organisation Unit" => isset($row[1]) ? $row[1] : '',
"Organisation Unit Code" => isset($row[2]) ? $row[2] : '',
"Organisation Unit Description" => isset($row[3]) ? $row[3] : '',
"Actual Reports" => isset($row[4]) ? $row[4] : '',
"Expected Reports" => isset($row[5]) ? $row[5] : '',
"Reporting Rate" => isset($row[6]) ? $row[6] : '',
"Actual Reports On Time" => isset($row[7]) ? $row[7] : '',
"Reporting Rate On Time" => isset($row[8]) ? $row[8] : ''
];
}

echo json_encode([
"Title" => $title,
"Data Set" => $dataSet,
"Report Period" => $reportPeriod, //$title, // Use title here for the report period
"Organisation Unit ID" => isset($rows[0]) ? $rows[0] : '',
"Organisation Unit" => isset($rows[1]) ? $rows[1] : '',
"Organisation Unit Code" => isset($rows[2]) ? $rows[2] : '',
"Organisation Unit Description" => isset($rows[3]) ? $rows[3] : '',
"Actual Reports" => isset($rows[4]) ? $rows[4] : '',
"Expected Reports" => isset($rows[5]) ? $rows[5] : '',
"Reporting Rate" => isset($rows[6]) ? $rows[6] : '',
"Actual Reports On Time" => isset($rows[7]) ? $rows[7] : '',
"Reporting Rate On Time" => isset($rows[8]) ? $rows[8] : ''
];

echo json_encode($output, JSON_PRETTY_PRINT);
"Data Set" => implode(', ', $dataSets),
"Report Period" => implode(', ', $reportPeriods),
"Data" => $formattedData
], JSON_PRETTY_PRINT);
} else {
http_response_code(500);
echo json_encode(['error' => 'Failed to fetch analytics data']);
Expand Down
91 changes: 91 additions & 0 deletions api/dhis2/report/dhis2reportapi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
// https://hispmd.merqconsultancy.org/api/dhis2/report/dhis2reportapi.php?action=fetchAnalyticsData&organisationUnit[]=unit1&organisationUnit[]=unit2&dataSet=dataSet1&reportPeriod=THIS_MONTH
// https://hispmd.merqconsultancy.org/api/dhis2/report/dhis2reportapi.php?action=fetchAnalyticsData&organisationUnit[]=Rp268JB6Ne4&organisationUnit[]=GvFqTavdpGE&dataSet=BfMAe6Itzgt&reportPeriod=THIS_MONTH

header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type");
header("Content-Type: application/json");

$apiUrl = "https://dhis2.merqconsultancy.org/api/";
$auth = base64_encode("admin:district");

function fetchData($url) {
global $auth;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic $auth",
"Content-Type: application/json"
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
http_response_code(500);
echo json_encode(['error' => curl_error($ch)]);
curl_close($ch);
exit;
}
curl_close($ch);
return json_decode($result, true);
}

function fetchOrganisationUnits() {
global $apiUrl;
return fetchData($apiUrl . "organisationUnits.json?fields=id,displayName&paging=false");
}

function fetchDataSets() {
global $apiUrl;
return fetchData($apiUrl . "dataSets.json?fields=id,displayName&paging=false");
}

function fetchAnalyticsData($organisationUnits, $dataSet, $reportPeriod) {
global $apiUrl;
$indicators = [
"{$dataSet}.ACTUAL_REPORTS",
"{$dataSet}.EXPECTED_REPORTS",
"{$dataSet}.REPORTING_RATE",
"{$dataSet}.ACTUAL_REPORTS_ON_TIME",
"{$dataSet}.REPORTING_RATE_ON_TIME"
];
$dimensionDx = implode(';', $indicators);
$dimensionOu = implode(';', $organisationUnits);
$queryUrl = $apiUrl . "analytics.json?dimension=dx:$dimensionDx&dimension=ou:$dimensionOu&filter=pe:$reportPeriod&columns=dx&rows=ou&tableLayout=true&hideEmptyRows=true&displayProperty=SHORTNAME&includeNumDen=false";
return fetchData($queryUrl);
}

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
case 'fetchOrganisationUnits':
$data = fetchOrganisationUnits();
echo json_encode($data);
break;
case 'fetchDataSets':
$data = fetchDataSets();
echo json_encode($data);
break;
case 'fetchAnalyticsData':
$organisationUnits = isset($_GET['organisationUnit']) ? $_GET['organisationUnit'] : [];
$dataSet = isset($_GET['dataSet']) ? $_GET['dataSet'] : '';
$reportPeriod = isset($_GET['reportPeriod']) ? $_GET['reportPeriod'] : '';
if (!empty($organisationUnits) && !empty($dataSet) && !empty($reportPeriod)) {
$data = fetchAnalyticsData($organisationUnits, $dataSet, $reportPeriod);
echo json_encode($data);
} else {
http_response_code(400);
echo json_encode(['error' => 'Missing required parameters']);
}
break;
default:
http_response_code(400);
echo json_encode(['error' => 'Invalid action']);
break;
}
} else {
http_response_code(405);
echo json_encode(['error' => 'Method not allowed']);
}
?>
79 changes: 35 additions & 44 deletions api/dhis2/report/dhis2reports.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@
header("Access-Control-Allow-Headers: Content-Type");

// DHIS2 credentials and base URL
$baseUrl = 'https://dhis2.merqconsultancy.org';
$apiUrl = 'https://dhis2.merqconsultancy.org/api/';
$username = 'admin';
$password = 'district';
$auth = base64_encode("$username:$password");

// Fetch Data function
function fetchData($url, $username, $password) {
function fetchData($url, $auth) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Basic $auth",
"Content-Type: application/json"
));
$result = curl_exec($ch);
if (curl_errno($ch)) {
http_response_code(500);
Expand All @@ -28,58 +30,47 @@ function fetchData($url, $username, $password) {
return json_decode($result, true);
}

// Fetch Organisation Units
function fetchOrgUnits($baseUrl, $username, $password) {
$url = $baseUrl . '/api/organisationUnits.json?fields=id,displayName&paging=false';
return fetchData($url, $username, $password);
}

// Fetch Data Sets
function fetchDataSets($baseUrl, $username, $password) {
$url = $baseUrl . '/api/dataSets.json?fields=id,displayName&paging=false';
return fetchData($url, $username, $password);
}

// Fetch Analytics Data
function fetchAnalyticsData($baseUrl, $username, $password, $dataSet, $orgUnits, $reportPeriod) {
$indicators = [
"{$dataSet}.ACTUAL_REPORTS",
"{$dataSet}.EXPECTED_REPORTS",
"{$dataSet}.REPORTING_RATE",
"{$dataSet}.ACTUAL_REPORTS_ON_TIME",
"{$dataSet}.REPORTING_RATE_ON_TIME"
];
$dimensionDx = implode(';', $indicators);
$dimensionOu = implode(';', $orgUnits);
$url = $baseUrl . "/api/analytics.json?dimension=dx:$dimensionDx&dimension=ou:$dimensionOu&filter=pe:$reportPeriod&columns=dx&rows=ou&tableLayout=true&hideEmptyRows=true&displayProperty=SHORTNAME&includeNumDen=false";
return fetchData($url, $username, $password);
}

// Handle API requests
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
$action = isset($_GET['action']) ? $_GET['action'] : '';

switch ($action) {
case 'fetchOrgUnits':
$data = fetchOrgUnits($baseUrl, $username, $password);
$url = $apiUrl . "organisationUnits.json?fields=id,displayName&paging=false";
$data = fetchData($url, $auth);
echo json_encode($data);
break;
case 'fetchDataSets':
$data = fetchDataSets($baseUrl, $username, $password);
$url = $apiUrl . "dataSets.json?fields=id,displayName&paging=false";
$data = fetchData($url, $auth);
echo json_encode($data);
break;
case 'fetchAnalyticsData':
case 'fetchReport':
$organisationUnits = isset($_GET['organisationUnit']) ? $_GET['organisationUnit'] : '';
$dataSet = isset($_GET['dataSet']) ? $_GET['dataSet'] : '';
$orgUnits = isset($_GET['organisationUnit']) ? $_GET['organisationUnit'] : [];
$reportPeriod = isset($_GET['reportPeriod']) ? $_GET['reportPeriod'] : '';
if ($dataSet && !empty($orgUnits) && $reportPeriod) {
$analyticsData = fetchAnalyticsData($baseUrl, $username, $password, $dataSet, $orgUnits, $reportPeriod);
if ($analyticsData) {
echo json_encode($analyticsData);
} else {
http_response_code(500);
echo json_encode(['error' => 'Failed to fetch analytics data']);
}

if ($organisationUnits && $dataSet && $reportPeriod) {
$organisationUnits = explode(',', $organisationUnits);

// Constructing dimensions for the dataSet (assuming fixed indicator types)
$indicators = [
"{$dataSet}.ACTUAL_REPORTS",
"{$dataSet}.EXPECTED_REPORTS",
"{$dataSet}.REPORTING_RATE",
"{$dataSet}.ACTUAL_REPORTS_ON_TIME",
"{$dataSet}.REPORTING_RATE_ON_TIME"
];
$dimensionDx = implode(';', $indicators);

// Constructing the organisation units parameter
$dimensionOu = implode(';', $organisationUnits);

// Fetch data based on selected parameters
$queryUrl = $apiUrl . "analytics.json?dimension=dx:$dimensionDx&dimension=ou:$dimensionOu&filter=pe:$reportPeriod&columns=dx&rows=ou&tableLayout=true&hideEmptyRows=true&displayProperty=SHORTNAME&includeNumDen=false";
$analyticsData = fetchData($queryUrl, $auth);

echo json_encode($analyticsData);
} else {
http_response_code(400);
echo json_encode(['error' => 'Missing required parameters']);
Expand Down
Loading

0 comments on commit 4d18a46

Please sign in to comment.