4 * Flexible script for graphing entities in OpenEMR.
7 * @link https://www.open-emr.org
8 * @author Rod Roark <rod@sunsetsystems.com>
9 * @author Brady Miller <brady.g.miller@gmail.com>
10 * @copyright Copyright (c) 2011 Rod Roark <rod@sunsetsystems.com>
11 * @copyright Copyright (c) 2010-2018 Brady Miller <brady.g.miller@gmail.com>
12 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
15 require_once(dirname(__FILE__
) . "/../../interface/globals.php");
17 use OpenEMR\Common\Acl\AclMain
;
18 use OpenEMR\Common\Csrf\CsrfUtils
;
20 if (!CsrfUtils
::verifyCsrfToken($_POST["csrf_token_form"])) {
21 CsrfUtils
::csrfNotVerified();
24 // Collect passed variable(s)
25 // $table is the sql table (or form name if LBF)
26 // $name identifies the desired data item
27 // $title is used as the title of the graph
28 $table = trim($_POST['table']);
29 $name = trim($_POST['name']);
30 $title = trim($_POST['title']);
32 $is_lbf = substr($table, 0, 3) === 'LBF';
35 // For now, only allow access for med aco.
36 // This can be expanded depending on which table is accessed.
37 if (!AclMain
::aclCheckCore('patients', 'med')) {
41 // Conversion functions/constants
42 function convertFtoC($a)
44 return ($a - 32) * 0.5556;
46 function getLbstoKgMultiplier()
50 function getIntoCmMultiplier()
54 function getIdealYSteps($a)
69 function graphsGetValues($name)
71 global $is_lbf, $pid, $table;
73 // Like below, but for LBF data.
74 $values = sqlStatement(
76 "ld.field_value AS " . add_escape_custom($name) . ", " .
77 // If data was entered retroactively then cannot use the data entry date.
78 "IF (LEFT(f.date, 10) = LEFT(fe.date, 10), f.date, fe.date) AS date " .
79 "FROM forms AS f, form_encounter AS fe, lbf_data AS ld WHERE " .
81 "f.formdir = ? AND " .
82 "f.deleted = 0 AND " .
83 "fe.pid = f.pid AND fe.encounter = f.encounter AND " .
84 "ld.form_id = f.form_id AND " .
85 "ld.field_id = ? AND " .
86 "ld.field_value != '0' " .
88 array($pid, $table, $name)
91 // Collect the pertinent info and ranges
92 // (Note am skipping values of zero, this could be made to be
93 // optional in the future when using lab values)
94 $values = SqlStatement("SELECT " .
95 escape_sql_column_name($name, array($table)) . ", " .
97 "FROM " . escape_table_name($table) . " " .
98 "WHERE " . escape_sql_column_name($name, array($table)) . " != 0 " .
99 "AND pid = ? ORDER BY date", array($pid));
105 //Customizations (such as titles and conversions)
107 $titleGraph = $title;
108 if ($name == 'bp_systolic' ||
$name == 'bp_diastolic') {
109 $titleGraph = xl("Blood Pressure") . " (" . xl("mmHg") . ")";
110 $titleGraphLine1 = xl("BP Systolic");
111 $titleGraphLine2 = xl("BP Diastolic");
116 $titleGraph = $title . " (" . xl("lbs") . ")";
118 case "weight_metric":
119 $titleGraph = $title . " (" . xl("kg") . ")";
120 $multiplier = getLbstoKgMultiplier();
124 $titleGraph = $title . " (" . xl("in") . ")";
126 case "height_metric":
127 $titleGraph = $title . " (" . xl("cm") . ")";
128 $multiplier = getIntoCmMultiplier();
132 $titleGraph = xl("Blood Pressure") . " (" . xl("mmHg") . ")";
133 $titleGraphLine1 = xl("BP Systolic");
134 $titleGraphLine2 = xl("BP Diastolic");
137 $titleGraph = xl("Blood Pressure") . " (" . xl("mmHg") . ")";
138 $titleGraphLine1 = xl("BP Diastolic");
139 $titleGraphLine2 = xl("BP Systolic");
142 $titleGraph = $title . " (" . xl("per min") . ")";
145 $titleGraph = $title . " (" . xl("per min") . ")";
148 $titleGraph = $title . " (" . xl("F") . ")";
150 case "temperature_metric":
151 $titleGraph = $title . " (" . xl("C") . ")";
153 $name = "temperature";
155 case "oxygen_saturation":
156 $titleGraph = $title . " (" . xl("%") . ")";
159 $titleGraph = $title . " (" . xl("in") . ")";
161 case "head_circ_metric":
162 $titleGraph = $title . " (" . xl("cm") . ")";
163 $multiplier = getIntoCmMultiplier();
167 $titleGraph = $title . " (" . xl("in") . ")";
169 case "waist_circ_metric":
170 $titleGraph = $title . " (" . xl("cm") . ")";
171 $multiplier = getIntoCmMultiplier();
172 $name = "waist_circ";
175 $titleGraph = $title . " (" . xl("kg/m^2") . ")";
178 $titleGraph = $title;
184 // Like below, but for LBF data.
185 $values = graphsGetValues($name);
190 // If less than 2 values, then exit
191 if (sqlNumRows($values) < 2) {
195 // If blood pressure, then collect the other reading to allow graphing both in same graph
198 if ($name == "bp_systolic" ||
$name == "bp_diastolic") {
199 // Set BP flag and collect other pressure reading
201 if ($name == "bp_systolic") {
202 $name_alt = "bp_diastolic";
204 $name_alt = "bp_systolic";
207 // Collect the pertinent vitals and ranges.
208 $values_alt = graphsGetValues($name_alt);
211 if ($name == "bps" ||
$name == "bpd") {
212 // Set BP flag and collect other pressure reading
214 if ($name == "bps") {
218 if ($name == "bpd") {
222 // Collect the pertinent vitals and ranges.
223 $values_alt = graphsGetValues($name_alt);
229 while ($row = sqlFetchArray($values)) {
232 if ($multiplier ??
null) {
233 // apply unit conversion multiplier
234 $y = $row["$name"] * $multiplier;
235 } elseif ($isConvertFtoC ??
null) {
236 // apply temp F to C conversion
237 $y = convertFtoC($row["$name"]);
239 // no conversion, so use raw value
243 $data[$x][$name] = $y;
248 //set up the other blood pressure line
249 while ($row = sqlFetchArray($values_alt)) {
250 if ($row["$name_alt"]) {
252 if ($multiplier ??
null) {
253 // apply unit conversion multiplier
254 $y = $row["$name_alt"] * $multiplier;
255 } elseif ($isConvertFtoC ??
null) {
256 // apply temp F to C conversion
257 $y = convertFtoC($row["$name_alt"]);
259 // no conversion, so use raw value
260 $y = $row["$name_alt"];
263 $data[$x][$name_alt] = $y;
271 $data_final .= xl('Date') . "\t" . $titleGraphLine1 . "\t" . $titleGraphLine2 . "\n";
273 $data_final .= xl('Date') . "\t" . $titleGraph . "\n";
277 foreach ($data as $date => $value) {
279 $data_final .= $date . "\t" . $value[$name] . "\t" . $value[$name_alt] . "\n";
281 $data_final .= $date . "\t" . $value[$name] . "\n";
285 // Build and send back the json
286 $graph_build = array();
287 $graph_build['data_final'] = $data_final;
288 $graph_build['title'] = $titleGraph;
289 // Note need to also use " when building the $data_final rather
290 // than ' , or else JSON_UNESCAPED_SLASHES doesn't work and \n and
292 echo json_encode($graph_build, JSON_UNESCAPED_SLASHES
);