Skip to content

Commit

Permalink
Review copy course
Browse files Browse the repository at this point in the history
  • Loading branch information
WolfyWin committed Sep 5, 2024
1 parent 421462e commit 094a8b9
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/plugin/cursus/Manager/CourseManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ public function getCopyName(string $name): string
$index = count($existingNames);
do {
++$index;
$newName = $name.'_'.$index;
$newName = $name.$index;
} while (in_array($newName, $existingNames));

return $newName;
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/cursus/Manager/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private function getTemplatePlaceholders(Event $event): array

public function getCopyName(string $name): string
{
$existingNames = $this->eventRepo->findCodesWithPrefix($name);
$existingNames = $this->eventRepo->findNamesWithPrefix($name);

if (empty($existingNames)) {
return $name;
Expand All @@ -422,7 +422,7 @@ public function getCopyName(string $name): string
$index = count($existingNames);
do {
++$index;
$newName = $name.'_'.$index;
$newName = $name.$index;
} while (in_array($newName, $existingNames));

return $newName;
Expand Down
2 changes: 1 addition & 1 deletion src/plugin/cursus/Manager/SessionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ public function getCopyName(string $name): string
$index = count($existingNames);
do {
++$index;
$newName = $name.'_'.$index;
$newName = $name.$index;
} while (in_array($newName, $existingNames));

return $newName;
Expand Down
9 changes: 5 additions & 4 deletions src/plugin/cursus/Repository/EventRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,17 @@ private function countUsers(Event $event, string $type)
->getSingleScalarResult();
}

public function findCodesWithPrefix(string $prefix): array
public function findNamesWithPrefix(string $prefix): array
{
return array_map(
function (array $event) {
return $event['code'];
return $event['name'];
},
$this->getEntityManager()->createQuery('
SELECT e.code
SELECT po.name
FROM Claroline\CursusBundle\Entity\Event e
WHERE e.code LIKE :search
JOIN Claroline\CoreBundle\Entity\Planning\PlannedObject po WITH e.plannedObject = po.id
WHERE po.name LIKE :search
')
->setParameter('search', addcslashes($prefix, '%_').'%')
->getResult()
Expand Down
7 changes: 6 additions & 1 deletion src/plugin/cursus/Serializer/CourseSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ public function serialize(Course $course, array $options = []): array

public function deserialize(array $data, Course $course, array $options): Course
{
$this->sipe('id', 'setUuid', $data, $course);
if (!in_array(SerializerInterface::REFRESH_UUID, $options)) {
$this->sipe('id', 'setUuid', $data, $course);
} else {
$course->refreshUuid();
}

$this->sipe('code', 'setCode', $data, $course);
$this->sipe('name', 'setName', $data, $course);
$this->sipe('description', 'setDescription', $data, $course);
Expand Down
9 changes: 7 additions & 2 deletions src/plugin/cursus/Serializer/EventSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,16 @@ public function serialize(Event $event, array $options = []): array
return $serialized;
}

public function deserialize(array $data, Event $event): Event
public function deserialize(array $data, Event $event, array $options = []): Event
{
$this->plannedObjectSerializer->deserialize($data, $event->getPlannedObject());

$this->sipe('id', 'setUuid', $data, $event);
if (!in_array(SerializerInterface::REFRESH_UUID, $options)) {
$this->sipe('id', 'setUuid', $data, $event);
} else {
$event->refreshUuid();
}

$this->sipe('code', 'setCode', $data, $event);
$this->sipe('restrictions.users', 'setMaxUsers', $data, $event);
$this->sipe('registration.registrationType', 'setRegistrationType', $data, $event);
Expand Down
9 changes: 7 additions & 2 deletions src/plugin/cursus/Serializer/SessionSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,14 @@ public function serialize(Session $session, array $options = []): array
];
}

public function deserialize(array $data, Session $session): Session
public function deserialize(array $data, Session $session, array $options): Session
{
$this->sipe('id', 'setUuid', $data, $session);
if (!in_array(SerializerInterface::REFRESH_UUID, $options)) {
$this->sipe('id', 'setUuid', $data, $session);
} else {
$session->refreshUuid();
}

$this->sipe('code', 'setCode', $data, $session);
$this->sipe('name', 'setName', $data, $session);
$this->sipe('description', 'setDescription', $data, $session);
Expand Down
12 changes: 7 additions & 5 deletions src/plugin/cursus/Subscriber/Crud/CourseSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,24 +147,26 @@ public function preCopy(CopyEvent $event): void
/** @var Course $copy */
$copy = $event->getCopy();

$copy->refreshUuid();
$copy->setCreatedAt(new \DateTime());
$copy->setUpdatedAt(new \DateTime());
$copyName = $this->manager->getCopyName($original->getName());
$copy->setSlug($copyName);
$copy->setName($copyName);
$copy->setCode($copyName);

foreach ($original->getSessions() as $session) {
$this->crud->copy($session, [], ['parent' => $copy]);
}
}

public function postCopy(CopyEvent $event): void
{
/** @var Course $course */
$course = $event->getCopy();

/** @var Course $original */
$original = $event->getObject();

foreach ($original->getSessions() as $session) {
$this->crud->copy($session, [], ['parent' => $course]);
}

if ($course->getPoster()) {
$this->fileManager->linkFile(Course::class, $course->getUuid(), $course->getPoster());
}
Expand Down
26 changes: 21 additions & 5 deletions src/plugin/cursus/Subscriber/Crud/SessionSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static function getSubscribedEvents(): array
CrudEvents::getEventName(CrudEvents::POST_UPDATE, Session::class) => 'postUpdate',
CrudEvents::getEventName(CrudEvents::POST_DELETE, Session::class) => 'postDelete',
CrudEvents::getEventName(CrudEvents::PRE_COPY, Session::class) => 'preCopy',
CrudEvents::getEventName(CrudEvents::POST_COPY, Session::class) => 'postCopy',
];
}

Expand Down Expand Up @@ -170,22 +171,37 @@ public function preCopy(CopyEvent $event): void
/** @var Course $course */
$course = $event->getExtra()['parent'];

/** @var Session $original */
$original = $event->getObject();

/** @var Session $copy */
$copy = $event->getCopy();

$copy->refreshUuid();
$copy->setCourse($course);
$copy->setCreatedAt(new \DateTime());
$copy->setUpdatedAt(new \DateTime());
$copyName = $this->sessionManager->getCopyName($original->getName());

$copyName = $this->sessionManager->getCopyName($copy->getName());
$copy->setName($copyName);
$copy->setCode($copyName);
}

public function postCopy(CopyEvent $event): void
{
/** @var Session $original */
$original = $event->getObject();

/** @var Session $copy */
$copy = $event->getCopy();

foreach ($original->getEvents() as $seance) {
$this->crud->copy($seance, [], ['parent' => $copy]);
}

if ($copy->getPoster()) {
$this->fileManager->linkFile(Session::class, $copy->getUuid(), $copy->getPoster());
}

if ($copy->getThumbnail()) {
$this->fileManager->linkFile(Session::class, $copy->getUuid(), $copy->getThumbnail());
}

}
}

0 comments on commit 094a8b9

Please sign in to comment.