/
q.php
121 lines (83 loc) · 1.87 KB
/
q.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
$districts = [];
if (!isset($_GET["d"])){
echo "{error:\"no school district specified.\"}";
exit();
}
if (!isset($_GET["r1"])){
echo "{error:\"no first race specified.\"}";
exit();
}
if (!isset($_GET["r2"])){
echo "{error:\"no second race specified.\"}";
exit();
}
if (!isset($_GET["s"])){
echo "{error:\"no subject specified.\"}";
exit();
}
else {
$districts = explode(",",$_GET["d"]);
}
function file_get_json ($json_file){
$json_string = file_get_contents($json_file);
return json_decode($json_string);
}
$json_file = "js/data.json";
$data = file_get_json($json_file);
$return = [];
function getGap($district_code) {
global $data;
$s = $_GET["s"];
$gap = "error";
$district = $data->$district_code->district;
// echo "district: " . $district . "<br>";
$r1 = $_GET["r1"];
$r2 = $_GET["r2"];
// echo "race: " . $r1 . "<br>";
// echo "race: " . $r2 . "<br>";
$r1_score = $data->$district_code->$r1->$s;
$r2_score = $data->$district_code->$r2->$s;
// echo "r1_score: " . $r1_score . "<br>";
// echo "r2_score: " . $r2_score . "<br>";
if (is_numeric($r1_score) && is_numeric($r2_score)){
$gap = $r1_score * 100 - $r2_score * 100;
$obj = [];
$obj["gap"] = $gap;
$obj["r1"] = $r1_score * 100;
$obj["r2"] = $r2_score * 100;
$obj["status"] = "SUCCESS";
$obj["district"] = $district;
}
else {
$obj = [];
$obj["status"] = "ERROR";
$obj["message"] = "Missing data";
}
return $obj;
}
function getAllGaps() {
global $data;
$return = [];
foreach($data as $district_code => $datum){
$return [$district_code] = getGap($district_code);
}
echo json_encode($return);
exit();
}
function getGaps(){
global $district_code;
$return = [];
foreach ($districts as $district_code){
$return[$district_code] = getGap($district_code);
}
echo json_encode($return);
exit();
}
if ($_GET["d"]="all") {
getAllGaps();
}
else {
getGaps();
}
?>