minor changes to prior commit
[openemr.git] / interface / patient_file / summary / labdata.php
blobb733631e60aef011c160ec46b67b5a5cf47fbb76
1 <?php
2 /**
3 * How to present clinical parameter.
5 * Copyright (C) 2014 Joe Slam <trackanything@produnis.de>
7 * LICENSE: This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>.
18 * @package OpenEMR
19 * @author Joe Slam <trackanything@produnis.de>
20 * @link http://www.open-emr.org
21 * ---------------------------------------------------------------------------------
23 * this script needs $pid to run...
25 * if you copy this file to another place,
26 * make sure you set $path_to_this_script
27 * to the propper path...
30 * Prepare your data:
31 * this script expects propper 'result_code' entries
32 * in table 'procedure_results'. If your data miss
33 * 'result_code' entries, you won't see anything,
34 * so make sure they are there.
35 * [additionally, the script will also look for 'units',
36 * 'range' and 'code_text'. If these data are not available,
37 * the script will run anyway...]
39 * the script will list all available patient's 'result_codes'
40 * from table 'procedure_results'. Check those you wish to view.
41 * If you see nothing to select, then
42 * a) there is actually no lab data of this patient available
43 * b) the lab data are missing 'result_code'-entries in table 'procedure_results'
47 // Some initial api-inputs
50 require_once("../../globals.php");
51 require_once("../../../library/options.inc.php");
52 include_once($GLOBALS["srcdir"] . "/api.inc");
54 // Set the path to this script
55 $path_to_this_script = $rootdir . "/patient_file/summary/labdata.php";
58 // is this the printable HTML-option?
59 $printable = $_POST['print'];
62 // main db-spell
63 //----------------------------------------
64 $main_spell = "SELECT procedure_result.procedure_result_id, procedure_result.result, procedure_result.result_text, procedure_result.result_code, procedure_result.units, procedure_result.abnormal, procedure_result.range, ";
65 $main_spell .= "procedure_report.date_collected, procedure_report.review_status, ";
66 $main_spell .= "procedure_order.encounter_id ";
67 $main_spell .= "FROM procedure_result ";
68 $main_spell .= "JOIN procedure_report ";
69 $main_spell .= " ON procedure_result.procedure_report_id = procedure_report.procedure_report_id ";
70 $main_spell .= "JOIN procedure_order ";
71 $main_spell .= " ON procedure_report.procedure_order_id = procedure_order.procedure_order_id ";
72 $main_spell .= "WHERE procedure_result.result_code = ? "; // '?'
73 $main_spell .= "AND procedure_order.patient_id = ? ";
74 $main_spell .= "AND procedure_result.result IS NOT NULL ";
75 $main_spell .= "AND procedure_result.result != ''";
76 $main_spell .= "ORDER BY procedure_report.date_collected DESC ";
77 //----------------------------------------
79 // some styles and javascripts
80 // ####################################################
81 echo "<html><head>";
83 <title><?php echo xlt("Labs"); ?></title>
85 <?php require $GLOBALS['srcdir'] . '/js/xl/dygraphs.js.php'; ?>
87 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
88 <link rel="stylesheet" href="<?php echo $web_root; ?>/interface/themes/labdata.css" type="text/css">
89 <link rel="stylesheet" href="<?php echo $GLOBALS['assets_static_relative']; ?>/modified/dygraphs-2-0-0/dygraph.css" type="text/css"></script>
90 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-1-3-2/jquery.js"></script>
91 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/modified/dygraphs-2-0-0/dygraph.js?v=<?php echo $v_js_includes; ?>"></script>
93 <script type="text/javascript" language="JavaScript">
94 function checkAll(bx) {
95 for (var tbls=document.getElementsByTagName("table"), i=tbls.length; i--; )
96 for (var bxs=tbls[i].getElementsByTagName("input"), j=bxs.length; j--; )
97 if (bxs[j].type=="checkbox")
98 bxs[j].checked = bx.checked;
101 </script>
102 <?php ##############################################################################
103 echo "</head><body class='body_top'>";
104 echo "<div id='labdata'>";
105 echo "<h2>" . xlt('Labs') . "</h2>";
106 echo "<span class='text'>";
107 // some patient data...
108 $spell = "SELECT * ";
109 $spell .= "FROM patient_data ";
110 $spell .= "WHERE id = ?";
111 //---
112 $myrow = sqlQuery($spell, array($pid));
113 $lastname = $myrow["lname"];
114 $firstname = $myrow["fname"];
115 $DOB = $myrow["DOB"];
119 if ($printable) {
120 echo "<table border=0>";
121 echo "<tr><td>" . xlt('Patient') . ": </td><td><b>" . text($lastname) . ", " . text($firstname) . "</b></td></tr>";
122 echo "<tr><td>" . xlt('Patient ID') . ": </td><td>" . text($pid) . "</td></tr>";
123 echo "<tr><td>" . xlt('Date of birth') . ": </td><td>" . text($DOB) . "</td></tr>";
124 echo "<tr><td>" . xlt('Access date') . ": </td><td>" . text(date('Y-m-d - H:i:s')) . "</td></tr>";
125 echo "</table>";
129 echo "<div id='reports_list'>";
130 if (!$printable) {
131 echo "<form method='post' action='" . $path_to_this_script . "' onsubmit='return top.restoreSession()'>";
132 // What items are there for patient $pid?
133 // -----------------------------------------------
134 $value_list = array();
135 $value_select = $_POST['value_code']; // what items are checkedboxed?
136 $tab = 0;
137 echo xlt('Select items') . ": ";
138 echo "<table border='1'>";
139 echo "<tr><td>";
141 $spell = "SELECT DISTINCT procedure_result.result_code AS value_code ";
142 $spell .= "FROM procedure_result ";
143 $spell .= "JOIN procedure_report ";
144 $spell .= " ON procedure_result.procedure_report_id = procedure_report.procedure_report_id ";
145 $spell .= "JOIN procedure_order ";
146 $spell .= " ON procedure_report.procedure_order_id = procedure_order.procedure_order_id ";
147 $spell .= "WHERE procedure_order.patient_id = ? ";
148 $spell .= "AND procedure_result.result IS NOT NULL ";
149 $spell .= "AND procedure_result.result != ''";
150 $spell .= "ORDER BY procedure_result.result_code ASC ";
151 $query = sqlStatement($spell, array($pid));
154 // Select which items to view...
155 $i = 0;
156 while ($myrow = sqlFetchArray($query)) {
157 echo "<input type='checkbox' name='value_code[]' value=" . attr($myrow['value_code']) . " ";
158 if ($value_select) {
159 if (in_array($myrow['value_code'], $value_select)) {
160 echo "checked='checked' ";
164 echo " /> " . text($myrow['value_code']) . "<br />";
165 $value_list[$i][value_code] = $myrow['value_code'];
166 $i++;
167 $tab++;
168 if ($tab == 10) {
169 echo "</td><td>";
170 $tab=0;
174 echo "</tr>";
175 echo "</table>";
176 echo "</div>";
178 ?><input type='checkbox' onclick="checkAll(this)" /> <?php echo xlt('Toggle All') . "<br/>";
179 echo "<table><tr>";
180 // Choose output mode [list vs. matrix]
181 echo "<td>" . xlt('Select output') . ":</td>";
182 echo "<td><input type='radio' name='mode' ";
183 $mode = $_POST['mode'];
184 if ($mode == 'list') {
185 echo "checked='checked' ";
188 echo " value='list'> " . xlt('List') . "<br>";
190 echo "<input type='radio' name='mode' ";
191 if ($mode != 'list') {
192 echo "checked='checked' ";
195 echo " value='matrix'> " . xlt('Matrix') . "<br>";
197 echo "<td></td></td>";
198 echo "</tr><tr>";
199 echo "<td>";
201 echo "<a href='../summary/demographics.php' ";
202 echo " class='css_button' onclick='top.restoreSession()'>";
203 echo "<span>" . xlt('Back to Patient') . "</span></a>";
205 echo "</td>";
206 echo "<td><input type='submit' name='submit' value='" . xla('Submit') . "' /></td>";
207 echo "</tr></table>";
208 echo "</form>";
209 } // end "if printable"
210 echo "<br><br><hr><br>";
212 // print results of patient's items
213 //-------------------------------------------
214 $mode = $_POST['mode'];
215 $value_select = $_POST['value_code'];
216 // are some Items selected?
217 if ($value_select) {
218 // print in List-Mode
219 if ($mode=='list') {
220 $i = 0;
221 $item_graph = 0;
222 $rowspan = count($value_select);
223 echo "<table border='1' cellspacing='3'>";
224 echo "<tr>";
225 #echo "<th class='list'>Item</td>";
226 echo "<th class='list'>" . xlt('Name') . "</th> ";
227 echo "<th class='list'>&nbsp;" . xlt('Result') . "&nbsp;</th> ";
228 echo "<th class='list'>" . xlt('Range') . "</th> ";
229 echo "<th class='list'>" . xlt('Units') . "</th> ";
230 echo "<th class='list'>" . xlt('Date') . "</th> ";
231 echo "<th class='list'>" . xlt('Review') . "</th> ";
232 echo "<th class='list'>" . xlt('Enc') . "</th> ";
233 #echo "<th class='list'>resultID</th> ";
234 echo "</tr>";
235 // get complete data of each item
236 foreach ($value_select as $this_value) {
237 // set a plot-spacer
238 echo "<tr><td colspan='7'><div id='graph_item_" . attr($item_graph) . "' class='chart-dygraphs'></div></td></tr>";
239 $value_count = 0;
240 $value_array = array(); // reset local array
241 $date_array = array();// reset local array
243 // get data from db
244 $spell = $main_spell;
245 $query = sqlStatement($spell, array($this_value,$pid));
246 while ($myrow = sqlFetchArray($query)) {
247 $value_array[0][$value_count] = $myrow['result'];
248 $date_array[$value_count] = $myrow['date_collected'];
249 $the_item = $myrow['result_text'];
250 echo "<tr>";
251 echo "<td class='list_item'>" . text($myrow['result_text']) . "</td>";
254 if ($myrow['abnormal'] == 'No' || $myrow['abnormal'] == 'no' || $myrow['abnormal'] == '' || $myrow['abnormal'] == null) {
255 echo "<td class='list_result'>&nbsp;&nbsp;&nbsp;" . text($myrow['result']) . "&nbsp;&nbsp;</td>";
256 } else {
257 echo "<td class='list_result_abnorm'>&nbsp;" ;
258 if ($myrow['abnormal'] == 'high') {
259 echo "+ ";
260 } elseif ($myrow['abnormal'] == 'low') {
261 echo "- ";
262 } else {
263 echo "&nbsp;&nbsp;";
266 echo text($myrow['result']) . "&nbsp;&nbsp;</td>";
269 echo "<td class='list_item'>" . text($myrow['range']) . "</td>";
271 // echo "<td class='list_item'>" . generate_display_field(array('data_type'=>'1','list_id'=>'proc_unit'),$myrow['units']) . "</td>";
272 echo "<td class='list_item'>" . text($myrow['units']) . "</td>";
274 echo "<td class='list_log'>" . text($myrow['date_collected']) . "</td>";
275 echo "<td class='list_log'>" . text($myrow['review_status']) . "</td>";
276 echo "<td class='list_log'>";
277 if (!$printable) {
278 echo "<a href='../../patient_file/encounter/encounter_top.php?set_encounter=". attr($myrow['encounter_id']) . "' target='RBot'>";
279 echo text($myrow['encounter_id']);
280 echo "</a>";
281 } else {
282 echo text($myrow['encounter_id']);
285 echo "</td>";
286 echo "</tr>";
287 $value_count++;
290 if ($value_count > 1 && !$printable) {
291 echo "<tr><td colspan='7' align='center'>";
292 echo "<input type='button' class='graph_button' onclick='get_my_graph" . attr($item_graph) . "()' name='' value='" . xla('Plot item') . " \"" . attr($the_item) . "\"'>";
293 echo "</td></tr>";
296 <script type="text/javascript">
297 // prepare to plot the stuff
298 top.restoreSession();
299 function get_my_graph<?php echo attr($item_graph) ?>(){
300 var thedates = JSON.stringify(<?php echo json_encode($date_array); ?>);
301 var thevalues = JSON.stringify(<?php echo json_encode($value_array); ?>);
302 var theitem = JSON.stringify(<?php echo json_encode(array($the_item)); ?>);
303 var thetitle = JSON.stringify(<?php echo json_encode($the_item); ?>);
304 var checkboxfake = JSON.stringify(<?php echo json_encode(array(0)); ?>);
306 $.ajax({ url: '<?php echo $web_root; ?>/library/ajax/graph_track_anything.php',
307 type: 'POST',
308 data: { dates: thedates,
309 values: thevalues,
310 track: thetitle,
311 items: theitem,
312 thecheckboxes: checkboxfake
314 dataType: "json",
315 success: function(returnData){
316 g2 = new Dygraph(
317 document.getElementById("graph_item_<?php echo $item_graph ?>"),
318 returnData.data_final,
320 title: returnData.title,
321 delimiter: '\t',
322 xRangePad: 20,
323 yRangePad: 20,
324 xlabel: xlabel_translate
328 error: function (XMLHttpRequest, textStatus, errorThrown) {
329 alert(XMLHttpRequest.responseText);
331 }); // end ajax query
333 //------------------------------------------------------------------------
334 </script>
335 <?php
336 echo "<tr><td colspan='9' class='list_spacer'><hr></td></tr>";
337 $item_graph++;
340 echo "</table><br>";
341 }// end if mode = list
343 //##########################################################################################################################
344 if ($mode=='matrix') {
345 $value_matrix = array();
346 $datelist = array();
347 $i = 0;
348 // get all data of patient's items
349 foreach ($value_select as $this_value) {
350 $spell = $main_spell;
351 $query = sqlStatement($spell, array($this_value,$pid));
353 while ($myrow = sqlFetchArray($query)) {
354 $value_matrix[$i][procedure_result_id] = $myrow['procedure_result_id'];
355 $value_matrix[$i][result_code] = $myrow['result_code'];
356 $value_matrix[$i][result_text] = $myrow['result_text'];
357 $value_matrix[$i][result] = $myrow['result'];
358 // $value_matrix[$i][units] = generate_display_field(array('data_type'=>'1','list_id'=>'proc_unit'),$myrow['units']) ;
359 $value_matrix[$i][units] = $myrow['units'];
360 $value_matrix[$i][range] = $myrow['range'];
361 $value_matrix[$i][abnormal] = $myrow['abnormal'];
362 $value_matrix[$i][review_status] = $myrow['review_status'];
363 $value_matrix[$i][encounter_id] = $myrow['encounter_id'];
364 $value_matrix[$i][date_collected] = $myrow['date_collected'];
365 $datelist[] = $myrow['date_collected'];
366 $i++;
370 // get unique datetime
371 $datelist = array_unique($datelist);
373 // sort datetime DESC
374 rsort($datelist);
376 // sort item-data
377 foreach ($value_matrix as $key => $row) {
378 $result_code[$key] = $row['result_code'];
379 $date_collected[$key] = $row['date_collected'];
382 array_multisort(array_map('strtolower', $result_code), SORT_ASC, $date_collected, SORT_DESC, $value_matrix);
384 $cellcount = count($datelist);
385 $itemcount = count($value_matrix);
387 // print matrix
388 echo "<table border='1' cellpadding='2'>";
389 echo "<tr>";
390 #echo "<th class='matrix'>Item</th>";
391 echo "<th class='matrix'>" . xlt('Name') . "</th>";
392 echo "<th class='matrix'>" . xlt('Range') . "</th>";
393 echo "<th class='matrix'>" . xlt('Unit') . "</th>";
394 echo "<th class='matrix_spacer'>|</td>";
395 foreach ($datelist as $this_date) {
396 echo "<th width='30' class='matrix_time'>" . text($this_date) . "</th>";
399 echo "</tr>";
401 $i=0;
402 $a=true;
403 while ($a==true) {
404 echo "<tr>";
405 #echo "<td class='matrix_item'>" . $value_matrix[$i]['result_code'] . "</td>";
406 echo "<td class='matrix_item'>" . text($value_matrix[$i]['result_text']) . "</td>";
407 echo "<td class='matrix_item'>" . text($value_matrix[$i]['range']) . "</td>";
408 echo "<td class='matrix_item'>" . text($value_matrix[$i]['units']) . "</td>";
409 echo "<td class='matrix_spacer'> | </td>";
411 $z=0;
412 while ($z < $cellcount) {
413 if ($value_matrix[$i]['date_collected'] == $datelist[$z]) {
414 if ($value_matrix[$i]['result'] == null) {
415 echo "<td class='matrix_result'> </td>";
416 } else {
417 if ($value_matrix[$i]['abnormal'] == 'No' || $value_matrix[$i]['abnormal'] == 'no' || $value_matrix[$i]['abnormal'] == '' || $value_matrix[$i]['abnormal'] == null) {
418 echo "<td class='matrix_result'>&nbsp;&nbsp;&nbsp;" . text($value_matrix[$i]['result']) . "&nbsp;&nbsp;</td>";
419 } else {
420 echo "<td class='matrix_result_abnorm'>&nbsp;&nbsp;" ;
421 if ($value_matrix[$i]['abnormal'] == 'high') {
422 echo "+ ";
423 } elseif ($value_matrix[$i]['abnormal'] == 'low') {
424 echo "- ";
427 echo text($value_matrix[$i]['result']) . "&nbsp;&nbsp;</td>";
431 $j = $i;
432 $i++;
434 if ($value_matrix[$i]['result_code'] != $value_matrix[$j]['result_code']) {
435 $z = $cellcount;
437 } else {
438 echo "<td class='matrix_result'>&nbsp;</td>";
441 $z++;
444 if ($i == $itemcount) {
445 $a=false;
449 echo "</table>";
450 }// end if mode = matrix
451 } else { // end of "are items selected"
452 echo "<p>" . xlt('No parameters selected') . ".</p>";
453 $nothing = true;
457 if (!$printable) {
458 if (!$nothing) {
459 echo "<p>";
460 echo "<form method='post' action='" . $path_to_this_script . "' target='_new' onsubmit='return top.restoreSession()'>";
461 echo "<input type='hidden' name='mode' value='". attr($mode) . "'>";
462 foreach ($_POST['value_code'] as $this_valuecode) {
463 echo "<input type='hidden' name='value_code[]' value='". attr($this_valuecode) . "'>";
466 echo "<input type='submit' name='print' value='" . xla('View Printable Version') . "' />";
467 echo "</form>";
468 echo "<br><a href='../summary/demographics.php' ";
469 echo " class='css_button' onclick='top.restoreSession()'>";
470 echo "<span>" . xlt('Back to Patient') . "</span></a>";
472 } else {
473 echo "<p>" . xlt('End of report') . ".</p>";
476 echo "</span>";
477 echo "<br><br>";
478 echo "</div>";
479 echo "</body></html>";