forked from dcbradley/building-access
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy.php
317 lines (284 loc) · 11.6 KB
/
policy.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
<?php
function getHoursScheduled($request,$room) {
$dbh = connectDB();
$sql = "SELECT SUM(TIME_TO_SEC(TIMEDIFF(END_TIME,START_TIME))) as SECONDS FROM building_access WHERE NETID = :NETID AND ROOM REGEXP :ROOM_REGEXP AND BUILDING = :BUILDING AND START_TIME >= :START_TIME AND START_TIME < :END_TIME AND (APPROVED = 'Y' OR ID = :ID)";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(":NETID",$request["NETID"]);
$stmt->bindValue(":ROOM_REGEXP","(^|, )$room(, |\$)");
$stmt->bindValue(":BUILDING",$request["BUILDING"]);
$stmt->bindValue(":ID",$request["ID"]);
$weekday = (int)date("w",strtotime($request["START_TIME"]));
$week_start = date_create(date("Y-m-d",strtotime($request["START_TIME"])));
date_sub($week_start,date_interval_create_from_date_string("$weekday days"));
$week_end = clone $week_start;
date_add($week_end,date_interval_create_from_date_string('7 days'));
$week_start = date_format($week_start,'Y-m-d H:i');
$week_end = date_format($week_end,'Y-m-d H:i');
$stmt->bindValue(":START_TIME",$week_start);
$stmt->bindValue(":END_TIME",$week_end);
$stmt->execute();
$row = $stmt->fetch();
$hours = $row[0]/3600.0;
return $hours;
}
function checkAutoApproval(&$why_not_approved,&$warnings,$id,&$request) {
$request = loadRequest($id);
if( !$request ) return false;
if( $request['APPROVED'] == 'Y' ) return true;
$approvals = getApprovals($request['DEPARTMENT']);
if( !$approvals ) {
return false;
}
$request_day = getDayChar($request["START_TIME"]);
$rooms = explode(", ",$request["ROOM"]);
$approved_rooms = array();
list($first,$last) = splitName($request["NAME"]);
foreach( $approvals as $approval ) {
if( array_key_exists("NETID",$approval) && $approval["NETID"] ) {
if( $approval["NETID"] != $request["NETID"] && $approval["NETID"] != "*" ) {
continue;
}
}
else if( array_key_exists("FIRST",$approval) && array_key_exists("LAST",$approval) && $approval["LAST"] ) {
if( !nameMatches($first,$last,$approval["FIRST"],$approval["LAST"]) && !($approval["FIRST"] == "*" && $approval["LAST"] == "*")) {
continue;
}
}
else {
continue;
}
foreach( $rooms as $room ) {
$room = canonicalRoom($room,$request["BUILDING"]);
$room_descr = $room . " " . buildingAbbreviation($request["BUILDING"]);
foreach( $approval["ROOM"] as $room_building ) {
if( ($room_building[0] == $room || $room_building[0] == "*") && ($room_building[1] == $request["BUILDING"] || !$room_building[1]) ) {
if( array_key_exists("HOURS",$approval) && $approval["HOURS"] != "" ) {
$hours_used = getHoursScheduled($request,$room);
if( $hours_used > 1.0*$approval["HOURS"] ) {
$why_not_approved[] = "This would exceed the weekly time allotment of " . $approval["HOURS"] . " hours in {$room_descr} (would be " . round($hours_used,2) . " hours used).";
continue;
}
}
if( array_key_exists("DAYS",$approval) && $approval["DAYS"] && strpos($approval["DAYS"],$request_day) === false ) {
$why_not_approved[] = date("l",strtotime($request["START_TIME"])) . " is not on the list of pre-approved days in {$room_descr}: " . implode_and(listFullWeekdayNames($approval["DAYS"])) . ".";
continue;
}
$approved_rooms[] = $room;
}
}
}
}
$unapproved_rooms = array();
foreach( $rooms as $room ) {
if( !in_array($room,$approved_rooms) ) {
$unapproved_rooms[] = $room;
}
}
$result = true;
if( count($unapproved_rooms) ) {
if( count($why_not_approved)==0 ) {
$plural_rooms = count($unapproved_rooms)>1 ? "rooms" : "room";
$why_not_approved[] = "No pre-approval for $plural_rooms " . implode(", ",$unapproved_rooms) . " was found.";
}
$result = false;
}
return $result;
}
function checkAutoApprovalAndRoomCap(&$why_not_approved,&$warnings,$id,&$request) {
$result = true;
if( !checkAutoApproval($why_not_approved,$warnings,$id,$request) ) {
$result = false;
}
if( !checkRoomCaps($why_not_approved,$warnings,$request) ) {
$result = false;
}
return $result;
}
function checkRoomCaps(&$why_not_approved,&$warnings,$request) {
$privacy_sql = USER_SETTABLE_PRIVACY ? "PRIVACY," : "";
$dbh = connectDB();
$sql = "
SELECT
START_TIME, END_TIME, NAME, NETID, ROOM, BUILDING, {$privacy_sql} DEPARTMENT
FROM building_access
WHERE
START_TIME < :END_TIME
AND END_TIME > :START_TIME
AND NETID <> :NETID
AND APPROVED = 'Y'
AND BUILDING = :BUILDING
AND ROOM REGEXP :ROOM_REGEXP
";
$overlap_stmt = $dbh->prepare($sql);
$overlap_stmt->bindValue(':NETID',$request['NETID']);
$overlap_stmt->bindValue(':START_TIME',$request['START_TIME']);
$overlap_stmt->bindValue(':END_TIME',$request['END_TIME']);
$overlap_stmt->bindValue(':BUILDING',$request['BUILDING']);
$result = true;
if( !checkFloorCaps($why_not_approved,$warnings,$request) ) {
$result = false;
}
$people_already_counted = array($request["NAME"] => 1);
$rooms = explode(", ",$request["ROOM"]);
foreach( $rooms as $room ) {
list($normcap,$maxcap,$description) = getRoomCap($room,$request['BUILDING']);
$overlap_stmt->bindValue(":ROOM_REGEXP","(^|, )$room(, |\$)");
$overlap_stmt->execute();
$possible_overlaps = array();
while( ($row=$overlap_stmt->fetch()) ) {
$possible_overlaps[] = $row;
}
$overlaps = getPeakOverlap($possible_overlaps,$request['START_TIME'],$request['END_TIME'],$people_already_counted);
$peak_cap = 1 + count($overlaps);
if( $peak_cap > $maxcap ) {
$result = false;
$why_not_approved[] = "Room " . $room . " would have {$peak_cap} people in it simultaneously, which exceeds the agreed limit of {$maxcap}. " . conflictDesc($overlaps);
}
else if( $peak_cap > $normcap ) {
$room_desc = "";
if( $description ) $room_desc = " Notes on this room: " . $description;
$warnings[] = "Be aware that room " . $room . " will have {$peak_cap} people in it simultaneously. " . conflictDesc($overlaps) . $room_desc;
}
}
return $result;
}
function checkFloorCaps(&$why_not_approved,&$warnings,$request) {
if( !BUILDING_FLOOR_MAX_CAP || !array_key_exists($request['BUILDING'],BUILDING_FLOOR_MAX_CAP) ) return true;
$privacy_sql = USER_SETTABLE_PRIVACY ? "PRIVACY," : "";
$dbh = connectDB();
$sql = "
SELECT
START_TIME, END_TIME, NAME, NETID, ROOM, BUILDING, {$privacy_sql} DEPARTMENT
FROM building_access
WHERE
START_TIME < :END_TIME
AND END_TIME > :START_TIME
AND NETID <> :NETID
AND APPROVED = 'Y'
AND BUILDING = :BUILDING
AND ROOM REGEXP :FLOOR_REGEXP
";
$overlap_stmt = $dbh->prepare($sql);
$overlap_stmt->bindValue(':NETID',$request['NETID']);
$overlap_stmt->bindValue(':START_TIME',$request['START_TIME']);
$overlap_stmt->bindValue(':END_TIME',$request['END_TIME']);
$overlap_stmt->bindValue(':BUILDING',$request['BUILDING']);
$result = true;
$rooms = explode(", ",$request["ROOM"]);
$floors = array();
foreach( $rooms as $room ) {
$floor = getFloor($room);
if( !in_array($floor,$floors) ) {
$floors[] = $floor;
}
}
$people_already_counted = array($request["NAME"] => 1);
foreach( $floors as $floor ) {
$floorcap = getFloorCap($floor,$request['BUILDING']);
if( !$floorcap ) continue;
list($normcap,$maxcap,$description) = $floorcap;
$overlap_stmt->bindValue(":FLOOR_REGEXP","(^|, ){$floor}[^,]*(, |\$)");
$overlap_stmt->execute();
$possible_overlaps = array();
while( ($row=$overlap_stmt->fetch()) ) {
$possible_overlaps[] = $row;
}
$overlaps = getPeakOverlap($possible_overlaps,$request['START_TIME'],$request['END_TIME'],$people_already_counted);
$peak_cap = 1 + count($overlaps);
if( $peak_cap > $maxcap ) {
$result = false;
$why_not_approved[] = "Floor " . $floor . " would have {$peak_cap} people in it simultaneously, which exceeds the agreed limit of {$maxcap}. " . conflictDesc($overlaps);
}
else if( $peak_cap > $normcap ) {
$floor_desc = "";
if( $description ) $floor_desc = " Notes on this floor: " . $description;
$warnings[] = "Be aware that floor " . $floor . " will have {$peak_cap} people in it simultaneously. " . conflictDesc($overlaps) . $floor_desc;
}
}
return $result;
}
function conflictDesc($overlaps) {
$overlap_names = array();
$invisible_occupants = 0;
foreach( $overlaps as $overlap ) {
if( isVisible($overlap['NETID'],$overlap['ROOM'],$overlap['BUILDING'],$overlap['DEPARTMENT'],getPrivacy($overlap)) ) {
$overlap_names[] = $overlap['NAME'];
} else {
$invisible_occupants += 1;
}
}
if( $invisible_occupants > 0 ) {
$overlap_names[] = "{$invisible_occupants} anonymous occupant" . ($invisible_occupants > 1 ? "s" : "");
}
$conflicts = implode_and($overlap_names);
$s_are = count($overlap_names)==1 ? " is" : "s are";
return "The other registration{$s_are} from " . $conflicts . ".";
}
function getPeakOverlap($possible_overlaps,$start_time,$end_time,$people_already_counted) {
$peak_overlaps = array();
foreach( $possible_overlaps as &$overlap1 ) {
if( array_key_exists($overlap1["NAME"],$people_already_counted) ) {
continue;
}
$start_time1 = max($start_time,$overlap1['START_TIME']);
$end_time1 = min($end_time,$overlap1['END_TIME']);
$other_possible_overlaps = array();
foreach( $possible_overlaps as &$overlap2 ) {
if( $overlap1 == $overlap2 ) continue;
if( $overlap2['START_TIME'] < $end_time1 && $overlap2['END_TIME'] > $start_time1 ) {
$other_possible_overlaps[] = $overlap2;
}
}
$people_already_counted[$overlap1["NAME"]] = 1;
$overlaps = getPeakOverlap($other_possible_overlaps,$start_time1,$end_time1,$people_already_counted);
unset($people_already_counted[$overlap1["NAME"]]);
if( count($overlaps)+1 > count($peak_overlaps) ) {
$peak_overlaps = array($overlap1);
foreach( $overlaps as $overlap ) {
$peak_overlaps[] = $overlap;
}
}
}
return $peak_overlaps;
}
function doAutoApproval(&$why_not_approved,&$warnings,$id,&$request=null) {
if( !checkAutoApprovalAndRoomCap($why_not_approved,$warnings,$id,$request) ) {
return false;
}
if( $request['APPROVED'] == 'Y' ) return true;
$dbh = connectDB();
$sql = "UPDATE building_access SET APPROVED = 'Y', APPROVED_TIME = NOW() WHERE ID = :ID";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(":ID",$id);
$stmt->execute();
return true;
}
function doAutoApprovalWithRepeats(&$why_not_approved,&$warnings,$id,&$children_not_approved,&$child_warnings) {
$request = loadRequest($id);
if( !$request ) return;
$result = true;
if( !doAutoApproval($why_not_approved,$warnings,$id,$request) ) {
$result = false;
}
$children_not_approved = array();
$child_warnings = array();
if( $request['REPEAT_PARENT'] ) {
$dbh = connectDB();
$sql = "SELECT * from building_access WHERE REPEAT_PARENT = :REPEAT_PARENT AND START_TIME > :START_TIME AND NETID = :NETID";
$stmt = $dbh->prepare($sql);
$stmt->bindValue(':REPEAT_PARENT',$request['REPEAT_PARENT']);
$stmt->bindValue(':START_TIME',$request['START_TIME']);
$stmt->bindValue(':NETID',$request['NETID']);
$stmt->execute();
while( ($row=$stmt->fetch()) ) {
$why_child_not_approved = array();
$this_child_warnings = array();
if( !doAutoApproval($why_child_not_approved,$this_child_warnings,$row['ID'],$row) ) {
$children_not_approved[] = array($row,$why_child_not_approved);
} else if( count($this_child_warnings) ) {
$child_warnings[] = array($row,$this_child_warnings);
}
}
}
return $result;
}