change eligibility batch from ssn to policy number, minor fix to filename with extra...
[openemr.git] / library / ajax / graphs.php
blobf962efe767d64484c270fbc3da30a35247317b43
1 <?php
2 /**
3 * Flexible script for graphing entities in OpenEMR.
5 * @package OpenEMR
6 * @link http://www.open-emr.org
7 * @author Brady Miller <brady.g.miller@gmail.com>
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
10 * @copyright Copyright (c) 2010-2017 Brady Miller <brady.g.miller@gmail.com>
11 * @copyright Copyright (c) 2011 Rod Roark <rod@sunsetsystems.com>
15 require_once(dirname(__FILE__) . "/../../interface/globals.php");
17 // Collect passed variable(s)
18 // $table is the sql table (or form name if LBF)
19 // $name identifies the desired data item
20 // $title is used as the title of the graph
21 $table = trim($_POST['table']);
22 $name = trim($_POST['name']);
23 $title = trim($_POST['title']);
25 $is_lbf = substr($table, 0, 3) === 'LBF';
27 // acl checks here
28 // For now, only allow access for med aco.
29 // This can be expanded depending on which table is accessed.
30 if (!acl_check('patients', 'med')) {
31 exit;
34 // Conversion functions/constants
35 function convertFtoC($a)
37 return ($a-32)*0.5556;
39 function getLbstoKgMultiplier()
41 return 0.45359237;
43 function getIntoCmMultiplier()
45 return 2.54;
47 function getIdealYSteps($a)
49 if ($a>1000) {
50 return 200;
51 } else if ($a>500) {
52 return 100;
53 } else if ($a>100) {
54 return 20;
55 } else if ($a>50) {
56 return 10;
57 } else {
58 return 5;
62 function graphsGetValues($name)
64 global $is_lbf, $pid, $table;
65 if ($is_lbf) {
66 // Like below, but for LBF data.
67 $values = sqlStatement(
68 "SELECT " .
69 "ld.field_value AS " . add_escape_custom($name) . ", " .
70 "f.date " .
71 "FROM forms AS f, lbf_data AS ld WHERE " .
72 "f.pid = ? AND " .
73 "f.formdir = ? AND " .
74 "f.deleted = 0 AND " .
75 "ld.form_id = f.form_id AND " .
76 "ld.field_id = ? AND " .
77 "ld.field_value != '0' " .
78 "ORDER BY f.date",
79 array($pid, $table, $name)
81 } else {
82 // Collect the pertinent info and ranges
83 // (Note am skipping values of zero, this could be made to be
84 // optional in the future when using lab values)
85 $values = SqlStatement("SELECT " .
86 add_escape_custom($name) . ", " .
87 "date " .
88 "FROM " . add_escape_custom($table) . " " .
89 "WHERE " . add_escape_custom($name) . " != 0 " .
90 "AND pid = ? ORDER BY date", array($pid));
93 return $values;
96 //Customizations (such as titles and conversions)
97 if ($is_lbf) {
98 $titleGraph = $title;
99 if ($name == 'bp_systolic' || $name == 'bp_diastolic') {
100 $titleGraph = xl("Blood Pressure") . " (" . xl("mmHg") . ")";
101 $titleGraphLine1 = xl("BP Systolic");
102 $titleGraphLine2 = xl("BP Diastolic");
104 } else {
105 switch ($name) {
106 case "weight":
107 $titleGraph = $title." (".xl("lbs").")";
108 break;
109 case "weight_metric":
110 $titleGraph = $title." (".xl("kg").")";
111 $multiplier = getLbstoKgMultiplier();
112 $name = "weight";
113 break;
114 case "height":
115 $titleGraph = $title." (".xl("in").")";
116 break;
117 case "height_metric":
118 $titleGraph = $title." (".xl("cm").")";
119 $multiplier = getIntoCmMultiplier();
120 $name = "height";
121 break;
122 case "bps":
123 $titleGraph = xl("Blood Pressure")." (".xl("mmHg").")";
124 $titleGraphLine1 = xl("BP Systolic");
125 $titleGraphLine2 = xl("BP Diastolic");
126 break;
127 case "bpd":
128 $titleGraph = xl("Blood Pressure")." (".xl("mmHg").")";
129 $titleGraphLine1 = xl("BP Diastolic");
130 $titleGraphLine2 = xl("BP Systolic");
131 break;
132 case "pulse":
133 $titleGraph = $title." (".xl("per min").")";
134 break;
135 case "respiration":
136 $titleGraph = $title." (".xl("per min").")";
137 break;
138 case "temperature":
139 $titleGraph = $title." (".xl("F").")";
140 break;
141 case "temperature_metric":
142 $titleGraph = $title." (".xl("C").")";
143 $isConvertFtoC = 1;
144 $name="temperature";
145 break;
146 case "oxygen_saturation":
147 $titleGraph = $title." (".xl("%").")";
148 break;
149 case "head_circ":
150 $titleGraph = $title." (".xl("in").")";
151 break;
152 case "head_circ_metric":
153 $titleGraph = $title." (".xl("cm").")";
154 $multiplier = getIntoCmMultiplier();
155 $name="head_circ";
156 break;
157 case "waist_circ":
158 $titleGraph = $title." (".xl("in").")";
159 break;
160 case "waist_circ_metric":
161 $titleGraph = $title." (".xl("cm").")";
162 $multiplier = getIntoCmMultiplier();
163 $name="waist_circ";
164 break;
165 case "BMI":
166 $titleGraph = $title." (".xl("kg/m^2").")";
167 break;
168 default:
169 $titleGraph = $title;
173 // Collect info
174 if ($table) {
175 // Like below, but for LBF data.
176 $values = graphsGetValues($name);
177 } else {
178 exit;
181 // If less than 2 values, then exit
182 if (sqlNumRows($values) < 2) {
183 exit;
186 // If blood pressure, then collect the other reading to allow graphing both in same graph
187 $isBP = 0;
188 if ($is_lbf) {
189 if ($name == "bp_systolic" || $name == "bp_diastolic") {
190 // Set BP flag and collect other pressure reading
191 $isBP = 1;
192 if ($name == "bp_systolic") {
193 $name_alt = "bp_diastolic";
194 } else {
195 $name_alt = "bp_systolic";
198 // Collect the pertinent vitals and ranges.
199 $values_alt = graphsGetValues($name_alt);
201 } else {
202 if ($name == "bps" || $name == "bpd") {
203 // Set BP flag and collect other pressure reading
204 $isBP = 1;
205 if ($name == "bps") {
206 $name_alt = "bpd";
209 if ($name == "bpd") {
210 $name_alt = "bps";
213 // Collect the pertinent vitals and ranges.
214 $values_alt = graphsGetValues($name_alt);
218 // Prepare data
219 $data = array();
220 while ($row = sqlFetchArray($values)) {
221 if ($row["$name"]) {
222 $x=$row['date'];
223 if ($multiplier) {
224 // apply unit conversion multiplier
225 $y=$row["$name"]*$multiplier;
226 } else if ($isConvertFtoC) {
227 // apply temp F to C conversion
228 $y=convertFtoC($row["$name"]);
229 } else {
230 // no conversion, so use raw value
231 $y=$row["$name"];
234 $data[$x][$name] = $y;
238 if ($isBP) {
239 //set up the other blood pressure line
240 while ($row = sqlFetchArray($values_alt)) {
241 if ($row["$name_alt"]) {
242 $x=$row['date'];
243 if ($multiplier) {
244 // apply unit conversion multiplier
245 $y=$row["$name_alt"]*$multiplier;
246 } else if ($isConvertFtoC) {
247 // apply temp F to C conversion
248 $y=convertFtoC($row["$name_alt"]);
249 } else {
250 // no conversion, so use raw value
251 $y=$row["$name_alt"];
254 $data[$x][$name_alt] = $y;
259 // Prepare label
260 $data_final = "";
261 if ($isBP) {
262 $data_final .= xl('Date') . "\t" . $titleGraphLine1 . "\t" . $titleGraphLine2 . "\n";
263 } else {
264 $data_final .= xl('Date') . "\t" . $titleGraph . "\n";
267 // Prepare data
268 foreach ($data as $date => $value) {
269 if ($isBP) {
270 $data_final .= $date . "\t" . $value[$name] . "\t" . $value[$name_alt] . "\n";
271 } else {
272 $data_final .= $date . "\t" . $value[$name] . "\n";
276 // Build and send back the json
277 $graph_build = array();
278 $graph_build['data_final'] = $data_final;
279 $graph_build['title'] = $titleGraph;
280 // Note need to also use " when building the $data_final rather
281 // than ' , or else JSON_UNESCAPED_SLASHES doesn't work and \n and
282 // \t get escaped.
283 echo json_encode($graph_build, JSON_UNESCAPED_SLASHES);