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;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
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"})
* @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', [
'user' => $this->getUser(),

View File

@@ -106,14 +106,17 @@ class WidgetRepository
->setTitle($widget['title'])
;
if ($widget['query'] == TimesheetRepository::STATS_QUERY_DURATION) {
if ($widget['query'] === TimesheetRepository::STATS_QUERY_DURATION) {
$model->setOption('dataType', 'duration');
} elseif ($widget['query'] == TimesheetRepository::STATS_QUERY_RATE) {
} elseif ($widget['query'] === TimesheetRepository::STATS_QUERY_RATE) {
$model->setOption('dataType', 'money');
} else {
$model->setOption('dataType', 'int');
}
if (isset($widget['user'])) {
$model->setQueryWithUser((bool) $widget['user']);
}
if (isset($widget['color'])) {
$model->setOption('color', $widget['color']);
}
@@ -130,7 +133,7 @@ class WidgetRepository
[
'userDurationToday' => [
'title' => 'stats.durationToday',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true,
'begin' => '00:00:00',
'end' => '23:59:59',
@@ -140,7 +143,7 @@ class WidgetRepository
],
'userDurationWeek' => [
'title' => 'stats.durationWeek',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true,
'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59',
@@ -150,7 +153,7 @@ class WidgetRepository
],
'userDurationMonth' => [
'title' => 'stats.durationMonth',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true,
'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59',
@@ -160,7 +163,7 @@ class WidgetRepository
],
'userDurationYear' => [
'title' => 'stats.durationYear',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true,
'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59',
@@ -170,7 +173,7 @@ class WidgetRepository
],
'userDurationTotal' => [
'title' => 'stats.durationTotal',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'user' => true,
'icon' => 'duration',
'color' => 'red',
@@ -178,7 +181,7 @@ class WidgetRepository
],
'userAmountToday' => [
'title' => 'stats.amountToday',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true,
'begin' => '00:00:00',
'end' => '23:59:59',
@@ -188,7 +191,7 @@ class WidgetRepository
],
'userAmountWeek' => [
'title' => 'stats.amountWeek',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true,
'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59',
@@ -198,7 +201,7 @@ class WidgetRepository
],
'userAmountMonth' => [
'title' => 'stats.amountMonth',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true,
'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59',
@@ -208,7 +211,7 @@ class WidgetRepository
],
'userAmountYear' => [
'title' => 'stats.amountYear',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true,
'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59',
@@ -218,7 +221,7 @@ class WidgetRepository
],
'userAmountTotal' => [
'title' => 'stats.amountTotal',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'user' => true,
'icon' => 'money',
'color' => 'red',
@@ -226,7 +229,7 @@ class WidgetRepository
],
'durationToday' => [
'title' => 'stats.durationToday',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => '00:00:00',
'end' => '23:59:59',
'icon' => 'duration',
@@ -236,7 +239,7 @@ class WidgetRepository
],
'durationWeek' => [
'title' => 'stats.durationWeek',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59',
'icon' => 'duration',
@@ -246,7 +249,7 @@ class WidgetRepository
],
'durationMonth' => [
'title' => 'stats.durationMonth',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59',
'icon' => 'duration',
@@ -256,7 +259,7 @@ class WidgetRepository
],
'durationYear' => [
'title' => 'stats.durationYear',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59',
'icon' => 'duration',
@@ -266,7 +269,7 @@ class WidgetRepository
],
'durationTotal' => [
'title' => 'stats.durationTotal',
'query' => 'duration',
'query' => TimesheetRepository::STATS_QUERY_DURATION,
'icon' => 'duration',
'color' => 'red',
'user' => false,
@@ -274,7 +277,7 @@ class WidgetRepository
],
'amountToday' => [
'title' => 'stats.amountToday',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => '00:00:00',
'end' => '23:59:59',
'icon' => 'money',
@@ -284,7 +287,7 @@ class WidgetRepository
],
'amountWeek' => [
'title' => 'stats.amountWeek',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59',
'icon' => 'money',
@@ -294,7 +297,7 @@ class WidgetRepository
],
'amountMonth' => [
'title' => 'stats.amountMonth',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59',
'icon' => 'money',
@@ -304,7 +307,7 @@ class WidgetRepository
],
'amountYear' => [
'title' => 'stats.amountYear',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59',
'icon' => 'money',
@@ -314,7 +317,7 @@ class WidgetRepository
],
'amountTotal' => [
'title' => 'stats.amountTotal',
'query' => 'rate',
'query' => TimesheetRepository::STATS_QUERY_RATE,
'icon' => 'money',
'color' => 'red',
'user' => false,
@@ -322,7 +325,7 @@ class WidgetRepository
],
'activeUsersToday' => [
'title' => 'stats.userActiveToday',
'query' => 'users',
'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => '00:00:00',
'end' => '23:59:59',
'icon' => 'user',
@@ -332,7 +335,7 @@ class WidgetRepository
],
'activeUsersWeek' => [
'title' => 'stats.userActiveWeek',
'query' => 'users',
'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => 'monday this week 00:00:00',
'end' => 'sunday this week 23:59:59',
'icon' => 'user',
@@ -342,7 +345,7 @@ class WidgetRepository
],
'activeUsersMonth' => [
'title' => 'stats.userActiveMonth',
'query' => 'users',
'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => 'first day of this month 00:00:00',
'end' => 'last day of this month 23:59:59',
'icon' => 'user',
@@ -352,7 +355,7 @@ class WidgetRepository
],
'activeUsersYear' => [
'title' => 'stats.userActiveYear',
'query' => 'users',
'query' => TimesheetRepository::STATS_QUERY_USER,
'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59',
'icon' => 'user',
@@ -362,7 +365,7 @@ class WidgetRepository
],
'activeUsersTotal' => [
'title' => 'stats.userActiveTotal',
'query' => 'users',
'query' => TimesheetRepository::STATS_QUERY_USER,
'icon' => 'user',
'color' => 'red',
'user' => false,
@@ -370,7 +373,7 @@ class WidgetRepository
],
'activeRecordings' => [
'title' => 'stats.activeRecordings',
'query' => 'active',
'query' => TimesheetRepository::STATS_QUERY_ACTIVE,
'icon' => 'duration',
'color' => 'red',
'user' => false,
@@ -378,7 +381,7 @@ class WidgetRepository
],
'userRecapThisYear' => [
'title' => 'stats.yourWorkingHours',
'query' => 'monthly',
'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true,
'begin' => '01 january this year 00:00:00',
'end' => '31 december this year 23:59:59',
@@ -388,7 +391,7 @@ class WidgetRepository
],
'userRecapLastYear' => [
'title' => 'stats.yourWorkingHours',
'query' => 'monthly',
'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true,
'begin' => '01 january last year 00:00:00',
'end' => '31 december last year 23:59:59',
@@ -398,7 +401,7 @@ class WidgetRepository
],
'userRecapTwoYears' => [
'title' => 'stats.yourWorkingHours',
'query' => 'monthly',
'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true,
'begin' => '01 january last year 00:00:00',
'end' => '31 december this year 23:59:59',
@@ -408,7 +411,7 @@ class WidgetRepository
],
'userRecapThreeYears' => [
'title' => 'stats.yourWorkingHours',
'query' => 'monthly',
'query' => TimesheetRepository::STATS_QUERY_MONTHLY,
'user' => true,
'begin' => '2 years ago first day of january 00:00:00',
'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\Widget\WidgetException;
class SimpleStatisticChart extends SimpleWidget
class SimpleStatisticChart extends SimpleWidget implements UserWidget
{
/**
* @var TimesheetRepository
@@ -32,9 +32,13 @@ class SimpleStatisticChart extends SimpleWidget
*/
private $end;
/**
* @var User
* @var User|null
*/
private $user;
/**
* @var bool
*/
private $queryWithUser = false;
public function __construct(TimesheetRepository $repository)
{
@@ -62,11 +66,9 @@ class SimpleStatisticChart extends SimpleWidget
return $this;
}
public function setUser(User $user): SimpleStatisticChart
public function setUser(User $user): void
{
$this->user = $user;
return $this;
}
public function setData($data): AbstractWidgetType
@@ -74,6 +76,13 @@ class SimpleStatisticChart extends SimpleWidget
throw new \InvalidArgumentException('Cannot set data on instances of SimpleStatisticChart');
}
public function setQueryWithUser(bool $queryWithUser): SimpleStatisticChart
{
$this->queryWithUser = $queryWithUser;
return $this;
}
/**
* @param array $options
* @return mixed|null
@@ -91,7 +100,11 @@ class SimpleStatisticChart extends SimpleWidget
$end = !empty($this->end) ? new \DateTime($this->end, $timezone) : null;
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) {
throw new WidgetException(
'Failed loading widget data: ' . $ex->getMessage()

View File

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

View File

@@ -9,10 +9,12 @@
namespace App\Tests\Widget\Type;
use App\Entity\User;
use App\Repository\TimesheetRepository;
use App\Widget\Type\AbstractWidgetType;
use App\Widget\Type\Counter;
use App\Widget\Type\SimpleWidget;
use DateTime;
/**
* @covers \App\Widget\Type\Counter
@@ -29,6 +31,41 @@ class CounterTest extends AbstractSimpleStatisticsWidgetTypeTest
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
{
return ['dataType' => 'int'];