From 67c3de733faad7dcc8ce09ff36afe8b9a15da11b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 7 Feb 2024 16:40:03 +0100 Subject: [PATCH] Refactor ActivityReasonAggregator: can be applied also on export for Activities linked with accompanying period This commit renames the ActivityReasonAggregator's namespace from PersonAggregators to Aggregator and modifies its methods. The join method in the query builder has been changed from innerJoin to leftJoin, 'group by' part is simplified, and the applyOn method now returns Declarations::ACTIVITY instead of Declarations::ACTIVITY_PERSON. Further modifications apply to the getFormDefaultData method and the corresponding test. --- .../unreleased/Feature-20240207-164038.yaml | 5 ++++ .../ActivityReasonAggregator.php | 26 +++++++++---------- .../ActivityReasonAggregatorTest.php | 23 +++++++++------- .../config/services/export.yaml | 2 +- 4 files changed, 32 insertions(+), 24 deletions(-) create mode 100644 .changes/unreleased/Feature-20240207-164038.yaml rename src/Bundle/ChillActivityBundle/Export/Aggregator/{PersonAggregators => }/ActivityReasonAggregator.php (86%) rename src/Bundle/ChillActivityBundle/Tests/Export/Aggregator/{PersonAggregators => }/ActivityReasonAggregatorTest.php (69%) diff --git a/.changes/unreleased/Feature-20240207-164038.yaml b/.changes/unreleased/Feature-20240207-164038.yaml new file mode 100644 index 000000000..ff5096036 --- /dev/null +++ b/.changes/unreleased/Feature-20240207-164038.yaml @@ -0,0 +1,5 @@ +kind: Feature +body: Allow to group activities linked with accompanying period by reason +time: 2024-02-07T16:40:38.408575109+01:00 +custom: + Issue: "229" diff --git a/src/Bundle/ChillActivityBundle/Export/Aggregator/PersonAggregators/ActivityReasonAggregator.php b/src/Bundle/ChillActivityBundle/Export/Aggregator/ActivityReasonAggregator.php similarity index 86% rename from src/Bundle/ChillActivityBundle/Export/Aggregator/PersonAggregators/ActivityReasonAggregator.php rename to src/Bundle/ChillActivityBundle/Export/Aggregator/ActivityReasonAggregator.php index 569c4ca9a..9945fd80a 100644 --- a/src/Bundle/ChillActivityBundle/Export/Aggregator/PersonAggregators/ActivityReasonAggregator.php +++ b/src/Bundle/ChillActivityBundle/Export/Aggregator/ActivityReasonAggregator.php @@ -9,7 +9,7 @@ declare(strict_types=1); * the LICENSE file that was distributed with this source code. */ -namespace Chill\ActivityBundle\Export\Aggregator\PersonAggregators; +namespace Chill\ActivityBundle\Export\Aggregator; use Chill\ActivityBundle\Export\Declarations; use Chill\ActivityBundle\Repository\ActivityReasonCategoryRepository; @@ -25,8 +25,11 @@ use Symfony\Component\Validator\Context\ExecutionContextInterface; class ActivityReasonAggregator implements AggregatorInterface, ExportElementValidatedInterface { - public function __construct(protected ActivityReasonCategoryRepository $activityReasonCategoryRepository, protected ActivityReasonRepository $activityReasonRepository, protected TranslatableStringHelper $translatableStringHelper) - { + public function __construct( + protected ActivityReasonCategoryRepository $activityReasonCategoryRepository, + protected ActivityReasonRepository $activityReasonRepository, + protected TranslatableStringHelper $translatableStringHelper + ) { } public function addRole(): ?string @@ -51,7 +54,7 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali // make a jointure only if needed if (!\in_array('actreasons', $qb->getAllAliases(), true)) { - $qb->innerJoin('activity.reasons', 'actreasons'); + $qb->leftJoin('activity.reasons', 'actreasons'); } // join category if necessary @@ -62,19 +65,12 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali } } - // add the "group by" part - $groupBy = $qb->getDQLPart('groupBy'); - - if (\count($groupBy) > 0) { - $qb->addGroupBy($alias); - } else { - $qb->groupBy($alias); - } + $qb->addGroupBy($alias); } public function applyOn(): string { - return Declarations::ACTIVITY_PERSON; + return Declarations::ACTIVITY; } public function buildForm(FormBuilderInterface $builder) @@ -96,7 +92,9 @@ class ActivityReasonAggregator implements AggregatorInterface, ExportElementVali public function getFormDefaultData(): array { - return []; + return [ + 'level' => 'reasons', + ]; } public function getLabels($key, array $values, $data) diff --git a/src/Bundle/ChillActivityBundle/Tests/Export/Aggregator/PersonAggregators/ActivityReasonAggregatorTest.php b/src/Bundle/ChillActivityBundle/Tests/Export/Aggregator/ActivityReasonAggregatorTest.php similarity index 69% rename from src/Bundle/ChillActivityBundle/Tests/Export/Aggregator/PersonAggregators/ActivityReasonAggregatorTest.php rename to src/Bundle/ChillActivityBundle/Tests/Export/Aggregator/ActivityReasonAggregatorTest.php index 004de0b99..3e1d473f0 100644 --- a/src/Bundle/ChillActivityBundle/Tests/Export/Aggregator/PersonAggregators/ActivityReasonAggregatorTest.php +++ b/src/Bundle/ChillActivityBundle/Tests/Export/Aggregator/ActivityReasonAggregatorTest.php @@ -9,10 +9,10 @@ declare(strict_types=1); * the LICENSE file that was distributed with this source code. */ -namespace Chill\ActivityBundle\Tests\Export\Aggregator\PersonAggregators; +namespace Chill\ActivityBundle\Tests\Export\Aggregator; use Chill\ActivityBundle\Entity\Activity; -use Chill\ActivityBundle\Export\Aggregator\PersonAggregators\ActivityReasonAggregator; +use Chill\ActivityBundle\Export\Aggregator\ActivityReasonAggregator; use Chill\MainBundle\Test\Export\AbstractAggregatorTest; use Doctrine\ORM\EntityManagerInterface; use Prophecy\PhpUnit\ProphecyTrait; @@ -33,14 +33,14 @@ final class ActivityReasonAggregatorTest extends AbstractAggregatorTest self::bootKernel(); $this->aggregator = self::$container->get(ActivityReasonAggregator::class); + /* + $request = $this->prophesize() + ->willExtend(\Symfony\Component\HttpFoundation\Request::class); - $request = $this->prophesize() - ->willExtend(\Symfony\Component\HttpFoundation\Request::class); + $request->getLocale()->willReturn('fr'); - $request->getLocale()->willReturn('fr'); - - self::$container->get('request_stack') - ->push($request->reveal()); + self::$container->get('request_stack') + ->push($request->reveal());*/ } public function getAggregator() @@ -65,7 +65,12 @@ final class ActivityReasonAggregatorTest extends AbstractAggregatorTest return [ $em->createQueryBuilder() ->select('count(activity.id)') - ->from(Activity::class, 'activity'), + ->from(Activity::class, 'activity') + ->join('activity.person', 'person'), + $em->createQueryBuilder() + ->select('count(activity.id)') + ->from(Activity::class, 'activity') + ->join('activity.accompanyingPeriod', 'accompanyingPeriod'), $em->createQueryBuilder() ->select('count(activity.id)') ->from(Activity::class, 'activity') diff --git a/src/Bundle/ChillActivityBundle/config/services/export.yaml b/src/Bundle/ChillActivityBundle/config/services/export.yaml index ef691ba0d..b52911295 100644 --- a/src/Bundle/ChillActivityBundle/config/services/export.yaml +++ b/src/Bundle/ChillActivityBundle/config/services/export.yaml @@ -153,7 +153,7 @@ services: ## Aggregators - Chill\ActivityBundle\Export\Aggregator\PersonAggregators\ActivityReasonAggregator: + Chill\ActivityBundle\Export\Aggregator\ActivityReasonAggregator: tags: - { name: chill.export_aggregator, alias: activity_reason_aggregator }