add user work log entries feature
parent
e7b0e9934d
commit
86a38a3372
@ -0,0 +1,189 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Delete a work log entry.
|
||||
*/
|
||||
function user_worklog_delete_controller()
|
||||
{
|
||||
global $user;
|
||||
|
||||
$request = request();
|
||||
$userWorkLog = UserWorkLog($request->input('user_worklog_id'));
|
||||
if (empty($userWorkLog)) {
|
||||
redirect(user_link($user));
|
||||
}
|
||||
$user_source = User($userWorkLog['user_id']);
|
||||
|
||||
if ($request->has('confirmed')) {
|
||||
UserWorkLog_delete($userWorkLog);
|
||||
|
||||
success(_('Work log entry deleted.'));
|
||||
redirect(user_link($user_source));
|
||||
}
|
||||
|
||||
return [
|
||||
UserWorkLog_delete_title(),
|
||||
UserWorkLog_delete_view($user_source, $userWorkLog)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit work log for user.
|
||||
*/
|
||||
function user_worklog_edit_controller()
|
||||
{
|
||||
global $user;
|
||||
|
||||
$request = request();
|
||||
$userWorkLog = UserWorkLog($request->input('user_worklog_id'));
|
||||
if (empty($userWorkLog)) {
|
||||
redirect(user_link($user));
|
||||
}
|
||||
$user_source = User($userWorkLog['user_id']);
|
||||
|
||||
if ($request->has('submit')) {
|
||||
list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);
|
||||
|
||||
if ($valid) {
|
||||
UserWorkLog_update($userWorkLog);
|
||||
|
||||
success(_('Work log entry updated.'));
|
||||
redirect(user_link($user_source));
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
UserWorkLog_edit_title(),
|
||||
UserWorkLog_edit_view($user_source, $userWorkLog)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param UserWorkLog $userWorkLog
|
||||
* @return [bool $valid, UserWorkLog $userWorkLog]
|
||||
*/
|
||||
function user_worklog_from_request($userWorkLog)
|
||||
{
|
||||
$request = request();
|
||||
|
||||
$valid = true;
|
||||
|
||||
$userWorkLog['work_hours'] = $request->input('work_hours');
|
||||
if (! preg_match("/[0-9]+(\.[0-9]+)?/", $userWorkLog['work_hours'])) {
|
||||
$valid = false;
|
||||
error(_('Please enter work hours in format ##[.##].'));
|
||||
}
|
||||
|
||||
$userWorkLog['comment'] = $request->input('comment');
|
||||
if (empty($userWorkLog['comment'])) {
|
||||
$valid = false;
|
||||
error(_('Please enter a comment.'));
|
||||
}
|
||||
|
||||
return [
|
||||
$valid,
|
||||
$userWorkLog
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Add work log entry to user.
|
||||
*/
|
||||
function user_worklog_add_controller()
|
||||
{
|
||||
global $user;
|
||||
|
||||
$request = request();
|
||||
$user_source = User($request->input('user_id'));
|
||||
if (empty($user_source)) {
|
||||
redirect(user_link($user));
|
||||
}
|
||||
|
||||
$userWorkLog = UserWorkLog_new($user_source);
|
||||
|
||||
if ($request->has('submit')) {
|
||||
list ($valid, $userWorkLog) = user_worklog_from_request($userWorkLog);
|
||||
|
||||
if ($valid) {
|
||||
UserWorkLog_create($userWorkLog);
|
||||
|
||||
success(_('Work log entry created.'));
|
||||
redirect(user_link($user_source));
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
UserWorkLog_add_title(),
|
||||
UserWorkLog_add_view($user_source, $userWorkLog)
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Link to work log entry add for given user.
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
function user_worklog_add_link($user)
|
||||
{
|
||||
return page_link_to('user_worklog', [
|
||||
'action' => 'add',
|
||||
'user_id' => $user['UID']
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Link to work log entry edit.
|
||||
*
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
function user_worklog_edit_link($userWorkLog)
|
||||
{
|
||||
return page_link_to('user_worklog', [
|
||||
'action' => 'edit',
|
||||
'user_worklog_id' => $userWorkLog['id']
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Link to work log entry delete.
|
||||
*
|
||||
* @param UserWorkLog $userWorkLog
|
||||
* @param array[] $parameters
|
||||
*/
|
||||
function user_worklog_delete_link($userWorkLog, $parameters = [])
|
||||
{
|
||||
return page_link_to('user_worklog', array_merge([
|
||||
'action' => 'delete',
|
||||
'user_worklog_id' => $userWorkLog['id']
|
||||
], $parameters));
|
||||
}
|
||||
|
||||
/**
|
||||
* Work log entry actions
|
||||
*/
|
||||
function user_worklogs_controller()
|
||||
{
|
||||
global $user, $privileges;
|
||||
|
||||
if (! in_array('admin_user_worklog', $privileges)) {
|
||||
redirect(user_link($user));
|
||||
}
|
||||
|
||||
$request = request();
|
||||
$action = $request->input('action');
|
||||
if (! $request->has('action')) {
|
||||
redirect(user_link($user));
|
||||
}
|
||||
|
||||
switch ($action) {
|
||||
case 'add':
|
||||
return user_worklog_add_controller();
|
||||
case 'edit':
|
||||
return user_worklog_edit_controller();
|
||||
case 'delete':
|
||||
return user_worklog_delete_controller();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
use Engelsystem\Database\Db;
|
||||
|
||||
/**
|
||||
* Load a single work log entry.
|
||||
* @param int $user_worklog_id
|
||||
*/
|
||||
function UserWorkLog($user_worklog_id) {
|
||||
return Db::selectOne("SELECT * FROM `UserWorkLog` WHERE `id`=?", [$user_worklog_id]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all work log entries for a user.
|
||||
* @param User $user
|
||||
*/
|
||||
function UserWorkLogsForUser($user) {
|
||||
return Db::select("SELECT * FROM `UserWorkLog` WHERE `user_id`=? ORDER BY `created_timestamp`", [$user['UID']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a work log entry.
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
function UserWorkLog_delete($userWorkLog)
|
||||
{
|
||||
$user_source = $userWorkLog['user_id'];
|
||||
$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']));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update work log entry (only work hours and comment)
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
function UserWorkLog_update($userWorkLog)
|
||||
{
|
||||
$user_source = $userWorkLog['user_id'];
|
||||
|
||||
$result = Db::update("UPDATE `UserWorkLog` SET
|
||||
`work_hours`=?,
|
||||
`comment`=?
|
||||
WHERE `id`=?", [
|
||||
$userWorkLog['work_hours'],
|
||||
$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']));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new work log entry
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
function UserWorkLog_create($userWorkLog)
|
||||
{
|
||||
global $user;
|
||||
|
||||
$user_source = $userWorkLog['user_id'];
|
||||
|
||||
$result = Db::insert("INSERT INTO `UserWorkLog` (
|
||||
`user_id`,
|
||||
`work_hours`,
|
||||
`comment`,
|
||||
`created_user_id`,
|
||||
`created_timestamp`
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?)", [
|
||||
$userWorkLog['user_id'],
|
||||
$userWorkLog['work_hours'],
|
||||
$userWorkLog['comment'],
|
||||
$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']));
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* New user work log entry
|
||||
*
|
||||
* @param array[] $user
|
||||
*/
|
||||
function UserWorkLog_new($user)
|
||||
{
|
||||
return [
|
||||
'user_id' => $user['UID'],
|
||||
'work_hours' => 0,
|
||||
'comment' => ''
|
||||
];
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Delete work log entry.
|
||||
* @param User $user_source
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
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),
|
||||
buttons([
|
||||
button(user_link($user_source), glyph('remove') . _('cancel')),
|
||||
button(user_worklog_delete_link($userWorkLog, [
|
||||
'confirmed' => 1
|
||||
]), glyph('ok') . _('delete'), 'btn-danger')
|
||||
])
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Title for work log delete.
|
||||
*/
|
||||
function UserWorkLog_delete_title()
|
||||
{
|
||||
return _('Delete work log entry');
|
||||
}
|
||||
|
||||
/**
|
||||
* Render edit table.
|
||||
*
|
||||
* @param User $user_source
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
function UserWorkLog_edit_form($user_source, $userWorkLog)
|
||||
{
|
||||
return form([
|
||||
form_info(_('User'), User_Nick_render($user_source)),
|
||||
form_text('work_hours', _('Work hours'), $userWorkLog['work_hours']),
|
||||
form_text('comment', _('Comment'), $userWorkLog['comment']),
|
||||
form_submit('submit', _('Add'))
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Form for edit a user work log entry.
|
||||
*
|
||||
* @param User $user_source
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
function UserWorkLog_edit_view($user_source, $userWorkLog)
|
||||
{
|
||||
return page_with_title(UserWorkLog_edit_title(), [
|
||||
buttons([
|
||||
button(user_link($user_source), _('back'))
|
||||
]),
|
||||
msg(),
|
||||
UserWorkLog_edit_form($user_source, $userWorkLog)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Form for adding a user work log entry.
|
||||
*
|
||||
* @param User $user_source
|
||||
* @param UserWorkLog $userWorkLog
|
||||
*/
|
||||
function UserWorkLog_add_view($user_source, $userWorkLog)
|
||||
{
|
||||
return page_with_title(UserWorkLog_add_title(), [
|
||||
buttons([
|
||||
button(user_link($user_source), _('back'))
|
||||
]),
|
||||
msg(),
|
||||
UserWorkLog_edit_form($user_source, $userWorkLog)
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Title text for editing work log entry.
|
||||
*/
|
||||
function UserWorkLog_edit_title()
|
||||
{
|
||||
return _('Edit work log entry');
|
||||
}
|
||||
|
||||
/**
|
||||
* Title text for adding work log entry.
|
||||
*/
|
||||
function UserWorkLog_add_title()
|
||||
{
|
||||
return _('Add work log entry');
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue