Add day of event in footer and on dashboard

This commit is contained in:
Michael Weimann 2023-08-13 16:09:56 +02:00 committed by msquare
parent e0b552d18b
commit f4030b86af
9 changed files with 190 additions and 9 deletions

View File

@ -350,6 +350,12 @@ return [
'4XL' => '4XLarge Straight-Cut',
],
// Whether to show the current day of the event (-2, -1, 0, 1, 2…) in footer and on the dashboard.
// The event start date has to be set for it to appear.
'enable_show_day_of_event' => false,
// If true there will be a day 0 (-1, 0, 1…). If false there won't (-1, 1…)
'event_has_day0' => true,
'metrics' => [
// User work buckets in seconds
'work' => [1 * 60 * 60, 1.5 * 60 * 60, 2 * 60 * 60, 3 * 60 * 60, 5 * 60 * 60, 10 * 60 * 60, 20 * 60 * 60],

View File

@ -1,5 +1,6 @@
<?php
use Engelsystem\Helpers\DayOfEvent;
use Engelsystem\Models\News;
use Illuminate\Support\Collection;
@ -43,17 +44,25 @@ function public_dashboard_view($stats, $free_shifts, $important_news)
]);
}
$stats = [
stats(__('Angels needed in the next 3 hrs'), $stats['needed-3-hours']),
stats(__('Angels needed for nightshifts'), $stats['needed-night']),
stats(__('Angels currently working'), $stats['angels-working'], 'default'),
stats(__('Hours to be worked'), $stats['hours-to-work'], 'default'),
];
$dayOfEvent = DayOfEvent::get();
if (config('enable_show_day_of_event') && $dayOfEvent !== null) {
$stats[] = stats(__('dashboard.day'), $dayOfEvent, 'default');
}
$isFiltered = request()->get('filtered');
$filter = collect(session()->get('shifts-filter'))->only(['rooms', 'types'])->toArray();
return page([
div('wrapper', [
div('public-dashboard', [
div('first row', [
stats(__('Angels needed in the next 3 hrs'), $stats['needed-3-hours']),
stats(__('Angels needed for nightshifts'), $stats['needed-night']),
stats(__('Angels currently working'), $stats['angels-working'], 'default'),
stats(__('Hours to be worked'), $stats['hours-to-work'], 'default'),
], 'statistics'),
div('first row', $stats, 'statistics'),
$news,
$needed_angels,
], 'public-dashboard'),

View File

@ -2426,3 +2426,9 @@ msgstr "Raum erstellen"
msgid "room.edit.title"
msgstr "Raum bearbeiten"
msgid "event.day"
msgstr "Tag %1$d"
msgid "dashboard.day"
msgstr "Tag"

View File

@ -524,3 +524,9 @@ msgstr "Create room"
msgid "room.edit.title"
msgstr "Edit room"
msgid "event.day"
msgstr "Day %1$d"
msgid "dashboard.day"
msgstr "Day"

View File

@ -11,11 +11,17 @@
config('event_start').format(__('Y-m-d')),
config('event_end').format(__('Y-m-d'))
]) }}
{%- if config('enable_show_day_of_event') and day_of_event is defined -%}
, {{ __('event.day', [day_of_event]) }}
{% endif %}
{% elseif config('event_start') %}
{{ __('%1$s, starting %2$s', [
config('name'),
config('event_start').format(__('Y-m-d'))
]) }}
{%- if config('enable_show_day_of_event') and day_of_event is defined -%}
, {{ __('event.day', [day_of_event]) }}
{% endif %}
{% else %}
{{ config('name') }}
{% endif %} <br>
@ -23,7 +29,10 @@
{{ __('Event from %1$s to %2$s', [
config('event_start').format(__('Y-m-d')),
config('event_end').format(__('Y-m-d'))
]) }} <br>
]) }}
{%- if config('enable_show_day_of_event') and day_of_event is defined -%}
, {{ __('event.day', [day_of_event]) }}
{% endif %} <br>
{% endif %}
{% endblock %}

View File

@ -0,0 +1,52 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Helpers;
use Engelsystem\Helpers\Carbon;
class DayOfEvent
{
/**
* @return The current day of the event.
* If "event_has_day0" is set to true in config,
* the first day of the event will be 0, else 1.
* Returns null if "event_start" is not set.
*/
public static function get(): int | null
{
$startOfEvent = config('event_start');
if (!$startOfEvent) {
return null;
}
/** @var Carbon $startOfEvent */
$startOfEvent = $startOfEvent->copy()->startOfDay();
$now = Carbon::now()->startOfDay();
$diff = $startOfEvent->diffInDays($now, false);
if ($diff >= 0) {
// The first day of the event (diff 0) should be 1.
// The seconds day of the event (diff 1) should be 2.
// Add one day to the diff.
return $diff + 1;
}
if (config('event_has_day0') && $diff < 0) {
// One day before the event (-1 diff) should day 0.
// Two days before the event (-2 diff) should be -1.
// Add one day to the diff.
return $diff + 1;
}
// This is the remaining case where the diff is negative (before event).
// One day before the event (-1 diff) should be day -1.
// Two days before the event (-2 diff) should be day -2.
// Return as it is.
return $diff;
}
}

View File

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Engelsystem\Renderer\Twig\Extensions;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\DayOfEvent;
use Engelsystem\Http\Request;
use Twig\Extension\AbstractExtension as TwigExtension;
use Twig\Extension\GlobalsInterface as GlobalsInterface;
@ -53,6 +54,7 @@ class Globals extends TwigExtension implements GlobalsInterface
'request' => $this->request,
'themeId' => $themeId,
'theme' => $theme,
'day_of_event' => DayOfEvent::get(),
];
}
}

View File

@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace Engelsystem\Test\Unit\Helpers;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Helpers\DayOfEvent;
use Engelsystem\Test\Unit\ServiceProviderTest;
class DayOfEventTest extends ServiceProviderTest
{
private const FORMAT = 'Y-m-d H:i:s';
private Config $config;
public function setUp(): void
{
$app = $this->createAndSetUpAppWithConfig([]);
$this->config = $app->get('config');
}
public function tearDown(): void
{
Carbon::setTestNow();
}
/**
* @return Array<string, array>
*/
public function provideTestGetData(): array
{
return [
'day -2 (10:00, with day 0)' => [-2, true, '2023-07-31 15:23:42', '2023-07-28 10:00:00'],
'day -2 (23:59, with day 0)' => [-2, true, '2023-07-31 15:23:42', '2023-07-28 23:59:59'],
'day -1 (23:59, with day 0)' => [-1, true, '2023-07-31 15:23:42', '2023-07-29 23:59:59'],
'day 0 (with day 0)' => [0, true, '2023-07-31 15:23:42', '2023-07-30 16:00:00'],
'day 1 (with day 0)' => [1, true, '2023-07-31 15:23:42', '2023-07-31 10:00:00'],
'day 2 (with day 0)' => [2, true, '2023-07-31 15:23:42', '2023-08-01 10:00:00'],
'day -2 (without day 0)' => [-2, false, '2023-07-31 15:23:42', '2023-07-29 10:00:00'],
'day -1 (without day 0)' => [-1, false, '2023-07-31 15:23:42', '2023-07-30 23:59:59'],
'day 1 (without day 0)' => [1, false, '2023-07-31 15:23:42', '2023-07-31 00:00:00'],
'day 2 (without day 0)' => [2, false, '2023-07-31 15:23:42', '2023-08-01 16:00:00'],
'no start date' => [null, false, null, '2023-08-01 16:00:00'],
];
}
/**
* @dataProvider provideTestGetData
* @covers \Engelsystem\Helpers\DayOfEvent
*/
public function testGet(
int | null $expected,
bool $eventHasDay0,
string | null $eventStart,
string $now
): void {
$this->config->set(
'event_start',
$eventStart ? Carbon::createFromFormat(self::FORMAT, $eventStart) : null
);
$this->config->set('event_has_day0', $eventHasDay0);
Carbon::setTestNow(Carbon::createFromFormat(self::FORMAT, $now));
$this->assertSame($expected, DayOfEvent::get());
}
}

View File

@ -6,6 +6,7 @@ namespace Engelsystem\Test\Unit\Renderer\Twig\Extensions;
use Engelsystem\Config\Config;
use Engelsystem\Helpers\Authenticator;
use Engelsystem\Helpers\Carbon;
use Engelsystem\Http\Request;
use Engelsystem\Models\User\Settings;
use Engelsystem\Models\User\User;
@ -17,6 +18,16 @@ class GlobalsTest extends ExtensionTest
{
use HasDatabase;
public static function setUpBeforeClass(): void
{
Carbon::setTestNow(Carbon::createFromFormat('Y-m-d', '2023-08-15'));
}
public static function tearDownAfterClass(): void
{
Carbon::setTestNow();
}
/**
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::__construct
* @covers \Engelsystem\Renderer\Twig\Extensions\Globals::getGlobals
@ -35,7 +46,17 @@ class GlobalsTest extends ExtensionTest
$user = User::factory()
->has(Settings::factory(['theme' => 42]))
->create();
$config = new Config(['theme' => 23, 'themes' => [42 => $theme, 23 => $theme2, 1337 => $theme3]]);
$config = new Config(
[
'event_start' => Carbon::createFromFormat('Y-m-d', '2023-08-13'),
'theme' => 23,
'themes' => [
42 => $theme,
23 => $theme2,
1337 => $theme3,
],
]
);
$auth->expects($this->exactly(4))
->method('user')
@ -49,9 +70,11 @@ class GlobalsTest extends ExtensionTest
$this->app->instance('config', $config);
$extension = new Globals($auth, $request);
$globals = $extension->getGlobals();
$this->assertGlobalsExists('day_of_event', -2, $globals);
// No user
$globals = $extension->getGlobals();
$this->assertGlobalsExists('user', [], $globals);
$this->assertGlobalsExists('user_messages', null, $globals);
$this->assertGlobalsExists('request', $request, $globals);