From 85504d72c29ae255ef3682996451a51648be90a3 Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Wed, 15 Nov 2023 15:31:18 +0100 Subject: [PATCH] reorganize exports calendar/ refactor config yaml --- .../{ => LinkedToAcp}/CountCalendars.php | 7 +- .../LinkedToAcp/StatCalendarAvgDuration.php | 109 ++++++++++++ .../LinkedToAcp/StatCalendarSumDuration.php | 109 ++++++++++++ .../Export/LinkedToPerson/CountCalendars.php | 140 +++++++++++++++ .../StatCalendarAvgDuration.php | 4 +- .../StatCalendarSumDuration.php | 4 +- .../Resources/config/services/exports.yaml | 162 ++++++++---------- 7 files changed, 437 insertions(+), 98 deletions(-) rename src/Bundle/ChillCalendarBundle/Export/Export/{ => LinkedToAcp}/CountCalendars.php (89%) create mode 100644 src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarAvgDuration.php create mode 100644 src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarSumDuration.php create mode 100644 src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/CountCalendars.php rename src/Bundle/ChillCalendarBundle/Export/Export/{ => LinkedToPerson}/StatCalendarAvgDuration.php (95%) rename src/Bundle/ChillCalendarBundle/Export/Export/{ => LinkedToPerson}/StatCalendarSumDuration.php (95%) diff --git a/src/Bundle/ChillCalendarBundle/Export/Export/CountCalendars.php b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/CountCalendars.php similarity index 89% rename from src/Bundle/ChillCalendarBundle/Export/Export/CountCalendars.php rename to src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/CountCalendars.php index f643eaa68..669926ef6 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Export/CountCalendars.php +++ b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/CountCalendars.php @@ -9,7 +9,7 @@ declare(strict_types=1); * the LICENSE file that was distributed with this source code. */ -namespace Chill\CalendarBundle\Export\Export; +namespace Chill\CalendarBundle\Export\Export\LinkedToAcp; use Chill\CalendarBundle\Export\Declarations; use Chill\CalendarBundle\Repository\CalendarRepository; @@ -17,9 +17,12 @@ use Chill\MainBundle\Export\AccompanyingCourseExportHelper; use Chill\MainBundle\Export\ExportInterface; use Chill\MainBundle\Export\FormatterInterface; use Chill\MainBundle\Export\GroupedExportInterface; +use Chill\PersonBundle\Entity\AccompanyingPeriodParticipation; +use Chill\PersonBundle\Entity\Person\PersonCenterHistory; use Chill\PersonBundle\Security\Authorization\PersonVoter; use Doctrine\ORM\AbstractQuery; use Doctrine\ORM\QueryBuilder; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\Validator\Exception\LogicException; @@ -51,7 +54,7 @@ class CountCalendars implements ExportInterface, GroupedExportInterface public function getGroup(): string { - return 'Exports of calendar'; + return 'Exports of calendars linked to an accompanying period'; } public function getLabels($key, array $values, $data) diff --git a/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarAvgDuration.php b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarAvgDuration.php new file mode 100644 index 000000000..e6d27cee7 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarAvgDuration.php @@ -0,0 +1,109 @@ +getTitle(); + + return static fn ($value) => $labels[$value]; + } + + public function getQueryKeys($data): array + { + return ['export_result']; + } + + public function getResult($query, $data) + { + return $query->getQuery()->getResult(Query::HYDRATE_SCALAR); + } + + public function getTitle(): string + { + return 'Average calendar duration'; + } + + public function getType(): string + { + return Declarations::CALENDAR_TYPE; + } + + public function initiateQuery(array $requiredModifiers, array $acl, array $data = []): QueryBuilder + { + $qb = $this->calendarRepository->createQueryBuilder('cal'); + + $qb->select('AVG(cal.endDate - cal.startDate) AS export_result'); + $qb->join('cal.accompanyingPeriod', 'acp'); + + AccompanyingCourseExportHelper::addClosingMotiveExclusionClause($qb); + + return $qb; + } + + public function requiredRole(): string + { + return AccompanyingPeriodVoter::STATS; + } + + public function supportsModifiers(): array + { + return [ + Declarations::CALENDAR_TYPE, + ]; + } +} diff --git a/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarSumDuration.php b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarSumDuration.php new file mode 100644 index 000000000..e16d0185c --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToAcp/StatCalendarSumDuration.php @@ -0,0 +1,109 @@ +getTitle(); + + return static fn ($value) => $labels[$value]; + } + + public function getQueryKeys($data): array + { + return ['export_result']; + } + + public function getResult($query, $data) + { + return $query->getQuery()->getResult(Query::HYDRATE_SCALAR); + } + + public function getTitle(): string + { + return 'Sum of calendar durations'; + } + + public function getType(): string + { + return Declarations::CALENDAR_TYPE; + } + + public function initiateQuery(array $requiredModifiers, array $acl, array $data = []): QueryBuilder + { + $qb = $this->calendarRepository->createQueryBuilder('cal'); + + $qb->select('SUM(cal.endDate - cal.startDate) AS export_result'); + $qb->join('cal.accompanyingPeriod', 'acp'); + + AccompanyingCourseExportHelper::addClosingMotiveExclusionClause($qb); + + return $qb; + } + + public function requiredRole(): string + { + return AccompanyingPeriodVoter::STATS; + } + + public function supportsModifiers(): array + { + return [ + Declarations::CALENDAR_TYPE, + ]; + } +} diff --git a/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/CountCalendars.php b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/CountCalendars.php new file mode 100644 index 000000000..8cad77f50 --- /dev/null +++ b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/CountCalendars.php @@ -0,0 +1,140 @@ +filterStatsByCenters = $parameterBag->get('chill_main')['acl']['filter_stats_by_center']; + } + + public function buildForm(FormBuilderInterface $builder) + { + // No form necessary + } + + public function getFormDefaultData(): array + { + return []; + } + + public function getAllowedFormattersTypes(): array + { + return [FormatterInterface::TYPE_TABULAR]; + } + + public function getDescription(): string + { + return 'Count calendars by various parameters.'; + } + + public function getGroup(): string + { + return 'Exports of calendars linked to a person'; + } + + public function getLabels($key, array $values, $data) + { + if ('export_result' !== $key) { + throw new LogicException("the key {$key} is not used by this export"); + } + + $labels = array_combine($values, $values); + $labels['_header'] = $this->getTitle(); + + return static fn ($value) => $labels[$value]; + } + + public function getQueryKeys($data): array + { + return ['export_result']; + } + + public function getResult($query, $data) + { + return $query->getQuery()->getResult(AbstractQuery::HYDRATE_SCALAR); + } + + public function getTitle(): string + { + return 'Count calendars'; + } + + public function getType(): string + { + return Declarations::CALENDAR_TYPE; + } + + /** + * Initiate the query. + */ + public function initiateQuery(array $requiredModifiers, array $acl, array $data = []): QueryBuilder + { + $centers = array_map(static fn ($el) => $el['center'], $acl); + + $qb = $this->calendarRepository->createQueryBuilder('cal'); + + $qb->select('COUNT(cal.id) AS export_result'); + $qb->leftJoin('cal.accompanyingPeriod', 'acp'); + + if ($this->filterStatsByCenters) { + $qb + ->andWhere( + $qb->expr()->exists( + 'SELECT 1 FROM '.AccompanyingPeriodParticipation::class.' acl_count_part + JOIN '.PersonCenterHistory::class.' acl_count_person_history WITH IDENTITY(acl_count_person_history.person) = IDENTITY(acl_count_part.person) + WHERE acl_count_part.accompanyingPeriod = acp.id AND acl_count_person_history.center IN (:authorized_centers) + ' + ) + ) + ->setParameter('authorized_centers', $centers); + } + + AccompanyingCourseExportHelper::addClosingMotiveExclusionClause($qb); + + return $qb; + } + + public function requiredRole(): string + { + // which role should we give here? + return PersonVoter::STATS; + } + + public function supportsModifiers(): array + { + return [ + Declarations::CALENDAR_TYPE, + ]; + } +} + diff --git a/src/Bundle/ChillCalendarBundle/Export/Export/StatCalendarAvgDuration.php b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/StatCalendarAvgDuration.php similarity index 95% rename from src/Bundle/ChillCalendarBundle/Export/Export/StatCalendarAvgDuration.php rename to src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/StatCalendarAvgDuration.php index b69185a17..14aa59539 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Export/StatCalendarAvgDuration.php +++ b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/StatCalendarAvgDuration.php @@ -9,7 +9,7 @@ declare(strict_types=1); * the LICENSE file that was distributed with this source code. */ -namespace Chill\CalendarBundle\Export\Export; +namespace Chill\CalendarBundle\Export\Export\LinkedToPerson; use Chill\CalendarBundle\Export\Declarations; use Chill\CalendarBundle\Repository\CalendarRepository; @@ -48,7 +48,7 @@ class StatCalendarAvgDuration implements ExportInterface, GroupedExportInterface public function getGroup(): string { - return 'Exports of calendar'; + return 'Exports of calendars linked to a person'; } public function getLabels($key, array $values, $data) diff --git a/src/Bundle/ChillCalendarBundle/Export/Export/StatCalendarSumDuration.php b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/StatCalendarSumDuration.php similarity index 95% rename from src/Bundle/ChillCalendarBundle/Export/Export/StatCalendarSumDuration.php rename to src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/StatCalendarSumDuration.php index 8ea23014c..092286581 100644 --- a/src/Bundle/ChillCalendarBundle/Export/Export/StatCalendarSumDuration.php +++ b/src/Bundle/ChillCalendarBundle/Export/Export/LinkedToPerson/StatCalendarSumDuration.php @@ -9,7 +9,7 @@ declare(strict_types=1); * the LICENSE file that was distributed with this source code. */ -namespace Chill\CalendarBundle\Export\Export; +namespace Chill\CalendarBundle\Export\Export\LinkedToPerson; use Chill\CalendarBundle\Export\Declarations; use Chill\CalendarBundle\Repository\CalendarRepository; @@ -48,7 +48,7 @@ class StatCalendarSumDuration implements ExportInterface, GroupedExportInterface public function getGroup(): string { - return 'Exports of calendar'; + return 'Exports of calendars linked to a person'; } public function getLabels($key, array $values, $data) diff --git a/src/Bundle/ChillCalendarBundle/Resources/config/services/exports.yaml b/src/Bundle/ChillCalendarBundle/Resources/config/services/exports.yaml index 3eb4bbfdc..8ab1b0d1e 100644 --- a/src/Bundle/ChillCalendarBundle/Resources/config/services/exports.yaml +++ b/src/Bundle/ChillCalendarBundle/Resources/config/services/exports.yaml @@ -1,118 +1,96 @@ services: + _defaults: + autowire: true + autoconfigure: true ## Indicators - chill.calendar.export.count_calendars: - class: Chill\CalendarBundle\Export\Export\CountCalendars - autowire: true - autoconfigure: true - tags: - - { name: chill.export, alias: count_calendars } + Chill\CalendarBundle\Export\Export\LinkedToAcp\CountCalendars: + tags: + - { name: chill.export, alias: 'count_calendars_linked_to_acp' } - chill.calendar.export.average_duration_calendars: - class: Chill\CalendarBundle\Export\Export\StatCalendarAvgDuration - autowire: true - autoconfigure: true - tags: - - { name: chill.export, alias: average_duration_calendars } + Chill\CalendarBundle\Export\Export\LinkedToAcp\StatCalendarAvgDuration: + tags: + - { name: chill.export, alias: 'average_duration_calendars_linked_to_acp' } - chill.calendar.export.sum_duration_calendars: - class: Chill\CalendarBundle\Export\Export\StatCalendarSumDuration - autowire: true - autoconfigure: true - tags: - - { name: chill.export, alias: sum_duration_calendars } + Chill\CalendarBundle\Export\Export\LinkedToAcp\StatCalendarSumDuration: + tags: + - { name: chill.export, alias: 'sum_duration_calendars_linked_to_acp' } + + Chill\CalendarBundle\Export\Export\LinkedToPerson\CountCalendars: + tags: + - { name: chill.export, alias: 'count_calendars_linked_to_person' } + + Chill\CalendarBundle\Export\Export\LinkedToPerson\StatCalendarAvgDuration: + tags: + - { name: chill.export, alias: 'average_duration_calendars_linked_to_person' } + + Chill\CalendarBundle\Export\Export\LinkedToPerson\StatCalendarSumDuration: + tags: + - { name: chill.export, alias: 'sum_duration_calendars_linked_to_person' } ## Filters - chill.calendar.export.agent_filter: - class: Chill\CalendarBundle\Export\Filter\AgentFilter - autowire: true - autoconfigure: true - tags: - - { name: chill.export_filter, alias: agent_filter } + Chill\CalendarBundle\Export\Filter\AgentFilter: + tags: + - { name: chill.export_filter, alias: 'agent_filter' } - chill.calendar.export.job_filter: - class: Chill\CalendarBundle\Export\Filter\JobFilter - autowire: true - autoconfigure: true - tags: - - { name: chill.export_filter, alias: job_filter } - chill.calendar.export.scope_filter: - class: Chill\CalendarBundle\Export\Filter\ScopeFilter - autowire: true - autoconfigure: true - tags: - - { name: chill.export_filter, alias: scope_filter } + Chill\CalendarBundle\Export\Filter\JobFilter: + tags: + - { name: chill.export_filter, alias: job_filter } - chill.calendar.export.between_dates_filter: - class: Chill\CalendarBundle\Export\Filter\BetweenDatesFilter - autowire: true - autoconfigure: true - tags: - - { name: chill.export_filter, alias: between_dates_filter } - chill.calendar.export.calendar_range_filter: - class: Chill\CalendarBundle\Export\Filter\CalendarRangeFilter - autowire: true - autoconfigure: true + Chill\CalendarBundle\Export\Filter\ScopeFilter: + tags: + - { name: chill.export_filter, alias: scope_filter } + + + Chill\CalendarBundle\Export\Filter\BetweenDatesFilter: + tags: + - { name: chill.export_filter, alias: between_dates_filter } + + + Chill\CalendarBundle\Export\Filter\CalendarRangeFilter: tags: - { name: chill.export_filter, alias: calendar_range_filter } ## Aggregator - chill.calendar.export.agent_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\AgentAggregator - autowire: true - autoconfigure: true - tags: - - { name: chill.export_aggregator, alias: agent_aggregator } - chill.calendar.export.job_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\JobAggregator - autowire: true - autoconfigure: true - tags: - - { name: chill.export_aggregator, alias: job_aggregator } + Chill\CalendarBundle\Export\Aggregator\AgentAggregator: + tags: + - { name: chill.export_aggregator, alias: agent_aggregator } - chill.calendar.export.scope_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\ScopeAggregator - autowire: true - autoconfigure: true - tags: - - { name: chill.export_aggregator, alias: scope_aggregator } - chill.calendar.export.location_type_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\LocationTypeAggregator - autowire: true - autoconfigure: true - tags: - - { name: chill.export_aggregator, alias: location_type_aggregator } + Chill\CalendarBundle\Export\Aggregator\JobAggregator: + tags: + - { name: chill.export_aggregator, alias: job_aggregator } - chill.calendar.export.location_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\LocationAggregator - autowire: true - autoconfigure: true - tags: - - { name: chill.export_aggregator, alias: location_aggregator } - chill.calendar.export.cancel_reason_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\CancelReasonAggregator - autowire: true - autoconfigure: true - tags: - - { name: chill.export_aggregator, alias: cancel_reason_aggregator } + Chill\CalendarBundle\Export\Aggregator\ScopeAggregator: + tags: + - { name: chill.export_aggregator, alias: scope_aggregator } - chill.calendar.export.month_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\MonthYearAggregator - autowire: true - autoconfigure: true - tags: - - { name: chill.export_aggregator, alias: month_aggregator } - chill.calendar.export.urgency_aggregator: - class: Chill\CalendarBundle\Export\Aggregator\UrgencyAggregator - autowire: true - autoconfigure: true + Chill\CalendarBundle\Export\Aggregator\LocationTypeAggregator: + tags: + - { name: chill.export_aggregator, alias: location_type_aggregator } + + + Chill\CalendarBundle\Export\Aggregator\LocationAggregator: + tags: + - { name: chill.export_aggregator, alias: location_aggregator } + + + Chill\CalendarBundle\Export\Aggregator\CancelReasonAggregator: + tags: + - { name: chill.export_aggregator, alias: cancel_reason_aggregator } + + + Chill\CalendarBundle\Export\Aggregator\MonthYearAggregator: + tags: + - { name: chill.export_aggregator, alias: month_aggregator } + + Chill\CalendarBundle\Export\Aggregator\UrgencyAggregator: tags: - { name: chill.export_aggregator, alias: urgency_aggregator }