示例#1
0
 /**
  * Write all the RAPTOR data of curent ticket into VISTA.
  * - Safety Checklist information
  * - General notes
  * @return boolean TRUE if success, else FALSE
  */
 function commitDataToVista($nSiteID, $nIEN, $nUID, $sCWFS, $myvalues, $encounterString = NULL)
 {
     $bSuccess = TRUE;
     $errormsg = NULL;
     error_log('Starting commitDataToVista(' . $nSiteID . ',' . $nIEN . ') at ' . microtime());
     $commit_dt = date("Y-m-d H:i:s", time());
     $prev_commit_dt = $this->getDateMostRecentVistaCommitDate($nSiteID, $nIEN);
     //Verify the electronic sigature
     $eSig = $myvalues['commit_esig'];
     $oMdwsDao = $this->m_oContext->getMdwsClient();
     //$bValidESig = MdwsUtils::validateEsig($oMdwsDao, $eSig);
     $bValidESig = $this->isValidEsig($eSig, $oMdwsDao);
     if (!$bValidESig) {
         $errormsg = 'Trouble committing ticket ' . $nSiteID . '-' . $nIEN . ' Safety Checklist note to Vista because invalid electronic signature';
         $bSuccess = FALSE;
     }
     if ($bSuccess) {
         module_load_include('php', 'raptor_datalayer', 'core/AllUsers');
         $oAllUsers = new \raptor\AllUsers();
         try {
             $oMdwsDao = $this->m_oContext->getMdwsClient();
             if ($encounterString == NULL) {
                 $aVisits = \raptor\MdwsUtils::getVisits($oMdwsDao);
                 if (is_array($aVisits) && count($aVisits) > 0) {
                     if (isset($myvalues['selected_vid']) && $myvalues['selected_vid'] != '') {
                         $selected_vid = $myvalues['selected_vid'];
                         //vid_<LOCATIONID>_<TIMESTAMP>
                         $vidparts = explode('_', $selected_vid);
                         $locationId = $vidparts[1];
                         $visitTimestamp = $vidparts[2];
                         foreach ($aVisits as $aVisit) {
                             if ($aVisit['locationId'] == $locationId && $aVisit['visitTimestamp'] == $visitTimestamp) {
                                 $encounterString = \raptor\MdwsUtils::getEncounterStringFromVisit($aVisit['visitTO']);
                                 //TODO ask the user to pick one!!!
                             }
                         }
                         if ($encounterString == NULL) {
                             die('Did NOT find an encounter string for $selected_vid=[' . $selected_vid . '] in ' . print_r($aVisits, TRUE));
                         }
                     } else {
                         throw new \Exception('Did not find any selected visit for the VISTA writeback!');
                         //drupal_set_message('TODO remove automatic selection of first visit for writeback.  Used for writing this record!','warning');
                         //error_log('commitChecklistToVista got visits='.print_r($aVisits,TRUE).'');
                         //$encounterString = \raptor\MdwsUtils::getEncounterStringFromVisit($aVisits[0]['visitTO']);   //TODO ask the user to pick one!!!
                         //error_log('commitChecklistToVista got most recent visit on ticket '.$nSiteID.'-'.$nIEN.' as encounterString=['.$encounterString.']');
                     }
                 } else {
                     drupal_set_message('Did NOT find any visits to which we can commit a note!', 'error');
                     $bSuccess = FALSE;
                 }
             }
             //Write the note(s).
             $newNoteIen = NULL;
             try {
                 $userDuz = $oMdwsDao->getDUZ();
                 //Pull values from database that have not yet been committed to VISTA
                 $aChecklistData = array();
                 $this->addUncommittedChecklistDetailsToNotesArray($nSiteID, $nIEN, $oAllUsers, $prev_commit_dt, $aChecklistData);
                 if (count($aChecklistData) > 0) {
                     //Write the checklist note
                     $newNoteIen = \raptor\MdwsUtils::writeRaptorSafetyChecklist($oMdwsDao, $aChecklistData, $encounterString, NULL);
                     MdwsUtils::signNote($oMdwsDao, $newNoteIen, $userDuz, $eSig);
                 }
                 //Pull values from database that have not yet been committed to VISTA
                 $noteTextArray = array();
                 $this->addUncommittedDetailsToNotesArray($nSiteID, $nIEN, $oAllUsers, $prev_commit_dt, $noteTextArray);
                 if (count($noteTextArray) > 0) {
                     //Yes, write the general note.
                     $newGeneralNoteIen = \raptor\MdwsUtils::writeRaptorGeneralNote($oMdwsDao, $noteTextArray, $encounterString, NULL);
                     MdwsUtils::signNote($oMdwsDao, $newGeneralNoteIen, $userDuz, $eSig);
                 }
             } catch (\Exception $ex) {
                 drupal_set_message('Trouble in commit because ' . $ex->getMessage(), 'error');
                 throw $ex;
             }
             if ($newNoteIen != NULL) {
                 error_log('commitDataToVista got newNoteIen=[' . $newNoteIen . '] for encounter string=' . $encounterString);
             }
         } catch (\Exception $ex) {
             $errormsg = 'Trouble committing ticket ' . $nSiteID . '-' . $nIEN . ' Safety Checklist note to Vista because ' . $ex->getMessage();
             throw $ex;
         }
     }
     if ($bSuccess) {
         //Okay, record that we successfully committed.
         try {
             db_insert('raptor_ticket_commit_tracking')->fields(array('siteid' => $nSiteID, 'IEN' => $nIEN, 'workflow_state' => $sCWFS, 'author_uid' => $nUID, 'commit_dt' => $commit_dt))->execute();
         } catch (\Exception $ex) {
             $errormsg = 'Trouble committing ticket ' . $nSiteID . '-' . $nIEN . ' to raptor_ticket_commit_tracking because ' . $ex->getMessage();
             throw $ex;
         }
     }
     if ($bSuccess) {
         drupal_set_message('Committed patient data to Vista');
     } else {
         if ($errormsg != NULL) {
             error_log('failed commit to vista>>> ' . $errormsg);
             drupal_set_message($errormsg, 'error');
         } else {
             drupal_set_message('Trouble committing patient data to Vista', 'error');
         }
     }
     error_log('Finished commitDataToVista on ticket ' . $nSiteID . '-' . $nIEN . ' at ' . microtime());
     return $bSuccess;
 }