3 * Script to display results for a given procedure order.
5 * Copyright (C) 2013-2016 Rod Roark <rod@sunsetsystems.com>
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>.
19 * @author Rod Roark <rod@sunsetsystems.com>
22 require_once($GLOBALS["srcdir"] . "/acl.inc");
23 require_once($GLOBALS["srcdir"] . "/options.inc.php");
25 function getListItem($listid, $value)
28 "SELECT title FROM list_options " .
29 "WHERE list_id = ? AND option_id = ? AND activity = 1",
30 array($listid, $value)
32 $tmp = xl_list_label($lrow['title']);
34 $tmp = (($value === '') ?
'' : "($value)");
40 function myCellText($s)
50 // Check if the given string already exists in the $aNotes array.
51 // If not, stores it as a new entry.
52 // Either way, returns the corresponding key which is a small integer.
53 function storeNote($s)
56 $key = array_search($s, $aNotes);
61 $key = count($aNotes);
66 // Display a single row of output including order, report and result information.
68 function generate_result_row(&$ctx, &$row, &$rrow, $priors_omitted = false)
70 $lab_id = empty($row['lab_id' ]) ?
0 : ($row['lab_id' ] +
0);
71 $order_type_id = empty($row['order_type_id' ]) ?
0 : ($row['order_type_id' ] +
0);
72 $order_seq = empty($row['procedure_order_seq']) ?
0 : ($row['procedure_order_seq'] +
0);
73 $report_id = empty($row['procedure_report_id']) ?
0 : ($row['procedure_report_id'] +
0);
74 $procedure_code = empty($row['procedure_code' ]) ?
'' : $row['procedure_code'];
75 $procedure_name = empty($row['procedure_name' ]) ?
'' : $row['procedure_name'];
76 $date_report = empty($row['date_report' ]) ?
'' : substr($row['date_report'], 0, 16);
77 $date_report_suf = empty($row['date_report_tz' ]) ?
'' : (' ' . $row['date_report_tz' ]);
78 $date_collected = empty($row['date_collected' ]) ?
'' : substr($row['date_collected'], 0, 16);
79 $date_collected_suf = empty($row['date_collected_tz' ]) ?
'' : (' ' . $row['date_collected_tz' ]);
80 $specimen_num = empty($row['specimen_num' ]) ?
'' : $row['specimen_num'];
81 $report_status = empty($row['report_status' ]) ?
'' : $row['report_status'];
82 $review_status = empty($row['review_status' ]) ?
'received' : $row['review_status'];
85 if ($report_id && !isset($ctx['seen_report_ids'][$report_id])) {
86 $ctx['seen_report_ids'][$report_id] = true;
87 if ($review_status != 'reviewed') {
88 if ($ctx['sign_list']) {
89 $ctx['sign_list'] .= ',';
92 $ctx['sign_list'] .= $report_id;
95 // Allowing for multiple report notes separated by newlines.
96 if (!empty($row['report_notes'])) {
97 $notes = explode("\n", $row['report_notes']);
98 foreach ($notes as $note) {
103 if ($report_noteid) {
104 $report_noteid .= ', ';
107 $report_noteid .= 1 +
storeNote($note);
112 // allow for 0 to be displayed as a result value
113 if ($rrow['result'] == '' && $rrow['result'] !== 0 && $rrow['result'] !== '0') {
116 $result_result = $rrow['result'];
119 $result_code = empty($rrow['result_code' ]) ?
'' : $rrow['result_code'];
120 $result_text = empty($rrow['result_text' ]) ?
'' : $rrow['result_text'];
121 $result_abnormal = empty($rrow['abnormal' ]) ?
'' : $rrow['abnormal'];
122 $result_units = empty($rrow['units' ]) ?
'' : $rrow['units'];
123 $result_facility = empty($rrow['facility' ]) ?
'' : $rrow['facility'];
124 $result_comments = empty($rrow['comments' ]) ?
'' : $rrow['comments'];
125 $result_range = empty($rrow['range' ]) ?
'' : $rrow['range'];
126 $result_status = empty($rrow['result_status' ]) ?
'' : $rrow['result_status'];
127 $result_document_id = empty($rrow['document_id' ]) ?
'' : $rrow['document_id'];
129 // Someone changed the delimiter in result comments from \n to \r.
130 // Have to make sure results are consistent with those before that change.
131 $result_comments = str_replace("\r", "\n", $result_comments);
133 if ($i = strpos($result_comments, "\n")) { // "=" is not a mistake!
134 // If the first line of comments is not empty, then it is actually a long textual
135 // result value with lines delimited by "~" characters.
136 $result_comments = str_replace("~", "\n", substr($result_comments, 0, $i)) .
137 substr($result_comments, $i);
140 $result_comments = trim($result_comments);
143 if (!empty($result_comments)) {
144 $result_noteid = 1 +
storeNote($result_comments);
147 if ($priors_omitted) {
148 if ($result_noteid) {
149 $result_noteid .= ', ';
152 $result_noteid .= 1 +
storeNote(xl('This is the latest of multiple result values.'));
153 $ctx['priors_omitted'] = true;
156 // If a performing organization is provided, make a note for it also.
157 $result_facility = trim(str_replace("\r", "\n", $result_facility));
158 if ($result_facility) {
159 if ($result_noteid) {
160 $result_noteid .= ', ';
163 $result_noteid .= 1 +
storeNote(xl('Performing organization') . ":\n" . $result_facility);
166 if ($ctx['lastpcid'] != $order_seq) {
170 $bgcolor = "#" . (($ctx['encount'] & 1) ?
"ddddff" : "ffdddd");
172 echo " <tr class='detail' bgcolor='$bgcolor'>\n";
174 if ($ctx['lastpcid'] != $order_seq) {
175 $ctx['lastprid'] = -1; // force report fields on first line of each procedure
176 $tmp = text("$procedure_code: $procedure_name");
177 // Get the LOINC code if one exists in the compendium for this order type.
178 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
180 "SELECT standard_code FROM procedure_type WHERE " .
181 "lab_id = ? AND procedure_code = ? AND procedure_type = 'ord' " .
182 "ORDER BY procedure_type_id LIMIT 1",
183 array($lab_id, $procedure_code)
185 if (!empty($trow['standard_code'])) {
186 $tmp = "<a href='javascript:educlick(\"LOINC\",\"" . attr($trow['standard_code']) .
191 echo " <td>$tmp</td>\n";
193 echo " <td style='background-color:transparent'> </td>";
196 // If this starts a new report or a new order, generate the report fields.
197 if ($report_id != $ctx['lastprid']) {
199 echo myCellText(oeFormatShortDate(substr($date_report, 0, 10)) . substr($date_report, 10) . $date_report_suf);
203 echo myCellText(oeFormatShortDate(substr($date_collected, 0, 10)) . substr($date_collected, 10) . $date_collected_suf);
207 echo myCellText($specimen_num);
210 echo " <td title='" . xla('Check mark indicates reviewed') . "'>";
211 echo myCellText(getListItem('proc_rep_status', $report_status));
212 if ($row['review_status'] == 'reviewed') {
213 echo " ✓"; // unicode check mark character
218 echo " <td align='center'>";
219 echo myCellText($report_noteid);
222 echo " <td colspan='5' style='background-color:transparent'> </td>\n";
225 if ($result_code !== '' ||
$result_document_id) {
226 $tmp = myCellText($result_code);
227 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE']) && !empty($result_code)) {
228 $tmp = "<a href='javascript:educlick(\"LOINC\",\"" . attr($result_code) .
232 echo " <td>$tmp</td>\n";
234 echo myCellText($result_text);
237 $tmp = myCellText(getListItem('proc_res_abnormal', $result_abnormal));
238 if ($result_abnormal && strtolower($result_abnormal) != 'no') {
239 echo "<b><font color='red'>$tmp</font></b>";
246 if ($result_document_id) {
247 $d = new Document($result_document_id);
248 echo " <td colspan='3'>";
249 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
250 echo "<a href='" . $GLOBALS['webroot'] . "/controller.php?document";
251 echo "&retrieve&patient_id=$patient_id&document_id=$result_document_id' ";
252 echo "onclick='top.restoreSession()'>";
255 echo $d->get_url_file();
256 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
261 $narrative_notes = sqlQuery("select group_concat(note SEPARATOR '\n') as notes from notes where foreign_id = ?", array($result_document_id));
262 if (!empty($narrative_notes)) {
263 $nnotes = explode("\n", $narrative_notes['notes']);
264 $narrative_note_list = '';
265 foreach ($nnotes as $nnote) {
266 if ($narrative_note_list == '') {
267 $narrative_note_list = 'Narrative Notes:';
270 $narrative_note_list .= $nnote;
273 if ($narrative_note_list != '') {
274 if ($result_noteid) {
275 $result_noteid .= ', ';
278 $result_noteid .= 1 +
storeNote($narrative_note_list);
283 echo myCellText($result_result);
286 echo myCellText($result_range);
289 // Units comes from the lab so might not match anything in the proc_unit list,
290 // but in that case the call will return the same value.
291 echo myCellText(getListItemTitle('proc_unit', $result_units));
295 echo " <td align='center'>";
296 echo myCellText($result_noteid);
299 echo " <td colspan='7' style='background-color:transparent'> </td>\n";
304 $ctx['lastpcid'] = $order_seq;
305 $ctx['lastprid'] = $report_id;
309 function generate_order_report($orderid, $input_form = false, $genstyles = true, $finals_only = false)
313 // Check authorization.
314 $thisauth = acl_check('patients', 'med');
316 return xl('Not authorized');
321 "po.procedure_order_id, po.date_ordered, po.control_id, " .
322 "po.order_status, po.specimen_type, po.patient_id, " .
323 "pd.pubpid, pd.lname, pd.fname, pd.mname, pd.cmsportal_login, pd.language, " .
325 "pp.name AS labname, " .
326 "u.lname AS ulname, u.fname AS ufname, u.mname AS umname " .
327 "FROM procedure_order AS po " .
328 "LEFT JOIN patient_data AS pd ON pd.pid = po.patient_id " .
329 "LEFT JOIN procedure_providers AS pp ON pp.ppid = po.lab_id " .
330 "LEFT JOIN users AS u ON u.id = po.provider_id " .
331 "LEFT JOIN form_encounter AS fe ON fe.pid = po.patient_id AND fe.encounter = po.encounter_id " .
332 "WHERE po.procedure_order_id = ?",
336 $patient_id = $orow['patient_id'];
337 $language = $orow['language'];
340 <?php
if ($genstyles) { ?
>
343 <?php
if (empty($_SESSION['language_direction']) ||
$_SESSION['language_direction'] == 'ltr') { ?
>
345 .labres tr
.head
{ font
-size
:10pt
; background
-color
:#cccccc; text-align:center; }
346 .labres tr
.detail
{ font
-size
:10pt
; }
347 .labres a
, .labres a
:visited
, .labres a
:hover
{ color
:#0000cc; }
351 border
-width
: 1px
0px
0px
1px
;
354 .labres td
, .labres th
{
356 border
-width
: 0px
1px
1px
0px
;
359 /***** What is this for? Seems ugly to me. --Rod
361 background-color: #cccccc;
367 .labres tr
.head
{ font
-size
:10pt
; text
-align
:center
; }
368 .labres tr
.detail
{ font
-size
:10pt
; }
372 border
-width
: 1px
0px
0px
1px
;
375 .labres td
, .labres th
{
377 border
-width
: 0px
1px
1px
0px
;
381 .labres table td
.td
-label
{
392 <?php
if ($input_form) { ?
>
393 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot']; ?>/library/textformat.js"></script
>
394 <?php
} // end if input form ?>
396 <?php
if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) { ?
>
398 <script type
="text/javascript" src
="<?php echo $GLOBALS['webroot']; ?>/library/dialog.js?v=<?php echo $v_js_includes; ?>"></script
>
399 <script language
="JavaScript">
401 var mypcc
= '<?php echo $GLOBALS['phone_country_code
'] ?>';
403 // Called to show patient notes related to this order in the "other" frame.
404 // This works even if we are in a separate window.
405 function showpnotes(orderid
) {
406 // Find the top or bottom frame that contains or opened this page; return if none.
407 var w
= window
.opener ? window
.opener
: window
;
408 for (; w
.name
!= 'RTop' && w
.name
!= 'RBot'; w
= w
.parent
) {
410 // This message is not translated because a developer will need to find it.
411 alert('Internal error locating target frame in ' +
(window
.opener ?
'opener' : 'window'));
415 var othername
= (w
.name
== 'RTop') ?
'RBot' : 'RTop';
416 w
.parent
.left_nav
.forceDual();
417 w
.parent
.left_nav
.loadFrame('pno1', othername
, 'patient_file/summary/pnotes_full.php?orderid=' + orderid
);
421 // Process click on LOINC code for patient education popup.
422 function educlick(codetype
, codevalue
) {
423 dlgopen('<?php echo $GLOBALS['webroot
']; ?>/interface/patient_file/education.php' +
424 '?type=' +
encodeURIComponent(codetype
) +
425 '&code=' +
encodeURIComponent(codevalue
) +
426 '&language=<?php echo urlencode($language); ?>',
427 '_blank', 1024, 750,true); // Force a new window instead of iframe to address cross site scripting potential
432 <?php
} // end if not patient report ?>
434 <?php
if ($input_form) { ?
>
435 <form method
='post' action
='single_order_results.php?orderid=<?php echo $orderid; ?>'>
436 <?php
} // end if input form ?>
440 <table width
='100%' cellpadding
='2' cellspacing
='0'>
442 <td
class="td-label" width
='5%' nowrap
><?php
echo xlt('Patient ID'); ?
></td
>
443 <td width
='45%'><?php
echo myCellText($orow['pubpid']); ?
></td
>
444 <td
class="td-label" width
='5%' nowrap
><?php
echo xlt('Order ID'); ?
></td
>
447 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
448 echo " <a href='" . $GLOBALS['webroot'];
449 echo "/interface/orders/order_manifest.php?orderid=";
450 echo attr($orow['procedure_order_id']);
451 echo "' target='_blank' onclick='top.restoreSession()'>";
454 echo myCellText($orow['procedure_order_id']);
455 if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) {
459 if ($orow['control_id']) {
460 echo myCellText(' ' . xl('Lab') . ': ' . $orow['control_id']);
466 <td
class="td-label" nowrap
><?php
echo xlt('Patient Name'); ?
></td
>
467 <td
><?php
echo myCellText($orow['lname'] . ', ' . $orow['fname'] . ' ' . $orow['mname']); ?
></td
>
468 <td
class="td-label" nowrap
><?php
echo xlt('Ordered By'); ?
></td
>
469 <td
><?php
echo myCellText($orow['ulname'] . ', ' . $orow['ufname'] . ' ' . $orow['umname']); ?
></td
>
472 <td
class="td-label" nowrap
><?php
echo xlt('Order Date'); ?
></td
>
473 <td
><?php
echo myCellText(oeFormatShortDate($orow['date_ordered'])); ?
></td
>
474 <td
class="td-label" nowrap
><?php
echo xlt('Print Date'); ?
></td
>
475 <td
><?php
echo text(oeFormatShortDate(date('Y-m-d'))); ?
></td
>
478 <td
class="td-label" nowrap
><?php
echo xlt('Order Status'); ?
></td
>
479 <td
><?php
echo myCellText($orow['order_status']); ?
></td
>
480 <td
class="td-label" nowrap
><?php
echo xlt('Encounter Date'); ?
></td
>
481 <td
><?php
echo myCellText(oeFormatShortDate(substr($orow['date'], 0, 10))); ?
></td
>
484 <td
class="td-label" nowrap
><?php
echo xlt('Lab'); ?
></td
>
485 <td
><?php
echo myCellText($orow['labname']); ?
></td
>
486 <td
class="td-label" nowrap
><?php
echo $orow['specimen_type'] ?
xlt('Specimen Type') : ' '; ?
></td
>
487 <td
><?php
echo myCellText($orow['specimen_type']); ?
></td
>
493 <table width
='100%' cellpadding
='2' cellspacing
='0'>
496 <td
class="td-label" rowspan
='2' valign
='middle'><?php
echo xlt('Ordered Procedure'); ?
></td
>
497 <td
class="td-label" colspan
='5'><?php
echo xlt('Report'); ?
></td
>
498 <td
class="td-label" colspan
='7'><?php
echo xlt('Results'); ?
></td
>
502 <td
><?php
echo xlt('Reported'); ?
></td
>
503 <td
><?php
echo xlt('Collected'); ?
></td
>
504 <td
><?php
echo xlt('Specimen'); ?
></td
>
505 <td
><?php
echo xlt('Status'); ?
></td
>
506 <td
><?php
echo xlt('Note'); ?
></td
>
507 <td
><?php
echo xlt('Code'); ?
></td
>
508 <td
><?php
echo xlt('Name'); ?
></td
>
509 <td
><?php
echo xlt('Abn'); ?
></td
>
510 <td
><?php
echo xlt('Value'); ?
></td
>
511 <td
><?php
echo xlt('Range'); ?
></td
>
512 <td
><?php
echo xlt('Units'); ?
></td
>
513 <td
><?php
echo xlt('Note'); ?
></td
>
518 "po.lab_id, po.date_ordered, pc.procedure_order_seq, pc.procedure_code, " .
519 "pc.procedure_name, " .
520 "pr.date_report, pr.date_report_tz, pr.date_collected, pr.date_collected_tz, " .
521 "pr.procedure_report_id, pr.specimen_num, pr.report_status, pr.review_status, pr.report_notes " .
522 "FROM procedure_order AS po " .
523 "JOIN procedure_order_code AS pc ON pc.procedure_order_id = po.procedure_order_id " .
524 "LEFT JOIN procedure_report AS pr ON pr.procedure_order_id = po.procedure_order_id AND " .
525 "pr.procedure_order_seq = pc.procedure_order_seq " .
526 "WHERE po.procedure_order_id = ? " .
527 "ORDER BY pc.procedure_order_seq, pr.date_report, pr.procedure_report_id";
529 $res = sqlStatement($query, array($orderid));
532 $empty_results = array('result_code' => '');
534 // Context for this call that may be used in other functions.
541 'seen_report_ids' => array(),
544 while ($row = sqlFetchArray($res)) {
545 $report_id = empty($row['procedure_report_id']) ?
0 : ($row['procedure_report_id'] +
0);
548 "ps.result_code, ps.result_text, ps.abnormal, ps.result, ps.range, " .
549 "ps.result_status, ps.facility, ps.units, ps.comments, ps.document_id, ps.date " .
550 "FROM procedure_result AS ps " .
551 "WHERE ps.procedure_report_id = ? " .
552 "ORDER BY ps.procedure_result_id";
554 $rres = sqlStatement($query, array($report_id));
557 // We are consolidating reports.
558 if (sqlNumRows($rres)) {
560 // First pass creates a $rrowsets[$key] for each unique result code in *this* report, with
561 // the value being an array of the corresponding result rows. This caters to multiple
562 // occurrences of the same result code in the same report.
563 while ($rrow = sqlFetchArray($rres)) {
564 $result_code = empty($rrow['result_code']) ?
'' : $rrow['result_code'];
565 $key = sprintf('%05d/', $row['procedure_order_seq']) . $result_code;
566 if (!isset($rrowsets[$key])) {
567 $rrowsets[$key] = array();
570 $rrowsets[$key][] = $rrow;
573 // Second pass builds onto the array of final results for *all* reports, where each final
574 // result for a given result code is its *array* of result rows from *one* of the reports.
575 foreach ($rrowsets as $key => $rrowset) {
576 // When two reports have the same date, use the result date to decide which is "latest".
577 if (isset($finals[$key]) &&
578 $row['date_report'] == $finals[$key][0]['date_report'] &&
579 !empty($rrow['date']) && !empty($finals[$key][1]['date']) &&
580 $rrow['date'] < $finals[$key][1]['date']) {
581 $finals[$key][2] = true; // see comment below
585 // $finals[$key][2] indicates if there are multiple results for this result code.
586 $finals[$key] = array($row, $rrowset, isset($finals[$key]));
589 // We have no results for this report.
590 $key = sprintf('%05d/', $row['procedure_order_seq']);
591 $finals[$key] = array($row, array($empty_results), false);
594 // We are showing all results for all reports.
595 if (sqlNumRows($rres)) {
596 while ($rrow = sqlFetchArray($rres)) {
597 generate_result_row($ctx, $row, $rrow, false);
600 generate_result_row($ctx, $row, $empty_results, false);
606 // The sort here was removed because $finals is already ordered by procedure_result_id
607 // within procedure_order_seq which is probably desirable. Sorting by result code defeats
608 // the sequencing of results chosen by the sender.
610 foreach ($finals as $final) {
611 foreach ($final[1] as $rrow) {
612 generate_result_row($ctx, $final[0], $rrow, $final[2]);
622 <table width
='100%' style
='border-width:0px;'>
624 <td style
='border-width:0px;'>
626 if (!empty($aNotes)) {
627 echo "<table cellpadding='3' cellspacing='0'>\n";
628 echo " <tr bgcolor='#cccccc'>\n";
629 echo " <th align='center' colspan='2'>" . xlt('Notes') . "</th>\n";
631 foreach ($aNotes as $key => $value) {
633 echo " <td valign='top'>" . ($key +
1) . "</td>\n";
634 // <pre> tag because white space and a fixed font are often used to line things up.
635 echo " <td><pre style='white-space:pre-wrap;'>" . text($value) . "</pre></td>\n";
643 <td style
='border-width:0px;' align
='right' valign
='top'>
644 <?php
if ($input_form && !empty($ctx['priors_omitted']) /* empty($_POST['form_showall']) */) { ?
>
645 <input type
='submit' name
='form_showall' value
='<?php echo xla('Show All Results
'); ?>'
646 title
='<?php echo xla('Include all values reported
for each result code
'); ?>' />
647 <?php
} else if ($input_form && !empty($_POST['form_showall'])) { ?
>
648 <input type
='submit' name
='form_latest' value
='<?php echo xla('Latest Results Only
'); ?>'
649 title
='<?php echo xla('Show only latest values reported
for each result code
'); ?>' />
651 <?php
if (empty($GLOBALS['PATIENT_REPORT_ACTIVE'])) { ?
>
653 <input type
='button' value
='<?php echo xla('Related Patient Notes
'); ?>'
654 onclick
='showpnotes(<?php echo $orderid; ?>)' />
656 <?php
if ($input_form && $ctx['sign_list']) { ?
>
658 <input type
='hidden' name
='form_sign_list' value
='<?php echo attr($ctx['sign_list
']); ?>' />
659 <input type
='submit' name
='form_sign' value
='<?php echo xla('Sign Results
'); ?>'
660 title
='<?php echo xla('Mark these reports
as reviewed
'); ?>' />
662 // If this is a portal patient, sending them a copy is an option.
663 if ($GLOBALS['gbl_portal_cms_enable'] && $orow['cmsportal_login'] !== '') {
665 echo "<input type='checkbox' name='form_send_to_portal' value='" .
666 attr($orow['cmsportal_login']) . "' checked />\n";
667 echo xlt('Send to portal');
671 <?php
if ($input_form) { ?
>
673 <input type
='button' value
='<?php echo xla('Close
'); ?>' onclick
='window.close()' />
681 <?php
if ($input_form) { ?
>
683 <?php
} // end if input form ?>
686 } // end function generate_order_report