engelsystem/includes/pages/admin_active.php

307 lines
11 KiB
PHP
Raw Normal View History

2011-12-17 15:18:13 +01:00
<?php
use Engelsystem\Models\User\State;
2018-10-09 21:47:31 +02:00
use Engelsystem\Models\User\User;
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_active_title()
{
return __('Active angels');
2013-11-25 21:04:58 +01:00
}
2017-01-03 03:22:48 +01:00
/**
* @return string
*/
2017-01-02 03:57:23 +01:00
function admin_active()
{
$tshirt_sizes = config('tshirt_sizes');
2018-09-17 12:33:15 +02:00
$shift_sum_formula = User_get_shifts_sum_query();
$request = request();
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
$msg = '';
$search = '';
$forced_count = State::whereForceActive(true)->count();
2017-01-02 03:57:23 +01:00
$count = $forced_count;
2017-01-03 14:12:17 +01:00
$limit = '';
$set_active = '';
2017-01-02 15:43:36 +01:00
if ($request->has('search')) {
2017-01-02 03:57:23 +01:00
$search = strip_request_item('search');
}
2017-01-02 15:43:36 +01:00
$show_all_shifts = $request->has('show_all_shifts');
2017-01-02 15:43:36 +01:00
if ($request->has('set_active')) {
2017-01-02 03:57:23 +01:00
$valid = true;
2017-01-02 15:43:36 +01:00
if ($request->has('count') && preg_match('/^\d+$/', $request->input('count'))) {
2017-01-02 03:57:23 +01:00
$count = strip_request_item('count');
if ($count < $forced_count) {
2017-01-02 15:43:36 +01:00
error(sprintf(
__('At least %s angels are forced to be active. The number has to be greater.'),
2017-01-02 15:43:36 +01:00
$forced_count
));
2017-01-02 03:57:23 +01:00
redirect(page_link_to('admin_active'));
}
} else {
$valid = false;
$msg .= error(__('Please enter a number of angels to be marked as active.'), true);
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
if ($valid) {
2017-01-03 14:12:17 +01:00
$limit = ' LIMIT ' . $count;
2017-01-02 03:57:23 +01:00
}
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`,
2017-12-29 17:19:27 +01:00
(%s + (
SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id`
2017-12-29 18:57:11 +01:00
AND `work_timestamp` < %s
2017-12-29 17:19:27 +01:00
)) 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,
2017-12-29 18:57:11 +01:00
time(),
$limit
));
2017-01-02 03:57:23 +01:00
$user_nicks = [];
foreach ($users as $usr) {
$usr->state->active = true;
$usr->state->save();
2017-01-02 03:57:23 +01:00
$user_nicks[] = User_Nick_render($usr);
}
State::whereForceActive(true)->update(['active' => 'true']);
2017-01-03 14:12:17 +01:00
engelsystem_log('These angels are active now: ' . join(', ', $user_nicks));
2017-01-02 15:43:36 +01:00
2017-01-03 14:12:17 +01:00
$limit = '';
$msg = success(__('Marked angels.'), true);
2017-01-02 03:57:23 +01:00
} else {
2017-08-28 16:21:10 +02:00
$set_active = '<a href="' . page_link_to('admin_active', ['search' => $search]) . '">&laquo; '
. __('back')
2017-08-28 16:21:10 +02:00
. '</a> | <a href="'
. page_link_to(
'admin_active',
['search' => $search, 'count' => $count, 'set_active' => 1, 'ack' => 1]
) . '">'
. __('apply')
2017-01-03 14:12:17 +01:00
. '</a>';
2017-01-02 03:57:23 +01:00
}
2012-12-26 14:02:27 +01:00
}
2017-01-02 15:43:36 +01:00
if ($request->has('active') && preg_match('/^\d+$/', $request->input('active'))) {
$user_id = $request->input('active');
2018-10-09 21:47:31 +02:00
$user_source = User::find($user_id);
if ($user_source) {
$user_source->state->active = true;
$user_source->state->save();
2017-01-03 14:12:17 +01:00
engelsystem_log('User ' . User_Nick_render($user_source) . ' is active now.');
$msg = success(__('Angel has been marked as active.'), true);
2017-01-02 03:57:23 +01:00
} else {
$msg = error(__('Angel not found.'), true);
2017-01-02 03:57:23 +01:00
}
} elseif ($request->has('not_active') && preg_match('/^\d+$/', $request->input('not_active'))) {
$user_id = $request->input('not_active');
2018-10-09 21:47:31 +02:00
$user_source = User::find($user_id);
if (!$user_source) {
$user_source->state->active = false;
$user_source->state->save();
2017-01-03 14:12:17 +01:00
engelsystem_log('User ' . User_Nick_render($user_source) . ' is NOT active now.');
$msg = success(__('Angel has been marked as not active.'), true);
2017-01-02 03:57:23 +01:00
} else {
$msg = error(__('Angel not found.'), true);
2017-01-02 03:57:23 +01:00
}
} elseif ($request->has('tshirt') && preg_match('/^\d+$/', $request->input('tshirt'))) {
$user_id = $request->input('tshirt');
2018-10-09 21:47:31 +02:00
$user_source = User::find($user_id);
if (!$user_source) {
$user_source->state->got_shirt = true;
$user_source->state->save();
2017-01-03 14:12:17 +01:00
engelsystem_log('User ' . User_Nick_render($user_source) . ' has tshirt now.');
$msg = success(__('Angel has got a t-shirt.'), true);
2017-01-02 03:57:23 +01:00
} else {
2017-01-03 14:12:17 +01:00
$msg = error('Angel not found.', true);
2017-01-02 03:57:23 +01:00
}
} elseif ($request->has('not_tshirt') && preg_match('/^\d+$/', $request->input('not_tshirt'))) {
$user_id = $request->input('not_tshirt');
2018-10-09 21:47:31 +02:00
$user_source = User::find($user_id);
if (!$user_source) {
$user_source->state->got_shirt = false;
$user_source->state->save();
2017-01-03 14:12:17 +01:00
engelsystem_log('User ' . User_Nick_render($user_source) . ' has NO tshirt.');
$msg = success(__('Angel has got no t-shirt.'), true);
2017-01-02 03:57:23 +01:00
} else {
$msg = error(__('Angel not found.'), true);
2017-01-02 03:57:23 +01:00
}
}
2017-01-02 15:43:36 +01:00
$users = User::query()->raw(sprintf('
SELECT
`users`.*,
COUNT(`ShiftEntry`.`id`) AS `shift_count`,
2017-12-29 17:19:27 +01:00
(%s + (
SELECT COALESCE(SUM(`work_hours`) * 3600, 0) FROM `UserWorkLog` WHERE `user_id`=`users`.`id`
2017-12-29 18:57:11 +01:00
AND `work_timestamp` < %s
2017-12-29 17:19:27 +01:00
)) 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,
2017-12-29 18:57:11 +01:00
time(),
$limit
));
2017-01-02 03:57:23 +01:00
$matched_users = [];
2017-01-03 14:12:17 +01:00
if ($search == '') {
2017-01-02 03:57:23 +01:00
$tokens = [];
} else {
2017-01-03 14:12:17 +01:00
$tokens = explode(' ', $search);
2012-12-26 14:02:27 +01:00
}
foreach ($users as $usr) {
2017-01-02 03:57:23 +01:00
if (count($tokens) > 0) {
$match = false;
foreach ($tokens as $t) {
if (stristr($usr->name, trim($t))) {
2017-01-02 03:57:23 +01:00
$match = true;
break;
}
}
2017-01-02 15:43:36 +01:00
if (!$match) {
2017-01-02 03:57:23 +01:00
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)
2017-12-30 12:07:10 +01:00
. ' min (' . sprintf('%.2f', $usr['shift_length'] / 3600) . '&nbsp;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);
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$actions = [];
if (!$usr->state->active) {
2017-08-28 16:21:10 +02:00
$parameters = [
'active' => $usr->id,
2017-08-28 16:21:10 +02:00
'search' => $search,
];
if ($show_all_shifts) {
$parameters['show_all_shifts'] = 1;
}
$actions[] = '<a href="' . page_link_to('admin_active', $parameters) . '">'
. __('set active')
2017-01-03 14:12:17 +01:00
. '</a>';
2017-01-02 03:57:23 +01:00
}
if ($usr->state->active) {
2017-08-28 16:21:10 +02:00
$parametersRemove = [
'not_active' => $usr->id,
2017-08-28 16:21:10 +02:00
'search' => $search,
];
2017-12-30 13:37:26 +01:00
if ($show_all_shifts) {
$parametersRemove['show_all_shifts'] = 1;
}
$actions[] = '<a href="' . page_link_to('admin_active', $parametersRemove) . '">'
. __('remove active')
2017-12-30 13:37:26 +01:00
. '</a>';
}
if (!$usr->state->got_shirt) {
2017-08-28 16:21:10 +02:00
$parametersShirt = [
'tshirt' => $usr->id,
2017-08-28 16:21:10 +02:00
'search' => $search,
];
if ($show_all_shifts) {
$parametersShirt['show_all_shifts'] = 1;
}
$actions[] = '<a href="' . page_link_to('admin_active', $parametersShirt) . '">'
. __('got t-shirt')
2017-01-03 14:12:17 +01:00
. '</a>';
2017-01-02 03:57:23 +01:00
}
if ($usr->state->got_shirt) {
2017-08-28 16:21:10 +02:00
$parameters = [
'not_tshirt' => $usr->id,
2017-08-28 16:21:10 +02:00
'search' => $search,
];
if ($show_all_shifts) {
$parameters['show_all_shifts'] = 1;
}
$actions[] = '<a href="' . page_link_to('admin_active', $parameters) . '">'
. __('remove t-shirt')
2017-01-03 14:12:17 +01:00
. '</a>';
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
$userData['actions'] = join(' ', $actions);
2017-01-02 15:43:36 +01:00
$matched_users[] = $userData;
2017-01-02 03:57:23 +01:00
}
2017-01-02 15:43:36 +01:00
2017-01-02 03:57:23 +01:00
$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();
2018-08-29 20:25:29 +02:00
$shirt_statistics[] = [
'size' => $size,
'given' => $gc
2018-08-29 20:25:29 +02:00
];
2015-08-14 14:16:09 +02:00
}
2017-01-02 03:57:23 +01:00
$shirt_statistics[] = [
'size' => '<b>' . __('Sum') . '</b>',
'given' => '<b>' . State::whereGotShirt(true)->count() . '</b>'
2017-01-02 15:43:36 +01:00
];
2017-01-02 03:57:23 +01:00
return page_with_title(admin_active_title(), [
2017-01-02 15:43:36 +01:00
form([
form_text('search', __('Search angel:'), $search),
form_checkbox('show_all_shifts', __('Show all shifts'), $show_all_shifts),
form_submit('submit', __('Search'))
2017-01-02 15:43:36 +01:00
], page_link_to('admin_active')),
2017-01-03 14:12:17 +01:00
$set_active == '' ? form([
form_text('count', __('How much angels should be active?'), $count),
form_submit('set_active', __('Preview'))
2017-01-02 15:43:36 +01:00
]) : $set_active,
2017-01-03 03:22:48 +01:00
$msg . msg(),
2017-01-02 15:43:36 +01:00
table([
'nick' => __('Nickname'),
'shirt_size' => __('Size'),
'shift_count' => __('Shifts'),
'work_time' => __('Length'),
'active' => __('Active?'),
'force_active' => __('Forced'),
'tshirt' => __('T-shirt?'),
2017-01-03 14:12:17 +01:00
'actions' => ''
2017-01-02 15:43:36 +01:00
], $matched_users),
'<h2>' . __('Shirt statistics') . '</h2>',
2017-01-02 15:43:36 +01:00
table([
'size' => __('Size'),
'given' => __('Given shirts')
2017-01-02 15:43:36 +01:00
], $shirt_statistics)
]);
2011-12-17 15:18:13 +01:00
}