-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.php
More file actions
91 lines (80 loc) · 2.77 KB
/
api.php
File metadata and controls
91 lines (80 loc) · 2.77 KB
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
<?php
header("Content-Type:application/json");
if (isset($_GET['temperature']) && isset($_GET['temperature_unit']) && isset($_GET['target_unit']) && isset($_GET['student_responce'])) {
$temperature = $_GET['temperature'];
$temperature_unit = $_GET['temperature_unit'];
$target_unit = $_GET['target_unit'];
$student_responce = $_GET['student_responce'];
//$grade = "";
$calculate_grade = calculate_temperature($temperature,$temperature_unit,$target_unit,$student_responce);
if (is_numeric($calculate_grade)) {
if(round($calculate_grade) == round($student_responce)){
$grade = "correct";
}
else if(round($calculate_grade) != round($student_responce)){
$grade = "incorrect";
}else{
$grade = "invalid";
}
}
else{
$grade = "invalid";
}
grade_response($grade,200,"success");
}else{
grade_response(NULL, 400,"Invalid Request");
}
//Kelvin Celsius Fahrenheit Rankine
function calculate_temperature($temperature,$temperature_unit,$target_unit,$student_responce){
if (is_numeric($temperature) && is_numeric($student_responce)) {
if($temperature_unit == "Fahrenheit" && $target_unit == "Kelvin"){
return ($temperature - 32) * 5/9 + 273.15;
}
else if($temperature_unit == "Fahrenheit" && $target_unit == "Celsius"){
return ($temperature - 32) * 5/9 ;
}
else if($temperature_unit == "Fahrenheit" && $target_unit == "Rankine"){
return $temperature + 459.67;
}
else if($temperature_unit == "Kelvin" && $target_unit == "Celsius"){
return $temperature - 273.15 ;
}
else if($temperature_unit == "Kelvin" && $target_unit == "Fahrenheit"){
return ($temperature- 273.15) * 9/5 + 32 ;
}
else if($temperature_unit == "Kelvin" && $target_unit == "Rankine"){
return $temperature * 1.8;
}
else if($temperature_unit == "Celsius" && $target_unit == "Kelvin"){
return $temperature + 273.15 ;
}
else if($temperature_unit == "Celsius" && $target_unit == "Fahrenheit"){
return ($temperature * 9/5) + 32 ;
}
else if($temperature_unit == "Celsius" && $target_unit == "Rankine"){
return $temperature * 9/5 + 491.67;
}
else if($temperature_unit == "Rankine" && $target_unit == "Kelvin"){
return $temperature * 5/9;
}
else if($temperature_unit == "Rankine" && $target_unit == "Fahrenheit"){
return $temperature - 459.67;
}
else if($temperature_unit == "Rankine" && $target_unit == "Celsius"){
return ($temperature - 491.67) * 5/9;
}
else{
return 0;
}
}else{
return "invalid";
}
}
function grade_response($grade,$response_code,$response_desc){
$response['grade'] = $grade;
$response['response_code'] = $response_code;
$response['response_desc'] = $response_desc;
$json_response = json_encode($response);
echo $json_response;
}
?>