added punch-in punch-out / time-clock mode (#812)

This commit is contained in:
Kevin Papst
2019-05-27 01:36:43 +02:00
committed by GitHub
parent ebff4a765a
commit d9dca96a32
20 changed files with 231 additions and 113 deletions

View File

@@ -8,5 +8,5 @@ A clear and concise description of what this pull request adds or changes.
## Checklist
- [ ] I verified that my code applies to the guidelines (`composer code-check`)
- [ ] I updated the documentation accordingly (see [here](https://github.com/kimai/www.kimai.org/tree/master/_documentation))
- [ ] I updated the documentation (see [here](https://github.com/kimai/www.kimai.org/tree/master/_documentation))
- [ ] I agree that this code is used in Kimai and will be published under the [MIT license](https://github.com/kevinpapst/kimai2/blob/master/LICENSE)

View File

@@ -34,16 +34,26 @@ export default class KimaiAjaxModalForm extends KimaiClickHandlerReducedInTableR
});
}
openUrlInModal(url) {
openUrlInModal(url, errorHandler) {
const self = this;
if (errorHandler === undefined) {
errorHandler = function(xhr, err) {
if (xhr.status !== undefined && xhr.status === 403) {
const alert = self.getContainer().getPlugin('alert');
alert.error(xhr.statusText);
return;
}
window.location = url;
};
}
jQuery.ajax({
url: url,
success: function(html) {
self._openFormInModal(html);
},
error: function(xhr, err) {
window.location = url;
}
error: errorHandler
});
}

View File

@@ -10,7 +10,7 @@ kimai:
# --------------------------------------------------------------------------------
# AUTHENTICATION
# You can disable some user management functions in the authentication screens.
# Both settings default to "true"
# Both settings default to "true", see https://www.kimai.org/documentation/users.html
# --------------------------------------------------------------------------------
# user:
# registration: false

File diff suppressed because one or more lines are too long

View File

@@ -1,5 +1,5 @@
{
"build/app.js": "./app.js?7f61ab551431df41b451",
"build/app.js": "./app.js?1a3cad2c36a19bafdfb6",
"build/app.css": "./app.css?954c34ace3717cfb9d9c80d2be5f8c68",
"build/fonts/fa-solid-900.woff2": "./fonts/fa-solid-900.woff2?e8a92a29",
"build/images/fa-solid-900.svg": "./images/fa-solid-900.svg?666a82cb",

View File

@@ -275,6 +275,7 @@ class TimesheetController extends BaseApiController
'csrf_protection' => false,
'include_rate' => $this->isGranted('edit_rate', $timesheet),
'include_exported' => $this->isGranted('edit_export', $timesheet),
'include_datetime' => !$this->configuration->isPunchInOut(),
'date_format' => self::DATE_FORMAT,
]);
@@ -352,6 +353,7 @@ class TimesheetController extends BaseApiController
'csrf_protection' => false,
'include_rate' => $this->isGranted('edit_rate', $timesheet),
'include_exported' => $this->isGranted('edit_export', $timesheet),
'include_datetime' => !$this->configuration->isPunchInOut(),
'date_format' => self::DATE_FORMAT,
]);

View File

@@ -13,6 +13,7 @@ class TimesheetConfiguration implements SystemBundleConfiguration
{
public const MODE_DURATION_ONLY = 'duration_only';
public const MODE_DEFAULT = 'default';
public const MODE_PUNCH_IN_OUT = 'punch';
use StringAccessibleConfigTrait;
@@ -31,6 +32,11 @@ class TimesheetConfiguration implements SystemBundleConfiguration
return $this->find('mode') === self::MODE_DURATION_ONLY;
}
public function isPunchInOut(): bool
{
return $this->find('mode') === self::MODE_PUNCH_IN_OUT;
}
public function isMarkdownEnabled(): bool
{
return (bool) $this->find('markdown_content');

View File

@@ -146,44 +146,7 @@ abstract class TimesheetAbstractController extends AbstractController
$entry->setUser($this->getUser());
$entry->setBegin($this->dateTime->createDateTime());
$start = $request->get('begin');
if ($start !== null) {
$start = $this->dateTime->createDateTimeFromFormat('Y-m-d', $start);
if ($start !== false) {
$entry->setBegin($start);
// only check for an end date if a begin date was given
$end = $request->get('end');
if ($end !== null) {
$end = $this->dateTime->createDateTimeFromFormat('Y-m-d', $end);
if ($end !== false) {
$start->setTime(10, 0, 0);
$end->setTime(18, 0, 0);
$entry->setEnd($end);
$entry->setDuration($end->getTimestamp() - $start->getTimestamp());
}
}
}
}
$from = $request->get('from');
if ($from !== null) {
$from = $this->dateTime->createDateTime($from);
if ($from !== false) {
$entry->setBegin($from);
// only check for an end datetime if a begin datetime was given
$to = $request->get('to');
if ($to !== null) {
$to = $this->dateTime->createDateTime($to);
if ($to !== false) {
$entry->setEnd($to);
$entry->setDuration($to->getTimestamp() - $from->getTimestamp());
}
}
}
}
$this->setBeginEndFromRequest($request, $entry);
if ($request->query->get('project')) {
$project = $projectRepository->find($request->query->get('project'));
@@ -225,6 +188,52 @@ abstract class TimesheetAbstractController extends AbstractController
]);
}
protected function setBeginEndFromRequest(Request $request, Timesheet $entry)
{
if ($this->configuration->isPunchInOut()) {
return;
}
$start = $request->get('begin');
if ($start !== null) {
$start = $this->dateTime->createDateTimeFromFormat('Y-m-d', $start);
if ($start !== false) {
$entry->setBegin($start);
// only check for an end date if a begin date was given
$end = $request->get('end');
if ($end !== null) {
$end = $this->dateTime->createDateTimeFromFormat('Y-m-d', $end);
if ($end !== false) {
$start->setTime(10, 0, 0);
$end->setTime(18, 0, 0);
$entry->setEnd($end);
$entry->setDuration($end->getTimestamp() - $start->getTimestamp());
}
}
}
}
$from = $request->get('from');
if ($from !== null) {
$from = $this->dateTime->createDateTime($from);
if ($from !== false) {
$entry->setBegin($from);
// only check for an end datetime if a begin datetime was given
$to = $request->get('to');
if ($to !== null) {
$to = $this->dateTime->createDateTime($to);
if ($to !== false) {
$entry->setEnd($to);
$entry->setDuration($to->getTimestamp() - $from->getTimestamp());
}
}
}
}
}
/**
* @param Request $request
* @param string $renderTemplate
@@ -274,10 +283,13 @@ abstract class TimesheetAbstractController extends AbstractController
*/
protected function getCreateForm(Timesheet $entry)
{
return $this->createForm(TimesheetEditForm::class, $entry, [
return $this->createForm($this->getCreateFormClassName(), $entry, [
'action' => $this->generateUrl($this->getCreateRoute()),
'include_rate' => $this->isGranted('edit_rate', $entry),
'include_exported' => $this->isGranted('edit_export', $entry),
'include_user' => $this->includeUserInForms(),
'use_duration' => $this->configuration->isDurationOnly(),
'include_datetime' => !$this->configuration->isPunchInOut(),
'customer' => true,
]);
}
@@ -289,7 +301,7 @@ abstract class TimesheetAbstractController extends AbstractController
*/
protected function getEditForm(Timesheet $entry, $page)
{
return $this->createForm(TimesheetEditForm::class, $entry, [
return $this->createForm($this->getEditFormClassName(), $entry, [
'action' => $this->generateUrl($this->getEditRoute(), [
'id' => $entry->getId(),
'page' => $page,
@@ -297,6 +309,8 @@ abstract class TimesheetAbstractController extends AbstractController
'include_rate' => $this->isGranted('edit_rate', $entry),
'include_exported' => $this->isGranted('edit_export', $entry),
'include_user' => $this->includeUserInForms(),
'include_datetime' => !$this->configuration->isPunchInOut(),
'use_duration' => $this->configuration->isDurationOnly(),
'customer' => true,
]);
}
@@ -316,6 +330,16 @@ abstract class TimesheetAbstractController extends AbstractController
]);
}
protected function getCreateFormClassName()
{
return TimesheetEditForm::class;
}
protected function getEditFormClassName()
{
return TimesheetEditForm::class;
}
protected function includeSummary(): bool
{
return (bool) $this->getUser()->getPreferenceValue('timesheet.daily_stats', false);

View File

@@ -10,6 +10,7 @@
namespace App\Controller;
use App\Entity\Timesheet;
use App\Form\TimesheetAdminEditForm;
use App\Repository\ActivityRepository;
use App\Repository\ProjectRepository;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
@@ -74,6 +75,16 @@ class TimesheetTeamController extends TimesheetAbstractController
return $this->create($request, 'timesheet-team/edit.html.twig', $projectRepository, $activityRepository);
}
protected function getCreateFormClassName()
{
return TimesheetAdminEditForm::class;
}
protected function getEditFormClassName()
{
return TimesheetAdminEditForm::class;
}
protected function includeUserInForms(): bool
{
return true;

View File

@@ -80,9 +80,9 @@ class Configuration implements ConfigurationInterface
->defaultValue('default')
->validate()
->ifTrue(function ($value) {
return !in_array($value, ['default', 'duration_only']);
return !in_array($value, ['default', 'duration_only', 'punch']);
})
->thenInvalid('Chosen timesheet mode is invalid, allowed values: default, duration_only')
->thenInvalid('Chosen timesheet mode is invalid, allowed values: default, duration_only, punch')
->end()
->end()
->booleanNode('markdown_content')

View File

@@ -0,0 +1,23 @@
<?php
/*
* This file is part of the Kimai time-tracking app.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Form;
class TimesheetAdminEditForm extends TimesheetEditForm
{
protected function showTimeFields(array $options): bool
{
return true;
}
protected function showCustomer(array $options, bool $isNew, int $customerCount): bool
{
return true;
}
}

View File

@@ -9,7 +9,6 @@
namespace App\Form;
use App\Configuration\TimesheetConfiguration;
use App\Entity\Activity;
use App\Entity\Customer;
use App\Entity\Project;
@@ -52,23 +51,17 @@ class TimesheetEditForm extends AbstractType
* @var UserDateTimeFactory
*/
protected $dateTime;
/**
* @var TimesheetConfiguration
*/
private $configuration;
/**
* @param CustomerRepository $customer
* @param ProjectRepository $project
* @param UserDateTimeFactory $dateTime
* @param TimesheetConfiguration $config
*/
public function __construct(CustomerRepository $customer, ProjectRepository $project, UserDateTimeFactory $dateTime, TimesheetConfiguration $config)
public function __construct(CustomerRepository $customer, ProjectRepository $project, UserDateTimeFactory $dateTime)
{
$this->customers = $customer;
$this->projects = $project;
$this->dateTime = $dateTime;
$this->configuration = $config;
}
/**
@@ -80,10 +73,10 @@ class TimesheetEditForm extends AbstractType
$project = null;
$customer = null;
$currency = false;
$end = null;
$begin = null;
$customerCount = $this->customers->countCustomer(true);
$projectCount = $this->projects->countProject(true);
$timezone = $this->dateTime->getTimezone()->getName();
$isNew = true;
if (isset($options['data'])) {
@@ -106,14 +99,9 @@ class TimesheetEditForm extends AbstractType
$currency = $customer->getCurrency();
}
$begin = $entry->getBegin();
$end = $entry->getEnd();
}
$timezone = $this->dateTime->getTimezone()->getName();
if (null !== $begin) {
$timezone = $begin->getTimezone()->getName();
if (null !== ($begin = $entry->getBegin())) {
$timezone = $begin->getTimezone()->getName();
}
}
$dateTimeOptions = [
@@ -126,44 +114,49 @@ class TimesheetEditForm extends AbstractType
$dateTimeOptions['format'] = $options['date_format'];
}
if ($isNew || null === $end || !$this->configuration->isDurationOnly()) {
if ($this->showTimeFields($options)) {
$this->addBegin($builder, $dateTimeOptions);
if ($options['use_duration']) {
$this->addDuration($builder);
} else {
$this->addEnd($builder, $dateTimeOptions);
}
}
if ($this->configuration->isDurationOnly()) {
$this->addDuration($builder);
} else {
$this->addEnd($builder, $dateTimeOptions);
}
$projectOptions = [];
if ($customerCount < 2) {
$projectOptions['group_by'] = null;
} elseif ($options['customer']) {
if ($this->showCustomer($options, $isNew, $customerCount)) {
$this->addCustomer($builder, $customer);
}
if ($projectCount <= 1) {
$projectOptions['group_by'] = null;
}
$this->addProject($builder, $projectOptions, $project, $customer);
$this->addProject($builder, $customerCount, $projectCount, $project, $customer);
$this->addActivity($builder, $activity, $project);
$this->addDescription($builder);
$this->addTags($builder);
$this->addRates($builder, $currency, $options);
$this->addUser($builder, $options);
$this->addExported($builder, $options);
}
if ($options['include_rate']) {
$this->addRates($builder, $currency);
protected function showCustomer(array $options, bool $isNew, int $customerCount): bool
{
if (!$isNew && $options['customer']) {
return true;
}
if ($options['include_user']) {
$this->addUser($builder);
if ($customerCount < 2) {
return false;
}
if ($options['include_exported']) {
$this->addExported($builder);
if (!$options['customer']) {
return false;
}
return true;
}
protected function showTimeFields(array $options): bool
{
return $options['include_datetime'];
}
protected function addCustomer(FormBuilderInterface $builder, ?Customer $customer = null)
@@ -181,8 +174,18 @@ class TimesheetEditForm extends AbstractType
]);
}
protected function addProject(FormBuilderInterface $builder, array $projectOptions, ?Project $project = null, ?Customer $customer = null)
protected function addProject(FormBuilderInterface $builder, int $customerCount, int $projectCount, ?Project $project = null, ?Customer $customer = null)
{
$projectOptions = [];
if ($customerCount < 2) {
$projectOptions['group_by'] = null;
}
if ($projectCount < 2) {
$projectOptions['group_by'] = null;
}
$builder
->add(
'project',
@@ -322,8 +325,12 @@ class TimesheetEditForm extends AbstractType
]);
}
protected function addRates(FormBuilderInterface $builder, $currency)
protected function addRates(FormBuilderInterface $builder, $currency, array $options)
{
if (!$options['include_rate']) {
return;
}
$builder
->add('fixedRate', FixedRateType::class, [
'currency' => $currency,
@@ -333,13 +340,21 @@ class TimesheetEditForm extends AbstractType
]);
}
protected function addUser(FormBuilderInterface $builder)
protected function addUser(FormBuilderInterface $builder, array $options)
{
if (!$options['include_user']) {
return;
}
$builder->add('user', UserType::class);
}
protected function addExported(FormBuilderInterface $builder)
protected function addExported(FormBuilderInterface $builder, array $options)
{
if (!$options['include_exported']) {
return;
}
$builder->add('exported', YesNoType::class, [
'label' => 'label.exported'
]);
@@ -362,6 +377,8 @@ class TimesheetEditForm extends AbstractType
'method' => 'POST',
'date_format' => null,
'customer' => false, // for API usage
'use_duration' => false, // duration instead of end (for duration_only mode)
'include_datetime' => true,
'attr' => [
'data-form-event' => 'kimai.timesheetUpdate',
'data-msg-success' => 'action.update.success',

View File

@@ -28,6 +28,7 @@ class TimesheetModeType extends AbstractType
'label' => 'label.timesheet.mode',
'choices' => [
'label.timesheet.mode_default' => TimesheetConfiguration::MODE_DEFAULT,
'label.timesheet.mode_punch' => TimesheetConfiguration::MODE_PUNCH_IN_OUT,
'label.timesheet.mode_duration_only' => TimesheetConfiguration::MODE_DURATION_ONLY,
],
]);

View File

@@ -32,6 +32,7 @@ class TimesheetConfigExtension extends AbstractExtension
{
return [
new TwigFunction('is_duration_only', [$this, 'isDurationOnly']),
new TwigFunction('is_punch_mode', [$this, 'isPunchInOut']),
];
}
@@ -39,4 +40,9 @@ class TimesheetConfigExtension extends AbstractExtension
{
return $this->configuration->isDurationOnly();
}
public function isPunchInOut(): bool
{
return $this->configuration->isPunchInOut();
}
}

View File

@@ -153,11 +153,11 @@
html: true
});
},
{% if is_granted('create_own_timesheet') %}
// day-clicks are always triggered, unless a selection was created
// so clicking in a day (month view) or any slot (week and day view) will trigger a dayClick
// BEFORE triggering a select - make sure not two create dialogs are requested
{% if not is_punch_mode() and is_granted('create_own_timesheet') %}
dayClick: function(date, jsEvent, view) {
// day-clicks are always triggered, unless a selection was created
// so clicking in a day (month view) or any slot (week and day view) will trigger a dayClick
// BEFORE triggering a select - make sure not two create dialogs are requested
if(view.type !== 'month') {
return;
}
@@ -177,8 +177,8 @@
var createUrl = '{{ path('timesheet_create') }}' + '?from=' + start.format() + '&to=' + end.format();
kimai.getPlugin('modal').openUrlInModal(createUrl);
},
{% endif %}
{% if is_granted('edit_own_timesheet') %}
{% endif %}
{% if is_granted('edit_own_timesheet') %}
eventClick: function(eventObj, jsEvent, view) {
if (eventObj.source.ajaxSettings !== undefined) {
jsEvent.preventDefault();
@@ -187,6 +187,7 @@
var editUrl = '{{ path('timesheet_edit', {id: '-XX-'}) }}'.replace('-XX-', eventObj.id);
kimai.getPlugin('modal').openUrlInModal(editUrl);
},
{% if not is_punch_mode() %}
editable: true,
eventDragStart: function(event, jsEvent, ui, view) {
window.hidePopover = true;
@@ -203,6 +204,7 @@
},
eventResize: changeHandler,
{% endif %}
{% endif %}
/*
slotDuration: '00:30:00', // TODO make me configurable
*/

View File

@@ -120,9 +120,9 @@ class TimesheetTeamControllerTest extends ControllerBaseTest
$this->request($client, '/team/timesheet/create');
$this->assertTrue($client->getResponse()->isSuccessful());
$form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form();
$form = $client->getCrawler()->filter('form[name=timesheet_admin_edit_form]')->form();
$client->submit($form, [
'timesheet_edit_form' => [
'timesheet_admin_edit_form' => [
'description' => 'Testing is fun!',
'project' => 1,
'activity' => 1,
@@ -170,9 +170,9 @@ class TimesheetTeamControllerTest extends ControllerBaseTest
'Could not find link to documentation'
);
$form = $client->getCrawler()->filter('form[name=timesheet_edit_form]')->form();
$form = $client->getCrawler()->filter('form[name=timesheet_admin_edit_form]')->form();
$client->submit($form, [
'timesheet_edit_form' => [
'timesheet_admin_edit_form' => [
'description' => 'foo-bar',
'tags' => 'foo,bar, testing, hello world,,',
'user' => $teamlead->getId()

View File

@@ -25,8 +25,9 @@ class TimesheetConfigExtensionTest extends TestCase
$config = new TimesheetConfiguration($loader, ['mode' => 'duration_only']);
$sut = new TimesheetConfigExtension($config);
$filters = $sut->getFunctions();
$this->assertCount(1, $filters);
$this->assertCount(2, $filters);
$this->assertEquals('is_duration_only', $filters[0]->getName());
$this->assertEquals('is_punch_mode', $filters[1]->getName());
}
public function testIsDurationOnly()
@@ -35,6 +36,7 @@ class TimesheetConfigExtensionTest extends TestCase
$config = new TimesheetConfiguration($loader, ['mode' => 'duration_only']);
$sut = new TimesheetConfigExtension($config);
$this->assertTrue($sut->isDurationOnly());
$this->assertFalse($sut->isPunchInOut());
}
public function testIsNotDurationOnly()
@@ -43,5 +45,15 @@ class TimesheetConfigExtensionTest extends TestCase
$config = new TimesheetConfiguration($loader, ['mode' => 'default']);
$sut = new TimesheetConfigExtension($config);
$this->assertFalse($sut->isDurationOnly());
$this->assertFalse($sut->isPunchInOut());
}
public function testIsPunchInOut()
{
$loader = $this->getMockBuilder(ConfigLoaderInterface::class)->getMock();
$config = new TimesheetConfiguration($loader, ['mode' => 'punch']);
$sut = new TimesheetConfigExtension($config);
$this->assertFalse($sut->isDurationOnly());
$this->assertTrue($sut->isPunchInOut());
}
}

View File

@@ -628,10 +628,6 @@ För närvarande har %user% användare %records% tidsrekord som räknas upp till
<source>ROLE_USER</source>
<target state="translated">Användare</target>
</trans-unit>
<trans-unit id="ROLE_CUSTOMER">
<source>ROLE_CUSTOMER</source>
<target state="translated">Kund</target>
</trans-unit>
<!--
Statistics data for Dashboard & Users profile

View File

@@ -36,11 +36,15 @@
</trans-unit>
<trans-unit id="label.timesheet.mode_default">
<source>label.timesheet.mode_default</source>
<target>Standard Modus: erfasst Start und Enddatum</target>
<target>[Standard] Start- und Endzeiten können bearbeitet werden</target>
</trans-unit>
<trans-unit id="label.timesheet.mode_duration_only">
<source>label.timesheet.mode_duration_only</source>
<target>Dauer: ersetzt das Enddatum durch ein Eingabefeld für Dauer</target>
<target>[Dauer] ersetzt die Endzeit durch ein Eingabefeld für Dauer</target>
</trans-unit>
<trans-unit id="label.timesheet.mode_punch">
<source>label.timesheet.mode_punch</source>
<target>[Stechuhr] Benutzer kann Aufzeichnungen starten und stoppen, aber weder Zeiten noch Dauer bearbeiten</target>
</trans-unit>
<trans-unit id="label.timesheet.rules.allow_future_times">
<source>label.timesheet.rules.allow_future_times</source>

View File

@@ -36,11 +36,15 @@
</trans-unit>
<trans-unit id="label.timesheet.mode_default">
<source>label.timesheet.mode_default</source>
<target>Default: accept start and end date</target>
<target>[Default] start and end times can be edited</target>
</trans-unit>
<trans-unit id="label.timesheet.mode_duration_only">
<source>label.timesheet.mode_duration_only</source>
<target>Duration only: replaces the end date field with an input for duration</target>
<target>[Duration] replaces the end time with a duration input-field</target>
</trans-unit>
<trans-unit id="label.timesheet.mode_punch">
<source>label.timesheet.mode_punch</source>
<target>[Time-clock] user can start and stop records, but not edit the times or duration</target>
</trans-unit>
<trans-unit id="label.timesheet.rules.allow_future_times">
<source>label.timesheet.rules.allow_future_times</source>