Highway to PSR2
[openemr.git] / interface / modules / zend_modules / module / Ccr / src / Ccr / Controller / CcrController.php
blob24a9e4598aeafeed45cfbf6376d2e842bf5b9f4e
1 <?php
2 /* +-----------------------------------------------------------------------------+
3 * OpenEMR - Open Source Electronic Medical Record
4 * Copyright (C) 2014 Z&H Consultancy Services Private Limited <sam@zhservices.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as
8 * published by the Free Software Foundation, either version 3 of the
9 * 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 Affero General Public License for more details.
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author Vinish K <vinish@zhservices.com>
20 * @author Riju KP <rijukp@zhservices.com>
21 * +------------------------------------------------------------------------------+
23 namespace Ccr\Controller;
25 use Zend\Mvc\Controller\AbstractActionController;
26 use Zend\View\Model\ViewModel;
27 use Zend\View\Model\JsonModel;
28 use Application\Listener\Listener;
30 class CcrController extends AbstractActionController
32 protected $ccrTable;
33 protected $listenerObject;
35 public function __construct()
37 $this->listenerObject = new Listener;
41 * Upload CCR XML file
43 public function indexAction()
45 $action = $this->getRequest()->getPost('action');
46 $am_id = $this->getRequest()->getPost('am_id');
47 if ($action == 'add_new_patient') {
48 $this->getCcrTable()->insert_patient($am_id);
51 $category_details = $this->getCcrTable()->fetch_cat_id('CCR');
53 $time_start = date('Y-m-d H:i:s');
54 $docid = \Documents\Controller\DocumentsController::uploadAction();
55 $uploaded_documents = array();
56 $uploaded_documents = $this->getCcrTable()->fetch_uploaded_documents(array('user' => $_SESSION['authId'], 'time_start' => $time_start, 'time_end' => date('Y-m-d H:i:s')));
58 if ($uploaded_documents[0]['id'] > 0) {
59 $_REQUEST["document_id"] = $uploaded_documents[0]['id'];
60 $_REQUEST["batch_import"] = 'YES';
61 $this->importAction();
62 } else {
63 $result = \Documents\Plugin\Documents::fetchXmlDocuments();
64 foreach ($result as $row) {
65 if ($row['doc_type'] == 'CCR') {
66 $_REQUEST["document_id"] = $row['doc_id'];
67 $this->importAction();
68 \Documents\Model\DocumentsTable::updateDocumentCategoryUsingCatname($row['doc_type'], $row['doc_id']);
73 $records = $this->getCcrTable()->document_fetch(array('cat_title' => 'CCR'));
74 $view = new ViewModel(array(
75 'records' => $records,
76 'category_id' => $category_details[0]['id'],
77 'file_location' => basename($_FILES['file']['name']),
78 'patient_id' => '00',
79 'listenerObject' => $this->listenerObject,
80 'commonplugin' => $this->CommonPlugin(),
81 ));
82 return $view;
86 * Import CCR data and update to audit tables
88 * @param document_id documents table ID to fetch the CCR XML file to import the data
90 public function importAction()
92 $request = $this->getRequest();
93 if ($request->getQuery('document_id')) {
94 $_REQUEST["document_id"] = $request->getQuery('document_id');
95 $category_details = $this->getCcrTable()->fetch_cat_id('CCR');
96 \Documents\Controller\DocumentsController::getDocumentsTable()->updateDocumentCategory($category_details[0]['id'], $_REQUEST["document_id"]);
99 $doc_id = $_REQUEST["document_id"];
100 $content = $this->getCcrTable()->getDocument($doc_id);
101 if ($request->getQuery('document_id')) {
102 $replace = array('<ccr:ContinuityOfCareRecord xsi:schemaLocation="urn:astm-org:CCR CCRV1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ccr="urn:astm-org:CCR">','ccr:');
103 $to_replace = array('<ContinuityOfCareRecord xmlns="urn:astm-org:CCR">','');
104 $content = str_replace($replace, $to_replace, $content);
105 $content = preg_replace('/BirthName/', 'CurrentName', $content, 2);
108 //fields to which the corresponding elements are to be inserted
109 //format - level 1 key is the main tag in the XML eg:- //Problems or //Problems/Problem according to the content in the XML.
110 //level 2 key is 'table name:field name' and level 2 value is the sub tag under the main tag given in level 1 key
111 //eg:- 'Type/Text' if the XML format is '//Problems/Problem/Type/Text' or 'id/@extension' if it is an attribute
112 //level 2 key can be 'table name:#some value' for checking whether a particular tag exits in the XML section
113 $field_mapping = array(
114 '//Problems/Problem' => array(
115 'lists1:diagnosis' => 'Description/Code/Value',
116 'lists1:comments' => 'CommentID',
117 'lists1:activity' => 'Status/Text',
118 'lists1:title' => 'Description/Text',
119 'lists1:date' => 'DateTime/ExactDateTime',
121 '//Alerts/Alert' => array(
122 'lists2:type' => 'Type/Text',
123 'lists2:diagnosis' => 'Description/Code/Value',
124 'lists2:date' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/DateTime/ExactDateTime',
125 'lists2:title' => 'Agent/EnvironmentalAgents/EnvironmentalAgent/Description/Text',
126 'lists2:reaction' => 'Reaction/Description/Text',
128 '//Medications/Medication' => array(
129 'prescriptions:date_added' => 'DateTime/ExactDateTime',
130 'prescriptions:active' => 'Status/Text',
131 'prescriptions:drug' => 'Product/ProductName/Text',
132 'prescriptions:size' => 'Product/Strength/Value',
133 'prescriptions:unit' => 'Product/Strength/Units/Unit',
134 'prescriptions:form' => 'Product/Form/Text',
135 'prescriptions:quantity' => 'Quantity/Value',
136 'prescriptions:note' => 'PatientInstructions/Instruction/Text',
137 'prescriptions:refills' => 'Refills/Refill/Number',
139 '//Immunizations/Immunization' => array(
140 'immunizations:administered_date' => 'DateTime/ExactDateTime',
141 'immunizations:note' => 'Directions/Direction/Description/Text',
143 '//Results/Result' => array(
144 'procedure_result:date' => 'DateTime/ExactDateTime',
145 'procedure_type:name' => 'Test/Description/Text',
146 'procedure_result:result' => 'Test/TestResult/Value',
147 'procedure_result:range' => 'Test/NormalResult/Normal/Value',
148 'procedure_result:abnormal' => 'Test/Flag/Text',
150 '//Actors/Actor' => array(
151 'patient_data:fname' => 'Person/Name/CurrentName/Given',
152 'patient_data:lname' => 'Person/Name/CurrentName/Family',
153 'patient_data:DOB' => 'Person/DateOfBirth/ExactDateTime',
154 'patient_data:sex' => 'Person/Gender/Text',
155 'patient_data:abname' => 'InformationSystem/Name',
156 'patient_data:#Type' => 'InformationSystem/Type',
157 'patient_data:pubpid' => 'IDs/ID',
158 'patient_data:street' => 'Address/Line1',
159 'patient_data:city' => 'Address/City',
160 'patient_data:state' => 'Address/State',
161 'patient_data:postal_code' => 'Address/PostalCode',
162 'patient_data:phone_contact' => 'Telephone/Value',
165 if (!empty($content)) {
166 $var = array();
167 $res = $this->getCcrTable()->parseXmlStream($content, $field_mapping);
168 $var = array(
169 'approval_status' => 1,
170 'type' => 11,
171 'ip_address' => $_SERVER['REMOTE_ADDR'],
173 foreach ($res as $sections => $details) {
174 foreach ($details as $cnt => $vals) {
175 foreach ($vals as $key => $val) {
176 if (array_key_exists('#Type', $res[$sections][$cnt])) {
177 if ($key == 'postal_code') {
178 $var['field_name_value_array']['misc_address_book'][$cnt]['zip'] = $val;
179 } elseif ($key == 'phone_contact') {
180 $var['field_name_value_array']['misc_address_book'][$cnt]['phone'] = $val;
181 } elseif ($key == 'abname') {
182 $values = explode(' ', $val);
183 if ($values[0]) {
184 $var['field_name_value_array']['misc_address_book'][$cnt]['lname'] = $values[0];
187 if ($values[1]) {
188 $var['field_name_value_array']['misc_address_book'][$cnt]['fname'] = $values[1];
190 } else {
191 $var['field_name_value_array']['misc_address_book'][$cnt][$key] = $val;
194 $var['entry_identification_array']['misc_address_book'][$cnt] = $cnt;
195 } else {
196 if ($sections == 'lists1' && $key == 'activity') {
197 if ($val == 'Active') {
198 $val = 1;
199 } else {
200 $val = 0;
204 if ($sections == 'lists2' && $key == 'type') {
205 if (strpos($val, "-")) {
206 $vals = explode("-", $val);
207 $val = $vals[0];
208 } else {
209 $val = "";
213 if ($sections == 'prescriptions' && $key == 'active') {
214 if ($val == 'Active') {
215 $val = 1;
216 } else {
217 $val = 0;
221 $var['field_name_value_array'][$sections][$cnt][$key] = $val;
222 $var['entry_identification_array'][$sections][$cnt] = $cnt;
226 if (array_key_exists('#Type', $var['field_name_value_array']['misc_address_book'][$cnt])) {
227 unset($var['field_name_value_array']['misc_address_book'][$cnt]['#Type']);
232 $var['field_name_value_array']['documents'][0]['id'] = $doc_id;
233 $audit_master_id = $this->getCcrTable()->insert_ccr_into_audit_data($var);
234 $this->getCcrTable()->update_imported($doc_id);
235 $this->getCcrTable()->update_document($doc_id, $audit_master_id);
237 if ($_REQUEST["batch_import"] == 'YES') {
238 return;
239 } else {
240 //echo('Imported');
241 //exit;
243 } else {
244 //exit('Could not read the file');
249 * Review the data imported from the CCR file
250 * Approve/Discard the data imported
252 * @param amid Audit mater table ID
253 * @param pid Patient ID to which the data has to be merged
254 * @param document_id documents table ID
256 public function revandapproveAction()
258 $request = $this->getRequest();
259 $audit_master_id = $request->getQuery('amid') ? $request->getQuery('amid') : $request->getPost('amid', null);
260 $pid = $request->getQuery('pid') ? $request->getQuery('pid') : $request->getPost('pid', null);
261 $document_id = $request->getQuery('document_id') ? $request->getQuery('document_id') : $request->getPost('document_id', null);
263 if ($request->getPost('setval') == 'approve') {
264 $this->getCcrTable()->insertApprovedData($_REQUEST);
265 return $this->redirect()->toRoute('ccr', array('action'=>'index'));
266 } else if ($request->getPost('setval') == 'discard') {
267 $this->getCcrTable()->discardCCRData(array('audit_master_id' => $audit_master_id));
268 return $this->redirect()->toRoute('ccr', array('action'=>'index'));
271 $demographics = $this->getCcrTable()->getDemographics(array('audit_master_id' => $audit_master_id));
272 $demographics_old = $this->getCcrTable()->getDemographicsOld(array('pid' => $pid));
274 $problems = $this->getCcrTable()->getProblems(array('pid' => $pid));
275 $problems_audit = $this->getCcrTable()->createAuditArray($audit_master_id, 'lists1');
277 $allergies = $this->getCcrTable()->getAllergies(array('pid' => $pid));
278 $allergies_audit = $this->getCcrTable()->createAuditArray($audit_master_id, 'lists2');
280 $medications = $this->getCcrTable()->getMedications(array('pid' => $pid));
281 $medications_audit = $this->getCcrTable()->createAuditArray($audit_master_id, 'prescriptions');
283 $immunizations = $this->getCcrTable()->getImmunizations(array('pid' => $pid));
284 $immunizations_audit = $this->getCcrTable()->createAuditArray($audit_master_id, 'immunizations');
286 $lab_results = $this->getCcrTable()->getLabResults(array('pid' => $pid));
287 $lab_results_audit = $this->getCcrTable()->createAuditArray($audit_master_id, 'procedure_result,procedure_type');
289 $view = new ViewModel(array(
290 'demographics' => $demographics,
291 'demographics_old' => $demographics_old,
292 'problems' => $problems,
293 'problems_audit' => $problems_audit,
294 'allergies' => $allergies,
295 'allergies_audit' => $allergies_audit,
296 'medications' => $medications,
297 'medications_audit' => $medications_audit,
298 'immunizations' => $immunizations,
299 'immunizations_audit' => $immunizations_audit,
300 'lab_results' => $lab_results,
301 'lab_results_audit' => $lab_results_audit,
302 'amid' => $audit_master_id,
303 'pid' => $pid,
304 'document_id' => $document_id,
305 'listenerObject' => $this->listenerObject,
306 'commonplugin' => $this->CommonPlugin(),
309 return $view;
313 * Table Gateway
315 * @return type
317 public function getCcrTable()
319 if (!$this->ccrTable) {
320 $sm = $this->getServiceLocator();
321 $this->ccrTable = $sm->get('Ccr\Model\CcrTable');
324 return $this->ccrTable;