PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/library/classes/rulesets/Amc/library/AbstractAmcReport.php

https://bitbucket.org/astawiarski/openemr
PHP | 208 lines | 17 code | 5 blank | 186 comment | 0 complexity | 8d53728194e7a6bc6a2e113d23e11c16 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0, GPL-2.0, MPL-2.0
  1. <?php
  2. // Copyright (C) 2011 Ken Chapple <ken@mi-squared.com>
  3. //
  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.
  8. //
  9. require_once( 'AmcFilterIF.php' );
  10. require_once( dirname(__FILE__)."/../../../../clinical_rules.php" );
  11. require_once( dirname(__FILE__)."/../../../../amc.php" );
  12. abstract class AbstractAmcReport implements RsReportIF
  13. {
  14. protected $_amcPopulation;
  15. protected $_resultsArray = array();
  16. protected $_rowRule;
  17. protected $_ruleId;
  18. protected $_beginMeasurement;
  19. protected $_endMeasurement;
  20. protected $_manualLabNumber;
  21. public function __construct( array $rowRule, array $patientIdArray, $dateTarget, $options )
  22. {
  23. // require all .php files in the report's sub-folder
  24. $className = get_class( $this );
  25. foreach ( glob( dirname(__FILE__)."/../reports/".$className."/*.php" ) as $filename ) {
  26. require_once( $filename );
  27. }
  28. // require common .php files
  29. foreach ( glob( dirname(__FILE__)."/../reports/common/*.php" ) as $filename ) {
  30. require_once( $filename );
  31. }
  32. // require clinical types
  33. foreach ( glob( dirname(__FILE__)."/../../../ClinicalTypes/*.php" ) as $filename ) {
  34. require_once( $filename );
  35. }
  36. $this->_amcPopulation = new AmcPopulation( $patientIdArray );
  37. $this->_rowRule = $rowRule;
  38. $this->_ruleId = isset( $rowRule['id'] ) ? $rowRule['id'] : '';
  39. // Parse measurement period, which is stored as array in $dateTarget ('dateBegin' and 'dateTarget').
  40. $this->_beginMeasurement = $dateTarget['dateBegin'];
  41. $this->_endMeasurement = $dateTarget['dateTarget'];
  42. $this->_manualLabNumber = $options['labs_manual'];
  43. }
  44. public abstract function createNumerator();
  45. public abstract function createDenominator();
  46. public abstract function getObjectToCount();
  47. public function getResults() {
  48. return $this->_resultsArray;
  49. }
  50. public function execute()
  51. {
  52. $numerator = $this->createNumerator();
  53. if ( !$numerator instanceof AmcFilterIF ) {
  54. throw new Exception( "Numerator must be an instance of AmcFilterIF" );
  55. }
  56. $denominator = $this->createDenominator();
  57. if ( !$denominator instanceof AmcFilterIF ) {
  58. throw new Exception( "Denominator must be an instance of AmcFilterIF" );
  59. }
  60. $totalPatients = count( $this->_amcPopulation );
  61. // Figure out object to be counted
  62. // (patients, labs, transitions, visits, or prescriptions)
  63. $object_to_count = $this->getObjectToCount();
  64. if (empty($object_to_count)) {
  65. $object_to_count="patients";
  66. }
  67. $numeratorObjects = 0;
  68. $denominatorObjects = 0;
  69. foreach ( $this->_amcPopulation as $patient )
  70. {
  71. // If begin measurement is empty, then make the begin
  72. // measurement the patient dob.
  73. $tempBeginMeasurement = "";
  74. if (empty($this->_beginMeasurement)) {
  75. $tempBeginMeasurement = $patient->dob;
  76. }
  77. else {
  78. $tempBeginMeasurement = $this->_beginMeasurement;
  79. }
  80. // Count Denominators
  81. if ($object_to_count == "patients") {
  82. // Counting patients
  83. if ( !$denominator->test( $patient, $tempBeginMeasurement, $this->_endMeasurement ) ) {
  84. continue;
  85. }
  86. $denominatorObjects++;
  87. }
  88. else {
  89. // Counting objects other than patients
  90. // First, collect the pertinent objects
  91. $objects = $this->collectObjects($patient,$object_to_count,$tempBeginMeasurement,$this->_endMeasurement);
  92. // Second, test each object
  93. $objects_pass=array();
  94. foreach ($objects as $object) {
  95. $patient->object=$object;
  96. if ( $denominator->test( $patient, $tempBeginMeasurement, $this->_endMeasurement ) ) {
  97. $denominatorObjects++;
  98. array_push($objects_pass,$object);
  99. }
  100. }
  101. }
  102. // Count Numerators
  103. if ($object_to_count == "patients") {
  104. // Counting patients
  105. if ( !$numerator->test( $patient, $tempBeginMeasurement, $this->_endMeasurement ) ) {
  106. continue;
  107. }
  108. $numeratorObjects++;
  109. }
  110. else {
  111. // Counting objects other than patients
  112. // test each object that passed the above denominator testing
  113. foreach ($objects_pass as $object) {
  114. $patient->object=$object;
  115. if ( $numerator->test( $patient, $tempBeginMeasurement, $this->_endMeasurement ) ) {
  116. $numeratorObjects++;
  117. }
  118. }
  119. }
  120. }
  121. // Deal with the manually added labs for the electronic labs AMC measure
  122. if ($object_to_count == "labs") {
  123. $denominatorObjects = $denominatorObjects + $this->_manualLabNumber;
  124. }
  125. $percentage = calculate_percentage( $denominatorObjects, 0, $numeratorObjects );
  126. $result = new AmcResult( $this->_rowRule, $totalPatients, $denominatorObjects, 0, $numeratorObjects, $percentage );
  127. $this->_resultsArray[]= $result;
  128. }
  129. private function collectObjects ($patient,$object_label,$begin,$end) {
  130. $results = array();
  131. $sqlBindArray = array();
  132. switch ($object_label) {
  133. case "transitions-in":
  134. $sql = "SELECT amc_misc_data.map_id as `encounter`, amc_misc_data.date_completed as `completed`, form_encounter.date as `date` " .
  135. "FROM `amc_misc_data`, `form_encounter` " .
  136. "WHERE amc_misc_data.map_id = form_encounter.encounter " .
  137. "AND amc_misc_data.map_category = 'form_encounter' " .
  138. "AND amc_misc_data.pid = form_encounter.pid = ? " .
  139. "AND amc_misc_data.amc_id = 'med_reconc_amc' " .
  140. "AND form_encounter.date >= ? AND form_encounter.date <= ?";
  141. array_push($sqlBindArray, $patient->id, $begin, $end);
  142. break;
  143. case "transitions-out":
  144. $sql = "SELECT * " .
  145. "FROM `transactions` " .
  146. "WHERE `title` = 'Referral' " .
  147. "AND `pid` = ? " .
  148. "AND `date` >= ? AND `date` <= ?";
  149. array_push($sqlBindArray, $patient->id, $begin, $end);
  150. break;
  151. case "encounters":
  152. $sql = "SELECT * " .
  153. "FROM `form_encounter` " .
  154. "WHERE `pid` = ? " .
  155. "AND `date` >= ? AND `date` <= ?";
  156. array_push($sqlBindArray, $patient->id, $begin, $end);
  157. break;
  158. case "prescriptions":
  159. $sql = "SELECT * " .
  160. "FROM `prescriptions` " .
  161. "WHERE `patient_id` = ? " .
  162. "AND `date_added` >= ? AND `date_added` <= ?";
  163. array_push($sqlBindArray, $patient->id, $begin, $end);
  164. break;
  165. case "labs":
  166. $sql = "SELECT procedure_result.result " .
  167. "FROM `procedure_type`, " .
  168. "`procedure_order`, " .
  169. "`procedure_report`, " .
  170. "`procedure_result` " .
  171. "WHERE procedure_type.procedure_type_id = procedure_order.procedure_type_id " .
  172. "AND procedure_order.procedure_order_id = procedure_report.procedure_order_id " .
  173. "AND procedure_report.procedure_report_id = procedure_result.procedure_report_id " .
  174. "AND procedure_order.patient_id = ? " .
  175. "AND procedure_report.date_collected >= ? AND procedure_report.date_collected <= ?";
  176. array_push($sqlBindArray, $patient->id, $begin, $end);
  177. break;
  178. }
  179. $rez = sqlStatement($sql, $sqlBindArray);
  180. for($iter=0; $row=sqlFetchArray($rez); $iter++)
  181. $results[$iter]=$row;
  182. return $results;
  183. }
  184. }