Formatting

This commit is contained in:
Bot 2018-01-14 18:09:34 +01:00 committed by Igor Scheller
parent fe58e4f422
commit 5227fe6a94
6 changed files with 143 additions and 79 deletions

View File

@ -2,6 +2,8 @@
/**
* Delete a work log entry.
*
* @return array
*/
function user_worklog_delete_controller()
{
@ -29,6 +31,8 @@ function user_worklog_delete_controller()
/**
* Edit work log for user.
*
* @return array
*/
function user_worklog_edit_controller()
{
@ -59,9 +63,10 @@ function user_worklog_edit_controller()
}
/**
* Handle form
*
* @param UserWorkLog $userWorkLog
* @return [bool $valid, UserWorkLog $userWorkLog]
* @param array $userWorkLog
* @return array [bool $valid, UserWorkLog $userWorkLog]
*/
function user_worklog_from_request($userWorkLog)
{
@ -69,14 +74,17 @@ function user_worklog_from_request($userWorkLog)
$valid = true;
$userWorkLog['work_timestamp'] = parse_date('Y-m-d H:i', $request->input('work_timestamp') . ' 00:00');
$userWorkLog['work_timestamp'] = parse_date(
'Y-m-d H:i',
$request->input('work_timestamp') . ' 00:00'
);
if (is_null($userWorkLog['work_timestamp'])) {
$valid = false;
error(_('Please enter work date.'));
}
$userWorkLog['work_hours'] = $request->input('work_hours');
if (! preg_match("/[0-9]+(\.[0-9]+)?/", $userWorkLog['work_hours'])) {
if (!preg_match("/[0-9]+(\.[0-9]+)?/", $userWorkLog['work_hours'])) {
$valid = false;
error(_('Please enter work hours in format ##[.##].'));
}
@ -95,6 +103,8 @@ function user_worklog_from_request($userWorkLog)
/**
* Add work log entry to user.
*
* @return array
*/
function user_worklog_add_controller()
{
@ -128,12 +138,14 @@ function user_worklog_add_controller()
/**
* Link to work log entry add for given user.
*
* @param User $user
* @param array $user
*
* @return string
*/
function user_worklog_add_link($user)
{
return page_link_to('user_worklog', [
'action' => 'add',
'action' => 'add',
'user_id' => $user['UID']
]);
}
@ -141,12 +153,13 @@ function user_worklog_add_link($user)
/**
* Link to work log entry edit.
*
* @param UserWorkLog $userWorkLog
* @param array $userWorkLog
* @return string
*/
function user_worklog_edit_link($userWorkLog)
{
return page_link_to('user_worklog', [
'action' => 'edit',
'action' => 'edit',
'user_worklog_id' => $userWorkLog['id']
]);
}
@ -154,31 +167,34 @@ function user_worklog_edit_link($userWorkLog)
/**
* Link to work log entry delete.
*
* @param UserWorkLog $userWorkLog
* @param array $userWorkLog
* @param array[] $parameters
* @return string
*/
function user_worklog_delete_link($userWorkLog, $parameters = [])
{
return page_link_to('user_worklog', array_merge([
'action' => 'delete',
'action' => 'delete',
'user_worklog_id' => $userWorkLog['id']
], $parameters));
}
/**
* Work log entry actions
*
* @return array
*/
function user_worklogs_controller()
function user_worklog_controller()
{
global $user, $privileges;
if (! in_array('admin_user_worklog', $privileges)) {
if (!in_array('admin_user_worklog', $privileges)) {
redirect(user_link($user));
}
$request = request();
$action = $request->input('action');
if (! $request->has('action')) {
if (!$request->has('action')) {
redirect(user_link($user));
}

View File

@ -1,10 +1,12 @@
<?php
use Engelsystem\Database\Db;
/**
* Load a single work log entry.
*
* @param int $user_worklog_id
*
* @param int $user_worklog_id
* @return array
*/
function UserWorkLog($user_worklog_id)
{
@ -15,8 +17,9 @@ function UserWorkLog($user_worklog_id)
/**
* Returns all work log entries for a user.
*
* @param User $user
*
* @param array $user
* @return array[]
*/
function UserWorkLogsForUser($user)
{
@ -27,8 +30,9 @@ function UserWorkLogsForUser($user)
/**
* Delete a work log entry.
*
* @param UserWorkLog $userWorkLog
*
* @param $userWorkLog
* @return int
*/
function UserWorkLog_delete($userWorkLog)
{
@ -36,21 +40,27 @@ function UserWorkLog_delete($userWorkLog)
$result = Db::delete("DELETE FROM `UserWorkLog` WHERE `id`=?", [
$userWorkLog['id']
]);
engelsystem_log(sprintf('Delete work log for %s, %s hours, %s', User_Nick_render($user_source), $userWorkLog['work_hours'], $userWorkLog['comment']));
engelsystem_log(sprintf(
'Delete work log for %s, %s hours, %s',
User_Nick_render($user_source),
$userWorkLog['work_hours'],
$userWorkLog['comment']
));
return $result;
}
/**
* Update work log entry (only work hours and comment)
*
* @param UserWorkLog $userWorkLog
*
* @param $userWorkLog
* @return int
*/
function UserWorkLog_update($userWorkLog)
{
$user_source = User($userWorkLog['user_id']);
$result = Db::update("UPDATE `UserWorkLog` SET
`work_timestamp`=?,
`work_hours`=?,
@ -61,23 +71,29 @@ function UserWorkLog_update($userWorkLog)
$userWorkLog['comment'],
$userWorkLog['id']
]);
engelsystem_log(sprintf('Updated work log for %s, %s hours, %s', User_Nick_render($user_source), $userWorkLog['work_hours'], $userWorkLog['comment']));
engelsystem_log(sprintf(
'Updated work log for %s, %s hours, %s',
User_Nick_render($user_source),
$userWorkLog['work_hours'],
$userWorkLog['comment'])
);
return $result;
}
/**
* Create a new work log entry
*
* @param UserWorkLog $userWorkLog
*
* @param $userWorkLog
* @return bool
*/
function UserWorkLog_create($userWorkLog)
{
global $user;
$user_source = User($userWorkLog['user_id']);
$result = Db::insert("INSERT INTO `UserWorkLog` (
`user_id`,
`work_timestamp`,
@ -94,30 +110,30 @@ function UserWorkLog_create($userWorkLog)
$user['UID'],
time()
]);
engelsystem_log(sprintf('Added work log entry for %s, %s hours, %s', User_Nick_render($user_source), $userWorkLog['work_hours'], $userWorkLog['comment']));
engelsystem_log(sprintf('Added work log entry for %s, %s hours, %s', User_Nick_render($user_source),
$userWorkLog['work_hours'], $userWorkLog['comment']));
return $result;
}
/**
* New user work log entry
*
* @param array[] $user
* @param array[] $user
* @return array
*/
function UserWorkLog_new($user)
{
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', time()));
$event_config = EventConfig();
if (! empty($event_config['buildup_start_date'])) {
if (!empty($event_config['buildup_start_date'])) {
$work_date = parse_date('Y-m-d H:i', date('Y-m-d 00:00', $event_config['buildup_start_date']));
}
return [
'user_id' => $user['UID'],
'user_id' => $user['UID'],
'work_timestamp' => $work_date,
'work_hours' => 0,
'comment' => ''
'work_hours' => 0,
'comment' => ''
];
}
?>

View File

@ -20,12 +20,14 @@ function User_delete($user_id)
/**
* Returns the tshirt score (number of hours counted for tshirt).
* Accounts only ended shifts.
*
*
* @param array[] $user
* @return int
*/
function User_tshirt_score($user) {
function User_tshirt_score($user)
{
$shift_sum_formula = config('shift_sum_formula');
$result_shifts = DB::selectOne('
SELECT ROUND((' . $shift_sum_formula . ') / 3600, 2) AS `tshirt_score`
FROM `User` LEFT JOIN `ShiftEntry` ON `User`.`UID` = `ShiftEntry`.`UID`
@ -33,7 +35,7 @@ function User_tshirt_score($user) {
WHERE `User`.`UID` = ?
AND `Shifts`.`end` < ?
GROUP BY `User`.`UID`
',[
', [
$user['UID'],
time()
]);
@ -43,11 +45,11 @@ function User_tshirt_score($user) {
LEFT JOIN `UserWorkLog` ON `User`.`UID` = `UserWorkLog`.`user_id`
WHERE `User`.`UID` = ?
AND `UserWorkLog`.`work_timestamp` < ?
',[
', [
$user['UID'],
time()
]);
return $result_shifts['tshirt_score'] + $result_worklog['tshirt_score'];
}
@ -385,7 +387,7 @@ function User_validate_planned_departure_date($planned_arrival_date, $planned_de
return new ValidationResult(false, $planned_arrival_date);
}
$event_config = EventConfig();
if (empty($event_config )) {
if (empty($event_config)) {
// Nothing to validate against
return new ValidationResult(true, $planned_departure_date);
}

View File

@ -2,13 +2,18 @@
/**
* Delete work log entry.
* @param User $user_source
* @param UserWorkLog $userWorkLog
*
* @param array $user_source
* @param array $userWorkLog
* @return string
*/
function UserWorkLog_delete_view($user_source, $userWorkLog)
{
return page_with_title(UserWorkLog_delete_title(), [
info(sprintf(_('Do you want to delete the worklog entry for %s?'), User_Nick_render($user_source)), true),
info(sprintf(
_('Do you want to delete the worklog entry for %s?'),
User_Nick_render($user_source)
), true),
buttons([
button(user_link($user_source), glyph('remove') . _('cancel')),
button(user_worklog_delete_link($userWorkLog, [
@ -28,9 +33,10 @@ function UserWorkLog_delete_title()
/**
* Render edit table.
*
* @param User $user_source
* @param UserWorkLog $userWorkLog
*
* @param array $user_source
* @param array $userWorkLog
* @return string
*/
function UserWorkLog_edit_form($user_source, $userWorkLog)
{
@ -46,8 +52,9 @@ function UserWorkLog_edit_form($user_source, $userWorkLog)
/**
* Form for edit a user work log entry.
*
* @param User $user_source
* @param UserWorkLog $userWorkLog
* @param array $user_source
* @param array $userWorkLog
* @return string
*/
function UserWorkLog_edit_view($user_source, $userWorkLog)
{
@ -63,8 +70,9 @@ function UserWorkLog_edit_view($user_source, $userWorkLog)
/**
* Form for adding a user work log entry.
*
* @param User $user_source
* @param UserWorkLog $userWorkLog
* @param array $user_source
* @param array $userWorkLog
* @return string
*/
function UserWorkLog_add_view($user_source, $userWorkLog)
{
@ -92,5 +100,3 @@ function UserWorkLog_add_title()
{
return _('Add work log entry');
}
?>

View File

@ -406,10 +406,21 @@ function User_view_myshift($shift, $user_source, $its_me)
* @param array[] $shifts
* @param array $user_source
* @param bool $its_me
* @param int $tshirt_score
* @param bool $tshirt_admin
* @param array[] $user_worklogs
* @param $admin_user_worklog_privilege
* @return array
*/
function User_view_myshifts($shifts, $user_source, $its_me, $tshirt_score, $tshirt_admin, $user_worklogs, $admin_user_worklog_privilege)
{
function User_view_myshifts(
$shifts,
$user_source,
$its_me,
$tshirt_score,
$tshirt_admin,
$user_worklogs,
$admin_user_worklog_privilege
) {
$myshifts_table = [];
$timesum = 0;
foreach ($shifts as $shift) {
@ -420,8 +431,8 @@ function User_view_myshifts($shifts, $user_source, $its_me, $tshirt_score, $tshi
}
}
if($its_me || $admin_user_worklog_privilege) {
foreach($user_worklogs as $worklog) {
if ($its_me || $admin_user_worklog_privilege) {
foreach ($user_worklogs as $worklog) {
$myshifts_table[$worklog['work_timestamp']] = User_view_worklog($worklog, $admin_user_worklog_privilege);
$timesum += $worklog['work_hours'] * 3600;
}
@ -453,12 +464,15 @@ function User_view_myshifts($shifts, $user_source, $its_me, $tshirt_score, $tshi
/**
* Renders table entry for user work log
* @param UserWorkLog $worklog
* @param bool $admin_user_worklog_privilege
*
* @param array $worklog
* @param bool $admin_user_worklog_privilege
* @return array
*/
function User_view_worklog($worklog, $admin_user_worklog_privilege) {
function User_view_worklog($worklog, $admin_user_worklog_privilege)
{
$actions = '';
if($admin_user_worklog_privilege) {
if ($admin_user_worklog_privilege) {
$actions = table_buttons([
button(
user_worklog_edit_link($worklog),
@ -479,11 +493,11 @@ function User_view_worklog($worklog, $admin_user_worklog_privilege) {
'room' => '',
'shift_info' => _('Work log entry'),
'comment' => $worklog['comment'] . '<br>'
. sprintf(
_('Added by %s at %s'),
User_Nick_render(User($worklog['created_user_id'])),
date('Y-m-d H:i', $worklog['created_timestamp'])
),
. sprintf(
_('Added by %s at %s'),
User_Nick_render(User($worklog['created_user_id'])),
date('Y-m-d H:i', $worklog['created_timestamp'])
),
'actions' => $actions
];
}
@ -500,6 +514,8 @@ function User_view_worklog($worklog, $admin_user_worklog_privilege) {
* @param bool $its_me
* @param int $tshirt_score
* @param bool $tshirt_admin
* @param bool $admin_user_worklog_privilege
* @param array[] $user_worklogs
* @return string
*/
function User_view(
@ -517,9 +533,17 @@ function User_view(
) {
$user_name = htmlspecialchars($user_source['Vorname']) . ' ' . htmlspecialchars($user_source['Name']);
$myshifts_table = '';
if($its_me || $admin_user_privilege) {
$my_shifts = User_view_myshifts($shifts, $user_source, $its_me, $tshirt_score, $tshirt_admin, $user_worklogs, $admin_user_worklog_privilege);
if(count($my_shifts) > 0) {
if ($its_me || $admin_user_privilege) {
$my_shifts = User_view_myshifts(
$shifts,
$user_source,
$its_me,
$tshirt_score,
$tshirt_admin,
$user_worklogs,
$admin_user_worklog_privilege
);
if (count($my_shifts) > 0) {
$myshifts_table = table([
'date' => _('Day &amp; time'),
'duration' => _('Duration'),
@ -528,7 +552,7 @@ function User_view(
'comment' => _('Comment'),
'actions' => _('Action')
], $my_shifts);
} elseif($user_source['force_active']) {
} elseif ($user_source['force_active']) {
$myshifts_table = success(_('You have done enough to get a t-shirt.'), true);
}
}

View File

@ -132,7 +132,7 @@ if (
$content = user_shifts();
break;
case 'user_worklog':
list($title, $content) = user_worklogs_controller();
list($title, $content) = user_worklog_controller();
break;
case 'user_messages':
$title = messages_title();