more organization of autoloaded files (#424)
[openemr.git] / interface / patient_file / summary / labdata.php
blobb81025feb33472b03e55ee4d48617e7fdad396d9
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
48 $sanitize_all_escapes = true;
49 $fake_register_globals = false;
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>
84 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
85 <link rel="stylesheet" href="<?php echo $web_root; ?>/interface/themes/labdata.css" type="text/css">
86 <script type="text/javascript" src="<?php echo $GLOBALS['assets_static_relative']; ?>/jquery-min-1-3-2/index.js"></script>
87 <script type="text/javascript" src="<?php echo $web_root; ?>/library/openflashchart/js/json/json2.js"></script>
88 <script type="text/javascript" src="<?php echo $web_root; ?>/library/openflashchart/js/swfobject.js"></script>
89 <script type="text/javascript" language="JavaScript">
90 function checkAll(bx) {
91 for (var tbls=document.getElementsByTagName("table"), i=tbls.length; i--; )
92 for (var bxs=tbls[i].getElementsByTagName("input"), j=bxs.length; j--; )
93 if (bxs[j].type=="checkbox")
94 bxs[j].checked = bx.checked;
97 // this is automatically called by swfobject.embedSWF()
98 //------------------------------------------------------
99 function open_flash_chart_data(){
100 return JSON.stringify(data);
102 //------------------------------------------------------
105 // set up flashvars for ofc
106 var flashvars = {};
107 var data;
109 </script>
110 <?php ##############################################################################
111 echo "</head><body class='body_top'>";
112 echo "<div id='labdata'>";
113 echo "<h2>" . xlt('Labs') . "</h2>";
114 echo "<span class='text'>";
115 // some patient data...
116 $spell = "SELECT * ";
117 $spell .= "FROM patient_data ";
118 $spell .= "WHERE id = ?";
119 //---
120 $myrow = sqlQuery($spell,array($pid));
121 $lastname = $myrow["lname"];
122 $firstname = $myrow["fname"];
123 $DOB = $myrow["DOB"];
127 if($printable) {
128 echo "<table border=0>";
129 echo "<tr><td>" . xlt('Patient') . ": </td><td><b>" . text($lastname) . ", " . text($firstname) . "</b></td></tr>";
130 echo "<tr><td>" . xlt('Patient ID') . ": </td><td>" . text($pid) . "</td></tr>";
131 echo "<tr><td>" . xlt('Date of birth') . ": </td><td>" . text($DOB) . "</td></tr>";
132 echo "<tr><td>" . xlt('Access date') . ": </td><td>" . text(date('Y-m-d - H:i:s')) . "</td></tr>";
133 echo "</table>";
137 echo "<div id='reports_list'>";
138 if(!$printable){
139 echo "<form method='post' action='" . $path_to_this_script . "' onsubmit='return top.restoreSession()'>";
140 // What items are there for patient $pid?
141 // -----------------------------------------------
142 $value_list = array();
143 $value_select = $_POST['value_code']; // what items are checkedboxed?
144 $tab = 0;
145 echo xlt('Select items') . ": ";
146 echo "<table border='1'>";
147 echo "<tr><td>";
149 $spell = "SELECT DISTINCT procedure_result.result_code AS value_code ";
150 $spell .= "FROM procedure_result ";
151 $spell .= "JOIN procedure_report ";
152 $spell .= " ON procedure_result.procedure_report_id = procedure_report.procedure_report_id ";
153 $spell .= "JOIN procedure_order ";
154 $spell .= " ON procedure_report.procedure_order_id = procedure_order.procedure_order_id ";
155 $spell .= "WHERE procedure_order.patient_id = ? ";
156 $spell .= "AND procedure_result.result IS NOT NULL ";
157 $spell .= "AND procedure_result.result != ''";
158 $spell .= "ORDER BY procedure_result.result_code ASC ";
159 $query = sqlStatement($spell,array($pid));
162 // Select which items to view...
163 $i = 0;
164 while($myrow = sqlFetchArray($query)){
166 echo "<input type='checkbox' name='value_code[]' value=" . attr($myrow['value_code']) . " ";
167 if($value_select){
168 if (in_array($myrow['value_code'], $value_select)){ echo "checked='checked' ";}
170 echo " /> " . text($myrow['value_code']) . "<br />";
171 $value_list[$i][value_code] = $myrow['value_code'];
172 $i++;
173 $tab++;
174 if($tab == 10) {
175 echo "</td><td>";
176 $tab=0;
179 echo "</tr>";
180 echo "</table>";
181 echo "</div>";
183 ?><input type='checkbox' onclick="checkAll(this)" /> <?php echo xlt('Toggle All') . "<br/>";
184 echo "<table><tr>";
185 // Choose output mode [list vs. matrix]
186 echo "<td>" . xlt('Select output') . ":</td>";
187 echo "<td><input type='radio' name='mode' ";
188 $mode = $_POST['mode'];
189 if($mode == 'list'){ echo "checked='checked' ";}
190 echo " value='list'> " . xlt('List') . "<br>";
192 echo "<input type='radio' name='mode' ";
193 if($mode != 'list'){ echo "checked='checked' ";}
194 echo " value='matrix'> " . xlt('Matrix') . "<br>";
196 echo "<td></td></td>";
197 echo "</tr><tr>";
198 echo "<td>";
200 echo "<a href='../summary/demographics.php' ";
201 echo " class='css_button' onclick='top.restoreSession()'>";
202 echo "<span>" . xlt('Back to Patient') . "</span></a>";
204 echo "</td>";
205 echo "<td><input type='submit' name='submit' value='" . xla('Submit') . "' /></td>";
206 echo "</tr></table>";
207 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){
219 // print in List-Mode
220 if($mode=='list'){
222 $i = 0;
223 $item_graph = 0;
224 $rowspan = count($value_select);
225 echo "<table border='1' cellspacing='3'>";
226 echo "<tr>";
227 #echo "<th class='list'>Item</td>";
228 echo "<th class='list'>" . xlt('Name') . "</th> ";
229 echo "<th class='list'>&nbsp;" . xlt('Result') . "&nbsp;</th> ";
230 echo "<th class='list'>" . xlt('Range') . "</th> ";
231 echo "<th class='list'>" . xlt('Units') . "</th> ";
232 echo "<th class='list'>" . xlt('Date') . "</th> ";
233 echo "<th class='list'>" . xlt('Review') . "</th> ";
234 echo "<th class='list'>" . xlt('Enc') . "</th> ";
235 #echo "<th class='list'>resultID</th> ";
236 echo "</tr>";
237 // get complete data of each item
238 foreach($value_select as $this_value){
239 // set a plot-spacer
240 echo "<tr><td colspan='7'><div id='graph_item_" . attr($item_graph) . "'</td></tr>";
241 $value_count = 0;
242 $value_array = array(); // reset local array
243 $date_array = array();// reset local array
245 // get data from db
246 $spell = $main_spell;
247 $query = sqlStatement($spell,array($this_value,$pid));
248 while($myrow = sqlFetchArray($query)){
250 $value_array[0][$value_count] = $myrow['result'];
251 $date_array[$value_count] = $myrow['date_collected'];
252 $the_item = $myrow['result_text'];
253 echo "<tr>";
254 echo "<td class='list_item'>" . text($myrow['result_text']) . "</td>";
257 if($myrow['abnormal'] == 'No' || $myrow['abnormal'] == 'no' || $myrow['abnormal'] == '' || $myrow['abnormal'] == NULL ) {
258 echo "<td class='list_result'>&nbsp;&nbsp;&nbsp;" . text($myrow['result']) . "&nbsp;&nbsp;</td>";
259 } else {
260 echo "<td class='list_result_abnorm'>&nbsp;" ;
261 if($myrow['abnormal'] == 'high') {
262 echo "+ ";
263 } elseif ($myrow['abnormal'] == 'low') {
264 echo "- ";
265 } else {
266 echo "&nbsp;&nbsp;";
268 echo text($myrow['result']) . "&nbsp;&nbsp;</td>";
270 echo "<td class='list_item'>" . text($myrow['range']) . "</td>";
272 // echo "<td class='list_item'>" . generate_display_field(array('data_type'=>'1','list_id'=>'proc_unit'),$myrow['units']) . "</td>";
273 echo "<td class='list_item'>" . text($myrow['units']) . "</td>";
275 echo "<td class='list_log'>" . text($myrow['date_collected']) . "</td>";
276 echo "<td class='list_log'>" . text($myrow['review_status']) . "</td>";
277 echo "<td class='list_log'>";
278 if(!$printable){
279 echo "<a href='../../patient_file/encounter/encounter_top.php?set_encounter=". attr($myrow['encounter_id']) . "' target='RBot'>";
280 echo text($myrow['encounter_id']);
281 echo "</a>";
282 } else {
283 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/openflashchart/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 // ofc will look after a variable named "ofc"
317 // inside of the flashvar
318 // However, we need to set both
319 // data and flashvars.ofc
320 data=returnData;
321 flashvars.ofc = returnData;
322 // call ofc with proper falshchart
323 swfobject.embedSWF('<?php echo $web_root; ?>/library/openflashchart/open-flash-chart.swf',
324 "graph_item_<?php echo $item_graph ?>", "650", "200", "9.0.0","",flashvars);
326 error: function (XMLHttpRequest, textStatus, errorThrown) {
327 alert(XMLHttpRequest.responseText);
329 }); // end ajax query
331 //------------------------------------------------------------------------
332 </script>
333 <?php
334 echo "<tr><td colspan='9' class='list_spacer'><hr></td></tr>";
335 $item_graph++;
337 echo "</table><br>";
339 }// end if mode = list
341 //##########################################################################################################################
342 if($mode=='matrix'){
344 $value_matrix = array();
345 $datelist = array();
346 $i = 0;
347 // get all data of patient's items
348 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'];
381 array_multisort(array_map('strtolower',$result_code), SORT_ASC, $date_collected, SORT_DESC, $value_matrix);
383 $cellcount = count($datelist);
384 $itemcount = count($value_matrix);
386 // print matrix
387 echo "<table border='1' cellpadding='2'>";
388 echo "<tr>";
389 #echo "<th class='matrix'>Item</th>";
390 echo "<th class='matrix'>" . xlt('Name') . "</th>";
391 echo "<th class='matrix'>" . xlt('Range') . "</th>";
392 echo "<th class='matrix'>" . xlt('Unit') . "</th>";
393 echo "<th class='matrix_spacer'>|</td>";
394 foreach($datelist as $this_date){
395 echo "<th width='30' class='matrix_time'>" . text($this_date) . "</th>";
397 echo "</tr>";
399 $i=0;
400 $a=TRUE;
401 while($a==TRUE){
402 echo "<tr>";
403 #echo "<td class='matrix_item'>" . $value_matrix[$i]['result_code'] . "</td>";
404 echo "<td class='matrix_item'>" . text($value_matrix[$i]['result_text']) . "</td>";
405 echo "<td class='matrix_item'>" . text($value_matrix[$i]['range']) . "</td>";
406 echo "<td class='matrix_item'>" . text($value_matrix[$i]['units']) . "</td>";
407 echo "<td class='matrix_spacer'> | </td>";
409 $z=0;
410 while($z < $cellcount){
412 if($value_matrix[$i]['date_collected'] == $datelist[$z]){
413 if($value_matrix[$i]['result'] == NULL){
414 echo "<td class='matrix_result'> </td>";
415 } 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>";
420 } else {
421 echo "<td class='matrix_result_abnorm'>&nbsp;&nbsp;" ;
422 if($value_matrix[$i]['abnormal'] == 'high') {
423 echo "+ ";
424 } elseif ($value_matrix[$i]['abnormal'] == 'low') {
425 echo "- ";
427 echo text($value_matrix[$i]['result']) . "&nbsp;&nbsp;</td>";
432 $j = $i;
433 $i++;
435 if($value_matrix[$i]['result_code'] != $value_matrix[$j]['result_code']){
436 $z = $cellcount;
438 } else {
439 echo "<td class='matrix_result'>&nbsp;</td>";
441 $z++;
443 if( $i == $itemcount){$a=FALSE;}
446 echo "</table>";
448 }// end if mode = matrix
449 } else { // end of "are items selected"
450 echo "<p>" . xlt('No parameters selected') . ".</p>";
451 $nothing = TRUE;
455 if(!$printable){
456 if(!$nothing){
457 echo "<p>";
458 echo "<form method='post' action='" . $path_to_this_script . "' target='_new' onsubmit='return top.restoreSession()'>";
459 echo "<input type='hidden' name='mode' value='". attr($mode) . "'>";
460 foreach($_POST['value_code'] as $this_valuecode) {
461 echo "<input type='hidden' name='value_code[]' value='". attr($this_valuecode) . "'>";
463 echo "<input type='submit' name='print' value='" . xla('View Printable Version') . "' />";
464 echo "</form>";
465 echo "<br><a href='../summary/demographics.php' ";
466 echo " class='css_button' onclick='top.restoreSession()'>";
467 echo "<span>" . xlt('Back to Patient') . "</span></a>";
471 } else {
472 echo "<p>" . xlt('End of report') . ".</p>";
474 echo "</span>";
475 echo "<br><br>";
476 echo "</div>";
477 echo "</body></html>";