Skip to content

Commit

Permalink
AllergyIntolerance Resource & GET methods FHIR (openemr#3334)
Browse files Browse the repository at this point in the history
* Implemented Post and Put for FHIR

* Update _rest_routes.inc.php

* Added routes for Patient Put & Post

* Search by address, phone, gender

* Psr2 fixes

* Psr2 fixes

* Psr2 again

* Organization resource with getAll function

* Psr2 fixes

* Updated naming conventions

* Psr2 fixes

* AllergyIntolerance Resource with GET methods

* Small fixes

* small fix

* some fixes

* Few Fixes

* Small fixes
  • Loading branch information
silvistershebin committed Apr 8, 2020
1 parent e48b36a commit 54fac8e
Show file tree
Hide file tree
Showing 3 changed files with 301 additions and 0 deletions.
9 changes: 9 additions & 0 deletions _rest_routes.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@

);

use OpenEMR\RestControllers\FHIR\FhirAllergyIntoleranceRestController;
use OpenEMR\RestControllers\FHIR\FhirPatientRestController;
use OpenEMR\RestControllers\FHIR\FhirEncounterRestController;
use OpenEMR\RestControllers\FHIR\FhirOrganizationRestController;
Expand Down Expand Up @@ -384,6 +385,14 @@
"GET /fhir/Organization/:oid" => function ($oid) {
return (new FhirOrganizationRestController(null))->getOne($oid);
},
"GET /fhir/AllergyIntolerance" => function () {
RestConfig::authorization_check("patients", "med");
return (new FhirAllergyIntoleranceRestController(null))->getAll($_GET);
},
"GET /fhir/AllergyIntolerance/:id" => function ($id) {
RestConfig::authorization_check("patients", "med");
return (new FhirAllergyIntoleranceRestController(null))->getOne($id);
},
"POST /fhir/QuestionnaireResponse" => function () {
RestConfig::authorization_check("patients", "demo");
$data = (array)(json_decode(file_get_contents("php:https://input"), true));
Expand Down
69 changes: 69 additions & 0 deletions src/RestControllers/FHIR/FhirAllergyIntoleranceRestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace OpenEMR\RestControllers\FHIR;

use OpenEMR\Services\FHIR\FhirAllergyIntoleranceService;
use OpenEMR\Services\FHIR\FhirResourcesService;
use OpenEMR\RestControllers\RestControllerHelper;
use OpenEMR\FHIR\R4\FHIRResource\FHIRBundle\FHIRBundleEntry;

class FhirAllergyIntoleranceRestController
{
private $fhirAllergyIntoleranceService;
private $fhirService;

public function __construct($id)
{
$this->fhirAllergyIntoleranceService = new FhirAllergyIntoleranceService();
$this->fhirAllergyIntoleranceService->setId($id);
$this->fhirService = new FhirResourcesService();
}

public function getAll($search)
{
$result = $this->fhirAllergyIntoleranceService->getAll(array('patient' => $search['patient']));
if ($result === false) {
http_response_code(404);
exit;
}
$entries = array();
$resourceURL = \RestConfig::$REST_FULL_URL;
foreach ($result as $allergy) {
$entryResource = $this->fhirAllergyIntoleranceService->createAllergyIntoleranceResource(
$allergy['id'],
$allergy,
false
);
$entry = array(
'fullUrl' => $resourceURL . "/" . $allergy['id'],
'resource' => $entryResource
);
$entries[] = new FHIRBundleEntry($entry);
}
$result = $this->fhirService->createBundle('AllergyIntolerance', $entries, false);
return RestControllerHelper::responseHandler($result, null, 200);
}

public function getOne($id)
{
$result = $this->fhirAllergyIntoleranceService->getOne($id);
if ($result) {
$resource = $this->fhirAllergyIntoleranceService->createAllergyIntoleranceResource(
$result['id'],
$result,
false
);
$statusCode = 200;
} else {
$statusCode = 404;
$resource = $this->fhirValidate->operationOutcomeResourceService(
'error',
'invalid',
false,
"Resource Id $id does not exist"
);
}

return RestControllerHelper::responseHandler($resource, null, $statusCode);
}
}
223 changes: 223 additions & 0 deletions src/Services/FHIR/FhirAllergyIntoleranceService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
<?php

namespace OpenEMR\Services\FHIR;

use OpenEMR\FHIR\R4\FHIRDomainResource\FHIRAllergyIntolerance;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceCategory;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceCriticality;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAllergyIntoleranceType;
use OpenEMR\FHIR\R4\FHIRElement\FHIRAnnotation;
use OpenEMR\FHIR\R4\FHIRElement\FHIRCodeableConcept;
use OpenEMR\FHIR\R4\FHIRElement\FHIRCoding;
use OpenEMR\FHIR\R4\FHIRElement\FHIRDateTime;
use OpenEMR\FHIR\R4\FHIRElement\FHIRId;
use OpenEMR\FHIR\R4\FHIRElement\FHIRMeta;
use OpenEMR\FHIR\R4\FHIRElement\FHIRReference;
use OpenEMR\FHIR\R4\FHIRElement\FHIRUrl;
use OpenEMR\FHIR\R4\FHIRResource\FHIRAllergyIntolerance\FHIRAllergyIntoleranceReaction;
use OpenEMR\FHIR\R4\FHIRResource\FHIRBundle;
use OpenEMR\FHIR\R4\FHIRResource\FHIRBundle\FHIRBundleLink;
use OpenEMR\FHIR\R4\PHPFHIRResponseParser;
use OpenEMR\Services\ListService;

class FhirAllergyIntoleranceService
{

private $id;

public function __construct()
{
}

public function setId($id)
{
$this->id = $id;
}

public function getAll($search)
{
$SQL = "SELECT id,
date as recorded_date,
type,
subtype,
title,
begdate,
enddate,
returndate,
referredby,
extrainfo,
diagnosis,
pid,
outcome,
reaction,
severity_al
FROM lists
WHERE type = 'allergy'";

if (isset($search['patient']))
$SQL .= " AND pid = ?;";

$allergyIntolerenceresults = sqlStatement($SQL, $search['patient']);
$results = array();
while ($row = sqlFetchArray($allergyIntolerenceresults)) {
$codeSQL = "SELECT dx_code
FROM icd10_dx_order_code
WHERE short_desc = ?;";
$code = sqlQuery($codeSQL, array($row['reaction']));
$row['code'] = $code['dx_code'];
array_push($results, $row);
}
return $results;
}

public function getOne($id)
{
$SQL = "SELECT id,
date as recorded_date,
type,
subtype,
title,
begdate,
enddate,
returndate,
referredby,
extrainfo,
diagnosis,
pid,
outcome,
reaction,
severity_al
FROM lists
WHERE type = 'allergy'
AND id = ?;";

$sqlResult = sqlStatement($SQL, $id);
$result = sqlFetchArray($sqlResult);
$codeSQL = "SELECT dx_code
FROM icd10_dx_order_code
WHERE short_desc = ?;";

$code = sqlQuery($codeSQL, array($result['reaction']));
$result['code'] = $code['dx_code'];
return $result;
}

public function createAllergyIntoleranceResource($id = '', $data = '', $encode = true)
{
$typeCoding = new FHIRCoding();
$typeCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/allergyintolerance-clinical");
$typeCoding->setCode($data['type']);
$typeCoding->setDisplay(ucwords($data['type']));
$type = new FHIRAllergyIntoleranceType();
$type->setValue($typeCoding);

$onSetDateTime = new FHIRDateTime();
$onSetDateTime->setValue($data['begdate']);

$recordedDate = new FHIRDateTime();
$recordedDate->setValue($data['recorded_date']);

$clinicalStatus = '';
if ($data['outcome'] == '1' && isset($data['enddate']))
$clinicalStatus = "resolved";
else if (!isset($data['enddate']))
$clinicalStatus = "active";
else
$clinicalStatus = "inactive";
$clinicalStatusCoding = new FHIRCoding();
$clinicalStatusCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/allergyintolerance-clinical");
$clinicalStatusCoding->setCode($clinicalStatus);
$clinicalStatusCoding->setDisplay($clinicalStatus);

$categoryCoding = new FHIRCoding();
$categoryCoding->setSystem("http:https://hl7.org/fhir/allergy-intolerance-category");
$categoryCoding->setCode("medication");
$categoryCoding->setDisplay("Medication");
$category = new FHIRAllergyIntoleranceCategory();
$category->setValue($categoryCoding);

$criticalityCoding = new FHIRCoding();
$criticality = new FHIRAllergyIntoleranceCriticality();
if (isset($data['severity_al'])) {
$severityToCriticalityCode = array(
"mild" => "low",
"moderate" => "high",
"severe" => "unable-to-assess"
);
$severityToCriticalityDisplay = array(
"mild" => "Low Risk",
"moderate" => "High Risk",
"severe" => "Unable to Assess Risk"
);
$criticalityCoding->setSystem("http:https://hl7.org/fhir/allergy-intolerance-criticality");
$criticalityCoding->setCode($severityToCriticalityCode[strtolower($data['severity_al'])]);
$criticalityCoding->setDisplay($severityToCriticalityDisplay[strtolower($data['severity_al'])]);
$criticality->setValue($criticalityCoding);
}
else {
$criticalityCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/data-absent-reason");
$criticalityCoding->setCode("unknown");
$criticalityCoding->setDisplay("Unknown");
$criticality->setValue($criticalityCoding);
}

$patient = new FHIRReference();
$patient->setReference('Patient/'.$data['pid']);

$recorder = new FHIRReference();
$recorder->setReference('Practitioner/'.$data['referredby']);

$asserter = new FHIRReference();
$asserter->setReference('Patient/'.$data['pid']);

$note = new FHIRAnnotation();
$note->setText($data['extrainfo']);

$lastOccurrence = new FHIRDateTime();
$lastOccurrence->setValue($data['returndate']);

$manifestation = new FHIRCoding();
$manifestation->setSystem("http:https://hl7.org/fhir/sid/icd-10-cm");
$manifestation->setCode($data['code']);
$manifestation->setDisplay($data['reaction']);

$description = $data['comments'];

$reaction = new FHIRAllergyIntoleranceReaction();
$reaction->addManifestation($manifestation->jsonSerialize());
$reaction->setDescription($description);
$reaction->setOnset($data['begdate']);
$severityCoding = new FHIRCoding();
if (isset($data['severity_al'])) {
$severityCoding->setSystem("http:https://hl7.org/fhir/reaction-event-severity");
$severityCoding->setCode($data['severity_al']);
$severityCoding->setDisplay(ucwords($data['severity_al']));
$reaction->setSeverity($severityCoding);
}
else {
$severityCoding->setSystem("http:https://terminology.hl7.org/CodeSystem/data-absent-reason");
$severityCoding->setCode("unknown");
$severityCoding->setDisplay("Unknown");
$reaction->setSeverity($severityCoding);
}

$resource = new FHIRAllergyIntolerance();
$resource->setClinicalStatus($clinicalStatusCoding);
$resource->setType($type);
$resource->addCategory($category);
$resource->setOnsetDateTime($onSetDateTime);
$resource->setRecordedDate($recordedDate);
$resource->setCriticality($criticality);
$resource->setPatient($patient);
$resource->setRecorder($recorder);
$resource->setAsserter($asserter);
$resource->setLastOccurrence($lastOccurrence);
$resource->addNote($note);
$resource->addReaction($reaction);
if ($encode) {
return json_encode($resource);
} else {
return $resource;
}
}
}

0 comments on commit 54fac8e

Please sign in to comment.