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
|
<?php
/**
* Check if a shift collides with other shifts (in time).
* @param Shift $shift
* @param array<Shift> $shifts
*/
function Shift_collides($shift, $shifts) {
foreach ($shifts as $other_shift) {
if ($shift['SID'] != $other_shift['SID']) {
if (! ($shift['start'] >= $other_shift['end'] || $shift['end'] <= $other_shift['start'])) {
return true;
}
}
}
return false;
}
/**
* Check if an angel can sign up for given shift.
*
* @param Shift $shift
* @param AngelType $angeltype
* @param array<Shift> $user_shifts
*/
function Shift_signup_allowed($shift, $angeltype, $user_angeltype = null, $user_shifts = null) {
global $user, $privileges;
if ($user_shifts == null) {
$user_shifts = Shifts_by_user($user);
if ($user_shifts === false) {
engelsystem_error('Unable to load users shifts.');
}
}
$collides = Shift_collides($shift, $user_shifts);
if ($user_angeltype == null) {
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
if ($user_angeltype === false) {
engelsystem_error('Unable to load user angeltype.');
}
}
$signed_up = false;
foreach ($user_shifts as $user_shift) {
if ($user_shift['SID'] == $shift['SID']) {
$signed_up = true;
break;
}
}
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
if ($needed_angeltypes === false) {
engelsystem_error('Unable to load needed angel types.');
}
// is the shift still running or alternatively is the user shift admin?
$user_may_join_shift = true;
// you canot join if shift is full
foreach ($needed_angeltypes as $needed_angeltype) {
if ($needed_angeltype['angel_type_id'] == $angeltype['id']) {
if ($needed_angeltype['taken'] >= $needed_angeltype['count'])
$user_may_join_shift = false;
break;
}
}
// you cannot join if user alread joined a parallel or this shift
$user_may_join_shift &= ! $collides;
// you cannot join if you already singed up for this shift
$user_may_join_shift &= ! $signed_up;
// you cannot join if user is not of this angel type
$user_may_join_shift &= $user_angeltype != null;
// you cannot join if you are not confirmed
if ($angeltype['restricted'] == 1 && $user_angeltype != null) {
$user_may_join_shift &= isset($user_angeltype['confirm_user_id']);
}
// you can only join if the shift is in future
$user_may_join_shift &= time() < $shift['start'];
// User shift admins may join anybody in every shift
$user_may_join_shift |= in_array('user_shifts_admin', $privileges);
return $user_may_join_shift;
}
/**
* Delete a shift by its external id.
*/
function Shift_delete_by_psid($shift_psid) {
return sql_query("DELETE FROM `Shifts` WHERE `PSID`='" . sql_escape($shift_psid) . "'");
}
/**
* Delete a shift.
*/
function Shift_delete($shift_id) {
mail_shift_delete(Shift($shift_id));
return sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'");
}
/**
* Update a shift.
*/
function Shift_update($shift) {
global $user;
$shift['name'] = ShiftType($shift['shifttype_id'])['name'];
mail_shift_change(Shift($shift['SID']), $shift);
return sql_query("UPDATE `Shifts` SET
`shifttype_id`='" . sql_escape($shift['shifttype_id']) . "',
`start`='" . sql_escape($shift['start']) . "',
`end`='" . sql_escape($shift['end']) . "',
`RID`='" . sql_escape($shift['RID']) . "',
`title`=" . sql_null($shift['title']) . ",
`URL`=" . sql_null($shift['URL']) . ",
`PSID`=" . sql_null($shift['PSID']) . ",
`edited_by_user_id`='" . sql_escape($user['UID']) . "',
`edited_at_timestamp`=" . time() . "
WHERE `SID`='" . sql_escape($shift['SID']) . "'");
}
/**
* Update a shift by its external id.
*/
function Shift_update_by_psid($shift) {
$shift_source = sql_select("SELECT `SID` FROM `Shifts` WHERE `PSID`=" . $shift['PSID']);
if ($shift_source === false) {
return false;
}
if (count($shift_source) == 0) {
return null;
}
$shift['SID'] = $shift_source[0]['SID'];
return Shift_update($shift);
}
/**
* Create a new shift.
*
* @return new shift id or false
*/
function Shift_create($shift) {
global $user;
$result = sql_query("INSERT INTO `Shifts` SET
`shifttype_id`='" . sql_escape($shift['shifttype_id']) . "',
`start`='" . sql_escape($shift['start']) . "',
`end`='" . sql_escape($shift['end']) . "',
`RID`='" . sql_escape($shift['RID']) . "',
`title`=" . sql_null($shift['title']) . ",
`URL`=" . sql_null($shift['URL']) . ",
`PSID`=" . sql_null($shift['PSID']) . ",
`created_by_user_id`='" . sql_escape($user['UID']) . "',
`created_at_timestamp`=" . time());
if ($result === false) {
return false;
}
return sql_id();
}
/**
* Return users shifts.
*/
function Shifts_by_user($user) {
return sql_select("
SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`, `ShiftEntry`.*, `Shifts`.*, `Room`.*
FROM `ShiftEntry`
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
WHERE `UID`='" . sql_escape($user['UID']) . "'
ORDER BY `start`
");
}
/**
* TODO: $_REQUEST is not allowed in model!
* Returns Shift id array
*/
function Shifts_filtered() {
global $_REQUEST;
$filter = "";
// filterRoom (Array of integer) - Array of Room IDs (optional, for list request)
if (isset($_REQUEST['filterRoom']) && is_array($_REQUEST['filterRoom'])) {
foreach ($_REQUEST['filterRoom'] as $key => $value) {
$filter .= ", `RID`='" . sql_escape($value) . "' ";
}
}
// filterTask (Array of integer) - Array if Task (optional, for list request)
if (isset($_REQUEST['filterTask']) && is_array($_REQUEST['filterTask'])) {
foreach ($_REQUEST['filterTask'] as $key => $value) {
// TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
}
}
// filterOccupancy (integer) - Occupancy state: (optional, for list request)
// 1 occupied, 2 free, 3 occupied and free
if (isset($_REQUEST['filterOccupancy']) && is_array($_REQUEST['filterOccupancy'])) {
foreach ($_REQUEST['filterOccupancy'] as $key => $value) {
// TODO $filter .= ", `RID`=" . sql_escape($value) . " ";
}
}
// format filter
if ($filter != "") {
$filter = ' WHERE ' . substr($filter, 1);
}
// real request
$shifts_source = sql_select("SELECT `SID` FROM `Shifts`" . $filter);
if ($shifts_source === false) {
return false;
}
if (count($shifts_source) > 0) {
return $shifts_source;
}
return null;
}
/**
* Returns Shift by id.
*
* @param $id Shift
* ID
*/
function Shift($id) {
$shifts_source = sql_select("
SELECT `Shifts`.*, `ShiftTypes`.`name`
FROM `Shifts`
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
WHERE `SID`='" . sql_escape($id) . "'");
$shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`='" . sql_escape($id) . "'");
if ($shifts_source === false) {
return false;
}
if (count($shifts_source) > 0) {
$result = $shifts_source[0];
$result['ShiftEntry'] = $shiftsEntry_source;
$result['NeedAngels'] = [];
$temp = NeededAngelTypes_by_shift($id);
foreach ($temp as $e) {
$result['NeedAngels'][] = array(
'TID' => $e['angel_type_id'],
'count' => $e['count'],
'restricted' => $e['restricted'],
'taken' => $e['taken']
);
}
return $result;
}
return null;
}
/**
* Returns all shifts with needed angeltypes and count of subscribed jobs.
*/
function Shifts() {
$shifts_source = sql_select("
SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`RID`, `Room`.`Name` as `room_name`
FROM `Shifts`
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID`
");
if ($shifts_source === false) {
return false;
}
foreach ($shifts_source as &$shift) {
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
if ($needed_angeltypes === false) {
return false;
}
$shift['angeltypes'] = $needed_angeltypes;
}
return $shifts_source;
}
?>
|