another minor fix to prior commit
[openemr.git] / library / forms.inc
blob65279a016aca64c17b95abd64a332e6a5d94c09b
1 <?php
3 require_once("{$GLOBALS['srcdir']}/sql.inc");
5 $GLOBALS['form_exit_url'] = "$rootdir/patient_file/encounter/encounter_top.php";
7 function getFormById ($id, $cols = "*")
9         $sql = "select $cols from forms where id='$id' order by date DESC limit 0,1";
10         //echo $sql . "<br />";
11         return sqlQuery($sql);
14 function getFormInfoById ($id, $cols = "*")
16         $sql = "select $cols from forms where id='$id' order by date DESC limit 0,1";
17         //echo $sql . "<br />";
18         $result =  sqlQuery($sql);
19         if ($result['formdir'] == "newpatient") {
20                 $result['formdir'] = "encounter";
21         }
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($pid,$encounter, $cols="form_id, form_name", $name="")
36         $arraySqlBind = array();
37         $sql = "select $cols from forms where encounter = ? and pid=? ";
38         array_push($arraySqlBind,$encounter,$pid);
39         if(!empty($name)){
40                 $sql .= "and form_name=? ";
41                 array_push($arraySqlBind,$name);
42         }
43   // This puts vitals first in the list, and newpatient last:
44   $sql .= "ORDER BY FIND_IN_SET(formdir,'vitals') DESC, date DESC";
46         $res = sqlStatement($sql,$arraySqlBind);
48         for($iter=0; $row=sqlFetchArray($res); $iter++)
49         {
50                 $all[$iter] = $row;
51         }
52         return $all;
55 function addForm($encounter, $form_name, $form_id, $formdir, $pid,
56  $authorized = "0", $date="NOW()", $user="", $group="")
58         if (!$user) $user = $_SESSION['authUser'];
59         if (!$group) $group = $_SESSION['authProvider'];
61         $arraySqlBind = array();
62         $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
63                 "user, groupname, authorized, formdir) values (";
64         if($date == "NOW()") {
65                 $sql .= "$date";
66         }
67         else {
68                 $sql .= "?";
69                 array_push($arraySqlBind,$date);
70         }
71         $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?)";
72         array_push($arraySqlBind,$encounter,$form_name,$form_id,$pid,$user,$group,$authorized,$formdir);
73         return sqlInsert($sql,$arraySqlBind);
76 function authorizeForm($id, $authorized = "1")
78         sqlQuery("update forms set authorized = '$authorized' where id = '$id'");
81 function getEncounters($pid,$dateStart='',$dateEnd='',$encounterRuleType='')
84         $arraySqlBind = array();
85         
86         if ($encounterRuleType) {
87                 // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
88                 $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
89                 $where = "enc_category_map.rule_enc_id = ? and ";
90                 array_push($arraySqlBind,$encounterRuleType);
91         }
92         else {
93                 // Collect all encounters
94                 $from = "form_encounter";
95         }
97         if ($dateStart && $dateEnd) {
98                 $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
99                 array_push($arraySqlBind,$pid,$dateStart,$dateEnd);
100         }
101         else if ($dateStart && !$dateEnd) {
102                 $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
103                 array_push($arraySqlBind,$pid,$dateStart);
104         }
105         else if (!$dateStart && $dateEnd) {
106                 $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
107                 array_push($arraySqlBind,$pid,$dateEnd);
108         }
109         else {
110                 $where .= "form_encounter.pid = ?";
111                 array_push($arraySqlBind,$pid);
112         }
114         $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
116         for($iter=0; $row=sqlFetchArray($res); $iter++)
117         {
118                 $all[$iter] = $row;
119         }
120         return $all;
123 function getEncounterDateByEncounter($encounter)
125         // $sql = "select date from forms where encounter='$encounter' order by date";
126         $sql = "select date from form_encounter where encounter='$encounter' order by date";
127         return sqlQuery($sql);
130 function getProviderIdOfEncounter($encounter)
132         $sql = "select provider_id from form_encounter where encounter='$encounter' order by date";
133         $res = sqlQuery($sql);
134         return $res['provider_id'];
137 function getFormNameByFormdirAndFormid ($formdir,$form_id)
139         return sqlQuery("select form_name from forms where formdir='$formdir' and form_id='$form_id'");
142 function getFormIdByFormdirAndFormid ($formdir,$form_id)
144     $result = sqlQuery("select id from forms where formdir = ? and form_id =? ", array( $formdir, $form_id ) );
145     return $result['id'];
148 function getFormNameByFormdir ($formdir)
150         return sqlQuery("select form_name from forms where formdir='$formdir'");
153 function getDocumentsByEncounter($patientID = null,$encounterID = null) {
154         $allDocuments = null;
155         $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
156         $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
157         
158         if($currentPatient != "" && $currentEncounter != "") {
159                 $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, 
160                         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";
161                 $res = sqlStatement($sql,array($currentPatient,$currentEncounter));
162         
163                 while ( $row = sqlFetchArray($res) ) {
164                         $allDocuments[] = $row;
165                 }
166         }
167         return $allDocuments;