]> Softwares of Agnibho - dietsurvey.git/blob - dietsurvey/ajax.php
Rewritten in PHP/Javascript
[dietsurvey.git] / dietsurvey / ajax.php
1 <?php
2 /**********************************************************************
3 * Title: Diet-Survey
4 * Description: Application for calculating nutrient intake
5 * Author: Agnibho Mondal
6 * Website: http://code.agnibho.com
7 **********************************************************************
8 Copyright (c) 2013-2015 Agnibho Mondal
9 All rights reserved
10 **********************************************************************
11 This file is part of Diet-Survey.
12
13 Diet-Survey is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
17
18 Diet-Survey is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with Diet-Survey. If not, see <http://www.gnu.org/licenses/>.
25 **********************************************************************/
26 ?>
27 <?php
28 ini_set('session.cookie_lifetime', 60 * 60 * 24 * 30);
29 ini_set('session.gc_maxlifetime', 60 * 60 * 24 * 30);
30 session_start();
31 session_regenerate_id(true);
32 if(!isSet($_SESSION["custom_food"])){
33 $_SESSION["custom_food"]=array();
34 }
35
36 define("HOST", "localhost");
37 define("USER", "root");
38 define("PASS", "data");
39 define("DTBS", "test");
40
41 if(isSet($_POST["subjects"]) && isSet($_POST["foods"])){
42 $sql=new mysqli(HOST, USER, PASS, DTBS);
43 $send=array();
44 $send["rda"]=subject_stat($_POST["subjects"], $sql);
45 $send["val"]=food_stat($_POST["foods"], $sql);
46 $sql->close();
47 echo json_encode($send);
48 }
49 else if(isSet($_POST["name"]) && isSet($_POST["energy"]) && isSet($_POST["protein"]) && isSet($_POST["iron"]) && isSet($_POST["vitA"]) && isSet($_POST["thiamine"]) && isSet($_POST["riboflavin"]) && isSet($_POST["vitC"])){
50 $_SESSION["custom_food"][]=array("name"=>$_POST["name"], "energy"=>$_POST["energy"], "protein"=>$_POST["protein"], "iron"=>$_POST["iron"], "vitA"=>$_POST["vitA"], "thiamine"=>$_POST["thiamine"], "riboflavin"=>$_POST["riboflavin"], "vitC"=>$_POST["vitC"]);
51 echo json_encode(["flag"=>true]);
52 }
53 else if(isSet($_POST["delete-custom-food"])){
54 array_splice($_SESSION["custom_food"], $_POST["delete-custom-food"], 1);
55 echo json_encode(["flag"=>true]);
56 }
57 else if(isSet($_GET["initdata"])){
58 $data["list"]=get_food_list();
59 $data["custom"]=get_custom_food();
60 echo json_encode($data);
61 }
62 else{
63 exit("Error");
64 }
65
66 function get_food_list(){
67 $preset=array();
68 $sql=new mysqli(HOST, USER, PASS, DTBS);
69 $stmt=$sql->prepare("SELECT name FROM food");
70 $stmt->execute();
71 $stmt->bind_result($name);
72 while($stmt->fetch()){
73 $preset[]=$name;
74 }
75 $stmt->close();
76 $sql->close();
77 foreach($_SESSION["custom_food"] as $item){
78 if(!in_array($item["name"], $preset)){
79 $preset[]=$item["name"];
80 }
81 }
82 return $preset;
83 }
84
85 function get_custom_food(){
86 return $_SESSION["custom_food"];
87 }
88
89 function subject_stat($subjects, $sql){
90 $rda=array();
91 for($i=0; $i<count($subjects); $i++){
92 $stmt=$sql->prepare("SELECT energy, protein, iron, vitA, thiamine, riboflavin, vitC FROM rda WHERE subject=?");
93 $stmt->bind_param("s", make_string($subjects[$i]));
94 $stmt->execute();
95 $stmt->bind_result($energy, $protein, $iron, $vitA, $thiamine, $riboflavin, $vitC);
96 $stmt->fetch();
97 $stmt->close();
98 $rda[$i]["energy"]=(float)$energy;
99 $rda[$i]["protein"]=(float)$protein;
100 $rda[$i]["iron"]=(float)$iron;
101 $rda[$i]["vitA"]=(float)$vitA;
102 $rda[$i]["thiamine"]=(float)$thiamine;
103 $rda[$i]["riboflavin"]=(float)$riboflavin;
104 $rda[$i]["vitC"]=(float)$vitC;
105 if($subjects[$i]["age"]>=18 && $subjects[$i]["age"]<=45 && $subjects[$i]["sex"]=="Female"){
106 if($subjects[$i]["state"]!="npnl"){
107 $qs="";
108 if($subjects[$i]["state"]=="preg"){
109 $qs="pregnant";
110 }
111 else if($subjects[$i]["state"]=="lactb6"){
112 $qs="lactating_below6";
113 }
114 else if($subjects[$i]["state"]=="lacto6"){
115 $qs="lactating_above6";
116 }
117 $stmt=$sql->prepare("SELECT energy, protein, iron, vitA, thiamine, riboflavin, vitC FROM rda WHERE subject=?");
118 $stmt->bind_param("s", $qs);
119 $stmt->execute();
120 $stmt->bind_result($energy, $protein, $iron, $vitA, $thiamine, $riboflavin, $vitC);
121 $stmt->fetch();
122 $stmt->close();
123 $rda[$i]["energy"]=$rda[$i]["energy"]+(float)$energy;
124 $rda[$i]["protein"]=(float)$protein;
125 $rda[$i]["iron"]=(float)$iron;
126 $rda[$i]["vitA"]=(float)$vitA;
127 $rda[$i]["thiamine"]=$rda[$i]["thiamine"]+(float)$thiamine;
128 $rda[$i]["riboflavin"]=$rda[$i]["riboflavin"]+(float)$riboflavin;
129 $rda[$i]["vitC"]=(float)$vitC;
130 }
131 }
132 }
133 return $rda;
134 }
135
136 function food_stat($foods, $sql){
137 $val=array();
138 if(count($foods)>0){
139 $qs="";
140 for($i=0; $i<count($foods); $i++){
141 if(has_custom($foods[$i]["name"])){
142 $val[$foods[$i]["name"]]["energy"]=(float)get_custom_by_name($foods[$i]["name"])["energy"];
143 $val[$foods[$i]["name"]]["protein"]=(float)get_custom_by_name($foods[$i]["name"])["protein"];
144 $val[$foods[$i]["name"]]["iron"]=(float)get_custom_by_name($foods[$i]["name"])["iron"];
145 $val[$foods[$i]["name"]]["vitA"]=(float)get_custom_by_name($foods[$i]["name"])["vitA"];
146 $val[$foods[$i]["name"]]["thiamine"]=(float)get_custom_by_name($foods[$i]["name"])["thiamine"];
147 $val[$foods[$i]["name"]]["riboflavin"]=(float)get_custom_by_name($foods[$i]["name"])["riboflavin"];
148 $val[$foods[$i]["name"]]["vitC"]=(float)get_custom_by_name($foods[$i]["name"])["vitC"];
149 }
150 else{
151 if($qs==""){
152 $qs="name='".$foods[0]["name"]."'";
153 }
154 else{
155 $qs=$qs." OR name='".$foods[$i]["name"]."'";
156 }
157 }
158 }
159 if($qs!=""){
160 $stmt=$sql->prepare("SELECT name, energy, protein, iron, vitA, thiamine, riboflavin, vitC FROM food WHERE ".$qs);
161 $stmt->execute();
162 $stmt->bind_result($name, $energy, $protein, $iron, $vitA, $thiamine, $riboflavin, $vitC);
163 while($stmt->fetch()){
164 $val[$name]["energy"]=(float)$energy;
165 $val[$name]["protein"]=(float)$protein;
166 $val[$name]["iron"]=(float)$iron;
167 $val[$name]["vitA"]=(float)$vitA;
168 $val[$name]["thiamine"]=(float)$thiamine;
169 $val[$name]["riboflavin"]=(float)$riboflavin;
170 $val[$name]["vitC"]=(float)$vitC;
171 }
172 $stmt->close();
173 }
174 }
175 return $val;
176 }
177
178 function make_string($sub){
179 $idx="";
180 if($sub["age"]==0){
181 $idx="infant_9m-12m";
182 }
183 else if($sub["age"]<18){
184 if($sub["sex"]=="Male"){
185 $idx="male_".$sub["age"]."y";
186 }
187 else if($sub["sex"]=="Female"){
188 $idx="female_".$sub["age"]."y";
189 }
190 }
191 else{
192 if($sub["sex"]=="Male"){
193 if($sub["work"]=="Sedentary"){
194 $idx="male_sedentary";
195 }
196 else if($sub["work"]=="Moderate"){
197 $idx="male_moderate";
198 }
199 if($sub["work"]=="Heavy"){
200 $idx="male_heavy";
201 }
202 }
203 else if($sub["sex"]=="Female"){
204 if($sub["work"]=="Sedentary"){
205 $idx="female_sedentary";
206 }
207 else if($sub["work"]=="Moderate"){
208 $idx="female_moderate";
209 }
210 if($sub["work"]=="Heavy"){
211 $idx="female_heavy";
212 }
213 }
214 }
215 return $idx;
216 }
217
218 function has_custom($value){
219 $flag=false;
220 foreach($_SESSION["custom_food"] as $item){
221 if($item["name"]==$value){
222 $flag=true;
223 break;
224 }
225 }
226 return $flag;
227 }
228 function get_custom_by_name($name){
229 $obj=false;
230 foreach($_SESSION["custom_food"] as $item){
231 if($item["name"]==$name){
232 $obj=$item;
233 break;
234 }
235 }
236 return $obj;
237 }
238 ?>