From 8a740a25da07bd6afcfbf7509c08ec0c844ad6bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Wed, 5 Oct 2022 14:57:15 +0200 Subject: [PATCH] [Export] Feature: create a aggregator for referrer's main scope on aggregator --- .../ReferrerScopeAggregator.php | 133 ++++++++++++++++++ .../ReferrerScopeAggregatorTest.php | 63 +++++++++ .../services/exports_accompanying_course.yaml | 9 +- .../translations/messages.fr.yml | 8 ++ 4 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php create mode 100644 src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php diff --git a/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php new file mode 100644 index 000000000..3701371a7 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregator.php @@ -0,0 +1,133 @@ +scopeRepository = $scopeRepository; + $this->translatableStringHelper = $translatableStringHelper; + } + + /** + * @inheritDoc + */ + public function getLabels($key, array $values, $data) + { + return function ($value) { + if ('_header' === $value) { + return 'export.aggregator.course.by_user_scope.Referrer\'s scope'; + } + + if (null === $value) { + return ''; + } + + $scope = $this->scopeRepository->find($value); + + if (null === $scope) { + throw new \LogicException('no scope found with this id: ' . $value); + } + + return $this->translatableStringHelper->localize($scope->getName()); + }; + } + + /** + * @inheritDoc + */ + public function getQueryKeys($data) + { + return [self::SCOPE_KEY]; + } + + /** + * @inheritDoc + */ + public function buildForm(FormBuilderInterface $builder) + { + $builder->add('date_calc', ChillDateType::class, [ + 'input' => 'datetime_immutable', + 'data' => new \DateTimeImmutable('now'), + 'label' => 'export.aggregator.course.by_user_scope.Computation date for referrer', + 'required' => true, + ]); + } + + /** + * @inheritDoc + */ + public function getTitle() + { + return 'export.aggregator.course.by_user_scope.Group course by referrer\'s scope'; + } + + /** + * @inheritDoc + */ + public function addRole(): ?string + { + return null; + } + + /** + * @inheritDoc + */ + public function alterQuery(QueryBuilder $qb, $data) + { + $userHistory = 'acp_agg_refscope_user_history'; + $ref = 'acp_agg_refscope_user_history_ref'; + $scopeName = self::SCOPE_KEY; + $dateCalc = 'acp_agg_refscope_user_history_date_calc'; + + $qb + ->leftJoin('acp.userHistories', $userHistory) + ->leftJoin($userHistory.'.user', $ref) + ->andWhere( + $qb->expr()->orX( + $qb->expr()->isNull($userHistory), + $qb->expr()->andX( + $qb->expr()->lte($userHistory.'.startDate', ':'.$dateCalc), + $qb->expr()->orX( + $qb->expr()->isNull($userHistory.'.endDate'), + $qb->expr()->lt($userHistory.'.endDate', ':'.$dateCalc) + ) + ) + ) + ) + ->setParameter($dateCalc, $data['date_calc']); + + // add groups + $qb + ->addSelect('IDENTITY('.$ref.'.mainScope) AS '.$scopeName) + ->addGroupBy($scopeName) + ; + } + + /** + * @inheritDoc + */ + public function applyOn() + { + return Declarations::ACP_TYPE; + } +} diff --git a/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php new file mode 100644 index 000000000..6069a024d --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Tests/Export/Aggregator/AccompanyingCourseAggregators/ReferrerScopeAggregatorTest.php @@ -0,0 +1,63 @@ +prophesize(TranslatableStringHelperInterface::class); + $translatableStringHelper->localize(Argument::type('array'))->willReturn('localized'); + + $scopeRepository = $this->prophesize(ScopeRepositoryInterface::class); + $scopeRepository->find(Argument::type('int'))->willReturn( + (new Scope())->setName(['fr' => 'scope']) + ); + + return new ReferrerScopeAggregator( + $scopeRepository->reveal(), + $translatableStringHelper->reveal() + ); + } + + public function getFormData() + { + return [ + [ + 'date_calc' => new \DateTimeImmutable('now') + ] + ]; + } + + public function getQueryBuilders() + { + if (null === self::$kernel) { + self::bootKernel(); + } + + $em = self::$container->get(EntityManagerInterface::class); + + return [ + $em->createQueryBuilder() + ->select('count(acp.id)') + ->from(AccompanyingPeriod::class, 'acp') + ->join('acp.scopes', 'acpscope') + , + ]; + } + + +} diff --git a/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml b/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml index 8a8b20cbe..874f28b72 100644 --- a/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml +++ b/src/Bundle/ChillPersonBundle/config/services/exports_accompanying_course.yaml @@ -148,7 +148,7 @@ services: autowire: true autoconfigure: true tags: - - { name: chill.export_aggregator, alias: accompanyingcourse_referrer_scope_aggregator } + - { name: chill.export_aggregator, alias: accompanyingcourse_scope_aggregator } chill.person.export.aggregator_referrer_job: class: Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators\JobAggregator @@ -255,3 +255,10 @@ services: tags: - { name: chill.export_aggregator, alias: accompanyingcourse_duration_aggregator } + Chill\PersonBundle\Export\Aggregator\AccompanyingCourseAggregators\REferrerScopeAggregator: + autoconfigure: true + autowire: true + tags: + - { name: chill.export_aggregator, alias: accompanyingcourse_ref_scope_aggregator } + + diff --git a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml index d8c60fca4..664498a95 100644 --- a/src/Bundle/ChillPersonBundle/translations/messages.fr.yml +++ b/src/Bundle/ChillPersonBundle/translations/messages.fr.yml @@ -941,3 +941,11 @@ reassign: notification: Notify referrer: Notifier le référent Notify any: Notifier d'autres utilisateurs + +export: + aggregator: + course: + by_user_scope: + Group course by referrer's scope: Grouper les parcours par service du référent + Computation date for referrer: Date à laquelle le référent était actif + Referrer's scope: Service du référent de parcours