2013-12-09 17:10:07 +01:00
|
|
|
<?php
|
2014-12-07 17:07:19 +01:00
|
|
|
|
2014-12-25 22:23:18 +01:00
|
|
|
/**
|
|
|
|
* Check if a shift collides with other shifts (in time).
|
|
|
|
* @param Shift $shift
|
|
|
|
* @param array<Shift> $shifts
|
|
|
|
*/
|
|
|
|
function Shift_collides($shift, $shifts) {
|
2016-09-29 11:18:17 +02:00
|
|
|
foreach ($shifts as $other_shift) {
|
|
|
|
if ($shift['SID'] != $other_shift['SID']) {
|
|
|
|
if (! ($shift['start'] >= $other_shift['end'] || $shift['end'] <= $other_shift['start'])) {
|
2014-12-25 22:23:18 +01:00
|
|
|
return true;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-12-25 22:23:18 +01:00
|
|
|
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);
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($user_shifts === false) {
|
2014-12-25 22:23:18 +01:00
|
|
|
engelsystem_error('Unable to load users shifts.');
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-12-25 22:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$collides = Shift_collides($shift, $user_shifts);
|
|
|
|
|
|
|
|
if ($user_angeltype == null) {
|
|
|
|
$user_angeltype = UserAngelType_by_User_and_AngelType($user, $angeltype);
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($user_angeltype === false) {
|
2014-12-25 22:23:18 +01:00
|
|
|
engelsystem_error('Unable to load user angeltype.');
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-12-25 22:23:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$signed_up = false;
|
2016-09-29 11:18:17 +02:00
|
|
|
foreach ($user_shifts as $user_shift) {
|
2014-12-25 22:23:18 +01:00
|
|
|
if ($user_shift['SID'] == $shift['SID']) {
|
|
|
|
$signed_up = true;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-12-26 23:48:16 +01:00
|
|
|
|
|
|
|
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($needed_angeltypes === false) {
|
2014-12-26 23:48:16 +01:00
|
|
|
engelsystem_error('Unable to load needed angel types.');
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// is the shift still running or alternatively is the user shift admin?
|
2014-12-25 22:23:18 +01:00
|
|
|
$user_may_join_shift = true;
|
|
|
|
|
2014-12-26 23:48:16 +01:00
|
|
|
// you canot join if shift is full
|
2016-09-29 11:18:17 +02:00
|
|
|
foreach ($needed_angeltypes as $needed_angeltype) {
|
2014-12-26 23:48:16 +01:00
|
|
|
if ($needed_angeltype['angel_type_id'] == $angeltype['id']) {
|
|
|
|
if ($needed_angeltype['taken'] >= $needed_angeltype['count'])
|
|
|
|
$user_may_join_shift = false;
|
|
|
|
break;
|
|
|
|
}
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// you cannot join if user alread joined a parallel or this shift
|
2014-12-25 22:23:18 +01:00
|
|
|
$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
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($angeltype['restricted'] == 1 && $user_angeltype != null) {
|
2014-12-25 22:23:18 +01:00
|
|
|
$user_may_join_shift &= isset($user_angeltype['confirm_user_id']);
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// you can only join if the shift is in future
|
2014-12-25 22:23:18 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2014-12-07 17:48:35 +01:00
|
|
|
/**
|
|
|
|
* Delete a shift by its external id.
|
|
|
|
*/
|
|
|
|
function Shift_delete_by_psid($shift_psid) {
|
2014-12-28 14:56:02 +01:00
|
|
|
return sql_query("DELETE FROM `Shifts` WHERE `PSID`='" . sql_escape($shift_psid) . "'");
|
2014-12-07 17:48:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a shift.
|
|
|
|
*/
|
|
|
|
function Shift_delete($shift_id) {
|
2014-12-07 19:44:59 +01:00
|
|
|
mail_shift_delete(Shift($shift_id));
|
2014-12-17 17:22:35 +01:00
|
|
|
|
2014-12-28 13:44:56 +01:00
|
|
|
return sql_query("DELETE FROM `Shifts` WHERE `SID`='" . sql_escape($shift_id) . "'");
|
2014-12-07 17:48:35 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:41:40 +01:00
|
|
|
/**
|
|
|
|
* Update a shift.
|
|
|
|
*/
|
|
|
|
function Shift_update($shift) {
|
2015-08-15 23:36:38 +02:00
|
|
|
global $user;
|
2014-12-22 20:06:37 +01:00
|
|
|
$shift['name'] = ShiftType($shift['shifttype_id'])['name'];
|
2014-12-07 19:44:59 +01:00
|
|
|
mail_shift_change(Shift($shift['SID']), $shift);
|
2014-12-17 17:22:35 +01:00
|
|
|
|
2014-12-07 17:41:40 +01:00
|
|
|
return sql_query("UPDATE `Shifts` SET
|
2014-12-28 13:44:56 +01:00
|
|
|
`shifttype_id`='" . sql_escape($shift['shifttype_id']) . "',
|
|
|
|
`start`='" . sql_escape($shift['start']) . "',
|
|
|
|
`end`='" . sql_escape($shift['end']) . "',
|
|
|
|
`RID`='" . sql_escape($shift['RID']) . "',
|
2014-12-17 17:22:35 +01:00
|
|
|
`title`=" . sql_null($shift['title']) . ",
|
2014-12-07 17:41:40 +01:00
|
|
|
`URL`=" . sql_null($shift['URL']) . ",
|
2015-08-15 23:36:38 +02:00
|
|
|
`PSID`=" . sql_null($shift['PSID']) . ",
|
|
|
|
`edited_by_user_id`='" . sql_escape($user['UID']) . "',
|
|
|
|
`edited_at_timestamp`=" . time() . "
|
2014-12-28 13:44:56 +01:00
|
|
|
WHERE `SID`='" . sql_escape($shift['SID']) . "'");
|
2014-12-07 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a shift by its external id.
|
|
|
|
*/
|
|
|
|
function Shift_update_by_psid($shift) {
|
2014-12-07 19:46:56 +01:00
|
|
|
$shift_source = sql_select("SELECT `SID` FROM `Shifts` WHERE `PSID`=" . $shift['PSID']);
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($shift_source === false) {
|
2014-12-07 19:46:56 +01:00
|
|
|
return false;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
|
|
|
if (count($shift_source) == 0) {
|
2014-12-07 19:46:56 +01:00
|
|
|
return null;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-12-22 20:06:37 +01:00
|
|
|
$shift['SID'] = $shift_source[0]['SID'];
|
2014-12-07 19:46:56 +01:00
|
|
|
return Shift_update($shift);
|
2014-12-07 17:41:40 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 17:34:29 +01:00
|
|
|
/**
|
|
|
|
* Create a new shift.
|
2014-12-07 17:41:40 +01:00
|
|
|
*
|
2014-12-07 17:34:29 +01:00
|
|
|
* @return new shift id or false
|
|
|
|
*/
|
|
|
|
function Shift_create($shift) {
|
2015-08-15 23:36:38 +02:00
|
|
|
global $user;
|
2014-12-07 17:34:29 +01:00
|
|
|
$result = sql_query("INSERT INTO `Shifts` SET
|
2014-12-28 13:44:56 +01:00
|
|
|
`shifttype_id`='" . sql_escape($shift['shifttype_id']) . "',
|
|
|
|
`start`='" . sql_escape($shift['start']) . "',
|
|
|
|
`end`='" . sql_escape($shift['end']) . "',
|
|
|
|
`RID`='" . sql_escape($shift['RID']) . "',
|
2014-12-17 17:22:35 +01:00
|
|
|
`title`=" . sql_null($shift['title']) . ",
|
2014-12-28 13:57:50 +01:00
|
|
|
`URL`=" . sql_null($shift['URL']) . ",
|
2015-08-15 23:36:38 +02:00
|
|
|
`PSID`=" . sql_null($shift['PSID']) . ",
|
|
|
|
`created_by_user_id`='" . sql_escape($user['UID']) . "',
|
|
|
|
`created_at_timestamp`=" . time());
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($result === false) {
|
2014-12-07 17:34:29 +01:00
|
|
|
return false;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-12-07 17:34:29 +01:00
|
|
|
return sql_id();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return users shifts.
|
|
|
|
*/
|
2014-08-23 01:55:18 +02:00
|
|
|
function Shifts_by_user($user) {
|
|
|
|
return sql_select("
|
2014-12-19 22:41:55 +01:00
|
|
|
SELECT `ShiftTypes`.`id` as `shifttype_id`, `ShiftTypes`.`name`, `ShiftEntry`.*, `Shifts`.*, `Room`.*
|
2014-08-23 01:55:18 +02:00
|
|
|
FROM `ShiftEntry`
|
|
|
|
JOIN `Shifts` ON (`ShiftEntry`.`SID` = `Shifts`.`SID`)
|
2014-12-17 17:22:35 +01:00
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2014-08-23 01:55:18 +02:00
|
|
|
JOIN `Room` ON (`Shifts`.`RID` = `Room`.`RID`)
|
2014-12-28 13:44:56 +01:00
|
|
|
WHERE `UID`='" . sql_escape($user['UID']) . "'
|
2014-08-23 01:55:18 +02:00
|
|
|
ORDER BY `start`
|
|
|
|
");
|
|
|
|
}
|
2013-12-09 17:10:07 +01:00
|
|
|
|
2013-12-29 15:08:21 +01:00
|
|
|
/**
|
2014-01-07 15:50:16 +01:00
|
|
|
* TODO: $_REQUEST is not allowed in model!
|
2013-12-29 15:08:21 +01:00
|
|
|
* Returns Shift id array
|
|
|
|
*/
|
2014-01-07 15:50:16 +01:00
|
|
|
function Shifts_filtered() {
|
2014-08-23 01:55:18 +02:00
|
|
|
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) {
|
2014-12-28 13:44:56 +01:00
|
|
|
$filter .= ", `RID`='" . sql_escape($value) . "' ";
|
2014-08-23 01:55:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($shifts_source === false) {
|
2014-08-23 01:55:18 +02:00
|
|
|
return false;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-08-23 01:55:18 +02:00
|
|
|
if (count($shifts_source) > 0) {
|
|
|
|
return $shifts_source;
|
|
|
|
}
|
|
|
|
return null;
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns Shift by id.
|
|
|
|
*
|
2014-08-23 01:55:18 +02:00
|
|
|
* @param $id Shift
|
|
|
|
* ID
|
2013-12-29 15:08:21 +01:00
|
|
|
*/
|
2014-01-07 15:50:16 +01:00
|
|
|
function Shift($id) {
|
2014-12-17 17:22:35 +01:00
|
|
|
$shifts_source = sql_select("
|
|
|
|
SELECT `Shifts`.*, `ShiftTypes`.`name`
|
|
|
|
FROM `Shifts`
|
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2014-12-28 13:44:56 +01:00
|
|
|
WHERE `SID`='" . sql_escape($id) . "'");
|
|
|
|
$shiftsEntry_source = sql_select("SELECT `id`, `TID` , `UID` , `freeloaded` FROM `ShiftEntry` WHERE `SID`='" . sql_escape($id) . "'");
|
2014-08-23 01:55:18 +02:00
|
|
|
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($shifts_source === false) {
|
2014-08-23 01:55:18 +02:00
|
|
|
return false;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-08-23 01:55:18 +02:00
|
|
|
if (count($shifts_source) > 0) {
|
|
|
|
$result = $shifts_source[0];
|
|
|
|
|
|
|
|
$result['ShiftEntry'] = $shiftsEntry_source;
|
2014-12-22 20:06:37 +01:00
|
|
|
$result['NeedAngels'] = [];
|
2014-08-23 01:55:18 +02:00
|
|
|
|
|
|
|
$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;
|
2013-12-29 15:08:21 +01:00
|
|
|
}
|
|
|
|
|
2013-12-09 17:10:07 +01:00
|
|
|
/**
|
|
|
|
* Returns all shifts with needed angeltypes and count of subscribed jobs.
|
|
|
|
*/
|
|
|
|
function Shifts() {
|
|
|
|
$shifts_source = sql_select("
|
2014-12-17 17:22:35 +01:00
|
|
|
SELECT `ShiftTypes`.`name`, `Shifts`.*, `Room`.`RID`, `Room`.`Name` as `room_name`
|
2013-12-09 17:10:07 +01:00
|
|
|
FROM `Shifts`
|
2014-12-17 17:22:35 +01:00
|
|
|
JOIN `ShiftTypes` ON (`ShiftTypes`.`id` = `Shifts`.`shifttype_id`)
|
2013-12-09 17:10:07 +01:00
|
|
|
JOIN `Room` ON `Room`.`RID` = `Shifts`.`RID`
|
|
|
|
");
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($shifts_source === false) {
|
2013-12-09 17:10:07 +01:00
|
|
|
return false;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2013-12-09 17:10:07 +01:00
|
|
|
|
|
|
|
foreach ($shifts_source as &$shift) {
|
2014-01-05 19:33:52 +01:00
|
|
|
$needed_angeltypes = NeededAngelTypes_by_shift($shift['SID']);
|
2016-09-29 11:18:17 +02:00
|
|
|
if ($needed_angeltypes === false) {
|
2013-12-09 17:10:07 +01:00
|
|
|
return false;
|
2016-09-29 11:18:17 +02:00
|
|
|
}
|
2014-08-23 01:55:18 +02:00
|
|
|
|
2013-12-09 17:10:07 +01:00
|
|
|
$shift['angeltypes'] = $needed_angeltypes;
|
|
|
|
}
|
2013-12-09 17:17:23 +01:00
|
|
|
|
|
|
|
return $shifts_source;
|
2013-12-09 17:10:07 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 19:44:59 +01:00
|
|
|
?>
|