/
admin.class.php
332 lines (297 loc) · 9.79 KB
/
admin.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/* ================================================
Author: Rebecca Putinski
UWID: 20271463
Class: CS348 Section 3
This file:
API for the administration actions with
output intended for use with header.php/
footer.php.
================================================
*/
require_once('database.class.php');
class Admin {
private $dbh;
function __construct() {
try {
$this->dbh = new Database();
} catch( Exception $e ) {
die("Could not perform Admin action. Message: <br><br>".$e->getMessage());
}
}
private function isPatient( $p ) {
$q = "SELECT pid FROM Patients WHERE ";
if( is_numeric( $p ) )
$q .= "pid = ".$p;
else
$q .= "pname = '".$p."'";
return ($this->dbh->query($q));
}
private function isCheckedIn( $p ) {
$q = "SELECT cid FROM CheckInOuts WHERE pid = ".$p." AND outdate IS NULL";
return ($this->dbh->query($q) ? true : false);
}
private function UDTUpdate( $table, $data, $where=false ) {
$values = array(); $keys = array();
$UDTMagic = array(false,false);
$check = false; // Only execute update if at least one value is to be changed
$q = "UPDATE ".$table." SET ";
foreach( $data as $k=>$v ) {
if( $v == "" ) continue; // Values could be blank
$check = true;
switch( $k ) {
case 'contact_email':
if( !$UDTMagic[0] ) {
$keys[] = 'contact';
$values[] = "ContactInfo_t() ..email('".$v."')";
$UDTMagic[0] = true; // We've seen contact now
} else {
$values[] = " ..email('".$v."')";
}
break;
case 'contact_phone':
if( !$UDTMagic[0] ) {
$keys[] = 'contact';
$values[] = "ContactInfo_t() ..phone('".$v."')";
$UDTMagic[0] = true; // We've seen contact now
} else {
$values[] = " ..phone('".$v."')";
}
break;
case 'emerg_name':
if( !$UDTMagic[1] ) {
$keys[] = 'emergcontact';
$values[] = "EmergContact_t() ..ename('".$v."')";
$UDTMagic[1] = true; // We've seen emergcontact now
} else {
$values[] = " ..ename('".$v."')";
}
break;
case 'emerg_phone':
if( !$UDTMagic[1] ) {
$keys[] = 'emergcontact';
$values[] = "EmergContact_t() ..ephone('".$v."')";
$UDTMagic[1] = true;
} else {
$values[] = " ..ephone('".$v."')";
}
break;
default:
$keys[] = $k;
$values[] = "'".$v."'";
}
}
reset($keys); reset($values);
do {
$q .= current($keys)."=".current($values).",";
} while( next($keys) && next($values) );
$q = substr($q,0,-1); // Remove trailing comma
if( $where ) {
if( !array_key_exists('op',$where) ) $where['op'] = '=';
$q .= " WHERE ".$where['field']." ".$where['op']." '".$where['value']."'";
}
return (!$check || $this->dbh->directInsert($q));
}
private function UDTInsert( $table, $data ) {
$values = array(); $keys = array();
$data['dob'] = date('Y-m-d',strtotime($data['dob']));
$q = "INSERT INTO ".$table."(";
foreach( $data as $k=>$v ) {
switch( $k ) {
case 'contact_email':
$keys[] = 'contact';
$values[] = " ..email('".$v."'),";
break;
case 'contact_phone':
$values[] = "ContactInfo_t() ..phone('".$v."')";
break;
case 'emerg_name':
$keys[] = 'emergcontact';
$values[] = "EmergContact_t() ..ename('".$v."')";
break;
case 'emerg_phone':
$values[] = " ..ephone('".$v."'),";
break;
default:
$keys[] = $k;
$values[] = "'".$v."',";
}
}
reset($keys); reset($values);
$q .= current($keys);
while( $k = next($keys) )
$q .= ",".$k;
$q .= ") VALUES(";
$q .= current($values);
while( $v = next($values) )
$q .= $v;
$q = substr($q,0,-1);
$q .= ")";
return $this->dbh->directInsert($q);
}
public function addPatient( $data ) {
if( $this->isPatient($data['pname']) || !checkFilled($data) ||
!verifyDate($data['dob'])) return false;
return $this->UDTInsert('Patients',$data);
}
public function updatePatient( $data ) {
if( !$this->isPatient($data['pid']) || (isset($data['dob']) && !verifyDate($data['dob'])) )
return false;
$where = array('field'=>'pid','value'=>$data['pid']);
unset($data['pid']); // remove pid
return $this->UDTUpdate('Patients',$data,$where);
}
public function checkInPatient( $data ) {
if( !checkFilled($data) || !verifyDate($data['indate']) ||
!checkNumber($data['pid']) || !checkNumber($data['eidin']) ||
!$this->isPatient($data['pid']) )
return false;
if( $this->isCheckedIn($data['pid']) ) return true;
return ($this->dbh->insert('CheckInOuts',$data));
}
public function checkOutPatient( $data ) {
if( !$this->isPatient( $data['pid'] ) || !checkFilled($data) ||
!verifyDate($data['outdate']) || !checkNumber($data['eidout']) ||
!checkNumber($data['pid']) || !$this->isCheckedIn($data['pid']))
return false;
$where = array('field'=>'pid','value'=>$data['pid']);
// Add up costs of EDT records for that visit
$total = 0;
$t = new Treatment;
$edts = $t->getEDTRecords($data['pid'],true);
foreach( $edts as $e ) {
$total = $total + $e['cost'];
}
$data['totalbill'] = $total;
unset($data['pid']); // remove pid from elements
return ($this->dbh->update('CheckInOuts',$data,$where,true));
}
// Query functions
public function getAllPatients( $options=false ) {
$results = array();
if( $options && $options != 'current' )
$q = "SELECT P.pid, P.pname, C.indate, C.outdate, C.eidin, C.eidout FROM Patients P, CheckInOuts C";
else
$q = "SELECT pid,pname FROM Patients";
if( $options == 'current' ) {
$q .= " WHERE pid IN(" .
"SELECT pid FROM CheckInOuts WHERE outdate IS NULL) ";
} elseif ( $options ) {
$q .= " WHERE P.pid IN(" .
"SELECT pid FROM CheckInOuts WHERE outdate = '".$options."' OR indate = '".$options."')";
}
$q .= " ORDER BY pname DESC";
$result = $this->dbh->query($q);
if( $result ) {
if( !$options || $options == 'current' ) {
foreach( $result as $r )
$results[$r['pid']] = $r['pname'];
} else {
$results = $result;
}
}
return $results;
}
public function getCurrentPatients() {
return $this->getAllPatients( $options='current' );
}
public function getAllPhysicians() {
$results = array();
$q = "SELECT eid,ename FROM Employees WHERE eid IN(
SELECT eid FROM EmployeeJobs WHERE jid IN(
SELECT jid FROM Jobs WHERE jtype = 'C'))";
$result = $this->dbh->query($q);
if( $result ) {
foreach( $result as $r ) {
$results[$r['eid']] = $r['ename'];
}
}
return $results;
}
public function getActivityTypes() {
return array('E'=>"Examination",'D'=>"Diagnosis",'T'=>"Treatment");
}
public function getPatientsOnDate( $date ) {
return ($this->getAllPatients($date));
}
public function getPatientVisits( $pid ) {
$q = "SELECT P.pid,P.pname,C.indate,C.outdate,C.totalbill FROM Patients P, CheckInOuts C
WHERE P.pid = ".$pid;
return ($this->dbh->query($q));
}
public function getPhysiciansPatients( $eid ) {
$q = "SELECT P.pid, P.pname, C.indate, C.outdate FROM Patients P
JOIN CheckInOuts C ON P.pid = C.pid
WHERE C.eidin = ".$eid." OR
C.eidout = ".$eid;
return ($this->dbh->query($q));
}
// Display functions
private function display( $title, $headers, $data ) {
echo "<h2>".$title."</h2>";
if( !$data ) {
echo "<p class='notice'>Nothing to display.</p>";
return;
}
echo "<table>";
echo "<tr>";
foreach($headers as $h)
echo "<th>".$h."</th>";
echo "</tr>";
$moo = 0;
foreach( $data as $record ) {
echo "<tr".($moo % 2 ? "class='odd'" : "").">";
foreach( $record as $k=>$field ) {
echo "<td>".$field."</td>";
}
echo "</tr>";
$moo++;
}
echo "</table>";
}
public function displayPatientsOnDate( $date ) {
$allPhysicians = $this->getAllPhysicians();
$data = $this->getPatientsOnDate($date);
$title = "Patients Admitted/Released on ".$date;
$headers = array('Patient ID','Name','Physician','Action');
$adjusted = array();
foreach( $data as $record ) {
$record['physician'] = ($record['indate'] ? $allPhysicians[$record['eidin']] : $allPhysicians[$record['eidin']]);
$record['action'] = ($record['indate'] ? "Admitted" : "Released");
unset($record['indate'],$record['outdate'],$record['eidin'],$record['eidout']);
$adjusted[] = $record;
}
$this->display($title,$headers,$adjusted);
}
public function displayPatientVisits( $pid ) {
$data = $this->getPatientVisits($pid);
$title = "Patient Visits for ".($data ? $data[0]['pname'] : "N/A");
$headers = array('ID','Name','Admittance Date','Release Date','Billing Total');
$adjusted = array();
foreach( $data as $record ) {
$record['indate'] = date('m/d/Y',strtotime($record['indate']));
if($record['outdate'] != '')
$record['outdate'] = date('m/d/Y',strtotime($record['outdate']));
else
$record['outdate'] = "N/A";
$record['totalbill'] = "$".$record['totalbill'];
$adjusted[] = $record;
}
$this->display($title,$headers,$adjusted);
}
public function displayPhysiciansPatients( $eid ) {
$allPhysicians = $this->getAllPhysicians();
$data = $this->getPhysiciansPatients($eid);
$title = "Patients Admitted/Released by ".$allPhysicians[$eid];
$headers = array('Patient ID','Name','Date','Action');
$adjusted = array();
foreach( $data as $record ) {
$record['date'] = ($record['indate'] ? date('m/d/Y',strtotime($record['indate'])) : date('m/d/Y',strtotime($record['outdate'])));
$record['action'] = ($record['indate'] ? "Admitted" : "Released");
unset($record['indate'],$record['outdate']);
$adjusted[] = $record;
}
$this->display($title,$headers,$adjusted);
}
}
?>