public function setUp()
 {
     parent::setUp();
     $user = new User();
     $user->username = TEST_LOGIN_USERNAME;
     $user->populateWithUsername();
     Zend_Auth::getInstance()->getStorage()->write($user);
     $clinicalNoteTemplate = new ClinicalNoteTemplate();
     $clinicalNoteTemplate->name = 'Test Note';
     $clinicalNoteTemplate->template = "<progressNoteTemplate>\r\n\t<question label=\"Are you in pain?\">\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.inPain\" dbValue=\"tinyint\" label=\"y/n\">\r\n\t\t</dataPoint>\r\n\t</question>\r\n\t<question label=\"Describe the pain:\">\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.burning\" dbValue=\"tinyint\" label=\"Burning\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.cramping\" dbValue=\"tinyint\" label=\"Cramping\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.penetrating\" dbValue=\"tinyint\" label=\"Penetrating\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.colic\" dbValue=\"tinyint\" label=\"Colic\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.oppressive\" dbValue=\"tinyint\" label=\"Oppressive\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.shooting\" dbValue=\"tinyint\" label=\"Shooting\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.sharp\" dbValue=\"tinyint\" label=\"Sharp\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.other\" dbValue=\"tinyint\" label=\"Other\"/>\r\n\t</question>\r\n\t<question label=\"Duration of pain:\">\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.constant\" dbValue=\"tinyint\" label=\"Constant\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.intermittent\" dbValue=\"tinyint\" label=\"Intermittent\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.home\" dbValue=\"tinyint\" label=\"Home\"/>\r\n\t</question>\r\n\t<question label=\"What relieves the pain?\">\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.relief.sleep\" dbValue=\"tinyint\" label=\"Sleep\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.relief.heat\" dbValue=\"tinyint\" label=\"Heat\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.relief.cold\" dbValue=\"tinyint\" label=\"Cold\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.description.relief.medicine\" dbValue=\"tinyint\" label=\"Medicine\"/>\r\n\t</question>\r\n\t<question label=\"Problem Sleeping?\">\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.sleep.problemSleeping\" dbValue=\"tinyint\" label=\"y/n\"/>\r\n\t</question>\r\n\t<question label=\"Is pain now controlled?\">\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.nowControlled\" dbValue=\"tinyint\" label=\"y/n\"/>\r\n\t\t<dataPoint type=\"text\" namespace=\"assessment.pain.comment\" dbValue=\"varchar:255\" label=\"Pain Comment:\"/>\r\n\t</question>\r\n\t<question label=\"Is patient able to communicate?\">\r\n\t\t<heading>Verbal:</heading>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.verbal.positive\" dbValue=\"tinyint\" label=\"Positive\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.verbal.plaintativeWhining\" dbValue=\"tinyint\" label=\"Plaintative/Whining\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.verbal.weeping\" dbValue=\"tinyint\" label=\"Weeping\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.verbal.screaming\" dbValue=\"tinyint\" label=\"Screaming\"/>\r\n\t\t<heading>Body Movements:</heading>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.bodyMovement.easeOfMovement\" dbValue=\"tinyint\" label=\"Ease of Movement\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.bodyMovement.neutral\" dbValue=\"tinyint\" label=\"Neutral\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.bodyMovement.tense\" dbValue=\"tinyint\" label=\"Tense\" />\r\n\t\t<heading>Facial:</heading>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.facial.smiling\" dbValue=\"tinyint\" label=\"Smiling\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.facial.neutral\" dbValue=\"tinyint\" label=\"Neutral\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.facial.grin\" dbValue=\"tinyint\" label=\"Grin\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.communicate.facial.grittedTeeth\" dbValue=\"tinyint\" label=\"Gritted Teeth\"/>\r\n\t\t<heading>Area of pain:</heading>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.areaNonPalpable\" dbValue=\"tinyint\" label=\"Non-palpable\"/>\r\n\t\t<dataPoint type=\"checkbox\" namespace=\"assessment.pain.areaReagent\" dbValue=\"tinyint\" label=\"Reagent\"/>\r\n\t\t<dataPoint type=\"text\" namespace=\"assessment.pain.areaIndicated\" dbValue=\"varchar:255\" label=\"Indicated:\"/>\r\n\t</question>\r\n</progressNoteTemplate>";
     $clinicalNoteTemplate->persist();
     $this->_objects['noteTemplate'] = $clinicalNoteTemplate;
     $clinicalNoteDefinition = new ClinicalNoteDefinition();
     $clinicalNoteDefinition->title = 'Test Note Definition';
     $clinicalNoteDefinition->clinicalNoteTemplateId = $clinicalNoteTemplate->clinicalNoteTemplateId;
     $clinicalNoteDefinition->active = 1;
     $clinicalNoteDefinition->persist();
     $this->_objects['noteDefinition'] = $clinicalNoteDefinition;
 }
 public function indexAction()
 {
     $personId = (int) $this->_getParam('personId');
     $clinicalNoteId = (int) $this->_getParam('clinicalNoteId');
     trigger_error('ttpid: ' . $personId, E_USER_NOTICE);
     $cn = new ClinicalNote();
     $cn->clinicalNoteId = (int) $clinicalNoteId;
     $cn->populate();
     $templateId = $cn->clinicalNoteTemplateId;
     $cnTemplate = new ClinicalNoteTemplate();
     $cnTemplate->clinicalNoteTemplateId = (int) $templateId;
     $cnTemplate->populate();
     $xml = simplexml_load_string($cnTemplate->template);
     $objective = '';
     foreach ($xml as $question) {
         foreach ($question as $key => $item) {
             if ($key != "dataPoint") {
                 continue;
             }
             $namespace = (string) $item->attributes()->template;
             // extract the nsdr: format
             preg_match('/{nsdr:(.*)}/', $namespace, $matches);
             if (isset($matches[1])) {
                 $namespace = str_replace('[selectedPatientId]', $personId, $matches[1]);
                 $result = NSDR::populate($namespace);
                 $objective .= $result[$namespace];
             }
         }
     }
     $this->view->objective = $objective;
     $filter = array('personId' => $personId);
     $pl = new ProblemList();
     $pli = $pl->getIterator();
     $pli->setFilters($filter);
     $this->view->problemListIterator = $pli;
 }
 /**
  * Process the modified clinical notes template
  */
 public function processEditTemplateAction()
 {
     $params = $this->_getParam('cnTemplate');
     $autoAdd = (int) $this->_getParam('autoAdd');
     $cnTemplate = new ClinicalNoteTemplate();
     $cnTemplate->populateWithArray($params);
     $data = array();
     try {
         $xml = new SimpleXMLElement($cnTemplate->template);
         $data['msg'] = __('Record saved successfully.');
         $cnTemplate->persist();
     } catch (Exception $e) {
         $data['error'] = __('Error: ' . $e->getMessage());
     }
     if (!isset($data['error']) && (string) $xml->attributes()->useNSDR && (string) $xml->attributes()->useNSDR == 'true') {
         $namespaceAdd = false;
         $namespaces = array();
         $nsdrDefinition = new NSDRDefinition();
         foreach ($xml as $questions) {
             foreach ($questions as $key => $item) {
                 $namespace = (string) $item->attributes()->namespace;
                 if ($key != 'dataPoint' || $namespace && !strlen($namespace) > 0) {
                     continue;
                 }
                 // extract namespace only
                 $namespace = NSDR2::extractNamespace($namespace);
                 // check if namespace exists then auto-add if does not
                 if (!$nsdrDefinition->isNamespaceExists($namespace) && $autoAdd) {
                     $nsdrDefinition->addNamespace($namespace, 'GenericData');
                     $namespaceAdd = true;
                 }
             }
         }
         if ($namespaceAdd) {
             $data['msg'] .= "\n\n" . __('Please reload NSDR');
         }
     }
     $json = Zend_Controller_Action_HelperBroker::getStaticHelper('json');
     $json->suppressExit = true;
     $json->direct($data);
 }
 public static function upgradeClinicalNote()
 {
     /* NOTE: take output file and
     		# cat upnote.sql | sort -n | uniq > upnote2.sql
     		For it to run quickly all the fields used in the join need to be indexed, otherwise 5 seconds per row give or take */
     set_time_limit(0);
     $db = Zend_Registry::get('dbAdapter');
     $genericData = new self();
     $sql = "\n\t\tSELECT genericData.name, clinicalNoteDefinitions.clinicalNoteTemplateId\n\t\t\tFROM `genericData`\n\t\t\tINNER JOIN clinicalNotes ON clinicalNotes.clinicalNoteId = genericData.objectId\n\t\t\tINNER JOIN clinicalNoteDefinitions on clinicalNoteDefinitions.clinicalNoteDefinitionId = clinicalNotes.clinicalNoteDefinitionId\n\t\t\tGROUP BY genericData.name, clinicalNoteDefinitions.clinicalNoteTemplateId";
     $res = $db->query($sql);
     while (($row = $res->fetch()) !== false) {
         $cn = new ClinicalNoteTemplate();
         $cn->clinicalNoteTemplateId = (int) $row['clinicalNoteTemplateId'];
         if (!$cn->populate()) {
             $error = 'Error populating clinical note ' . $cn->clinicalNoteId;
             trigger_error($error);
             continue;
         }
         try {
             $xml = new SimpleXMLElement($cn->template);
             foreach ($xml as $question) {
                 foreach ($question as $key => $item) {
                     if ($key != 'dataPoint') {
                         continue;
                     }
                     $namespace = (string) $item->attributes()->namespace;
                     $html = preg_replace('/[-\\.]/', '_', $namespace);
                     $output = 'UPDATE ' . $genericData->_table . ' INNER JOIN clinicalNotes ON clinicalNotes.clinicalNoteId = genericData.objectId INNER JOIN clinicalNoteDefinitions on clinicalNoteDefinitions.clinicalNoteDefinitionId = clinicalNotes.clinicalNoteDefinitionId SET `name`=' . $db->quote($namespace) . ' WHERE `name`=' . $db->quote($html) . ' AND objectClass=\'ClinicalNote\' AND clinicalNoteDefinitions.clinicalNoteTemplateId=' . $cn->clinicalNoteTemplateId . ";\n";
                     file_put_contents('/tmp/upnote.sql', $output, FILE_APPEND);
                     echo ".";
                 }
             }
         } catch (Exception $e) {
             $error = 'Error parsing template for clinical note ' . $cn->clinicalNoteId . ' template ' . $templateId;
             trigger_error($error);
             continue;
         }
     }
 }