/** * Retrieves the results and creates a JSON string * * @param testId the id of the test to send * @param */ public function createJsonString($testId) { //if($comments==null or $comments==''){$comments = 'No Comments'; //We use curl to send the requests $httpCurl = curl_init(Config::get('kblis.sanitas-url')); curl_setopt($httpCurl, CURLOPT_RETURNTRANSFER, true); curl_setopt($httpCurl, CURLOPT_POST, true); //If testID is null we cannot handle this test as we cannot know the results if ($testId == null) { return null; } //Get the test and results $test = Test::find($testId); $testResults = $test->testResults; //Measures $testTypeId = $test->testType()->get()->lists('id')[0]; $testType = TestType::find($testTypeId); $testMeasures = $testType->measures; //Get external requests and all its children $externalDump = new ExternalDump(); $externRequest = ExternalDump::where('test_id', '=', $testId)->get(); if (!$externRequest->first()) { //Not a request we can send back return null; } $labNo = $externRequest->lists('lab_no')[0]; $externlabRequestTree = $externalDump->getLabRequestAndMeasures($labNo); $interpretation = ""; //IF the test has no children prepend the status to the result if ($externlabRequestTree->isEmpty()) { if ($test->test_status_id == Test::COMPLETED) { $interpretation = "Done: " . $test->interpretation; } elseif ($test->test_status_id == Test::VERIFIED) { $interpretation = "Tested and verified: " . $test->interpretation; } } else { if ($test->test_status_id == Test::COMPLETED) { $interpretation = "Done " . $test->interpretation; } elseif ($test->test_status_id == Test::VERIFIED) { $interpretation = "Tested and verified " . $test->interpretation; } } //TestedBy $tested_by = ExternalUser::where('internal_user_id', '=', $test->tested_by)->get()->first(); if ($tested_by == null) { $tested_by = "59"; } else { if ($tested_by->external_user_id == null) { $tested_by = "59"; } else { $tested_by = $tested_by->external_user_id; } } if ($test->verified_by == 0 || $test->verified_by == null) { $verified_by = "59"; } else { $verified_by = ExternalUser::where('internal_user_id', '=', $test->verified_by)->get()->first(); if ($verified_by == null) { $verified_by = "59"; } else { if ($verified_by->external_user_id == null) { $verified_by = "59"; } else { $verified_by = $verified_by->external_user_id; } } } //TODO - relate measure to test-result $range = Measure::getRange($test->visit->patient, $testResults->first()->measure_id); $unit = Measure::find($testResults->first()->measure_id)->unit; $result = $testResults->first()->result . " " . $range . " " . $unit; $jsonResponseString = sprintf('{"labNo": "%s","requestingClinician": "%s", "result": "%s", "verifiedby": "%s", "techniciancomment": "%s"}', $labNo, $tested_by, $result, $verified_by, trim($interpretation)); $this->sendRequest($httpCurl, urlencode($jsonResponseString), $labNo); //loop through labRequests and foreach of them get the result and put in an array foreach ($externlabRequestTree as $key => $externlabRequest) { $mKey = array_search($externlabRequest->investigation, $testMeasures->lists('name')); if ($mKey === false) { Log::error("MEASURE NOT FOUND: Measure {$externlabRequest->investigation} not found in our system"); } else { $measureId = $testMeasures->get($mKey)->id; $rKey = array_search($measureId, $testResults->lists('measure_id')); $matchingResult = $testResults->get($rKey); $range = Measure::getRange($test->visit->patient, $measureId); $unit = Measure::find($measureId)->unit; $result = $matchingResult->result . " " . $range . " " . $unit; $jsonResponseString = sprintf('{"labNo": "%s","requestingClinician": "%s", "result": "%s", "verifiedby": "%s", "techniciancomment": "%s"}', $externlabRequest->lab_no, $tested_by, $result, $verified_by, ""); $this->sendRequest($httpCurl, urlencode($jsonResponseString), $externlabRequest->lab_no); } } curl_close($httpCurl); }