2 // Copyright (C) 2011 Ken Chapple <ken@mi-squared.com>
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 require_once( 'ClinicalType.php' );
11 class LabResult
extends ClinicalType
13 const OPTION_RANGE
= 'range';
15 const HB1AC_TEST
= 'lab_hb1ac_test';
16 const LDL_TEST
= 'lab_ldl_test';
18 public function getListId()
20 return 'Clinical_Rules_Lab_Res_Types';
23 public function doPatientCheck( RsPatient
$patient, $beginDate = null, $endDate = null, $options = null )
25 $data = Codes
::lookup( $this->getOptionId() );
27 $range = new Range( Range
::NEG_INF
, Range
::POS_INF
);
28 if ( isset( $options[self
::OPTION_RANGE
] ) &&
29 is_a( $options[self
::OPTION_RANGE
], 'Range' ) ) {
30 $range = $options[self
::OPTION_RANGE
];
33 foreach( $data as $codeType => $codes ) {
34 foreach ( $codes as $code ) {
35 // search through vitals to find the most recent lab result in the date range
36 // if the result value is within range using Range->test(val), return true
37 $sql = "SELECT procedure_result.result, procedure_result.date " .
38 "FROM `procedure_type`, " .
39 "`procedure_order`, " .
40 "`procedure_report`, " .
41 "`procedure_result` " .
42 "WHERE procedure_type.procedure_type_id = procedure_order.procedure_type_id " .
43 "AND procedure_order.procedure_order_id = procedure_report.procedure_order_id " .
44 "AND procedure_report.procedure_report_id = procedure_result.procedure_report_id " .
45 "AND procedure_type.standard_code = ? " .
46 "AND procedure_result.date >= ? " .
47 "AND procedure_result.date < ? " .
48 "AND procedure_order.patient_id = ? ";
49 if ( $range->lowerBound
!= Range
::NEG_INF
) {
50 $sql .= "AND procedure_result.result >= ? ";
52 if ( $range->upperBound
!= Range
::POS_INF
) {
53 $sql .= "AND procedure_result.result < ? ";
56 // TODO should this be ': or '::'
57 $bindings = array( $codeType.':'.$code, $beginDate, $endDate, $patient->id
);
58 if ( $range->lowerBound
!= Range
::NEG_INF
) {
59 $bindings []= $range->lowerBound
;
61 if ( $range->upperBound
!= Range
::POS_INF
) {
62 $bindings []= $range->upperBound
;
64 $result = sqlStatement( $sql, $bindings );
66 $number = sqlNumRows($result);