public function templatedTextAction() { $personId = $this->_getParam('personId', 0); $templateId = $this->_getParam('templateId', 0); $templateName = $this->_getParam('templateName'); $templatedText = new TemplatedText(); if ($templateId > 0) { $templatedText->templateId = $templateId; $templatedText->populate(); } else { $templatedText->populateByName($templateName); } $template = $templatedText->template; preg_match_all('/{nsdr:(.*)}/', $template, $matches); if (count($matches[1]) > 0) { foreach ($matches[1] as $val) { $namespace = str_replace('[selectedPatientId]', $personId, $val); if (Zend_Registry::get('memcacheStatus') === 0) { $resultValue = __("Memcache server not started"); } else { $resultValue = NSDR2::populate($namespace); } $template = preg_replace('/{nsdr:(.*)}/', $resultValue, $template, 1); } } $this->view->template = $template; $this->render(); }
/** * Returns an array */ protected function populateGET() { return NSDR2::populate($this->_nsdrNamespace); $result = ''; $key = $this->_nsdrNamespace; // extract method name from namespace preg_match('/\\[(.*)\\(\\)\\]$/', $this->_nsdrNamespace, $matches); // return if no method defined if (!isset($matches[1])) { return $result; } $nsdrMethodName = $matches[1]; if ($method = $this->_memcache->get($key)) { // get returns FALSE if error or key not found $nsdrBase = new NSDRBase(); // create anonymous function and use the code from memcache $anonFunc = create_function('$tthis', $method); if ($anonFunc === false) { return $result; } // assign the unique function name returned by create_function to NSDRBase property $nsdrBase->{$nsdrMethodName} = $anonFunc; // execute the function and pass the base as $tthis instead of $this $result = $nsdrBase->{$nsdrMethodName}($nsdrBase); } return $result; }
public function imagingAction() { $personId = (int) $this->_getParam('personId'); $orderId = (int) $this->_getParam('orderId'); $copy = (int) $this->_getParam('copy'); $orderImaging = new OrderImaging(); $orderImaging->orderId = $orderId; if (!$orderId > 0 || !$orderImaging->populate()) { $orderImaging->order->patientId = $personId; } if ($copy) { $orderImaging->orderId = 0; } $form = new WebVista_Form(array('name' => 'imagingId')); $form->setAction(Zend_Registry::get('baseUrl') . 'orders.raw/process-imaging'); $form->loadORM($orderImaging, 'imaging'); $form->setWindow('windowImagingId'); $this->view->form = $form; $namespace = $personId . '::com.clearhealth.person.examsOver7days'; $this->view->examsOver7days = NSDR2::populate($namespace); $imagingList = array(); $categories = array(); $urgencies = array(); $transports = array(); $pregnants = array(); $enumeration = new Enumeration(); $enumeration->populateByUniqueName(OrderImaging::IMAGING_ENUM_NAME); $enumerationClosure = new EnumerationClosure(); $imagingEnums = $enumerationClosure->getAllDescendants($enumeration->enumerationId, 1, true); foreach ($imagingEnums as $imagingEnum) { $rowset = $enumerationClosure->getAllDescendants($imagingEnum->enumerationId, 1, true); if ($imagingEnum->key == OrderImaging::IMAGING_TYPES_ENUM_KEY) { foreach ($rowset as $enum) { $enumKey = $enum->enumerationId; $imagingList[$enumKey] = array(); $imagingList[$enumKey]['name'] = $enum->name; $imagingList[$enumKey]['procedures'] = array(); $imagingList[$enumKey]['modifiers'] = array(); $rows = $enumerationClosure->getAllDescendants($enum->enumerationId, 1, true); foreach ($rows as $row) { if ($row->name == 'Procedures') { $rowsProcedures = $enumerationClosure->getAllDescendants($row->enumerationId, 1, true); foreach ($rowsProcedures as $rowProcedure) { $rowProcedureKey = $rowProcedure->enumerationId; $imagingList[$enumKey]['procedures'][$rowProcedureKey]['procedure'] = $rowProcedure->name; $rowsUnitComment = $enumerationClosure->getAllDescendants($rowProcedure->enumerationId, 1, true); $comment = ''; foreach ($rowsUnitComment as $unitComment) { $comment = $unitComment->name; break; // only one comment is expected } $imagingList[$enumKey]['procedures'][$rowProcedureKey]['comment'] = $comment; } } else { if ($row->name == 'Modifiers') { $rowsModifiers = $enumerationClosure->getAllDescendants($row->enumerationId, 1, true); foreach ($rowsModifiers as $rowModifier) { $rowModifierKey = $rowModifier->enumerationId; $imagingList[$enumKey]['modifiers'][$rowModifierKey] = $rowModifier->name; } } } } } } else { if ($imagingEnum->key == OrderImaging::IMAGING_CATEGORIES_ENUM_KEY) { foreach ($rowset as $enum) { $categories[$enum->key] = $enum->name; } } else { if ($imagingEnum->key == OrderImaging::IMAGING_URGENCIES_ENUM_KEY) { foreach ($rowset as $enum) { $urgencies[$enum->key] = $enum->name; } } else { if ($imagingEnum->key == OrderImaging::IMAGING_TRANSPORTS_ENUM_KEY) { foreach ($rowset as $enum) { $transports[$enum->key] = $enum->name; } } else { if ($imagingEnum->key == OrderImaging::IMAGING_PREGNANTS_ENUM_KEY) { foreach ($rowset as $enum) { $pregnants[$enum->key] = $enum->name; } } } } } } } $this->view->imagingList = $imagingList; $this->view->categories = $categories; $this->view->urgencies = $urgencies; $this->view->transports = $transports; $this->view->pregnants = $pregnants; $this->view->imagingSubmitTo = array('CLEARHEALTH HOSPITAL'); // temporarily hard-coded $this->render(); }
public function executeQueries(array $filters, ReportView $view) { $config = Zend_Registry::get('config'); $dbName = $config->database->params->dbname; $db = Zend_Registry::get('dbAdapter'); $ret = array(); $filterQueries = array(); $reportFilters = array(); foreach ($this->reportFilters as $key => $filter) { if ($filter->type == 'QUERY') { $filterQueries[$filter->query] = $filter->query; } $reportFilters['{{' . $filter->name . '}}'] = $filter; } //trigger_error(print_r($reportFilters,true),E_USER_NOTICE); $reportQuery = new ReportQuery(); $reportQueryIterator = $reportQuery->getIteratorByBaseId($this->reportBaseId); $viewColumnDefinitions = array(); $unserializedColumnDefinitions = $view->unserializedColumnDefinitions; if ($unserializedColumnDefinitions === null) { $unserializedColumnDefinitions = array(); } foreach ($unserializedColumnDefinitions as $id => $value) { $viewColumnDefinitions[$value->queryId][$id] = $value; } //file_put_contents('/tmp/columns.txt',print_r($columnDefinitions,true)); foreach ($reportQueryIterator as $query) { if (isset($filterQueries[$query->reportQueryId])) { continue; } // report query associated with filter not included $row = array(); $row['reportQuery'] = $query->toArray(); $row['reportQuery']['customColNames'] = (bool) $view->customizeColumnNames; $queryValue = $query->query; $tokens = $this->_extractTokens($queryValue); if (isset($tokens[0])) { // tokens defined // check for undefined/orphaned filter $undefinedTokens = array(); foreach ($tokens as $token) { if (!isset($reportFilters[$token])) { $undefinedTokens[] = $token; } } if (isset($undefinedTokens[0])) { $error = 'Query "' . $query->displayName . '" contains undefined tokens: ' . implode(', ', $undefinedTokens); $row['error'] = $error; trigger_error($error, E_USER_NOTICE); $ret[] = $row; continue; } $queryValue = $this->_applyFilters($filters, $queryValue); } $columnDefinitions = array(); if (isset($viewColumnDefinitions[$query->reportQueryId])) { $columnDefinitions = $viewColumnDefinitions[$query->reportQueryId]; } else { foreach ($viewColumnDefinitions as $id => $value) { foreach ($value as $i => $v) { if ($v->queryId != 0 || $v->queryName != $query->reportQueryId) { continue; } $columnDefinitions[$i] = $v; } } } //file_put_contents('/tmp/columns.txt',$query->reportQueryId.' = '.print_r($columnDefinitions,true),FILE_APPEND); $columnDefinitionLen = count($columnDefinitions); switch ($query->type) { case ReportQuery::TYPE_SQL: //file_put_contents('/tmp/query.sql',$queryValue); trigger_error($queryValue, E_USER_NOTICE); // extract and execute defined SQL variables $tmp = explode(';', $queryValue); if (count($tmp) > 0) { foreach ($tmp as $key => $line) { $line = trim($line); if (substr($line, 0, 5) == 'SET @') { unset($tmp[$key]); trigger_error('EXECUTING SQL: ' . $line); $db->query($line); } } $queryValue = implode(';', $tmp); } try { $results = array(); $headers = array(); $stmt = $db->query($queryValue, array(), Zend_Db::FETCH_NUM); $columnInfo = array(); $rowCount = $stmt->rowCount(); for ($i = 0; $i < $rowCount; $i++) { $fetchRow = $stmt->fetch(Zend_Db::FETCH_NUM, null, $i); if ($i == 0) { for ($ctr = 0, $rowLen = count($fetchRow); $ctr < $rowLen; $ctr++) { $columnMeta = $stmt->getColumnMeta($ctr); if ($view->customizeColumnNames && $columnDefinitionLen > 0) { $resultSetName = $dbName . '.' . $columnMeta['table'] . '.' . $columnMeta['name']; foreach ($columnDefinitions as $id => $mapping) { // id, queryId, queryName, resultSetName, displayName, transform if ($mapping->resultSetName == $resultSetName) { $headers[$ctr] = $mapping->displayName; $columnInfo[$ctr] = $mapping; break; } } } else { $headers[$ctr] = $columnMeta['name']; } } } $tmpResult = array('id' => $i, 'data' => array()); if ($view->customizeColumnNames && $columnDefinitionLen > 0) { $tmp = array(); foreach ($columnInfo as $index => $mapping) { $tmp[$mapping->displayName] = $this->_applyTransforms($mapping->transforms, $fetchRow[$index]); } $tmpHeaders = $headers; $headers = array(); foreach ($columnDefinitions as $id => $mapping) { // id, queryId, queryName, resultSetName, displayName, transform $tmpResult['data'][] = $tmp[$mapping->displayName]; foreach ($tmpHeaders as $key => $header) { if ($header != $mapping->displayName) { continue; } $headers[] = $header; unset($tmpHeaders[$key]); break; } } } else { foreach ($headers as $index => $header) { $tmpResult['data'][] = $fetchRow[$index]; } } $results[] = $tmpResult; } $row['headers'] = $headers; $row['rows'] = $results; } catch (Exception $e) { $uniqErrCode = uniqid(); $row['error'] = 'There was a problem executing query: ' . $query->displayName . '. Contact your administrator with error code: ' . $uniqErrCode; trigger_error('Exception error (' . $uniqErrCode . '): ' . $e->getMessage(), E_USER_NOTICE); trigger_error('SQL Query (' . $uniqErrCode . '): ' . $queryValue, E_USER_NOTICE); } $ret[] = $row; break; case ReportQuery::TYPE_NSDR: $nsdr = explode("\n", $queryValue); $nsdrResults = array(); $allDefaults = true; foreach ($nsdr as $key => $value) { $value = $this->_applyFilters($filters, $value); $resultSetName = ReportView::extractNamespace($value); //$displayName = ReportView::metaDataPrettyName($resultSetName); $nsdrResult = NSDR2::populate($value); // NOTE: return of NSDR2::populate() can be either of the following: string, associative array (all keys are treated as column names and values as row/ cell values), and multidimensional array (contains multiple associative arrays) $data = array($resultSetName, $nsdrResult); if (is_array($nsdrResult)) { if (array_key_exists(0, $nsdrResult)) { if (!is_array($nsdrResult[0])) { $data[1] = implode("\n", $nsdrResult); } else { // multi dimensional array $allDefaults = false; foreach ($nsdrResult as $index => $results) { $data = array(); $row['headers'] = array(); foreach ($results as $k => $v) { $row['headers'][] = ReportView::metaDataPrettyName($k); $data[] = $v; } $nsdrResults[$resultSetName][] = array('id' => $key . $index, 'data' => $data); } continue; } } else { $allDefaults = false; $row['headers'] = array(); $data = array(); foreach ($nsdrResult as $k => $v) { $row['headers'][] = ReportView::metaDataPrettyName($k); $data[] = $v; } } } $nsdrResults[$resultSetName][] = array('id' => $key, 'data' => $data); } if ($allDefaults) { $row['headers'] = array('Name', 'Value'); } /*else { if (isset($row['headers'][0]) && $row['headers'][0] != 'Name') $row['headers'][0] .= ' / Name'; if (isset($row['headers'][1]) && $row['headers'][1] != 'Value') $row['headers'][1] .= ' / Value'; }*/ if ($view->customizeColumnNames && $columnDefinitionLen > 0) { foreach ($columnDefinitions as $id => $mapping) { // id, queryId, queryName, resultSetName, displayName, transform $displayName = $mapping->resultSetName; if (!isset($nsdrResults[$displayName]) || ($mapping->queryId != 0 && $mapping->queryId != $query->reportQueryId || $mapping->queryId == 0 && $mapping->queryName != $query->reportQueryId) && $mapping->queryName != $query->displayName) { continue; } //$displayName = ReportView::metaDataPrettyName($mapping->resultSetName); foreach ($nsdrResults[$displayName] as $key => $value) { $data = $nsdrResults[$displayName][$key]['data']; if (!array_key_exists(0, $data)) { continue; } foreach ($data as $k => $v) { if ($k == 0 && $v == $displayName) { $v = $mapping->displayName; } else { $v = $this->_applyTransforms($mapping->transforms, $v); } $nsdrResults[$displayName][$key]['data'][$k] = $v; } $data = $nsdrResults[$displayName][$key]['data'][1]; } } } $results = array(); foreach ($nsdrResults as $result) { foreach ($result as $key => $value) { $results[] = $value; } } $row['rows'] = $results; $ret[] = $row; break; } } return $ret; }
public static function getNSDRData(SimpleXMLElement $xml, self $clinicalNote, $revisionId = 0) { $ret = array(); if ((string) $xml->attributes()->useNSDR != 'true') { return $ret; } foreach ($xml as $questions) { foreach ($questions as $key => $item) { $namespace = (string) $item->attributes()->namespace; if ($key != 'dataPoint' || $namespace && !strlen($namespace) > 0) { continue; } $default = 0; $selectedPatientId = '[selectedPatientId]'; $key = str_replace($selectedPatientId, $clinicalNote->personId, $namespace); if (preg_match('/^[^.]*/', $key, $matches) && strpos($matches[0], '::') === false) { // context is not defined, default to *? $key = '*::' . $key; $default = 1; } if (preg_match('/(.*)\\[(.*)\\]$/', $key, $matches)) { $args = $matches[2]; $x = explode(',', $args); $x[] = '@revisionId=' . $revisionId; $x[] = '@clinicalNoteId=' . $clinicalNote->clinicalNoteId; $x[] = '@isDefaultContext=' . $default; $key = str_replace($args, implode(',', $x), $key); } else { $key .= '[@revisionId=' . $revisionId . ',@clinicalNoteId=' . $clinicalNote->clinicalNoteId . ',@isDefaultContext=' . $default . ']'; } $namespace = NSDR2::extractNamespace($namespace); $result = NSDR2::populate($key); $value = $result; if (is_array($result) && isset($result[$key])) { $value = $result[$key]; } // this MUST coincide with the $elementName of ClinicalNotesFormController::_buildForm() $elementName = preg_replace('/[-\\.]/', '_', $namespace); $ret[$elementName] = $value; } } return $ret; }
public function testPopulate() { $person = new Person(); $person->personId = 1234; $person->populate(); $firstName = $person->firstName; $person->firstName = 'John'; $lastName = $person->lastName; $person->lastName = 'Doe'; $middleName = $person->middleName; $person->middleName = 'C'; $person->persist(); // test case 1 $result = NSDR2::populate('1234::com.clearhealth.person'); $this->assertEquals($result['first_name'], $person->firstName, 'TEST CASE 1 failed.'); $this->assertEquals($result['last_name'], $person->lastName, 'TEST CASE 1 failed.'); $this->assertEquals($result['middle_name'], $person->middleName, 'TEST CASE 1 failed.'); // test case 2 $result = NSDR2::populate('1234::com.clearhealth.person.middleName'); $this->assertEquals($result, 'C', 'TEST CASE 2 failed.'); // test case 5 $result = NSDR2::populate('1234::com.clearhealth.person[aggregateDisplay()]'); $this->assertContains('Doe John C', $result, 'TEST CASE 5 failed.'); // revert the person $person->firstName = $firstName; $person->lastName = $lastName; $person->middleName = $middleName; $person->persist(); }