security fix in master branch
[openemr.git] / library / forms.inc
blob62b26683900399984650a669779a45d3c69b5471
1 <?php
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";
20     }
22     $sql = "select * from form_" . $result['formdir'] . " where id='" . $result['form_id']  . "'";
23     //echo $sql . "<br />";
24     $result =  sqlQuery($sql);
25     //print_r($result);
26     return $result;
29 function getFormsByPid($pid, $cols = "*")
31     return sqlQuery("select $cols from forms where pid ='$pid'");
34 function getFormByEncounter($attendant_id, $encounter, $cols = "form_id, form_name", $name = "")
36     global $attendant_type;
37     $arraySqlBind = array();
38     $sql = "select $cols from forms where encounter = ? ";
39     array_push($arraySqlBind, $encounter);
40     if (!empty($name)) {
41         $sql .= "and form_name=? ";
42         array_push($arraySqlBind, $name);
43     }
45     if ($attendant_type == 'pid') {
46         $sql .= " and pid=? and therapy_group_id IS NULL ";
47     } else {
48         $sql .= " and therapy_group_id = ? and pid IS NULL ";
49     }
51     array_push($arraySqlBind, $attendant_id);
52   // This puts vitals first in the list, and newpatient last:
53     $sql .= "ORDER BY FIND_IN_SET(formdir,'vitals') DESC, date DESC";
55     $res = sqlStatement($sql, $arraySqlBind);
57     for ($iter=0; $row=sqlFetchArray($res); $iter++) {
58         $all[$iter] = $row;
59     }
61     return $all;
64 function addForm(
65     $encounter,
66     $form_name,
67     $form_id,
68     $formdir,
69     $pid,
70     $authorized = "0",
71     $date = "NOW()",
72     $user = "",
73     $group = "",
74     $therapy_group = 'not_given'
75 ) {
77     global $attendant_type;
78     if (!$user) {
79         $user = $_SESSION['authUser'];
80     }
82     if (!$group) {
83         $group = $_SESSION['authProvider'];
84     }
86     if ($therapy_group == 'not_given') {
87         $therapy_group = $attendant_type == 'pid' ? null : $_SESSION['therapy_group'];
88     }
90     //print_r($_SESSION['therapy_group']);die;
91         $arraySqlBind = array();
92     $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
93         "user, groupname, authorized, formdir, therapy_group_id) values (";
94     if ($date == "NOW()") {
95         $sql .= "$date";
96     } else {
97         $sql .= "?";
98                 array_push($arraySqlBind, $date);
99     }
101     $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?, ?)";
102         array_push($arraySqlBind, $encounter, $form_name, $form_id, $pid, $user, $group, $authorized, $formdir, $therapy_group);
103     return sqlInsert($sql, $arraySqlBind);
106 function authorizeForm($id, $authorized = "1")
108     sqlQuery("update forms set authorized = '$authorized' where id = '$id'");
111 function getEncounters($pid, $dateStart = '', $dateEnd = '', $encounterRuleType = '')
114         $arraySqlBind = array();
116     if ($encounterRuleType) {
117         // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
118         $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
119         $where = "enc_category_map.rule_enc_id = ? and ";
120         array_push($arraySqlBind, $encounterRuleType);
121     } else {
122         // Collect all encounters
123         $from = "form_encounter";
124     }
126     if ($dateStart && $dateEnd) {
127         $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
128         array_push($arraySqlBind, $pid, $dateStart, $dateEnd);
129     } else if ($dateStart && !$dateEnd) {
130         $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
131         array_push($arraySqlBind, $pid, $dateStart);
132     } else if (!$dateStart && $dateEnd) {
133         $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
134         array_push($arraySqlBind, $pid, $dateEnd);
135     } else {
136         $where .= "form_encounter.pid = ?";
137         array_push($arraySqlBind, $pid);
138     }
140         $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
142     for ($iter=0; $row=sqlFetchArray($res); $iter++) {
143         $all[$iter] = $row;
144     }
146         return $all;
149 function getEncounterDateByEncounter($encounter)
151     global $attendant_type;
152     $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
153     // $sql = "select date from forms where encounter='$encounter' order by date";
154     $sql = "select date from $table where encounter='$encounter' order by date";
155     return sqlQuery($sql);
158 function getProviderIdOfEncounter($encounter)
160         global $attendant_type;
161         $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
162         $sql = "select provider_id from $table where encounter='$encounter' order by date";
163         $res = sqlQuery($sql);
164         return $res['provider_id'];
167 function getFormNameByFormdirAndFormid($formdir, $form_id)
169         return sqlQuery("select form_name from forms where formdir='$formdir' and form_id='$form_id'");
172 function getFormIdByFormdirAndFormid($formdir, $form_id)
174     $result = sqlQuery("select id from forms where formdir = ? and form_id =? ", array( $formdir, $form_id ));
175     return $result['id'];
178 function getFormNameByFormdir($formdir)
180     return sqlQuery("select form_name from forms where formdir='$formdir'");
183 function getDocumentsByEncounter($patientID = null, $encounterID = null)
185     $allDocuments = null;
186     $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
187     $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
189     if ($currentPatient != "" && $currentEncounter != "") {
190         $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,
191                         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";
192         $res = sqlStatement($sql, array($currentPatient,$currentEncounter));
194         while ($row = sqlFetchArray($res)) {
195             $allDocuments[] = $row;
196         }
197     }
199     return $allDocuments;