count(); $count = $forced_count; $limit = ''; $set_active = ''; if ($request->has('search')) { $search = strip_request_item('search'); } $show_all_shifts = $request->has('show_all_shifts'); if ($request->has('set_active')) { $valid = true; if ($request->has('count') && preg_match('/^\d+$/', $request->input('count'))) { $count = strip_request_item('count'); if ($count < $forced_count) { error(sprintf( __('At least %s angels are forced to be active. The number has to be greater.'), $forced_count )); redirect(page_link_to('admin_active')); } } else { $valid = false; $msg .= error(__('Please enter a number of angels to be marked as active.'), true); } if ($valid) { $limit = ' LIMIT ' . $count; } if ($request->has('ack')) { State::query() ->where('got_shirt', '=', false) ->update(['active' => false]); /** @var User[] $users */ $users = User::query()->raw(sprintf(' SELECT `users`.*, COUNT(`ShiftEntry`.`id`) AS `shift_count`, (%s + ( SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id` AND `work_timestamp` < %s )) AS `shift_length` FROM `users` LEFT JOIN `ShiftEntry` ON `users`.`id` = `ShiftEntry`.`UID` LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID` LEFT JOIN `users_state` ON `users`.`id` = `users_state`.`user_id` WHERE `users_state`.`arrived` = 1 AND `users_state`.`force_active` = 0 GROUP BY `users`.`id` ORDER BY `force_active` DESC, `shift_length` DESC %s ', $shift_sum_formula, time(), $limit )); $user_nicks = []; foreach ($users as $usr) { $usr->state->active = true; $usr->state->save(); $user_nicks[] = User_Nick_render($usr); } State::whereForceActive(true)->update(['active' => 'true']); engelsystem_log('These angels are active now: ' . join(', ', $user_nicks)); $limit = ''; $msg = success(__('Marked angels.'), true); } else { $set_active = '« ' . __('back') . ' | ' . __('apply') . ''; } } if ($request->has('active') && preg_match('/^\d+$/', $request->input('active'))) { $user_id = $request->input('active'); $user_source = User::find($user_id); if ($user_source) { $user_source->state->active = true; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source) . ' is active now.'); $msg = success(__('Angel has been marked as active.'), true); } else { $msg = error(__('Angel not found.'), true); } } elseif ($request->has('not_active') && preg_match('/^\d+$/', $request->input('not_active'))) { $user_id = $request->input('not_active'); $user_source = User::find($user_id); if (!$user_source) { $user_source->state->active = false; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source) . ' is NOT active now.'); $msg = success(__('Angel has been marked as not active.'), true); } else { $msg = error(__('Angel not found.'), true); } } elseif ($request->has('tshirt') && preg_match('/^\d+$/', $request->input('tshirt'))) { $user_id = $request->input('tshirt'); $user_source = User::find($user_id); if (!$user_source) { $user_source->state->got_shirt = true; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source) . ' has tshirt now.'); $msg = success(__('Angel has got a t-shirt.'), true); } else { $msg = error('Angel not found.', true); } } elseif ($request->has('not_tshirt') && preg_match('/^\d+$/', $request->input('not_tshirt'))) { $user_id = $request->input('not_tshirt'); $user_source = User::find($user_id); if (!$user_source) { $user_source->state->got_shirt = false; $user_source->state->save(); engelsystem_log('User ' . User_Nick_render($user_source) . ' has NO tshirt.'); $msg = success(__('Angel has got no t-shirt.'), true); } else { $msg = error(__('Angel not found.'), true); } } $users = User::query()->raw(sprintf(' SELECT `users`.*, COUNT(`ShiftEntry`.`id`) AS `shift_count`, (%s + ( SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id` AND `work_timestamp` < %s )) AS `shift_length` FROM `users` LEFT JOIN `users_state` ON `users`.`id` = `users_state`.`user_id` LEFT JOIN `ShiftEntry` ON `users`.`id` = `ShiftEntry`.`UID` LEFT JOIN `Shifts` ON `ShiftEntry`.`SID` = `Shifts`.`SID` ' . ($show_all_shifts ? '' : 'AND (`Shifts`.`end` < ' . time() . " OR `Shifts`.`end` IS NULL)") . ' WHERE `users_state`.`arrived` = 1 GROUP BY `users`.`id` ORDER BY `users_state`.`force_active` DESC, `shift_length` DESC %s ', $shift_sum_formula, time(), $limit )); $matched_users = []; if ($search == '') { $tokens = []; } else { $tokens = explode(' ', $search); } foreach ($users as $usr) { if (count($tokens) > 0) { $match = false; foreach ($tokens as $t) { if (stristr($usr->name, trim($t))) { $match = true; break; } } if (!$match) { continue; } } $userData = []; $userData['nick'] = User_Nick_render($usr); $userData['shirt_size'] = $tshirt_sizes[$usr->personalData->shirt_size]; $userData['work_time'] = round($usr['shift_length'] / 60) . ' min (' . sprintf('%.2f', $usr['shift_length'] / 3600) . ' h)'; $userData['active'] = glyph_bool($usr->state->active == 1); $userData['force_active'] = glyph_bool($usr->state->force_active == 1); $userData['tshirt'] = glyph_bool($usr->state->got_shirt == 1); $actions = []; if (!$usr->state->active) { $parameters = [ 'active' => $usr->id, 'search' => $search, ]; if ($show_all_shifts) { $parameters['show_all_shifts'] = 1; } $actions[] = '' . __('set active') . ''; } if ($usr->state->active) { $parametersRemove = [ 'not_active' => $usr->id, 'search' => $search, ]; if ($show_all_shifts) { $parametersRemove['show_all_shifts'] = 1; } $actions[] = '' . __('remove active') . ''; } if (!$usr->state->got_shirt) { $parametersShirt = [ 'tshirt' => $usr->id, 'search' => $search, ]; if ($show_all_shifts) { $parametersShirt['show_all_shifts'] = 1; } $actions[] = '' . __('got t-shirt') . ''; } if ($usr->state->got_shirt) { $parameters = [ 'not_tshirt' => $usr->id, 'search' => $search, ]; if ($show_all_shifts) { $parameters['show_all_shifts'] = 1; } $actions[] = '' . __('remove t-shirt') . ''; } $userData['actions'] = join(' ', $actions); $matched_users[] = $userData; } $shirt_statistics = []; foreach (array_keys($tshirt_sizes) as $size) { $gc = State::query() ->leftJoin('users_settings', 'users_state.user_id', '=', 'users_settings.user_id') ->where('users_state.got_shirt', '=', true) ->where('users_personal_data.shirt_size', '=', $size) ->count(); $shirt_statistics[] = [ 'size' => $size, 'given' => $gc ]; } $shirt_statistics[] = [ 'size' => '' . __('Sum') . '', 'given' => '' . State::whereGotShirt(true)->count() . '' ]; return page_with_title(admin_active_title(), [ form([ form_text('search', __('Search angel:'), $search), form_checkbox('show_all_shifts', __('Show all shifts'), $show_all_shifts), form_submit('submit', __('Search')) ], page_link_to('admin_active')), $set_active == '' ? form([ form_text('count', __('How much angels should be active?'), $count), form_submit('set_active', __('Preview')) ]) : $set_active, $msg . msg(), table([ 'nick' => __('Nickname'), 'shirt_size' => __('Size'), 'shift_count' => __('Shifts'), 'work_time' => __('Length'), 'active' => __('Active?'), 'force_active' => __('Forced'), 'tshirt' => __('T-shirt?'), 'actions' => '' ], $matched_users), '

' . __('Shirt statistics') . '

', table([ 'size' => __('Size'), 'given' => __('Given shirts') ], $shirt_statistics) ]); }