forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FhirAllergyIntoleranceService.php
256 lines (229 loc) · 9.97 KB
/
FhirAllergyIntoleranceService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace OpenEMR\Services\FHIR;
use OpenEMR\Services\FHIR\FhirServiceBase;
use OpenEMR\Services\AllergyIntoleranceService;
use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRAllergyIntolerance;
use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRProvenance;
use OpenEMR\FHIR\R4\FHIRResource\FHIRProvenance\FHIRProvenanceAgent;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAddress;
use OpenEMR\FHIR\R4\FHIRElement\FHIRHumanName;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAdministrativeGender;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceCategory;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceCriticality;
use OpenEMR\FHIR\R4\FHIRElement\FHIRCodeableConcept;
use OpenEMR\FHIR\R4\FHIRElement\FHIRCoding;
use OpenEMR\FHIR\R4\FHIRElement\FHIRId;
use OpenEMR\FHIR\R4\FHIRElement\FHIRReference;
use OpenEMR\Validators\ProcessingResult;
use OpenEMR\Common\Uuid;
use OpenEMR\Common\Uuid\UuidRegistry;
/**
* FHIR AllergyIntolerance Service
*
* @coversDefaultClass OpenEMR\Services\FHIR\FhirAllergyIntoleranceService
* @package OpenEMR
* @author Yash Bothra <yashrajbothra786gmail.com>
* @copyright Copyright (c) 2020 Yash Bothra <yashrajbothra786gmail.com>
* @link http:https://www.open-emr.org
* @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
*
*/
class FhirAllergyIntoleranceService extends FhirServiceBase
{
/**
* @var AllergyIntoleranceService
*/
private $allergyIntoleranceService;
public function __construct()
{
parent::__construct();
$this->allergyIntoleranceService = new AllergyIntoleranceService();
}
/**
* Returns an array mapping FHIR AllergyIntolerance Resource search parameters to OpenEMR AllergyIntolerance search parameters
* @return array The search parameters
*/
protected function loadSearchParameters()
{
return [
'patient' => ['lists.pid'],
'_id' => ['lists.id']
];
}
/**
* Parses an OpenEMR allergyIntolerance record, returning the equivalent FHIR AllergyIntolerance Resource
*
* @param array $dataRecord The source OpenEMR data record
* @param boolean $encode Indicates if the returned resource is encoded into a string. Defaults to false.
* @return FHIRAllergyIntolerance
*/
public function createProvenanceResource($dataRecord = array(), $encode = false)
{
$allergyProvenance = new FHIRProvenance();
$meta = array('versionId' => '1', 'lastUpdated' => gmdate('c'));
$allergyProvenance->setMeta($meta);
$id = new FHIRId();
$uuidString = UuidRegistry::uuidToString((new UuidRegistry(['disable_tracker' => true]))->createUuid());
$id->setValue($uuidString);
$allergyProvenance->setId($id);
if (isset($dataRecord['uuid'])) {
$allergyReference = new FHIRReference();
$allergyReference->setReference('AllergyIntolerance/' . $dataRecord['uuid']);
$allergyProvenance->addTarget($allergyReference);
}
if (isset($dataRecord['date'])) {
$allergyProvenance->setRecorded($dataRecord['date']);
}
if ((isset($dataRecord['practitioner'])) && isset($dataRecord['organization'])) {
$agent = new FHIRProvenanceAgent();
$agentType = new FHIRCodeableConcept();
$agentTypeCoding = array(
'system' => "http:https://terminology.hl7.org/CodeSystem/provenance-participant-type",
'code' => 'author',
'display' => 'Author',
);
$agentType->addCoding($agentTypeCoding);
$agent->setType($agentType);
$allergyProvenance->addAgent($agent);
$agentWho = new FHIRReference();
$agentWho->setReference('Practitioner/' . $dataRecord['practitioner']);
$agent->setWho($agentWho);
$agentBehalf = new FHIRReference();
$agentBehalf->setReference('Organization/' . $dataRecord['organization']);
$agent->setOnBehalfOf($agentBehalf);
}
if ($encode) {
return json_encode($allergyProvenance);
} else {
return $allergyProvenance;
}
}
/**
* Parses an OpenEMR allergyIntolerance record, returning the equivalent FHIR AllergyIntolerance Resource
*
* @param array $dataRecord The source OpenEMR data record
* @param boolean $encode Indicates if the returned resource is encoded into a string. Defaults to false.
* @return FHIRAllergyIntolerance
*/
public function parseOpenEMRRecord($dataRecord = array(), $encode = false)
{
$allergyIntoleranceResource = new FHIRAllergyIntolerance();
$meta = array('versionId' => '1', 'lastUpdated' => gmdate('c'));
$allergyIntoleranceResource->setMeta($meta);
$id = new FHIRId();
$id->setValue($dataRecord['uuid']);
$allergyIntoleranceResource->setId($id);
$clinicalStatus = "inactive";
if ($dataRecord['outcome'] == '1' && isset($dataRecord['enddate'])) {
$clinicalStatus = "resolved";
} elseif (!isset($dataRecord['enddate'])) {
$clinicalStatus = "active";
}
$clinical_Status = new FHIRCodeableConcept();
$clinical_Status->addCoding(
array(
'system' => "http:https://terminology.hl7.org/CodeSystem/allergyintolerance-clinical",
'code' => $clinicalStatus,
'display' => ucwords($clinicalStatus),
)
);
$allergyIntoleranceResource->setClinicalStatus($clinical_Status);
$allergyIntoleranceCategory = new FHIRAllergyIntoleranceCategory();
$allergyIntoleranceCategory->setValue("medication");
$allergyIntoleranceResource->addCategory($allergyIntoleranceCategory);
if (isset($dataRecord['severity_al'])) {
$criticalityCode = array(
"mild" => ["code" => "low", "display" => "Low Risk"],
"mild_to_moderate" => ["code" => "low", "display" => "Low Risk"],
"moderate" => ["code" => "low", "display" => "Low Risk"],
"moderate_to_severe" => ["code" => "high", "display" => "High Risk"],
"severe" => ["code" => "high", "display" => "High Risk"],
"life_threatening_severity" => ["code" => "high", "display" => "High Risk"],
"fatal" => ["code" => "high", "display" => "High Risk"],
"unassigned" => ["code" => "unable-to-assess", "display" => "Unable to Assess Risk"],
);
$criticality = new FHIRAllergyIntoleranceCriticality();
$criticality->setValue($criticalityCode[$dataRecord['severity_al']]['code']);
$allergyIntoleranceResource->setCriticality($criticality);
}
if (isset($dataRecord['puuid'])) {
$patient = new FHIRReference();
$patient->setReference('Patient/' . $dataRecord['puuid']);
$allergyIntoleranceResource->setPatient($patient);
}
if (isset($dataRecord['practitioner'])) {
$recorder = new FHIRReference();
$recorder->setReference('Practitioner/' . $dataRecord['practitioner']);
$allergyIntoleranceResource->setRecorder($recorder);
}
if (!empty($dataRecord['diagnosis'])) {
$diagnosisCoding = new FHIRCoding();
$diagnosisCode = new FHIRCodeableConcept();
foreach ($dataRecord['diagnosis'] as $code => $display) {
$diagnosisCoding->setCode($code);
$diagnosisCoding->setDisplay($display);
$diagnosisCode->addCoding($diagnosisCoding);
}
$allergyIntoleranceResource->setCode($diagnosisCode);
}
$verificationStatus = new FHIRCodeableConcept();
$verificationCoding = array(
'system' => "http:https://terminology.hl7.org/CodeSystem/allergyintolerance-verification",
'code' => 'unconfirmed',
'display' => 'Unconfirmed',
);
if (!empty($dataRecord['verification'])) {
$verificationCoding = array(
'system' => "http:https://terminology.hl7.org/CodeSystem/allergyintolerance-verification",
'code' => $dataRecord['verification'],
'display' => $dataRecord['verification_title']
);
}
$verificationStatus->addCoding($verificationCoding);
$allergyIntoleranceResource->setVerificationStatus($verificationStatus);
if ($encode) {
return json_encode($allergyIntoleranceResource);
} else {
return $allergyIntoleranceResource;
}
}
/**
* Performs a FHIR AllergyIntolerance Resource lookup by FHIR Resource ID
* @param $fhirResourceId //The OpenEMR record's FHIR AllergyIntolerance Resource ID.
*/
public function getOne($fhirResourceId)
{
$processingResult = $this->allergyIntoleranceService->getOne($fhirResourceId);
if (!$processingResult->hasErrors()) {
if (count($processingResult->getData()) > 0) {
$openEmrRecord = $processingResult->getData()[0];
$fhirRecord = $this->parseOpenEMRRecord($openEmrRecord);
$processingResult->setData([]);
$processingResult->addData($fhirRecord);
}
}
return $processingResult;
}
/**
* Searches for OpenEMR records using OpenEMR search parameters
*
* @param array openEMRSearchParameters OpenEMR search fields
* @return ProcessingResult
*/
public function searchForOpenEMRRecords($openEMRSearchParameters)
{
return $this->allergyIntoleranceService->getAll($openEMRSearchParameters, false);
}
public function parseFhirResource($fhirResource = array())
{
// TODO: If Required in Future
}
public function insertOpenEMRRecord($openEmrRecord)
{
// TODO: If Required in Future
}
public function updateOpenEMRRecord($fhirResourceId, $updatedOpenEMRRecord)
{
// TODO: If Required in Future
}
}