From aadedea48b9226b687c6c50380f9ef74b81b1df3 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 6 Mar 2024 17:00:49 -0500 Subject: [PATCH] Calendar: Refactoring to separate function to load personal events --- .../Extension/CCalendarEventExtension.php | 53 +++++++++------- src/CoreBundle/ServiceHelper/CidReqHelper.php | 60 +++++++++++++++++++ 2 files changed, 91 insertions(+), 22 deletions(-) create mode 100644 src/CoreBundle/ServiceHelper/CidReqHelper.php diff --git a/src/CoreBundle/DataProvider/Extension/CCalendarEventExtension.php b/src/CoreBundle/DataProvider/Extension/CCalendarEventExtension.php index 1464f97f4b..d87a9329b6 100644 --- a/src/CoreBundle/DataProvider/Extension/CCalendarEventExtension.php +++ b/src/CoreBundle/DataProvider/Extension/CCalendarEventExtension.php @@ -12,8 +12,11 @@ use ApiPlatform\Doctrine\Orm\Extension\QueryCollectionExtensionInterface; use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; use ApiPlatform\Metadata\Operation; use Chamilo\CoreBundle\Entity\User; +use Chamilo\CoreBundle\ServiceHelper\CidReqHelper; +use Chamilo\CoreBundle\Settings\SettingsManager; use Chamilo\CourseBundle\Entity\CCalendarEvent; use Doctrine\ORM\QueryBuilder; +use phpDocumentor\Reflection\Types\Integer; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\Security\Core\Security; @@ -23,7 +26,8 @@ final class CCalendarEventExtension implements QueryCollectionExtensionInterface public function __construct( private readonly Security $security, - private readonly RequestStack $requestStack + private readonly CidReqHelper $cidReqHelper, + private readonly SettingsManager $settingsManager, ) {} public function applyToCollection( @@ -42,8 +46,19 @@ final class CCalendarEventExtension implements QueryCollectionExtensionInterface return; } - /** @var User $user */ + $courseId = $this->cidReqHelper->getCourseId(); + $sessionId = $this->cidReqHelper->getSessionId(); + $groupId = $this->cidReqHelper->getGroupId(); + + /** @var ?User $user */ $user = $this->security->getUser(); + + $inCourseBase = !empty($courseId); + $inSession = !empty($sessionId); + $inCourseSession = $inCourseBase && $inSession; + + $inPersonalList = !$inCourseBase && !$inCourseSession; + $alias = $qb->getRootAliases()[0]; $qb @@ -51,27 +66,21 @@ final class CCalendarEventExtension implements QueryCollectionExtensionInterface ->leftJoin('node.resourceLinks', 'resource_links') ; - $request = $this->requestStack->getCurrentRequest(); - $courseId = $request->query->getInt('cid'); - $sessionId = $request->query->getInt('sid'); - $groupId = $request->query->getInt('gid'); - - $inCourseBase = !empty($courseId); - $inSession = !empty($sessionId); - $inCourseSession = $inCourseBase && $inSession; - - $inPersonalList = !$inCourseBase && !$inCourseSession; + if ($inPersonalList && $user) { + $this->addPersonalCalendarConditions($qb, $user); + } + } - if ($inPersonalList) { - $qb - ->andWhere( - $qb->expr()->orX( - $qb->expr()->eq('resource_links.user', ':user'), - $qb->expr()->eq('node.creator', ':user') - ) + private function addPersonalCalendarConditions(QueryBuilder $qb, User $user): void + { + $qb + ->andWhere( + $qb->expr()->orX( + $qb->expr()->eq('resource_links.user', ':user'), + $qb->expr()->eq('node.creator', ':user') ) - ->setParameter('user', $user->getId()) - ; - } + ) + ->setParameter('user', $user->getId()) + ; } } diff --git a/src/CoreBundle/ServiceHelper/CidReqHelper.php b/src/CoreBundle/ServiceHelper/CidReqHelper.php new file mode 100644 index 0000000000..4218fd8c79 --- /dev/null +++ b/src/CoreBundle/ServiceHelper/CidReqHelper.php @@ -0,0 +1,60 @@ +requestStack->getCurrentRequest(); + } + + private function getSessionHandler(): SessionInterface + { + return $this->getRequest()->getSession(); + } + + public function getSessionId(): ?int + { + return $this->getSessionHandler()->get('sid'); + } + + public function getSessionEntity(): ?Session + { + return $this->getSessionHandler()->get('session'); + } + + public function getCourseId() + { + return $this->getSessionHandler()->get('cid'); + } + + public function getCourseEntity(): ?Course + { + return $this->getSessionHandler()->get('course'); + } + + public function getGroupId(): ?int + { + return $this->getSessionHandler()->get('gid'); + } +}