query widget data for user (#1917)

This commit is contained in:
Kevin Papst
2020-08-24 18:37:42 +02:00
committed by GitHub
parent c2c4f087e4
commit 8464cd925e
5 changed files with 95 additions and 40 deletions

View File

@@ -10,7 +10,6 @@
namespace App\Controller; namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Routing\Annotation\Route;
@@ -24,7 +23,7 @@ final class WidgetController extends AbstractController
* @Route(path="/working-time/{year}/{week}", requirements={"year": "[1-9]\d*", "week": "[0-9]\d*"}, name="widgets_working_time_chart", methods={"GET"}) * @Route(path="/working-time/{year}/{week}", requirements={"year": "[1-9]\d*", "week": "[0-9]\d*"}, name="widgets_working_time_chart", methods={"GET"})
* @Security("is_granted('view_own_timesheet')") * @Security("is_granted('view_own_timesheet')")
*/ */
public function workingtimechartAction($year, $week, Request $request): Response public function workingtimechartAction($year, $week): Response
{ {
return $this->render('widget/paginatedworkingtimechart.html.twig', [ return $this->render('widget/paginatedworkingtimechart.html.twig', [
'user' => $this->getUser(), 'user' => $this->getUser(),

View File

@@ -106,14 +106,17 @@ class WidgetRepository
->setTitle($widget['title']) ->setTitle($widget['title'])
; ;
if ($widget['query'] == TimesheetRepository::STATS_QUERY_DURATION) { if ($widget['query'] === TimesheetRepository::STATS_QUERY_DURATION) {
$model->setOption('dataType', 'duration'); $model->setOption('dataType', 'duration');
} elseif ($widget['query'] == TimesheetRepository::STATS_QUERY_RATE) { } elseif ($widget['query'] === TimesheetRepository::STATS_QUERY_RATE) {
$model->setOption('dataType', 'money'); $model->setOption('dataType', 'money');
} else { } else {
$model->setOption('dataType', 'int'); $model->setOption('dataType', 'int');
} }
if (isset($widget['user'])) {
$model->setQueryWithUser((bool) $widget['user']);
}
if (isset($widget['color'])) { if (isset($widget['color'])) {
$model->setOption('color', $widget['color']); $model->setOption('color', $widget['color']);
} }
@@ -130,7 +133,7 @@ class WidgetRepository
[ [
'userDurationToday' => [ 'userDurationToday' => [
'title' => 'stats.durationToday', 'title' => 'stats.durationToday',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true, 'user' => true,
'begin' => '00:00:00', 'begin' => '00:00:00',
'end' => '23:59:59', 'end' => '23:59:59',
@@ -140,7 +143,7 @@ class WidgetRepository
], ],
'userDurationWeek' => [ 'userDurationWeek' => [
'title' => 'stats.durationWeek', 'title' => 'stats.durationWeek',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true, 'user' => true,
'begin' => 'monday this week 00:00:00', 'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59', 'end' => 'sunday this week 23:59:59',
@@ -150,7 +153,7 @@ class WidgetRepository
], ],
'userDurationMonth' => [ 'userDurationMonth' => [
'title' => 'stats.durationMonth', 'title' => 'stats.durationMonth',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true, 'user' => true,
'begin' => 'first day of this month 00:00:00', 'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59', 'end' => 'last day of this month 23:59:59',
@@ -160,7 +163,7 @@ class WidgetRepository
], ],
'userDurationYear' => [ 'userDurationYear' => [
'title' => 'stats.durationYear', 'title' => 'stats.durationYear',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true, 'user' => true,
'begin' => '01 january this year 00:00:00', 'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59', 'end' => '31 december this year 23:59:59',
@@ -170,7 +173,7 @@ class WidgetRepository
], ],
'userDurationTotal' => [ 'userDurationTotal' => [
'title' => 'stats.durationTotal', 'title' => 'stats.durationTotal',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true, 'user' => true,
'icon' => 'duration', 'icon' => 'duration',
'color' => 'red', 'color' => 'red',
@@ -178,7 +181,7 @@ class WidgetRepository
], ],
'userAmountToday' => [ 'userAmountToday' => [
'title' => 'stats.amountToday', 'title' => 'stats.amountToday',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true, 'user' => true,
'begin' => '00:00:00', 'begin' => '00:00:00',
'end' => '23:59:59', 'end' => '23:59:59',
@@ -188,7 +191,7 @@ class WidgetRepository
], ],
'userAmountWeek' => [ 'userAmountWeek' => [
'title' => 'stats.amountWeek', 'title' => 'stats.amountWeek',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true, 'user' => true,
'begin' => 'monday this week 00:00:00', 'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59', 'end' => 'sunday this week 23:59:59',
@@ -198,7 +201,7 @@ class WidgetRepository
], ],
'userAmountMonth' => [ 'userAmountMonth' => [
'title' => 'stats.amountMonth', 'title' => 'stats.amountMonth',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true, 'user' => true,
'begin' => 'first day of this month 00:00:00', 'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59', 'end' => 'last day of this month 23:59:59',
@@ -208,7 +211,7 @@ class WidgetRepository
], ],
'userAmountYear' => [ 'userAmountYear' => [
'title' => 'stats.amountYear', 'title' => 'stats.amountYear',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true, 'user' => true,
'begin' => '01 january this year 00:00:00', 'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59', 'end' => '31 december this year 23:59:59',
@@ -218,7 +221,7 @@ class WidgetRepository
], ],
'userAmountTotal' => [ 'userAmountTotal' => [
'title' => 'stats.amountTotal', 'title' => 'stats.amountTotal',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true, 'user' => true,
'icon' => 'money', 'icon' => 'money',
'color' => 'red', 'color' => 'red',
@@ -226,7 +229,7 @@ class WidgetRepository
], ],
'durationToday' => [ 'durationToday' => [
'title' => 'stats.durationToday', 'title' => 'stats.durationToday',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => '00:00:00', 'begin' => '00:00:00',
'end' => '23:59:59', 'end' => '23:59:59',
'icon' => 'duration', 'icon' => 'duration',
@@ -236,7 +239,7 @@ class WidgetRepository
], ],
'durationWeek' => [ 'durationWeek' => [
'title' => 'stats.durationWeek', 'title' => 'stats.durationWeek',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => 'monday this week 00:00:00', 'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59', 'end' => 'sunday this week 23:59:59',
'icon' => 'duration', 'icon' => 'duration',
@@ -246,7 +249,7 @@ class WidgetRepository
], ],
'durationMonth' => [ 'durationMonth' => [
'title' => 'stats.durationMonth', 'title' => 'stats.durationMonth',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => 'first day of this month 00:00:00', 'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59', 'end' => 'last day of this month 23:59:59',
'icon' => 'duration', 'icon' => 'duration',
@@ -256,7 +259,7 @@ class WidgetRepository
], ],
'durationYear' => [ 'durationYear' => [
'title' => 'stats.durationYear', 'title' => 'stats.durationYear',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => '01 january this year 00:00:00', 'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59', 'end' => '31 december this year 23:59:59',
'icon' => 'duration', 'icon' => 'duration',
@@ -266,7 +269,7 @@ class WidgetRepository
], ],
'durationTotal' => [ 'durationTotal' => [
'title' => 'stats.durationTotal', 'title' => 'stats.durationTotal',
'query' => 'duration', 'query' => TimesheetRepository::STATS_QUERY_DURATION,
'icon' => 'duration', 'icon' => 'duration',
'color' => 'red', 'color' => 'red',
'user' => false, 'user' => false,
@@ -274,7 +277,7 @@ class WidgetRepository
], ],
'amountToday' => [ 'amountToday' => [
'title' => 'stats.amountToday', 'title' => 'stats.amountToday',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => '00:00:00', 'begin' => '00:00:00',
'end' => '23:59:59', 'end' => '23:59:59',
'icon' => 'money', 'icon' => 'money',
@@ -284,7 +287,7 @@ class WidgetRepository
], ],
'amountWeek' => [ 'amountWeek' => [
'title' => 'stats.amountWeek', 'title' => 'stats.amountWeek',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => 'monday this week 00:00:00', 'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59', 'end' => 'sunday this week 23:59:59',
'icon' => 'money', 'icon' => 'money',
@@ -294,7 +297,7 @@ class WidgetRepository
], ],
'amountMonth' => [ 'amountMonth' => [
'title' => 'stats.amountMonth', 'title' => 'stats.amountMonth',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => 'first day of this month 00:00:00', 'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59', 'end' => 'last day of this month 23:59:59',
'icon' => 'money', 'icon' => 'money',
@@ -304,7 +307,7 @@ class WidgetRepository
], ],
'amountYear' => [ 'amountYear' => [
'title' => 'stats.amountYear', 'title' => 'stats.amountYear',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => '01 january this year 00:00:00', 'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59', 'end' => '31 december this year 23:59:59',
'icon' => 'money', 'icon' => 'money',
@@ -314,7 +317,7 @@ class WidgetRepository
], ],
'amountTotal' => [ 'amountTotal' => [
'title' => 'stats.amountTotal', 'title' => 'stats.amountTotal',
'query' => 'rate', 'query' => TimesheetRepository::STATS_QUERY_RATE,
'icon' => 'money', 'icon' => 'money',
'color' => 'red', 'color' => 'red',
'user' => false, 'user' => false,
@@ -322,7 +325,7 @@ class WidgetRepository
], ],
'activeUsersToday' => [ 'activeUsersToday' => [
'title' => 'stats.userActiveToday', 'title' => 'stats.userActiveToday',
'query' => 'users', 'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => '00:00:00', 'begin' => '00:00:00',
'end' => '23:59:59', 'end' => '23:59:59',
'icon' => 'user', 'icon' => 'user',
@@ -332,7 +335,7 @@ class WidgetRepository
], ],
'activeUsersWeek' => [ 'activeUsersWeek' => [
'title' => 'stats.userActiveWeek', 'title' => 'stats.userActiveWeek',
'query' => 'users', 'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => 'monday this week 00:00:00', 'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59', 'end' => 'sunday this week 23:59:59',
'icon' => 'user', 'icon' => 'user',
@@ -342,7 +345,7 @@ class WidgetRepository
], ],
'activeUsersMonth' => [ 'activeUsersMonth' => [
'title' => 'stats.userActiveMonth', 'title' => 'stats.userActiveMonth',
'query' => 'users', 'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => 'first day of this month 00:00:00', 'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59', 'end' => 'last day of this month 23:59:59',
'icon' => 'user', 'icon' => 'user',
@@ -352,7 +355,7 @@ class WidgetRepository
], ],
'activeUsersYear' => [ 'activeUsersYear' => [
'title' => 'stats.userActiveYear', 'title' => 'stats.userActiveYear',
'query' => 'users', 'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => '01 january this year 00:00:00', 'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59', 'end' => '31 december this year 23:59:59',
'icon' => 'user', 'icon' => 'user',
@@ -362,7 +365,7 @@ class WidgetRepository
], ],
'activeUsersTotal' => [ 'activeUsersTotal' => [
'title' => 'stats.userActiveTotal', 'title' => 'stats.userActiveTotal',
'query' => 'users', 'query' => TimesheetRepository::STATS_QUERY_USER,
'icon' => 'user', 'icon' => 'user',
'color' => 'red', 'color' => 'red',
'user' => false, 'user' => false,
@@ -370,7 +373,7 @@ class WidgetRepository
], ],
'activeRecordings' => [ 'activeRecordings' => [
'title' => 'stats.activeRecordings', 'title' => 'stats.activeRecordings',
'query' => 'active', 'query' => TimesheetRepository::STATS_QUERY_ACTIVE,
'icon' => 'duration', 'icon' => 'duration',
'color' => 'red', 'color' => 'red',
'user' => false, 'user' => false,
@@ -378,7 +381,7 @@ class WidgetRepository
], ],
'userRecapThisYear' => [ 'userRecapThisYear' => [
'title' => 'stats.yourWorkingHours', 'title' => 'stats.yourWorkingHours',
'query' => 'monthly', 'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true, 'user' => true,
'begin' => '01 january this year 00:00:00', 'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59', 'end' => '31 december this year 23:59:59',
@@ -388,7 +391,7 @@ class WidgetRepository
], ],
'userRecapLastYear' => [ 'userRecapLastYear' => [
'title' => 'stats.yourWorkingHours', 'title' => 'stats.yourWorkingHours',
'query' => 'monthly', 'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true, 'user' => true,
'begin' => '01 january last year 00:00:00', 'begin' => '01 january last year 00:00:00',
'end' => '31 december last year 23:59:59', 'end' => '31 december last year 23:59:59',
@@ -398,7 +401,7 @@ class WidgetRepository
], ],
'userRecapTwoYears' => [ 'userRecapTwoYears' => [
'title' => 'stats.yourWorkingHours', 'title' => 'stats.yourWorkingHours',
'query' => 'monthly', 'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true, 'user' => true,
'begin' => '01 january last year 00:00:00', 'begin' => '01 january last year 00:00:00',
'end' => '31 december this year 23:59:59', 'end' => '31 december this year 23:59:59',
@@ -408,7 +411,7 @@ class WidgetRepository
], ],
'userRecapThreeYears' => [ 'userRecapThreeYears' => [
'title' => 'stats.yourWorkingHours', 'title' => 'stats.yourWorkingHours',
'query' => 'monthly', 'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true, 'user' => true,
'begin' => '2 years ago first day of january 00:00:00', 'begin' => '2 years ago first day of january 00:00:00',
'end' => 'this year last day of december 23:59:59', 'end' => 'this year last day of december 23:59:59',

View File

@@ -13,7 +13,7 @@ use App\Entity\User;
use App\Repository\TimesheetRepository; use App\Repository\TimesheetRepository;
use App\Widget\WidgetException; use App\Widget\WidgetException;
class SimpleStatisticChart extends SimpleWidget class SimpleStatisticChart extends SimpleWidget implements UserWidget
{ {
/** /**
* @var TimesheetRepository * @var TimesheetRepository
@@ -32,9 +32,13 @@ class SimpleStatisticChart extends SimpleWidget
*/ */
private $end; private $end;
/** /**
* @var User * @var User|null
*/ */
private $user; private $user;
/**
* @var bool
*/
private $queryWithUser = false;
public function __construct(TimesheetRepository $repository) public function __construct(TimesheetRepository $repository)
{ {
@@ -62,11 +66,9 @@ class SimpleStatisticChart extends SimpleWidget
return $this; return $this;
} }
public function setUser(User $user): SimpleStatisticChart public function setUser(User $user): void
{ {
$this->user = $user; $this->user = $user;
return $this;
} }
public function setData($data): AbstractWidgetType public function setData($data): AbstractWidgetType
@@ -74,6 +76,13 @@ class SimpleStatisticChart extends SimpleWidget
throw new \InvalidArgumentException('Cannot set data on instances of SimpleStatisticChart'); throw new \InvalidArgumentException('Cannot set data on instances of SimpleStatisticChart');
} }
public function setQueryWithUser(bool $queryWithUser): SimpleStatisticChart
{
$this->queryWithUser = $queryWithUser;
return $this;
}
/** /**
* @param array $options * @param array $options
* @return mixed|null * @return mixed|null
@@ -91,7 +100,11 @@ class SimpleStatisticChart extends SimpleWidget
$end = !empty($this->end) ? new \DateTime($this->end, $timezone) : null; $end = !empty($this->end) ? new \DateTime($this->end, $timezone) : null;
try { try {
return $this->repository->getStatistic($this->query, $begin, $end, $this->user); if (true === $this->queryWithUser) {
return $this->repository->getStatistic($this->query, $begin, $end, $this->user);
} else {
return $this->repository->getStatistic($this->query, $begin, $end, null);
}
} catch (\Exception $ex) { } catch (\Exception $ex) {
throw new WidgetException( throw new WidgetException(
'Failed loading widget data: ' . $ex->getMessage() 'Failed loading widget data: ' . $ex->getMessage()

View File

@@ -9,6 +9,8 @@
namespace App\Tests\Widget\Type; namespace App\Tests\Widget\Type;
use App\Widget\Type\SimpleStatisticChart;
/** /**
* @covers \App\Widget\Type\SimpleStatisticChart * @covers \App\Widget\Type\SimpleStatisticChart
*/ */
@@ -17,6 +19,7 @@ abstract class AbstractSimpleStatisticsWidgetTypeTest extends AbstractWidgetType
public function testData() public function testData()
{ {
$sut = $this->createSut(); $sut = $this->createSut();
self::assertInstanceOf(SimpleStatisticChart::class, $sut);
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart'); $this->expectExceptionMessage('Cannot set data on instances of SimpleStatisticChart');

View File

@@ -9,10 +9,12 @@
namespace App\Tests\Widget\Type; namespace App\Tests\Widget\Type;
use App\Entity\User;
use App\Repository\TimesheetRepository; use App\Repository\TimesheetRepository;
use App\Widget\Type\AbstractWidgetType; use App\Widget\Type\AbstractWidgetType;
use App\Widget\Type\Counter; use App\Widget\Type\Counter;
use App\Widget\Type\SimpleWidget; use App\Widget\Type\SimpleWidget;
use DateTime;
/** /**
* @covers \App\Widget\Type\Counter * @covers \App\Widget\Type\Counter
@@ -29,6 +31,41 @@ class CounterTest extends AbstractSimpleStatisticsWidgetTypeTest
return $sut; return $sut;
} }
public function testQueryWithUser()
{
$user = new User();
$user->setAlias('foo');
$repository = $this->createMock(TimesheetRepository::class);
$repository->expects($this->once())->method('getStatistic')->willReturnCallback(function (string $type, ?DateTime $begin, ?DateTime $end, ?User $user) {
self::assertEquals($type, 'active');
self::assertNull($begin);
self::assertNull($end);
self::assertNull($user);
});
$sut = new Counter($repository);
$sut->setQuery(TimesheetRepository::STATS_QUERY_ACTIVE);
$sut->setUser($user);
$sut->getData([]);
$user = new User();
$user->setAlias('bar');
$repository = $this->createMock(TimesheetRepository::class);
$repository->expects($this->once())->method('getStatistic')->willReturnCallback(function (string $type, ?DateTime $begin, ?DateTime $end, ?User $user) {
self::assertEquals($type, 'active');
self::assertNull($begin);
self::assertNull($end);
self::assertNotNull($user);
self::assertEquals('bar', $user->getAlias());
});
$sut = new Counter($repository);
$sut->setQuery(TimesheetRepository::STATS_QUERY_ACTIVE);
$sut->setUser($user);
$sut->setQueryWithUser(true);
$sut->getData([]);
}
public function getDefaultOptions(): array public function getDefaultOptions(): array
{ {
return ['dataType' => 'int']; return ['dataType' => 'int'];