From 006d39236e99bea59e2ff76ca00b545e33d6e094 Mon Sep 17 00:00:00 2001 From: Jerry Padgett Date: Sun, 18 Feb 2018 04:15:42 -0500 Subject: [PATCH] update functionality. (#1427) * update functionality. * translate --- phpfhir/classes/src/fetch_live.class.php | 52 ++++++++++++++-- phpfhir/classes/src/oeFHIRHttpClient.php | 12 +++- phpfhir/classes/src/oeFHIRResource.php | 80 +++++++++++++++++++++++-- phpfhir/libs/controller/ClientAppController.php | 41 ++++++++++--- phpfhir/providerPublishUI.php | 33 ++++++++-- 5 files changed, 197 insertions(+), 21 deletions(-) diff --git a/phpfhir/classes/src/fetch_live.class.php b/phpfhir/classes/src/fetch_live.class.php index 2c5ee7630..6188e7a8d 100644 --- a/phpfhir/classes/src/fetch_live.class.php +++ b/phpfhir/classes/src/fetch_live.class.php @@ -188,6 +188,21 @@ class FetchLiveData return $records; } + public function getUser($id) + { + + $query = "SELECT * + FROM users + WHERE id = ?"; + $result = $this->oeQuery($query, array($id)); + $records = array(); + foreach ($result as $row) { + $records[] = $row; + } + + return $records[0]; + } + /* * Fetch the current Vitals of a patient from form_vitals table * @@ -232,25 +247,54 @@ class FetchLiveData } /* + * Fetch the list of encounter Id's of a patient from form_encounter table + * + * @param pid Integer patient id + * @return records Array encounter data + */ + public function getEncounterIdList($pid) + { + + $query = "SELECT form_encounter.encounter,form_encounter.reason + FROM form_encounter + WHERE pid = ?"; + + $result = $this->oeQuery($query, array($pid)); + $records = array(); + foreach ($result as $row) { + $records[] = $row; + } + + return $records; + } + + /* * Fetch the encounter data of a patient from form_encounter table * * @param pid Integer patient id * @return records Array encounter data */ - public function getEncounterData($pid) + public function getEncounterData($pid, $eid = '') { - $query = "SELECT form_encounter.*,u.fname AS provider_name + $query = "SELECT form_encounter.*,u.lname AS provider_name FROM form_encounter LEFT JOIN users AS u ON form_encounter.provider_id=u.id WHERE pid = ?"; - $result = $this->oeQuery($query, array($pid)); + $data = array($pid); + if ($eid) { + $query .= " && encounter=?"; + $data = array($pid, $eid); + } + $result = $this->oeQuery($query, $data); $records = array(); foreach ($result as $row) { $records[] = $row; } - + if ($eid) { + $records = $records[0]; + } return $records; } diff --git a/phpfhir/classes/src/oeFHIRHttpClient.php b/phpfhir/classes/src/oeFHIRHttpClient.php index 679c5e305..652709c25 100644 --- a/phpfhir/classes/src/oeFHIRHttpClient.php +++ b/phpfhir/classes/src/oeFHIRHttpClient.php @@ -42,7 +42,7 @@ class oeFHIRHttpClient { $uri = $type . '/' . $id; $returned = $this->client->request('PUT', $uri, ['body' => $data]); - $head = 'Transaction Status: ' . $returned->getStatusCode() . ' ' . $returned->getReasonPhrase() . '
'; + $head = 'Transaction Status: ' . $returned->getStatusCode() . ' ' . $returned->getReasonPhrase() . '
'; foreach ($returned->getHeaders() as $name => $values) { $head .= $name . ': ' . implode(', ', $values) . "
"; } @@ -59,4 +59,14 @@ class oeFHIRHttpClient return $body; } + + // @todo for now search for type by patient + public function searchResource($type = 'Patient', $id = '', $search = '') + { + $uri = $type . '?patient=' . $id . '&_format=json&_pretty=true'; + $returned = $this->client->request('GET', $uri); + $body = $returned->getBody()->getContents(); + + return $body; + } } diff --git a/phpfhir/classes/src/oeFHIRResource.php b/phpfhir/classes/src/oeFHIRResource.php index eaa77c838..2e926d72b 100644 --- a/phpfhir/classes/src/oeFHIRResource.php +++ b/phpfhir/classes/src/oeFHIRResource.php @@ -11,12 +11,23 @@ namespace oeFHIR; +use HL7\FHIR\STU3\FHIRDomainResource\FHIREncounter; use HL7\FHIR\STU3\FHIRDomainResource\FHIRPatient; +use HL7\FHIR\STU3\FHIRDomainResource\FHIRPractitioner; use HL7\FHIR\STU3\FHIRElement\FHIRAddress; +use HL7\FHIR\STU3\FHIRElement\FHIRAdministrativeGender; +use HL7\FHIR\STU3\FHIRElement\FHIRCodeableConcept; use HL7\FHIR\STU3\FHIRElement\FHIRHumanName; use HL7\FHIR\STU3\FHIRElement\FHIRId; +use HL7\FHIR\STU3\FHIRElement\FHIRReference; +use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterParticipant; use HL7\FHIR\STU3\PHPFHIRResponseParser; +//use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterLocation; +//use HL7\FHIR\STU3\FHIRResource\FHIREncounter\FHIREncounterDiagnosis; +//use HL7\FHIR\STU3\FHIRElement\FHIRPeriod; +//use HL7\FHIR\STU3\FHIRElement\FHIRParticipantRequired; + class oeFHIRResource { public function createBundle($pid = '', $encode = true, $resource_array = '') @@ -24,7 +35,7 @@ class oeFHIRResource $fs = new FetchLiveData(); } - public function createPatientResource($pid = '', $encode = true) + public function createPatientResource($pid = '', $rid = '', $encode = true) { $fs = new FetchLiveData(); $patientResource = new FHIRPatient(); @@ -32,10 +43,9 @@ class oeFHIRResource $name = new FHIRHumanName(); $address = new FHIRAddress(); $oept = $fs->getDemographicsCurrent($pid); - // maybe public id is better here? // fhir spec allows either create or update on update put endpoint, but only if id contains alphanumerics. // uri id and resource patient id must match. - $id->setValue('oe-' . $oept['pid']); + $id->setValue($rid); $name->setUse('official'); $name->setFamily($oept['lname']); $name->given = [$oept['fname'], $oept['mname']]; @@ -45,16 +55,76 @@ class oeFHIRResource $address->setPostalCode($oept['postal_code']); $patientResource->setId($id); $patientResource->setActive(true); - $patientResource->setGender(strtolower($oept['sex'])); + $gender = new FHIRAdministrativeGender(); + $gender->setValue(strtolower($oept['sex'])); + $patientResource->setGender($gender); $patientResource->addName($name); $patientResource->addAddress($address); return json_encode($patientResource); } - public function createPractionerResource($pid = '', $encode = true) + public function createPractitionerResource($id = '', $rid = '', $encode = true) + { + $fs = new FetchLiveData(); + $oept = $fs->getUser($id); + + $resource = new FHIRPractitioner(); + $id = new FhirId(); + $name = new FHIRHumanName(); + $address = new FHIRAddress(); + $id->setValue($rid); + $name->setUse('official'); + $name->setFamily($oept['lname']); + $name->given = [$oept['fname'], $oept['mname']]; + $address->addLine($oept['street']); + $address->setCity($oept['city']); + $address->setState($oept['state']); + $address->setPostalCode($oept['zip']); + $resource->setId($id); + $resource->setActive(true); + $gender = new FHIRAdministrativeGender(); + $gender->setValue('unknown'); + $resource->setGender($gender); + $resource->addName($name); + $resource->addAddress($address); + + return json_encode($resource); + } + + public function createEncounterResource($pid = '', $rid = '', $eid = '', $encode = true) { + // If you set an endpoint to an element and resource doesn't exist on server, then create will fail. + // Practitioner here is an example so I auto create or update the resource so encounter will go... + // Same with conditions once it it built. Encounters can be tied to episodes and care plans e.t.c. $fs = new FetchLiveData(); + $oept = $fs->getEncounterData($pid, $eid); + $temp = 'provider-' . $oept['provider_id']; + $rs = new oeFHIRResource(); // update + $r = $rs->createPractitionerResource($oept['provider_id'], $temp); + $client = new oeFHIRHttpClient(); + $pt = $client->sendResource('Practitioner', $temp, $r); // update or add practitioner + $resource = new FHIREncounter(); + $id = new FhirId(); + $id->setValue($rid); + $resource->setId($id); + $participant = new FHIREncounterParticipant(); + $prtref = new FHIRReference; + $temp = 'Practitioner/provider-' . $oept['provider_id']; + $prtref->setReference($temp); + $participant->setIndividual($prtref); + $date = date('Y-m-d', strtotime($oept['date'])); + $participant->setPeriod(['start' => $date]); + + $resource->addParticipant($participant); + $reason = new FHIRCodeableConcept(); + $reason->setText($oept['reason']); + $resource->addReason($reason); + $resource->status = 'finished'; + + $resource->setSubject(['reference' => 'Patient/patient-' . $pid]); + + return json_encode($resource); } public function parseResource($rjson = '', $scheme = 'json') diff --git a/phpfhir/libs/controller/ClientAppController.php b/phpfhir/libs/controller/ClientAppController.php index 0144d8161..4d78aa76d 100644 --- a/phpfhir/libs/controller/ClientAppController.php +++ b/phpfhir/libs/controller/ClientAppController.php @@ -11,6 +11,7 @@ require_once("oeDispatcher.php"); +use oeFHIR\FetchLiveData; use oeFHIR\oeFHIRHttpClient; use oeFHIR\oeFHIRResource; @@ -33,25 +34,47 @@ class clientController extends oeDispatchController return null; } + public function createEncounterAllAction() + { + $pid = $this->getRequest(pid); + $oeid = $this->getRequest(oeid); + $type = $this->getRequest(type); + $fs = new FetchLiveData(); + $oept = $fs->getEncounterIdList($pid); + $notify = ''; + foreach ($oept as $e) { + $client = new oeFHIRHttpClient(); + $id = 'encounter-' . $e['encounter']; + $rs = new oeFHIRResource(); + $r = $rs->createEncounterResource($pid, $id, $e['encounter']); + $pt = $client->sendResource($type, $id, $r); + $notify .= 'Sent:
' . $r . '
' . $pt; + } + return $notify; + } + public function createAction() { $pid = $this->getRequest(pid); + $oeid = $this->getRequest(oeid); $type = $this->getRequest(type); $client = new oeFHIRHttpClient(); - $id = 'oe-' . $pid; + $id = strtolower($type) . '-' . $oeid; + $method = 'create' . $type . 'Resource'; $rs = new oeFHIRResource(); - $r = $rs->createPatientResource($pid); + $r = $rs->$method($pid, $id); $pt = $client->sendResource($type, $id, $r); - return $pt; + return 'Sent:
' . $r . '
' . $pt; } public function historyAction() { $pid = $this->getRequest(pid); + $oeid = $this->getRequest(oeid); $type = $this->getRequest(type); $client = new oeFHIRHttpClient(); - $id = 'oe-' . $pid; + $id = strtolower($type) . '-' . $oeid; $pt = $client->requestResource($type, $id, 'history'); return $pt; } @@ -59,9 +82,10 @@ class clientController extends oeDispatchController public function readAction() { $pid = $this->getRequest(pid); + $oeid = $this->getRequest(oeid); $type = $this->getRequest(type); $client = new oeFHIRHttpClient(); - $id = 'oe-' . $pid; + $id = strtolower($type) . '-' . $oeid; $pt = $client->requestResource($type, $id, ''); // gets latest version. return $pt; } @@ -70,9 +94,12 @@ class clientController extends oeDispatchController { $pid = $this->getRequest(pid); $type = $this->getRequest(type); + if ($type === 'Patient') { + return xlt('Search Not Available'); + } $client = new oeFHIRHttpClient(); - $id = 'oe-' . $pid; - $pt = $client->requestResource($type, $id, 'search'); // gets latest version. + $id = 'patient-' . $pid; + $pt = $client->searchResource($type, $id, 'search'); return $pt; } } diff --git a/phpfhir/providerPublishUI.php b/phpfhir/providerPublishUI.php index 2ab4d8a21..efd2ce669 100644 --- a/phpfhir/providerPublishUI.php +++ b/phpfhir/providerPublishUI.php @@ -34,13 +34,23 @@ echo ""; top.restoreSession(); e.preventDefault(); e.stopPropagation(); - let profile = 'Patient'; // @TODO get profile from view + let wait = ''; + let profile = getSelResource(); + if (profile === 'Encounter' && req === 'create') { + req = req + 'EncounterAll'; + } let actionUrl = '?action=' + req; - return $.post(actionUrl, {'type': profile, 'pid': pid}).done(function (data) { - $("#dashboard").empty().html(data); + let id = pid; // eventually will be other live id's + $("#dashboard").empty().html(wait); + return $.post(actionUrl, {'type': profile, 'pid': pid, oeid: id}).done(function (data) { + $("#dashboard").empty().html('
' + data + '
'); }); }; + function getSelResource() { + return $('#resource option:selected').val() + } + @@ -89,16 +99,31 @@ echo "";
+
+
+
+
+ + +
+
+
+
+ +
-- 2.11.4.GIT