Improved Code Sniffing (#928)
[openemr.git] / library / forms.inc
blobb77d1d18dd7e40db3e3e04a48a7ed334d157f09f
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     }
21     $sql = "select * from form_" . $result['formdir'] . " where id='" . $result['form_id']  . "'";
22     //echo $sql . "<br />";
23     $result =  sqlQuery($sql);
24     //print_r($result);
25     return $result;
28 function getFormsByPid($pid, $cols = "*")
30     return sqlQuery("select $cols from forms where pid ='$pid'");
33 function getFormByEncounter($attendant_id,$encounter, $cols="form_id, form_name", $name="")
35     global $attendant_type;
36     $arraySqlBind = array();
37     $sql = "select $cols from forms where encounter = ? ";
38     array_push($arraySqlBind,$encounter);
39     if(!empty($name)){
40         $sql .= "and form_name=? ";
41         array_push($arraySqlBind,$name);
42     }
43     if($attendant_type == 'pid'){
44         $sql .= " and pid=? and therapy_group_id IS NULL ";
45     }
46     else{
47         $sql .= " and therapy_group_id = ? and pid IS NULL ";
48     }
49     array_push($arraySqlBind,$attendant_id);
50   // This puts vitals first in the list, and newpatient last:
51     $sql .= "ORDER BY FIND_IN_SET(formdir,'vitals') DESC, date DESC";
53     $res = sqlStatement($sql,$arraySqlBind);
55     for($iter=0; $row=sqlFetchArray($res); $iter++)
56     {
57         $all[$iter] = $row;
58     }
59     return $all;
62 function addForm(
63     $encounter,
64     $form_name,
65     $form_id,
66     $formdir,
67     $pid,
68     $authorized = "0",
69     $date="NOW()",
70     $user="",
71     $group="",
72     $therapy_group = 'not_given'
73 ) {
75     global $attendant_type;
76     if (!$user) $user = $_SESSION['authUser'];
77     if (!$group) $group = $_SESSION['authProvider'];
79     if($therapy_group == 'not_given'){
80         $therapy_group = $attendant_type == 'pid' ? null : $_SESSION['therapy_group'];
81     }
82     //print_r($_SESSION['therapy_group']);die;
83         $arraySqlBind = array();
84     $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
85         "user, groupname, authorized, formdir, therapy_group_id) values (";
86     if($date == "NOW()") {
87         $sql .= "$date";
88     }
89     else {
90         $sql .= "?";
91                 array_push($arraySqlBind,$date);
92     }
93     $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?, ?)";
94         array_push($arraySqlBind,$encounter,$form_name,$form_id,$pid,$user,$group,$authorized,$formdir,$therapy_group);
95     return sqlInsert($sql,$arraySqlBind);
98 function authorizeForm($id, $authorized = "1")
100     sqlQuery("update forms set authorized = '$authorized' where id = '$id'");
103 function getEncounters($pid,$dateStart='',$dateEnd='',$encounterRuleType='')
106         $arraySqlBind = array();
108     if ($encounterRuleType) {
109         // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
110         $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
111         $where = "enc_category_map.rule_enc_id = ? and ";
112         array_push($arraySqlBind,$encounterRuleType);
113     }
114     else {
115         // Collect all encounters
116         $from = "form_encounter";
117     }
119     if ($dateStart && $dateEnd) {
120         $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
121         array_push($arraySqlBind,$pid,$dateStart,$dateEnd);
122     }
123     else if ($dateStart && !$dateEnd) {
124         $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
125         array_push($arraySqlBind,$pid,$dateStart);
126     }
127     else if (!$dateStart && $dateEnd) {
128         $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
129         array_push($arraySqlBind,$pid,$dateEnd);
130     }
131     else {
132         $where .= "form_encounter.pid = ?";
133         array_push($arraySqlBind,$pid);
134     }
136         $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
138     for($iter=0; $row=sqlFetchArray($res); $iter++)
139         {
140         $all[$iter] = $row;
141     }
142         return $all;
145 function getEncounterDateByEncounter($encounter)
147     global $attendant_type;
148     $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
149     // $sql = "select date from forms where encounter='$encounter' order by date";
150     $sql = "select date from $table where encounter='$encounter' order by date";
151     return sqlQuery($sql);
154 function getProviderIdOfEncounter($encounter)
156         global $attendant_type;
157         $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
158         $sql = "select provider_id from $table where encounter='$encounter' order by date";
159         $res = sqlQuery($sql);
160         return $res['provider_id'];
163 function getFormNameByFormdirAndFormid($formdir,$form_id)
165         return sqlQuery("select form_name from forms where formdir='$formdir' and form_id='$form_id'");
168 function getFormIdByFormdirAndFormid($formdir,$form_id)
170     $result = sqlQuery("select id from forms where formdir = ? and form_id =? ", array( $formdir, $form_id ) );
171     return $result['id'];
174 function getFormNameByFormdir($formdir)
176     return sqlQuery("select form_name from forms where formdir='$formdir'");
179 function getDocumentsByEncounter($patientID = null,$encounterID = null)
181     $allDocuments = null;
182     $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
183     $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
185     if($currentPatient != "" && $currentEncounter != "") {
186         $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,
187                         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";
188         $res = sqlStatement($sql,array($currentPatient,$currentEncounter));
190         while ( $row = sqlFetchArray($res) ) {
191             $allDocuments[] = $row;
192         }
193     }
194     return $allDocuments;