2 $GLOBALS['form_exit_url'] = "javascript:parent.closeTab(window.name, false)";
4 function getFormById($id, $cols = "*")
6 $sql = "select $cols from forms where id='$id' and deleted = 0 order by date DESC limit 0,1";
7 //echo $sql . "<br />";
11 function getFormInfoById($id, $cols = "*")
13 $sql = "select $cols from forms where id = '$id' and deleted = 0 order by date DESC limit 0,1";
14 //echo $sql . "<br />";
15 $result = sqlQuery($sql);
16 if ($result['formdir'] == "newpatient") {
17 $result['formdir'] = "encounter";
20 $sql = "select * from form_" . $result['formdir'] . " where id='" . $result['form_id'] . "'";
21 //echo $sql . "<br />";
22 $result = sqlQuery($sql);
27 function getFormsByPid($pid, $cols = "*")
29 return sqlQuery("select $cols from forms where pid = '$pid' and deleted = 0");
32 function getFormByEncounter(
35 $cols = "form_id, form_name",
37 $orderby = "FIND_IN_SET(formdir,'vitals') DESC, date DESC"
40 global $attendant_type;
41 $arraySqlBind = array();
42 $sql = "select $cols from forms where encounter = ? and deleted = 0 ";
43 array_push($arraySqlBind, $encounter);
45 $sql .= "and form_name=? ";
46 array_push($arraySqlBind, $name);
49 if ($attendant_type == 'pid') {
50 $sql .= " and pid=? and therapy_group_id IS NULL ";
52 $sql .= " and therapy_group_id = ? and pid IS NULL ";
55 array_push($arraySqlBind, $attendant_id);
57 // Default $orderby puts vitals first in the list, and newpatient last:
58 $sql .= "ORDER BY $orderby";
60 $res = sqlStatement($sql, $arraySqlBind);
62 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
79 $therapy_group = 'not_given'
82 global $attendant_type;
84 $user = $_SESSION['authUser'];
88 $group = $_SESSION['authProvider'];
91 if ($therapy_group == 'not_given') {
92 $therapy_group = $attendant_type == 'pid' ? null : $_SESSION['therapy_group'];
95 //print_r($_SESSION['therapy_group']);die;
96 $arraySqlBind = array();
97 $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
98 "user, groupname, authorized, formdir, therapy_group_id) values (";
99 if ($date == "NOW()") {
103 array_push($arraySqlBind, $date);
106 $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?, ?)";
107 array_push($arraySqlBind, $encounter, $form_name, $form_id, $pid, $user, $group, $authorized, $formdir, $therapy_group);
108 return sqlInsert($sql, $arraySqlBind);
111 function authorizeForm($id, $authorized = "1")
113 sqlQuery("update forms set authorized = '$authorized' where id = '$id' and deleted = 0");
116 function getEncounters($pid, $dateStart = '', $dateEnd = '', $encounterRuleType = '')
119 $arraySqlBind = array();
121 if ($encounterRuleType) {
122 // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
123 $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
124 $where = "enc_category_map.rule_enc_id = ? and ";
125 array_push($arraySqlBind, $encounterRuleType);
127 // Collect all encounters
128 $from = "form_encounter";
131 if ($dateStart && $dateEnd) {
132 $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
133 array_push($arraySqlBind, $pid, $dateStart, $dateEnd);
134 } else if ($dateStart && !$dateEnd) {
135 $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
136 array_push($arraySqlBind, $pid, $dateStart);
137 } else if (!$dateStart && $dateEnd) {
138 $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
139 array_push($arraySqlBind, $pid, $dateEnd);
141 $where .= "form_encounter.pid = ?";
142 array_push($arraySqlBind, $pid);
145 $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
147 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
154 function getEncounterDateByEncounter($encounter)
156 global $attendant_type;
157 $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
158 // $sql = "select date from forms where encounter='$encounter' order by date";
159 $sql = "select date from $table where encounter='$encounter' order by date";
160 return sqlQuery($sql);
163 function getProviderIdOfEncounter($encounter)
165 global $attendant_type;
166 $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
167 $sql = "select provider_id from $table where encounter=? order by date";
168 $res = sqlQuery($sql, array($encounter));
169 return $res['provider_id'];
172 function getFormNameByFormdirAndFormid($formdir, $form_id)
174 return sqlQuery("select form_name from forms where formdir = '$formdir' and form_id = '$form_id' and deleted = 0");
177 function getFormIdByFormdirAndFormid($formdir, $form_id)
179 $result = sqlQuery("select id from forms where formdir = ? and form_id = ? and deleted = 0 ", array( $formdir, $form_id ));
180 return $result['id'];
183 function getFormNameByFormdir($formdir)
185 return sqlQuery("select form_name from forms where formdir = '$formdir' and deleted = 0");
188 function getDocumentsByEncounter($patientID = null, $encounterID = null)
190 $allDocuments = null;
191 $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
192 $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
194 if ($currentPatient != "" && $currentEncounter != "") {
195 $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,
196 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";
197 $res = sqlStatement($sql, array($currentPatient,$currentEncounter));
199 while ($row = sqlFetchArray($res)) {
200 $allDocuments[] = $row;
204 return $allDocuments;