Skip to content

Commit

Permalink
UniqueValueFinder trait added
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyWin committed Sep 9, 2024
1 parent 2d26563 commit b03afaa
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 77 deletions.
19 changes: 0 additions & 19 deletions src/plugin/cursus/Manager/CourseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class CourseManager
private SessionManager $sessionManager;

private $courseUserRepo;
private $courseRepo;

public function __construct(
TranslatorInterface $translator,
Expand All @@ -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
Expand Down Expand Up @@ -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;
}
}
17 changes: 0 additions & 17 deletions src/plugin/cursus/Manager/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
19 changes: 3 additions & 16 deletions src/plugin/cursus/Repository/CourseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,6 +22,8 @@

class CourseRepository extends EntityRepository
{
use UniqueValueFinder;

public function search(string $search, int $nbResults)
{
return $this->createQueryBuilder('c')
Expand Down Expand Up @@ -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()
);
}
}
3 changes: 3 additions & 0 deletions src/plugin/cursus/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@

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;
use Doctrine\ORM\EntityRepository;

class EventRepository extends EntityRepository
{
use UniqueValueFinder;

public function countParticipants(Event $event): array
{
return [
Expand Down
19 changes: 3 additions & 16 deletions src/plugin/cursus/Repository/SessionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -20,6 +21,8 @@

class SessionRepository extends EntityRepository
{
use UniqueValueFinder;

public function findByWorkspace(Workspace $workspace)
{
return $this->getEntityManager()
Expand Down Expand Up @@ -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()
);
}
}
2 changes: 1 addition & 1 deletion src/plugin/cursus/Resources/config/services/subscriber.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }

Expand All @@ -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'
Expand Down
9 changes: 4 additions & 5 deletions src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -33,7 +32,6 @@ public function __construct(
private readonly ObjectManager $om,
private readonly FileManager $fileManager,
private readonly Crud $crud,
private readonly CourseManager $manager,
) {
}

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/plugin/cursus/Subscriber/Crud/EventSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
8 changes: 6 additions & 2 deletions src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b03afaa

Please sign in to comment.