3 * Flexible script for graphing entities in 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';
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')) {
34 // Conversion functions/constants
35 function convertFtoC($a)
37 return ($a-32)*0.5556;
39 function getLbstoKgMultiplier()
43 function getIntoCmMultiplier()
47 function getIdealYSteps($a)
62 function graphsGetValues($name)
64 global $is_lbf, $pid, $table;
66 // Like below, but for LBF data.
67 $values = sqlStatement(
69 "ld.field_value AS " . add_escape_custom($name) . ", " .
71 "FROM forms AS f, lbf_data AS ld WHERE " .
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' " .
79 array($pid, $table, $name)
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) . ", " .
88 "FROM " . add_escape_custom($table) . " " .
89 "WHERE " . add_escape_custom($name) . " != 0 " .
90 "AND pid = ? ORDER BY date", array($pid));
96 //Customizations (such as titles and conversions)
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");
107 $titleGraph = $title." (".xl("lbs").")";
109 case "weight_metric":
110 $titleGraph = $title." (".xl("kg").")";
111 $multiplier = getLbstoKgMultiplier();
115 $titleGraph = $title." (".xl("in").")";
117 case "height_metric":
118 $titleGraph = $title." (".xl("cm").")";
119 $multiplier = getIntoCmMultiplier();
123 $titleGraph = xl("Blood Pressure")." (".xl("mmHg").")";
124 $titleGraphLine1 = xl("BP Systolic");
125 $titleGraphLine2 = xl("BP Diastolic");
128 $titleGraph = xl("Blood Pressure")." (".xl("mmHg").")";
129 $titleGraphLine1 = xl("BP Diastolic");
130 $titleGraphLine2 = xl("BP Systolic");
133 $titleGraph = $title." (".xl("per min").")";
136 $titleGraph = $title." (".xl("per min").")";
139 $titleGraph = $title." (".xl("F").")";
141 case "temperature_metric":
142 $titleGraph = $title." (".xl("C").")";
146 case "oxygen_saturation":
147 $titleGraph = $title." (".xl("%").")";
150 $titleGraph = $title." (".xl("in").")";
152 case "head_circ_metric":
153 $titleGraph = $title." (".xl("cm").")";
154 $multiplier = getIntoCmMultiplier();
158 $titleGraph = $title." (".xl("in").")";
160 case "waist_circ_metric":
161 $titleGraph = $title." (".xl("cm").")";
162 $multiplier = getIntoCmMultiplier();
166 $titleGraph = $title." (".xl("kg/m^2").")";
169 $titleGraph = $title;
175 // Like below, but for LBF data.
176 $values = graphsGetValues($name);
181 // If less than 2 values, then exit
182 if (sqlNumRows($values) < 2) {
186 // If blood pressure, then collect the other reading to allow graphing both in same graph
189 if ($name == "bp_systolic" ||
$name == "bp_diastolic") {
190 // Set BP flag and collect other pressure reading
192 if ($name == "bp_systolic") {
193 $name_alt = "bp_diastolic";
195 $name_alt = "bp_systolic";
198 // Collect the pertinent vitals and ranges.
199 $values_alt = graphsGetValues($name_alt);
202 if ($name == "bps" ||
$name == "bpd") {
203 // Set BP flag and collect other pressure reading
205 if ($name == "bps") {
209 if ($name == "bpd") {
213 // Collect the pertinent vitals and ranges.
214 $values_alt = graphsGetValues($name_alt);
220 while ($row = sqlFetchArray($values)) {
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"]);
230 // no conversion, so use raw value
234 $data[$x][$name] = $y;
239 //set up the other blood pressure line
240 while ($row = sqlFetchArray($values_alt)) {
241 if ($row["$name_alt"]) {
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"]);
250 // no conversion, so use raw value
251 $y=$row["$name_alt"];
254 $data[$x][$name_alt] = $y;
262 $data_final .= xl('Date') . "\t" . $titleGraphLine1 . "\t" . $titleGraphLine2 . "\n";
264 $data_final .= xl('Date') . "\t" . $titleGraph . "\n";
268 foreach ($data as $date => $value) {
270 $data_final .= $date . "\t" . $value[$name] . "\t" . $value[$name_alt] . "\n";
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
283 echo json_encode($graph_build, JSON_UNESCAPED_SLASHES
);