From b03afaa5b31da29e7b4fdc2db3c3d417a0c5b8fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9becca=20Tinchon?= Date: Mon, 9 Sep 2024 11:22:56 +0200 Subject: [PATCH] UniqueValueFinder trait added --- src/plugin/cursus/Manager/CourseManager.php | 19 ------------------- src/plugin/cursus/Manager/SessionManager.php | 17 ----------------- .../cursus/Repository/CourseRepository.php | 19 +++---------------- .../cursus/Repository/EventRepository.php | 3 +++ .../cursus/Repository/SessionRepository.php | 19 +++---------------- .../Resources/config/services/subscriber.yml | 2 +- .../Subscriber/Crud/CourseSubscriber.php | 9 ++++----- .../Subscriber/Crud/EventSubscriber.php | 3 ++- .../Subscriber/Crud/SessionSubscriber.php | 8 ++++++-- 9 files changed, 22 insertions(+), 77 deletions(-) diff --git a/src/plugin/cursus/Manager/CourseManager.php b/src/plugin/cursus/Manager/CourseManager.php index d1f7ca19761..02506d42d10 100644 --- a/src/plugin/cursus/Manager/CourseManager.php +++ b/src/plugin/cursus/Manager/CourseManager.php @@ -38,7 +38,6 @@ class CourseManager private SessionManager $sessionManager; private $courseUserRepo; - private $courseRepo; public function __construct( TranslatorInterface $translator, @@ -60,7 +59,6 @@ public function __construct( $this->sessionManager = $sessionManager; $this->courseUserRepo = $this->om->getRepository(CourseUser::class); - $this->courseRepo = $this->om->getRepository(Course::class); } public function generateFromTemplate(Course $course, string $locale): string @@ -193,21 +191,4 @@ public function moveToPending(Course $course, array $sessionUsers): void } } } - - public function getCopyName(string $name): string - { - $existingNames = $this->courseRepo->findNamesWithPrefix($name); - - if (empty($existingNames)) { - return $name; - } - - $index = count($existingNames); - do { - ++$index; - $newName = $name.$index; - } while (in_array($newName, $existingNames)); - - return $newName; - } } diff --git a/src/plugin/cursus/Manager/SessionManager.php b/src/plugin/cursus/Manager/SessionManager.php index 90718a14b16..ef77a24f3d9 100644 --- a/src/plugin/cursus/Manager/SessionManager.php +++ b/src/plugin/cursus/Manager/SessionManager.php @@ -504,21 +504,4 @@ public function unregisterGroup(SessionGroup $sessionGroup): void $this->sessionEventManager->removeGroups($eventRegistration->getEvent(), [$eventRegistration]); } } - - public function getCopyName(string $name): string - { - $existingNames = $this->sessionRepo->findNamesWithPrefix($name); - - if (empty($existingNames)) { - return $name; - } - - $index = count($existingNames); - do { - ++$index; - $newName = $name.$index; - } while (in_array($newName, $existingNames)); - - return $newName; - } } diff --git a/src/plugin/cursus/Repository/CourseRepository.php b/src/plugin/cursus/Repository/CourseRepository.php index a9061d1bf88..f3f107783ac 100644 --- a/src/plugin/cursus/Repository/CourseRepository.php +++ b/src/plugin/cursus/Repository/CourseRepository.php @@ -11,6 +11,7 @@ namespace Claroline\CursusBundle\Repository; +use Claroline\AppBundle\Repository\UniqueValueFinder; use Claroline\CoreBundle\Entity\Facet\FieldFacet; use Claroline\CoreBundle\Entity\User; use Claroline\CoreBundle\Entity\Workspace\Workspace; @@ -21,6 +22,8 @@ class CourseRepository extends EntityRepository { + use UniqueValueFinder; + public function search(string $search, int $nbResults) { return $this->createQueryBuilder('c') @@ -248,20 +251,4 @@ private function countUsers(Course $course, string $type, Session $session = nul return (int) $query->getSingleScalarResult(); } - - public function findNamesWithPrefix(string $prefix): array - { - return array_map( - function (array $course) { - return $course['name']; - }, - $this->getEntityManager()->createQuery(' - SELECT c.name - FROM Claroline\CursusBundle\Entity\Course c - WHERE c.name LIKE :search - ') - ->setParameter('search', addcslashes($prefix, '%_').'%') - ->getResult() - ); - } } diff --git a/src/plugin/cursus/Repository/EventRepository.php b/src/plugin/cursus/Repository/EventRepository.php index 58506742fbc..d12aface36c 100644 --- a/src/plugin/cursus/Repository/EventRepository.php +++ b/src/plugin/cursus/Repository/EventRepository.php @@ -11,6 +11,7 @@ namespace Claroline\CursusBundle\Repository; +use Claroline\AppBundle\Repository\UniqueValueFinder; use Claroline\CoreBundle\Entity\User; use Claroline\CursusBundle\Entity\Event; use Claroline\CursusBundle\Entity\Registration\AbstractRegistration; @@ -18,6 +19,8 @@ class EventRepository extends EntityRepository { + use UniqueValueFinder; + public function countParticipants(Event $event): array { return [ diff --git a/src/plugin/cursus/Repository/SessionRepository.php b/src/plugin/cursus/Repository/SessionRepository.php index 4977fe03a29..4ccc951c446 100644 --- a/src/plugin/cursus/Repository/SessionRepository.php +++ b/src/plugin/cursus/Repository/SessionRepository.php @@ -11,6 +11,7 @@ namespace Claroline\CursusBundle\Repository; +use Claroline\AppBundle\Repository\UniqueValueFinder; use Claroline\CoreBundle\Entity\User; use Claroline\CoreBundle\Entity\Workspace\Workspace; use Claroline\CursusBundle\Entity\Course; @@ -20,6 +21,8 @@ class SessionRepository extends EntityRepository { + use UniqueValueFinder; + public function findByWorkspace(Workspace $workspace) { return $this->getEntityManager() @@ -130,20 +133,4 @@ private function countUsers(Session $session, string $type): int ]) ->getSingleScalarResult(); } - - public function findNamesWithPrefix(string $prefix): array - { - return array_map( - function (array $session) { - return $session['name']; - }, - $this->getEntityManager()->createQuery(' - SELECT s.name - FROM Claroline\CursusBundle\Entity\Session s - WHERE s.name LIKE :search - ') - ->setParameter('search', addcslashes($prefix, '%_').'%') - ->getResult() - ); - } } diff --git a/src/plugin/cursus/Resources/config/services/subscriber.yml b/src/plugin/cursus/Resources/config/services/subscriber.yml index 8354306d065..af2a307c4cd 100644 --- a/src/plugin/cursus/Resources/config/services/subscriber.yml +++ b/src/plugin/cursus/Resources/config/services/subscriber.yml @@ -21,7 +21,6 @@ services: - '@Claroline\AppBundle\Persistence\ObjectManager' - '@Claroline\CoreBundle\Manager\FileManager' - '@Claroline\AppBundle\API\Crud' - - '@Claroline\CursusBundle\Manager\CourseManager' tags: - { name: kernel.event_subscriber } @@ -35,6 +34,7 @@ services: Claroline\CursusBundle\Subscriber\Crud\SessionSubscriber: arguments: - '@security.token_storage' + - '@Claroline\AppBundle\Persistence\ObjectManager' - '@Claroline\CoreBundle\Manager\FileManager' - '@Claroline\CursusBundle\Manager\SessionManager' - '@Claroline\AppBundle\API\Crud' diff --git a/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php b/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php index 7eb1a2dae97..26b40352f8a 100644 --- a/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php +++ b/src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php @@ -22,7 +22,6 @@ use Claroline\CoreBundle\Entity\User; use Claroline\CoreBundle\Manager\FileManager; use Claroline\CursusBundle\Entity\Course; -use Claroline\CursusBundle\Manager\CourseManager; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; @@ -33,7 +32,6 @@ public function __construct( private readonly ObjectManager $om, private readonly FileManager $fileManager, private readonly Crud $crud, - private readonly CourseManager $manager, ) { } @@ -149,10 +147,11 @@ public function preCopy(CopyEvent $event): void $copy->setCreatedAt(new \DateTime()); $copy->setUpdatedAt(new \DateTime()); - $copyName = $this->manager->getCopyName($original->getName()); - $copy->setSlug($copyName); + $copyName = $this->om->getRepository(Course::class)->findNextUnique('name', $original->getName()); + $copyCode = $this->om->getRepository(Course::class)->findNextUnique('code', $original->getCode()); $copy->setName($copyName); - $copy->setCode($copyName); + $copy->setCode($copyCode); + $copy->setSlug($copyName); } public function postCopy(CopyEvent $event): void diff --git a/src/plugin/cursus/Subscriber/Crud/EventSubscriber.php b/src/plugin/cursus/Subscriber/Crud/EventSubscriber.php index 4424b9e69b0..6ab07dea850 100644 --- a/src/plugin/cursus/Subscriber/Crud/EventSubscriber.php +++ b/src/plugin/cursus/Subscriber/Crud/EventSubscriber.php @@ -117,7 +117,8 @@ public function preCopy(CopyEvent $event): void $copy->setUuid(BaseUuid::uuid4()->toString()); $copyName = $this->manager->getCopyName($original->getName()); - $copy->setCode($copyName); + $copyCode = $this->om->getRepository(Event::class)->findNextUnique('code', $original->getCode()); + $copy->setCode($copyCode); $copy->setSession($session); $copy->getPlannedObject()->setName($copyName); } diff --git a/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php b/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php index 705110a5c22..cb6d808b4a7 100644 --- a/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php +++ b/src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php @@ -17,6 +17,7 @@ use Claroline\AppBundle\Event\Crud\DeleteEvent; use Claroline\AppBundle\Event\Crud\UpdateEvent; use Claroline\AppBundle\Event\CrudEvents; +use Claroline\AppBundle\Persistence\ObjectManager; use Claroline\CoreBundle\Entity\User; use Claroline\CoreBundle\Manager\FileManager; use Claroline\CursusBundle\Entity\Course; @@ -29,6 +30,7 @@ class SessionSubscriber implements EventSubscriberInterface { public function __construct( private readonly TokenStorageInterface $tokenStorage, + private readonly ObjectManager $om, private readonly FileManager $fileManager, private readonly SessionManager $sessionManager, private readonly Crud $crud, @@ -178,9 +180,11 @@ public function preCopy(CopyEvent $event): void $copy->setCreatedAt(new \DateTime()); $copy->setUpdatedAt(new \DateTime()); - $copyName = $this->sessionManager->getCopyName($copy->getName()); +// $copyName = $this->sessionManager->getCopyName($copy->getName()); + $copyName = $this->om->getRepository(Session::class)->findNextUnique('name', $copy->getName()); + $copyCode = $this->om->getRepository(Session::class)->findNextUnique('code', $copy->getCode()); $copy->setName($copyName); - $copy->setCode($copyName); + $copy->setCode($copyCode); } public function postCopy(CopyEvent $event): void