/** * 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; }
function shift_controller() { global $user, $privileges; if (!in_array('user_shifts', $privileges)) { redirect(page_link_to('?')); } if (!isset($_REQUEST['shift_id'])) { redirect(page_link_to('user_shifts')); } $shift = Shift($_REQUEST['shift_id']); if ($shift === false) { engelsystem_error('Unable to load shift.'); } if ($shift == null) { error(_('Shift could not be found.')); redirect(page_link_to('user_shifts')); } $shifttype = ShiftType($shift['shifttype_id']); if ($shifttype === false || $shifttype == null) { engelsystem_error('Unable to load shift type.'); } $room = Room($shift['RID']); if ($room === false || $room == null) { engelsystem_error('Unable to load room.'); } $angeltypes = AngelTypes(); if ($angeltypes === false) { engelsystem_error('Unable to load angeltypes.'); } $user_shifts = Shifts_by_user($user); if ($user_shifts === false) { engelsystem_error('Unable to load users shifts.'); } $signed_up = false; foreach ($user_shifts as $user_shift) { if ($user_shift['SID'] == $shift['SID']) { $signed_up = true; break; } } return [$shift['name'], Shift_view($shift, $shifttype, $room, in_array('admin_shifts', $privileges), $angeltypes, in_array('user_shifts_admin', $privileges), in_array('admin_rooms', $privileges), in_array('shifttypes', $privileges), $user_shifts, $signed_up)]; }
function user_controller() { global $privileges, $user; if (isset($_REQUEST['user_id'])) { $user_source = User($_REQUEST['user_id']); } else { $user_source = $user; } $shifts = Shifts_by_user($user_source); foreach ($shifts as &$shift) { // TODO: Move queries to model $shift['needed_angeltypes'] = sql_select("SELECT DISTINCT `AngelTypes`.* FROM `ShiftEntry` JOIN `AngelTypes` ON `ShiftEntry`.`TID`=`AngelTypes`.`id` WHERE `ShiftEntry`.`SID`='" . sql_escape($shift['SID']) . "' ORDER BY `AngelTypes`.`name`"); foreach ($shift['needed_angeltypes'] as &$needed_angeltype) { $needed_angeltype['users'] = sql_select("\n SELECT `ShiftEntry`.`freeloaded`, `User`.*\n FROM `ShiftEntry`\n JOIN `User` ON `ShiftEntry`.`UID`=`User`.`UID`\n WHERE `ShiftEntry`.`SID`='" . sql_escape($shift['SID']) . "'\n AND `ShiftEntry`.`TID`='" . sql_escape($needed_angeltype['id']) . "'"); } } if ($user_source['api_key'] == "") { User_reset_api_key($user_source, false); } return array($user_source['Nick'], User_view($user_source, in_array('admin_user', $privileges), User_is_freeloader($user_source), User_angeltypes($user_source), User_groups($user_source), $shifts, $user['UID'] == $user_source['UID'])); }