4 $GLOBALS['form_exit_url'] = "$rootdir/patient_file/encounter/encounter_top.php";
6 function getFormById ($id, $cols = "*")
8 $sql = "select $cols from forms where id='$id' order by date DESC limit 0,1";
9 //echo $sql . "<br />";
10 return sqlQuery($sql);
13 function getFormInfoById ($id, $cols = "*")
15 $sql = "select $cols from forms where id='$id' order by date DESC limit 0,1";
16 //echo $sql . "<br />";
17 $result = sqlQuery($sql);
18 if ($result['formdir'] == "newpatient") {
19 $result['formdir'] = "encounter";
21 $sql = "select * from form_" . $result['formdir'] . " where id='" . $result['form_id'] . "'";
22 //echo $sql . "<br />";
23 $result = sqlQuery($sql);
28 function getFormsByPid ($pid, $cols = "*")
30 return sqlQuery("select $cols from forms where pid ='$pid'");
33 function getFormByEncounter($pid,$encounter, $cols="form_id, form_name", $name="")
35 $arraySqlBind = array();
36 $sql = "select $cols from forms where encounter = ? and pid=? ";
37 array_push($arraySqlBind,$encounter,$pid);
39 $sql .= "and form_name=? ";
40 array_push($arraySqlBind,$name);
42 // This puts vitals first in the list, and newpatient last:
43 $sql .= "ORDER BY FIND_IN_SET(formdir,'vitals') DESC, date DESC";
45 $res = sqlStatement($sql,$arraySqlBind);
47 for($iter=0; $row=sqlFetchArray($res); $iter++)
54 function addForm($encounter, $form_name, $form_id, $formdir, $pid,
55 $authorized = "0", $date="NOW()", $user="", $group="")
57 if (!$user) $user = $_SESSION['authUser'];
58 if (!$group) $group = $_SESSION['authProvider'];
60 $arraySqlBind = array();
61 $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
62 "user, groupname, authorized, formdir) values (";
63 if($date == "NOW()") {
68 array_push($arraySqlBind,$date);
70 $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?)";
71 array_push($arraySqlBind,$encounter,$form_name,$form_id,$pid,$user,$group,$authorized,$formdir);
72 return sqlInsert($sql,$arraySqlBind);
75 function authorizeForm($id, $authorized = "1")
77 sqlQuery("update forms set authorized = '$authorized' where id = '$id'");
80 function getEncounters($pid,$dateStart='',$dateEnd='',$encounterRuleType='')
83 $arraySqlBind = array();
85 if ($encounterRuleType) {
86 // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
87 $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
88 $where = "enc_category_map.rule_enc_id = ? and ";
89 array_push($arraySqlBind,$encounterRuleType);
92 // Collect all encounters
93 $from = "form_encounter";
96 if ($dateStart && $dateEnd) {
97 $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
98 array_push($arraySqlBind,$pid,$dateStart,$dateEnd);
100 else if ($dateStart && !$dateEnd) {
101 $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
102 array_push($arraySqlBind,$pid,$dateStart);
104 else if (!$dateStart && $dateEnd) {
105 $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
106 array_push($arraySqlBind,$pid,$dateEnd);
109 $where .= "form_encounter.pid = ?";
110 array_push($arraySqlBind,$pid);
113 $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
115 for($iter=0; $row=sqlFetchArray($res); $iter++)
122 function getEncounterDateByEncounter($encounter)
124 // $sql = "select date from forms where encounter='$encounter' order by date";
125 $sql = "select date from form_encounter where encounter='$encounter' order by date";
126 return sqlQuery($sql);
129 function getProviderIdOfEncounter($encounter)
131 $sql = "select provider_id from form_encounter where encounter='$encounter' order by date";
132 $res = sqlQuery($sql);
133 return $res['provider_id'];
136 function getFormNameByFormdirAndFormid ($formdir,$form_id)
138 return sqlQuery("select form_name from forms where formdir='$formdir' and form_id='$form_id'");
141 function getFormIdByFormdirAndFormid ($formdir,$form_id)
143 $result = sqlQuery("select id from forms where formdir = ? and form_id =? ", array( $formdir, $form_id ) );
144 return $result['id'];
147 function getFormNameByFormdir ($formdir)
149 return sqlQuery("select form_name from forms where formdir='$formdir'");
152 function getDocumentsByEncounter($patientID = null,$encounterID = null) {
153 $allDocuments = null;
154 $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
155 $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
157 if($currentPatient != "" && $currentEncounter != "") {
158 $sql = "SELECT d.id, d.type, d.url, d.docdate, d.list_id, c.name,d.encounter_id FROM documents AS d, categories_to_documents AS cd,
159 categories AS c WHERE d.foreign_id = ? AND d.encounter_id=? AND cd.document_id = d.id AND c.id = cd.category_id ORDER BY d.docdate DESC, d.id DESC";
160 $res = sqlStatement($sql,array($currentPatient,$currentEncounter));
162 while ( $row = sqlFetchArray($res) ) {
163 $allDocuments[] = $row;
166 return $allDocuments;