Display fix: width in manila for demographics (#1909)
[openemr.git] / library / forms.inc
blobabf79c15eb4a67eff9e0ca06a37a37b6a9d19c43
1 <?php
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 />";
8     return sqlQuery($sql);
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";
18     }
20     $sql = "select * from form_" . $result['formdir'] . " where id='" . $result['form_id']  . "'";
21     //echo $sql . "<br />";
22     $result =  sqlQuery($sql);
23     //print_r($result);
24     return $result;
27 function getFormsByPid($pid, $cols = "*")
29     return sqlQuery("select $cols from forms where pid = '$pid' and deleted = 0");
32 function getFormByEncounter(
33     $attendant_id,
34     $encounter,
35     $cols = "form_id, form_name",
36     $name = "",
37     $orderby = "FIND_IN_SET(formdir,'vitals') DESC, date DESC"
38 ) {
40     global $attendant_type;
41     $arraySqlBind = array();
42     $sql = "select $cols from forms where encounter = ? and deleted = 0 ";
43     array_push($arraySqlBind, $encounter);
44     if (!empty($name)) {
45         $sql .= "and form_name=? ";
46         array_push($arraySqlBind, $name);
47     }
49     if ($attendant_type == 'pid') {
50         $sql .= " and pid=? and therapy_group_id IS NULL ";
51     } else {
52         $sql .= " and therapy_group_id = ? and pid IS NULL ";
53     }
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++) {
63         $all[$iter] = $row;
64     }
66     return $all;
69 function addForm(
70     $encounter,
71     $form_name,
72     $form_id,
73     $formdir,
74     $pid,
75     $authorized = "0",
76     $date = "NOW()",
77     $user = "",
78     $group = "",
79     $therapy_group = 'not_given'
80 ) {
82     global $attendant_type;
83     if (!$user) {
84         $user = $_SESSION['authUser'];
85     }
87     if (!$group) {
88         $group = $_SESSION['authProvider'];
89     }
91     if ($therapy_group == 'not_given') {
92         $therapy_group = $attendant_type == 'pid' ? null : $_SESSION['therapy_group'];
93     }
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()") {
100         $sql .= "$date";
101     } else {
102         $sql .= "?";
103                 array_push($arraySqlBind, $date);
104     }
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);
126     } else {
127         // Collect all encounters
128         $from = "form_encounter";
129     }
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);
140     } else {
141         $where .= "form_encounter.pid = ?";
142         array_push($arraySqlBind, $pid);
143     }
145         $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
147     for ($iter=0; $row=sqlFetchArray($res); $iter++) {
148         $all[$iter] = $row;
149     }
151         return $all;
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;
201         }
202     }
204     return $allDocuments;