diff --git a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php index 1f2680f9e..c56489afd 100644 --- a/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php +++ b/src/Bundle/ChillPersonBundle/Controller/AccompanyingCourseWorkController.php @@ -161,7 +161,7 @@ class AccompanyingCourseWorkController extends AbstractController 'types' => $filter->getEntityChoiceData('typesFilter'), 'before' => $filter->getDateRangeData('dateFilter')['to'], 'after' => $filter->getDateRangeData('dateFilter')['from'], - 'user' => $filter->getUserPickerData('userFilter') + 'users' => $filter->getUserPickerData('userFilter') ]; $totalItems = $this->workRepository->countByAccompanyingPeriod($period); @@ -219,7 +219,7 @@ class AccompanyingCourseWorkController extends AbstractController { $filterBuilder = $this->filterOrderHelperFactory->create(self::class); - $types = $this->workRepository->findByAccompanyingPeriod($associatedPeriod); + $types = $this->workRepository->findActionTypeByPeriod($associatedPeriod); $filterBuilder ->addDateRange('dateFilter', 'accompanying_course_work.date_filter') diff --git a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php index 60a00e12a..a5e27afd5 100644 --- a/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php +++ b/src/Bundle/ChillPersonBundle/Repository/AccompanyingPeriod/AccompanyingPeriodWorkRepository.php @@ -108,8 +108,32 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository CASE WHEN enddate IS NULL THEN '-infinity'::timestamp ELSE 'infinity'::timestamp END ASC, startdate DESC, enddate DESC, - id DESC - LIMIT :limit OFFSET :offset"; + id DESC"; + + // implement filters + + if([] !== ($filters['types'] ?? [])) + { + $sql .= "AND WHERE w.socialAction IN (:types)"; + } + + if([] !== ($filters['users'] ?? [])) + { + $sql .= "AND WHERE w.createdBy IN (:users)"; + + foreach ($filters['users'] as $key => $user) { + $sql .= "OR :user_" . $key . " IN w.referrers)"; + + $nq = $this->em->createNativeQuery($sql, $rsm) + ->setParameter(':user_' . $key); + } + + // ... to be continued + } + + // set limit and offset + + $sql .= " LIMIT :limit OFFSET :offset"; $nq = $this->em->createNativeQuery($sql, $rsm) ->setParameter('periodId', $period->getId(), Types::INTEGER) @@ -119,6 +143,36 @@ final class AccompanyingPeriodWorkRepository implements ObjectRepository return $nq->getResult(); } + /** + * Return a list of types of social actions associated to the accompanying period + * + * @return array + */ + public function findActionTypeByPeriod(AccompanyingPeriod $period): array + { + $in = $this->em->createQueryBuilder(); + $in + ->select('1') + ->from(AccompanyingPeriodWork::class, 'apw'); + + + $in->andWhere('apw.accompanyingPeriod = :period')->setParameter('period', $period); + + + // join between the embedded exist query and the main query + $in->andWhere('apw.socialAction = sa'); + + $qb = $this->em->createQueryBuilder()->setParameters($in->getParameters()); + $qb + ->select('sa') + ->from(SocialAction::class, 'sa') + ->where( + $qb->expr()->exists($in->getDQL()) + ); + + return $qb->getQuery()->getResult(); + } + public function findNearEndDateByUser(User $user, DateTimeImmutable $since, DateTimeImmutable $until, int $limit = 20, int $offset = 0): array { return $this->buildQueryNearEndDateByUser($user, $since, $until)