Changed wording of some Print buttons and links to survive MU2 testing.
[openemr.git] / interface / patient_file / summary / labdata.php
blob88225d85fe6c59e4b5f6666578f998c40187b7ad
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>";
82 ?>
83 <link rel="stylesheet" href="<?php echo $css_header;?>" type="text/css">
84 <link rel="stylesheet" href="<?php echo $web_root; ?>/interface/themes/labdata.css" type="text/css">
85 <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery.1.3.2.js"></script>
86 <script type="text/javascript" src="<?php echo $web_root; ?>/library/openflashchart/js/json/json2.js"></script>
87 <script type="text/javascript" src="<?php echo $web_root; ?>/library/openflashchart/js/swfobject.js"></script>
88 <script type="text/javascript" language="JavaScript">
89 function checkAll(bx) {
90 for (var tbls=document.getElementsByTagName("table"), i=tbls.length; i--; )
91 for (var bxs=tbls[i].getElementsByTagName("input"), j=bxs.length; j--; )
92 if (bxs[j].type=="checkbox")
93 bxs[j].checked = bx.checked;
96 // this is automatically called by swfobject.embedSWF()
97 //------------------------------------------------------
98 function open_flash_chart_data(){
99 return JSON.stringify(data);
101 //------------------------------------------------------
104 // set up flashvars for ofc
105 var flashvars = {};
106 var data;
108 </script>
109 <?php ##############################################################################
110 echo "</head><body class='body_top'>";
111 echo "<div id='labdata'>";
112 echo "<h2>" . xlt('Labs') . "</h2>";
113 echo "<span class='text'>";
114 // some patient data...
115 $spell = "SELECT * ";
116 $spell .= "FROM patient_data ";
117 $spell .= "WHERE id = ?";
118 //---
119 $myrow = sqlQuery($spell,array($pid));
120 $lastname = $myrow["lname"];
121 $firstname = $myrow["fname"];
122 $DOB = $myrow["DOB"];
126 if($printable) {
127 echo "<table border=0>";
128 echo "<tr><td>" . xlt('Patient') . ": </td><td><b>" . text($lastname) . ", " . text($firstname) . "</b></td></tr>";
129 echo "<tr><td>" . xlt('Patient ID') . ": </td><td>" . text($pid) . "</td></tr>";
130 echo "<tr><td>" . xlt('Date of birth') . ": </td><td>" . text($DOB) . "</td></tr>";
131 echo "<tr><td>" . xlt('Access date') . ": </td><td>" . text(date('Y-m-d - H:i:s')) . "</td></tr>";
132 echo "</table>";
136 echo "<div id='reports_list'>";
137 if(!$printable){
138 echo "<form method='post' action='" . $path_to_this_script . "' onsubmit='return top.restoreSession()'>";
139 // What items are there for patient $pid?
140 // -----------------------------------------------
141 $value_list = array();
142 $value_select = $_POST['value_code']; // what items are checkedboxed?
143 $tab = 0;
144 echo xlt('Select items') . ": ";
145 echo "<table border='1'>";
146 echo "<tr><td>";
148 $spell = "SELECT DISTINCT procedure_result.result_code AS value_code ";
149 $spell .= "FROM procedure_result ";
150 $spell .= "JOIN procedure_report ";
151 $spell .= " ON procedure_result.procedure_report_id = procedure_report.procedure_report_id ";
152 $spell .= "JOIN procedure_order ";
153 $spell .= " ON procedure_report.procedure_order_id = procedure_order.procedure_order_id ";
154 $spell .= "WHERE procedure_order.patient_id = ? ";
155 $spell .= "AND procedure_result.result IS NOT NULL ";
156 $spell .= "AND procedure_result.result != ''";
157 $spell .= "ORDER BY procedure_result.result_code ASC ";
158 $query = sqlStatement($spell,array($pid));
161 // Select which items to view...
162 $i = 0;
163 while($myrow = sqlFetchArray($query)){
165 echo "<input type='checkbox' name='value_code[]' value=" . attr($myrow['value_code']) . " ";
166 if($value_select){
167 if (in_array($myrow['value_code'], $value_select)){ echo "checked='checked' ";}
169 echo " /> " . text($myrow['value_code']) . "<br />";
170 $value_list[$i][value_code] = $myrow['value_code'];
171 $i++;
172 $tab++;
173 if($tab == 10) {
174 echo "</td><td>";
175 $tab=0;
178 echo "</tr>";
179 echo "</table>";
180 echo "</div>";
182 ?><input type='checkbox' onclick="checkAll(this)" /> <?php echo xlt('Toggle All') . "<br/>";
183 echo "<table><tr>";
184 // Choose output mode [list vs. matrix]
185 echo "<td>" . xlt('Select output') . ":</td>";
186 echo "<td><input type='radio' name='mode' ";
187 $mode = $_POST['mode'];
188 if($mode == 'list'){ echo "checked='checked' ";}
189 echo " value='list'> " . xlt('List') . "<br>";
191 echo "<input type='radio' name='mode' ";
192 if($mode != 'list'){ echo "checked='checked' ";}
193 echo " value='matrix'> " . xlt('Matrix') . "<br>";
195 echo "<td></td></td>";
196 echo "</tr><tr>";
197 echo "<td>";
199 echo "<a href='../summary/demographics.php' ";
200 if (!$GLOBALS['concurrent_layout']){ echo "target='Main'"; }
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_log'>" . text($myrow['date_collected']) . "</td>";
274 echo "<td class='list_log'>" . text($myrow['review_status']) . "</td>";
275 echo "<td class='list_log'>";
276 if(!$printable){
277 echo "<a href='../../patient_file/encounter/encounter_top.php?set_encounter=". attr($myrow['encounter_id']) . "' target='RBot'>";
278 echo text($myrow['encounter_id']);
279 echo "</a>";
280 } else {
281 echo text($myrow['encounter_id']);
283 echo "</td>";
284 echo "</tr>";
285 $value_count++;
288 if($value_count > 1 && !$printable) {
289 echo "<tr><td colspan='7' align='center'>";
290 echo "<input type='button' class='graph_button' onclick='get_my_graph" . attr($item_graph) . "()' name='' value='" . xla('Plot item') . " \"" . attr($the_item) . "\"'>";
291 echo "</td></tr>";
294 <script type="text/javascript">
295 // prepare to plot the stuff
296 top.restoreSession();
297 function get_my_graph<?php echo attr($item_graph) ?>(){
298 var thedates = JSON.stringify(<?php echo json_encode($date_array); ?>);
299 var thevalues = JSON.stringify(<?php echo json_encode($value_array); ?>);
300 var theitem = JSON.stringify(<?php echo json_encode(array($the_item)); ?>);
301 var thetitle = JSON.stringify(<?php echo json_encode($the_item); ?>);
302 var checkboxfake = JSON.stringify(<?php echo json_encode(array(0)); ?>);
304 $.ajax({ url: '<?php echo $web_root; ?>/library/openflashchart/graph_track_anything.php',
305 type: 'POST',
306 data: { dates: thedates,
307 values: thevalues,
308 track: thetitle,
309 items: theitem,
310 thecheckboxes: checkboxfake
312 dataType: "json",
313 success: function(returnData){
314 // ofc will look after a variable named "ofc"
315 // inside of the flashvar
316 // However, we need to set both
317 // data and flashvars.ofc
318 data=returnData;
319 flashvars.ofc = returnData;
320 // call ofc with proper falshchart
321 swfobject.embedSWF('<?php echo $web_root; ?>/library/openflashchart/open-flash-chart.swf',
322 "graph_item_<?php echo $item_graph ?>", "650", "200", "9.0.0","",flashvars);
324 error: function (XMLHttpRequest, textStatus, errorThrown) {
325 alert(XMLHttpRequest.responseText);
327 }); // end ajax query
329 //------------------------------------------------------------------------
330 </script>
331 <?php
332 echo "<tr><td colspan='9' class='list_spacer'><hr></td></tr>";
333 $item_graph++;
335 echo "</table><br>";
337 }// end if mode = list
339 //##########################################################################################################################
340 if($mode=='matrix'){
342 $value_matrix = array();
343 $datelist = array();
344 $i = 0;
345 // get all data of patient's items
346 foreach($value_select as $this_value){
348 $spell = $main_spell;
349 $query = sqlStatement($spell,array($this_value,$pid));
351 while($myrow = sqlFetchArray($query)){
352 $value_matrix[$i][procedure_result_id] = $myrow['procedure_result_id'];
353 $value_matrix[$i][result_code] = $myrow['result_code'];
354 $value_matrix[$i][result_text] = $myrow['result_text'];
355 $value_matrix[$i][result] = $myrow['result'];
356 $value_matrix[$i][units] = generate_display_field(array('data_type'=>'1','list_id'=>'proc_unit'),$myrow['units']) ;
357 $value_matrix[$i][range] = $myrow['range'];
358 $value_matrix[$i][abnormal] = $myrow['abnormal'];
359 $value_matrix[$i][review_status] = $myrow['review_status'];
360 $value_matrix[$i][encounter_id] = $myrow['encounter_id'];
361 $value_matrix[$i][date_collected] = $myrow['date_collected'];
362 $datelist[] = $myrow['date_collected'];
363 $i++;
367 // get unique datetime
368 $datelist = array_unique($datelist);
370 // sort datetime DESC
371 rsort($datelist);
373 // sort item-data
374 foreach($value_matrix as $key => $row) {
375 $result_code[$key] = $row['result_code'];
376 $date_collected[$key] = $row['date_collected'];
378 array_multisort(array_map('strtolower',$result_code), SORT_ASC, $date_collected, SORT_DESC, $value_matrix);
380 $cellcount = count($datelist);
381 $itemcount = count($value_matrix);
383 // print matrix
384 echo "<table border='1' cellpadding='2'>";
385 echo "<tr>";
386 #echo "<th class='matrix'>Item</th>";
387 echo "<th class='matrix'>" . xlt('Name') . "</th>";
388 echo "<th class='matrix'>" . xlt('Range') . "</th>";
389 echo "<th class='matrix'>" . xlt('Unit') . "</th>";
390 echo "<th class='matrix_spacer'>|</td>";
391 foreach($datelist as $this_date){
392 echo "<th width='30' class='matrix_time'>" . text($this_date) . "</th>";
394 echo "</tr>";
396 $i=0;
397 $a=TRUE;
398 while($a==TRUE){
399 echo "<tr>";
400 #echo "<td class='matrix_item'>" . $value_matrix[$i]['result_code'] . "</td>";
401 echo "<td class='matrix_item'>" . text($value_matrix[$i]['result_text']) . "</td>";
402 echo "<td class='matrix_item'>" . text($value_matrix[$i]['range']) . "</td>";
403 echo "<td class='matrix_item'>" . text($value_matrix[$i]['units']) . "</td>";
404 echo "<td class='matrix_spacer'> | </td>";
406 $z=0;
407 while($z < $cellcount){
409 if($value_matrix[$i]['date_collected'] == $datelist[$z]){
410 if($value_matrix[$i]['result'] == NULL){
411 echo "<td class='matrix_result'> </td>";
412 } else {
414 if($value_matrix[$i]['abnormal'] == 'No' || $value_matrix[$i]['abnormal'] == 'no' || $value_matrix[$i]['abnormal'] == '' || $value_matrix[$i]['abnormal'] == NULL ) {
415 echo "<td class='matrix_result'>&nbsp;&nbsp;&nbsp;" . text($value_matrix[$i]['result']) . "&nbsp;&nbsp;</td>";
417 } else {
418 echo "<td class='matrix_result_abnorm'>&nbsp;&nbsp;" ;
419 if($value_matrix[$i]['abnormal'] == 'high') {
420 echo "+ ";
421 } elseif ($value_matrix[$i]['abnormal'] == 'low') {
422 echo "- ";
424 echo text($value_matrix[$i]['result']) . "&nbsp;&nbsp;</td>";
429 $j = $i;
430 $i++;
432 if($value_matrix[$i]['result_code'] != $value_matrix[$j]['result_code']){
433 $z = $cellcount;
435 } else {
436 echo "<td class='matrix_result'>&nbsp;</td>";
438 $z++;
440 if( $i == $itemcount){$a=FALSE;}
443 echo "</table>";
445 }// end if mode = matrix
446 } else { // end of "are items selected"
447 echo "<p>" . xlt('No parameters selected') . ".</p>";
448 $nothing = TRUE;
452 if(!$printable){
453 if(!$nothing){
454 echo "<p>";
455 echo "<form method='post' action='" . $path_to_this_script . "' target='_new' onsubmit='return top.restoreSession()'>";
456 echo "<input type='hidden' name='mode' value='". attr($mode) . "'>";
457 foreach($_POST['value_code'] as $this_valuecode) {
458 echo "<input type='hidden' name='value_code[]' value='". attr($this_valuecode) . "'>";
460 echo "<input type='submit' name='print' value='" . xla('View Printable Version') . "' />";
461 echo "</form>";
462 echo "<br><a href='../summary/demographics.php' ";
463 if (!$GLOBALS['concurrent_layout']){ echo "target='Main'"; }
464 echo " class='css_button' onclick='top.restoreSession()'>";
465 echo "<span>" . xlt('Back to Patient') . "</span></a>";
469 } else {
470 echo "<p>" . xlt('End of report') . ".</p>";
472 echo "</span>";
473 echo "<br><br>";
474 echo "</div>";
475 echo "</body></html>";