3 require_once("{$GLOBALS['srcdir']}/sql.inc");
5 $GLOBALS['form_exit_url'] = $GLOBALS['concurrent_layout'] ?
6 "$rootdir/patient_file/encounter/encounter_top.php" :
7 "$rootdir/patient_file/encounter/patient_encounter.php";
9 function getFormById ($id, $cols = "*")
11 $sql = "select $cols from forms where id='$id' order by date DESC limit 0,1";
12 //echo $sql . "<br />";
13 return sqlQuery($sql);
16 function getFormInfoById ($id, $cols = "*")
18 $sql = "select $cols from forms where id='$id' order by date DESC limit 0,1";
19 //echo $sql . "<br />";
20 $result = sqlQuery($sql);
21 if ($result['formdir'] == "newpatient") {
22 $result['formdir'] = "encounter";
24 $sql = "select * from form_" . $result['formdir'] . " where id='" . $result['form_id'] . "'";
25 //echo $sql . "<br />";
26 $result = sqlQuery($sql);
31 function getFormsByPid ($pid, $cols = "*")
33 return sqlQuery("select $cols from forms where pid ='$pid'");
36 function getFormByEncounter($pid,$encounter, $cols="form_id, form_name", $name="")
38 $arraySqlBind = array();
39 $sql = "select $cols from forms where encounter = ? and pid=? ";
40 array_push($arraySqlBind,$encounter,$pid);
42 $sql .= "and form_name=? ";
43 array_push($arraySqlBind,$name);
45 // This puts vitals first in the list, and newpatient last:
46 $sql .= "ORDER BY FIND_IN_SET(formdir,'vitals') DESC, date DESC";
48 $res = sqlStatement($sql,$arraySqlBind);
50 for($iter=0; $row=sqlFetchArray($res); $iter++)
57 function addForm($encounter, $form_name, $form_id, $formdir, $pid,
58 $authorized = "0", $date="NOW()", $user="", $group="")
60 if (!$user) $user = $_SESSION['authUser'];
61 if (!$group) $group = $_SESSION['authProvider'];
63 $arraySqlBind = array();
64 $sql = "insert into forms (date, encounter, form_name, form_id, pid, " .
65 "user, groupname, authorized, formdir) values (";
66 if($date == "NOW()") {
71 array_push($arraySqlBind,$date);
73 $sql .= ", ?, ?, ?, ?, ?, ?, ?, ?)";
74 array_push($arraySqlBind,$encounter,$form_name,$form_id,$pid,$user,$group,$authorized,$formdir);
75 return sqlInsert($sql,$arraySqlBind);
78 function authorizeForm($id, $authorized = "1")
80 sqlQuery("update forms set authorized = '$authorized' where id = '$id'");
83 function getEncounters($pid,$dateStart='',$dateEnd='',$encounterRuleType='')
86 $arraySqlBind = array();
88 if ($encounterRuleType) {
89 // Only collect certain type of encounters (list_options item from the rule_enc_types list that is mapped via enc_category_map table)
90 $from = "form_encounter LEFT JOIN enc_category_map ON (form_encounter.pc_catid = enc_category_map.main_cat_id)";
91 $where = "enc_category_map.rule_enc_id = ? and ";
92 array_push($arraySqlBind,$encounterRuleType);
95 // Collect all encounters
96 $from = "form_encounter";
99 if ($dateStart && $dateEnd) {
100 $where .= "form_encounter.pid = ? and form_encounter.date >= ? and form_encounter.date <= ?";
101 array_push($arraySqlBind,$pid,$dateStart,$dateEnd);
103 else if ($dateStart && !$dateEnd) {
104 $where .= "form_encounter.pid = ? and form_encounter.date >= ?";
105 array_push($arraySqlBind,$pid,$dateStart);
107 else if (!$dateStart && $dateEnd) {
108 $where .= "form_encounter.pid = ? and form_encounter.date <= ?";
109 array_push($arraySqlBind,$pid,$dateEnd);
112 $where .= "form_encounter.pid = ?";
113 array_push($arraySqlBind,$pid);
116 $res = sqlStatement("SELECT distinct encounter FROM $from WHERE $where ORDER by date desc;", $arraySqlBind);
118 for($iter=0; $row=sqlFetchArray($res); $iter++)
125 function getEncounterDateByEncounter($encounter)
127 // $sql = "select date from forms where encounter='$encounter' order by date";
128 $sql = "select date from form_encounter where encounter='$encounter' order by date";
129 return sqlQuery($sql);
132 function getProviderIdOfEncounter($encounter)
134 $sql = "select provider_id from form_encounter where encounter='$encounter' order by date";
135 $res = sqlQuery($sql);
136 return $res['provider_id'];
139 function getFormNameByFormdirAndFormid ($formdir,$form_id)
141 return sqlQuery("select form_name from forms where formdir='$formdir' and form_id='$form_id'");
144 function getFormIdByFormdirAndFormid ($formdir,$form_id)
146 $result = sqlQuery("select id from forms where formdir = ? and form_id =? ", array( $formdir, $form_id ) );
147 return $result['id'];
150 function getFormNameByFormdir ($formdir)
152 return sqlQuery("select form_name from forms where formdir='$formdir'");
155 function getDocumentsByEncounter($patientID = null,$encounterID = null) {
156 $allDocuments = null;
157 $currentEncounter = ( $encounterID ) ? $encounterID : $_SESSION['encounter'];
158 $currentPatient = ( $patientID ) ? $patientID : $_SESSION['pid'];
160 if($currentPatient != "" && $currentEncounter != "") {
161 $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,
162 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";
163 $res = sqlStatement($sql,array($currentPatient,$currentEncounter));
165 while ( $row = sqlFetchArray($res) ) {
166 $allDocuments[] = $row;
169 return $allDocuments;