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' and deleted = 0 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' and deleted = 0 order by date DESC limit 0,1";
16 //echo $sql . "<br />";
17 $result = sqlQuery($sql);
18 if ($result['formdir'] == "newpatient") {
19 $result['formdir'] = "encounter";
22 $sql = "select * from form_" . $result['formdir'] . " where id='" . $result['form_id'] . "'";
23 //echo $sql . "<br />";
24 $result = sqlQuery($sql);
29 function getFormsByPid($pid, $cols = "*")
31 return sqlQuery("select $cols from forms where pid = '$pid' and deleted = 0");
34 function getFormByEncounter(
37 $cols = "form_id, form_name",
39 $orderby = "FIND_IN_SET(formdir,'vitals') DESC, date DESC"
42 global $attendant_type;
43 $arraySqlBind = array();
44 $sql = "select $cols from forms where encounter = ? and deleted = 0 ";
45 array_push($arraySqlBind, $encounter);
47 $sql .= "and form_name=? ";
48 array_push($arraySqlBind, $name);
51 if ($attendant_type == 'pid') {
52 $sql .= " and pid=? and therapy_group_id IS NULL ";
54 $sql .= " and therapy_group_id = ? and pid IS NULL ";
57 array_push($arraySqlBind, $attendant_id);
59 // Default $orderby puts vitals first in the list, and newpatient last:
60 $sql .= "ORDER BY $orderby";
62 $res = sqlStatement($sql, $arraySqlBind);
64 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
81 $therapy_group = 'not_given'
84 global $attendant_type;
86 $user = $_SESSION['authUser'];
90 $group = $_SESSION['authProvider'];
93 if ($therapy_group == 'not_given') {
94 $therapy_group = $attendant_type == 'pid' ? null : $_SESSION['therapy_group'];
97 //print_r($_SESSION['therapy_group']);die;
98 $arraySqlBind = array();
99 $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
100 "user, groupname, authorized, formdir, therapy_group_id) values (";
101 if ($date == "NOW()") {
105 array_push($arraySqlBind, $date);
108 $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?, ?)";
109 array_push($arraySqlBind, $encounter, $form_name, $form_id, $pid, $user, $group, $authorized, $formdir, $therapy_group);
110 return sqlInsert($sql, $arraySqlBind);
113 function authorizeForm($id, $authorized = "1")
115 sqlQuery("update forms set authorized = '$authorized' where id = '$id' and deleted = 0");
118 function getEncounters($pid, $dateStart = '', $dateEnd = '', $encounterRuleType = '')
121 $arraySqlBind = array();
123 if ($encounterRuleType) {
124 // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
125 $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
126 $where = "enc_category_map.rule_enc_id = ? and ";
127 array_push($arraySqlBind, $encounterRuleType);
129 // Collect all encounters
130 $from = "form_encounter";
133 if ($dateStart && $dateEnd) {
134 $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
135 array_push($arraySqlBind, $pid, $dateStart, $dateEnd);
136 } else if ($dateStart && !$dateEnd) {
137 $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
138 array_push($arraySqlBind, $pid, $dateStart);
139 } else if (!$dateStart && $dateEnd) {
140 $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
141 array_push($arraySqlBind, $pid, $dateEnd);
143 $where .= "form_encounter.pid = ?";
144 array_push($arraySqlBind, $pid);
147 $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
149 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
156 function getEncounterDateByEncounter($encounter)
158 global $attendant_type;
159 $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
160 // $sql = "select date from forms where encounter='$encounter' order by date";
161 $sql = "select date from $table where encounter='$encounter' order by date";
162 return sqlQuery($sql);
165 function getProviderIdOfEncounter($encounter)
167 global $attendant_type;
168 $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
169 $sql = "select provider_id from $table where encounter='$encounter' order by date";
170 $res = sqlQuery($sql);
171 return $res['provider_id'];
174 function getFormNameByFormdirAndFormid($formdir, $form_id)
176 return sqlQuery("select form_name from forms where formdir = '$formdir' and form_id = '$form_id' and deleted = 0");
179 function getFormIdByFormdirAndFormid($formdir, $form_id)
181 $result = sqlQuery("select id from forms where formdir = ? and form_id = ? and deleted = 0 ", array( $formdir, $form_id ));
182 return $result['id'];
185 function getFormNameByFormdir($formdir)
187 return sqlQuery("select form_name from forms where formdir = '$formdir' and deleted = 0");
190 function getDocumentsByEncounter($patientID = null, $encounterID = null)
192 $allDocuments = null;
193 $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
194 $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
196 if ($currentPatient != "" && $currentEncounter != "") {
197 $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,
198 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";
199 $res = sqlStatement($sql, array($currentPatient,$currentEncounter));
201 while ($row = sqlFetchArray($res)) {
202 $allDocuments[] = $row;
206 return $allDocuments;