public function getSocialHistory($pid, $encounter)
 {
     $social_history = '';
     $arr = array('alcohol' => '160573003', 'drug' => '363908000', 'employment' => '364703007', 'exercise' => '256235009', 'other_social_history' => '228272008', 'diet' => '364393001', 'smoking' => '229819007', 'toxic_exposure' => '425400000');
     $arr_status = array('currenttobacco' => 'Current', 'quittobacco' => 'Quit', 'nevertobacco' => 'Never', 'currentalcohol' => 'Current', 'quitalcohol' => 'Quit', 'neveralcohol' => 'Never');
     $snomeds_status = array('currenttobacco' => 'completed', 'quittobacco' => 'completed', 'nevertobacco' => 'completed', 'not_applicabletobacco' => 'completed');
     $snomeds = array('1' => '449868002', '2' => '428041000124106', '3' => '8517006', '4' => '266919005', '5' => '77176002');
     $alcohol_status = array('currentalcohol' => 'completed', 'quitalcohol' => 'completed', 'neveralcohol' => 'completed');
     $alcohol_status_codes = array('currentalcohol' => '11', 'quitalcohol' => '22', 'neveralcohol' => '33');
     $query = "SELECT id, tobacco, alcohol, exercise_patterns, recreational_drugs FROM history_data WHERE pid=? ORDER BY id DESC LIMIT 1";
     $appTable = new ApplicationTable();
     $res = $appTable->zQuery($query, array($pid));
     $social_history .= "<social_history>";
     foreach ($res as $row) {
         $tobacco = explode('|', $row['tobacco']);
         $status_code = \Carecoordination\Model\CarecoordinationTable::getListCodes($tobacco[3], 'smoking_status');
         $status_code = str_replace("SNOMED-CT:", "", $status_code);
         $social_history .= "<history_element>\n                                  <extension>" . htmlspecialchars(base64_encode('smoking' . $_SESSION['site_id'] . $row['id']), ENT_QUOTES) . "</extension>\n                                  <sha_extension>" . htmlspecialchars("9b56c25d-9104-45ee-9fa4-e0f3afaa01c1", ENT_QUOTES) . "</sha_extension>\n                                  <element>" . htmlspecialchars('Smoking', ENT_QUOTES) . "</element>\n                                  <description>" . htmlspecialchars(\Carecoordination\Model\CarecoordinationTable::getListTitle($tobacco[3], 'smoking_status'), ENT_QUOTES) . "</description>\n                                  <status_code>" . htmlspecialchars($status_code ? $status_code : 0, ENT_QUOTES) . "</status_code>\n                                  <status>" . htmlspecialchars($snomeds_status[$tobacco[1]] ? $snomeds_status[$tobacco[1]] : 'NULL', ENT_QUOTES) . "</status>\n                                  <date>" . ($tobacco[2] ? htmlspecialchars($this->date_format($tobacco[2]), ENT_QUOTES) : 0) . "</date>\n                                  <date_formatted>" . ($tobacco[2] ? htmlspecialchars(preg_replace('/-/', '', $tobacco[2]), ENT_QUOTES) : 0) . "</date_formatted>\n                                  <code>" . htmlspecialchars($arr['smoking'] ? $arr['smoking'] : 0, ENT_QUOTES) . "</code>\n                            </history_element>";
         $alcohol = explode('|', $row['alcohol']);
         $social_history .= "<history_element>\n                                  <extension>" . htmlspecialchars(base64_encode('alcohol' . $_SESSION['site_id'] . $row['id']), ENT_QUOTES) . "</extension>\n                                  <sha_extension>" . htmlspecialchars("37f76c51-6411-4e1d-8a37-957fd49d2cef", ENT_QUOTES) . "</sha_extension>\n                                  <element>" . htmlspecialchars('Alcohol', ENT_QUOTES) . "</element>\n                                  <description>" . htmlspecialchars($alcohol[0], ENT_QUOTES) . "</description>\n                                  <status_code>" . htmlspecialchars($alcohol_status_codes[$alcohol[1]] ? $alcohol_status_codes[$alcohol[1]] : 0, ENT_QUOTES) . "</status_code> \n                                  <status>" . htmlspecialchars($alcohol_status[$alcohol[1]] ? $alcohol_status[$alcohol[1]] : 'completed', ENT_QUOTES) . "</status>\n                                  <date>" . ($alcohol[2] ? htmlspecialchars($this->date_format($alcohol[2]), ENT_QUOTES) : 0) . "</date>\n                                  <date_formatted>" . ($alcohol[2] ? htmlspecialchars(preg_replace('/-/', '', $alcohol[2]), ENT_QUOTES) : 0) . "</date_formatted>\n                                  <code>" . htmlspecialchars($arr['alcohol'], ENT_QUOTES) . "</code>\n                            </history_element>";
     }
     $social_history .= "</social_history>";
     return $social_history;
 }
 public function getAllergies($pid, $encounter)
 {
     $allergies = '';
     $query = "SELECT l.id, l.title, l.begdate, l.enddate, lo.title AS observation, \n            SUBSTRING(lo.codes, LOCATE(':',lo.codes)+1, LENGTH(lo.codes)) AS observation_code,\n\t\t\t\t\t\tSUBSTRING(l.`diagnosis`,1,LOCATE(':',l.diagnosis)-1) AS code_type_real,\n\t\t\t\t\t\tl.reaction, l.diagnosis, l.diagnosis AS code\n\t\t\t\t\t\tFROM lists AS l\n\t\t\t\t\t\tLEFT JOIN list_options AS lo ON lo.list_id = ? AND lo.option_id = l.severity_al\n\t\t\t\t\t\tWHERE l.type = ? AND l.pid = ?";
     $appTable = new ApplicationTable();
     $res = $appTable->zQuery($query, array('severity_ccda', 'allergy', $pid));
     $allergies = "<allergies>";
     foreach ($res as $row) {
         $split_codes = explode(';', $row['code']);
         foreach ($split_codes as $key => $single_code) {
             $code = $code_text = $code_rx = $code_text_rx = $code_snomed = $code_text_snomed = $reaction_text = $reaction_code = '';
             $get_code_details = explode(':', $single_code);
             if ($get_code_details[0] == 'RxNorm') {
                 $code_rx = $get_code_details[1];
                 $code_text_rx = lookup_code_descriptions($single_code);
             } else {
                 if ($get_code_details[0] == 'SNOMED') {
                     $code_snomed = $get_code_details[1];
                     $code_text_snomed = lookup_code_descriptions($row['code']);
                 } else {
                     $code = $get_code_details[1];
                     $code_text = lookup_code_descriptions($single_code);
                 }
             }
             $active = $status_table = '';
             if ($row['enddate']) {
                 $active = 'completed';
                 $allergy_status = 'completed';
                 $status_table = 'Resolved';
                 $status_code = '73425007';
             } else {
                 $active = 'completed';
                 $allergy_status = 'active';
                 $status_table = 'Active';
                 $status_code = '55561003';
             }
             if ($row['reaction']) {
                 $reaction_text = \Carecoordination\Model\CarecoordinationTable::getListTitle($row['reaction'], 'reaction', '');
                 $reaction_code = \Carecoordination\Model\CarecoordinationTable::getCodes($row['reaction'], 'reaction');
             }
             $allergies .= "<allergy>\n\t\t\t\t\t\t\t<id>" . htmlspecialchars(base64_encode($_SESSION['site_id'] . $row['id'] . $single_code), ENT_QUOTES) . "</id>\n\t\t\t\t\t\t\t<sha_id>" . htmlspecialchars("36e3e930-7b14-11db-9fe1-0800200c9a66", ENT_QUOTES) . "</sha_id>\n\t\t\t\t\t\t\t<title>" . htmlspecialchars($row['title'], ENT_QUOTES) . ($single_code ? " [" . htmlspecialchars($single_code, ENT_QUOTES) . "]" : '') . "</title>\n\t\t\t\t\t\t\t<diagnosis_code>" . htmlspecialchars($code ? $code : 0, ENT_QUOTES) . "</diagnosis_code>\n\t\t\t\t\t\t\t<diagnosis>" . htmlspecialchars($code_text ? \Application\Listener\Listener::z_xlt($code_text) : 'NULL', ENT_QUOTES) . "</diagnosis>\n\t\t\t\t\t\t\t<rxnorm_code>" . htmlspecialchars($code_rx ? $code_rx : 0, ENT_QUOTES) . "</rxnorm_code>\n\t\t\t\t\t\t\t<rxnorm_code_text>" . htmlspecialchars($code_text_rx ? \Application\Listener\Listener::z_xlt($code_text_rx) : 'NULL', ENT_QUOTES) . "</rxnorm_code_text>\n\t\t\t\t\t\t\t<snomed_code>" . htmlspecialchars($code_snomed ? $code_snomed : 0, ENT_QUOTES) . "</snomed_code>\n\t\t\t\t\t\t\t<snomed_code_text>" . htmlspecialchars($code_text_snomed ? \Application\Listener\Listener::z_xlt($code_text_snomed) : 'NULL', ENT_QUOTES) . "</snomed_code_text>\n\t\t\t\t\t\t\t<status_table>" . ($status_table ? $status_table : 'NULL') . "</status_table>\n\t\t\t\t\t\t\t<status>" . ($active ? $active : 'NULL') . "</status>\n\t\t\t\t\t\t\t<allergy_status>" . ($allergy_status ? $allergy_status : 'NULL') . "</allergy_status>\n\t\t\t\t\t\t\t<status_code>" . ($status_code ? $status_code : 0) . "</status_code>\n\t\t\t\t\t\t\t<outcome>" . htmlspecialchars($row['observation'] ? \Application\Listener\Listener::z_xlt($row['observation']) : 'NULL', ENT_QUOTES) . "</outcome>\n\t\t\t\t\t\t\t<outcome_code>" . htmlspecialchars($row['observation_code'] ? $row['observation_code'] : 0, ENT_QUOTES) . "</outcome_code>\n\t\t\t\t\t\t\t<startdate>" . htmlspecialchars($row['begdate'] ? preg_replace('/-/', '', $row['begdate']) : "00000000", ENT_QUOTES) . "</startdate>\n\t\t\t\t\t\t\t<enddate>" . htmlspecialchars($row['enddate'] ? preg_replace('/-/', '', $row['enddate']) : "00000000", ENT_QUOTES) . "</enddate>\n\t\t\t\t\t\t\t<reaction_text>" . htmlspecialchars($reaction_text ? \Application\Listener\Listener::z_xlt($reaction_text) : 'NULL', ENT_QUOTES) . "</reaction_text>\n\t\t\t\t\t\t\t<reaction_code>" . htmlspecialchars($reaction_code ? $reaction_code : 0, ENT_QUOTES) . "</reaction_code>\n\t\t\t\t\t\t\t<RxNormCode>" . htmlspecialchars($code_rx, ENT_QUOTES) . "</RxNormCode>\n\t\t\t\t\t\t\t<RxNormCode_text>" . htmlspecialchars(!empty($code_text_rx) ? $code_text_rx : $row['title'], ENT_QUOTES) . "</RxNormCode_text>\n\t\t\t\t\t\t</allergy>";
         }
     }
     $allergies .= "</allergies>";
     return $allergies;
 }