Merge pull request #852 from bradymiller/dygraphs_2
[openemr.git] / library / forms.inc
blobfb4d65dd2d9204fe87f52299e751d1ad2f1db4d2
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($encounter, $form_name, $form_id, $formdir, $pid,
63  $authorized = "0", $date="NOW()", $user="", $group="", $therapy_group = 'not_given')
65         global $attendant_type;
66         if (!$user) $user = $_SESSION['authUser'];
67         if (!$group) $group = $_SESSION['authProvider'];
69         if($therapy_group == 'not_given'){
70         $therapy_group = $attendant_type == 'pid' ? NULL : $_SESSION['therapy_group'];
71     }
72         //print_r($_SESSION['therapy_group']);die;
73         $arraySqlBind = array();
74         $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
75                 "user, groupname, authorized, formdir, therapy_group_id) values (";
76         if($date == "NOW()") {
77                 $sql .= "$date";
78         }
79         else {
80                 $sql .= "?";
81                 array_push($arraySqlBind,$date);
82         }
83         $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?, ?)";
84         array_push($arraySqlBind,$encounter,$form_name,$form_id,$pid,$user,$group,$authorized,$formdir,$therapy_group);
85         return sqlInsert($sql,$arraySqlBind);
88 function authorizeForm($id, $authorized = "1")
90         sqlQuery("update forms set authorized = '$authorized' where id = '$id'");
93 function getEncounters($pid,$dateStart='',$dateEnd='',$encounterRuleType='')
96         $arraySqlBind = array();
98         if ($encounterRuleType) {
99                 // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
100                 $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
101                 $where = "enc_category_map.rule_enc_id = ? and ";
102                 array_push($arraySqlBind,$encounterRuleType);
103         }
104         else {
105                 // Collect all encounters
106                 $from = "form_encounter";
107         }
109         if ($dateStart && $dateEnd) {
110                 $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
111                 array_push($arraySqlBind,$pid,$dateStart,$dateEnd);
112         }
113         else if ($dateStart && !$dateEnd) {
114                 $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
115                 array_push($arraySqlBind,$pid,$dateStart);
116         }
117         else if (!$dateStart && $dateEnd) {
118                 $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
119                 array_push($arraySqlBind,$pid,$dateEnd);
120         }
121         else {
122                 $where .= "form_encounter.pid = ?";
123                 array_push($arraySqlBind,$pid);
124         }
126         $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
128         for($iter=0; $row=sqlFetchArray($res); $iter++)
129         {
130                 $all[$iter] = $row;
131         }
132         return $all;
135 function getEncounterDateByEncounter($encounter)
137         global $attendant_type;
138         $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
139         // $sql = "select date from forms where encounter='$encounter' order by date";
140         $sql = "select date from $table where encounter='$encounter' order by date";
141         return sqlQuery($sql);
144 function getProviderIdOfEncounter($encounter)
146                 global $attendant_type;
147                 $table = $attendant_type == 'pid' ? 'form_encounter' : 'form_groups_encounter';
148         $sql = "select provider_id from $table where encounter='$encounter' order by date";
149         $res = sqlQuery($sql);
150         return $res['provider_id'];
153 function getFormNameByFormdirAndFormid ($formdir,$form_id)
155         return sqlQuery("select form_name from forms where formdir='$formdir' and form_id='$form_id'");
158 function getFormIdByFormdirAndFormid ($formdir,$form_id)
160     $result = sqlQuery("select id from forms where formdir = ? and form_id =? ", array( $formdir, $form_id ) );
161     return $result['id'];
164 function getFormNameByFormdir ($formdir)
166         return sqlQuery("select form_name from forms where formdir='$formdir'");
169 function getDocumentsByEncounter($patientID = null,$encounterID = null) {
170         $allDocuments = null;
171         $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
172         $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
174         if($currentPatient != "" && $currentEncounter != "") {
175                 $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,
176                         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";
177                 $res = sqlStatement($sql,array($currentPatient,$currentEncounter));
179                 while ( $row = sqlFetchArray($res) ) {
180                         $allDocuments[] = $row;
181                 }
182         }
183         return $allDocuments;