From 53a53a3a0fca5d8574fb244d5113c24bb0f05946 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 23 Aug 2023 18:33:25 -0500 Subject: [PATCH] Internal: Course: Change entity listener to add tools when pre-persisting --- public/main/inc/lib/add_course.lib.inc.php | 1 - src/CoreBundle/Entity/Course.php | 11 +++-- .../Entity/Listener/CourseListener.php | 43 +------------------ 3 files changed, 7 insertions(+), 48 deletions(-) diff --git a/public/main/inc/lib/add_course.lib.inc.php b/public/main/inc/lib/add_course.lib.inc.php index 1045435bbc..9d01335c1f 100644 --- a/public/main/inc/lib/add_course.lib.inc.php +++ b/public/main/inc/lib/add_course.lib.inc.php @@ -753,7 +753,6 @@ class AddCourse ->setVideoUrl($params['video_url'] ?? '') ->setUnsubscribe($unsubscribe) ->setVisualCode($visual_code) - ->addAccessUrl(api_get_url_entity()) ->setCreator(api_get_user_entity()) ; diff --git a/src/CoreBundle/Entity/Course.php b/src/CoreBundle/Entity/Course.php index 6b51029973..8889e06801 100644 --- a/src/CoreBundle/Entity/Course.php +++ b/src/CoreBundle/Entity/Course.php @@ -966,12 +966,11 @@ class Course extends AbstractResource implements return $this->urls; } - public function setUrls(Collection $urls): self + public function addUrls(AccessUrlRelCourse $urlRelCourse): static { - $this->urls = new ArrayCollection(); - foreach ($urls as $url) { - $this->addAccessUrl($url); - } + $urlRelCourse->setCourse($this); + + $this->urls->add($urlRelCourse); return $this; } @@ -979,7 +978,7 @@ class Course extends AbstractResource implements public function addAccessUrl(?AccessUrl $url): self { $urlRelCourse = (new AccessUrlRelCourse())->setCourse($this)->setUrl($url); - $this->addUrlRelCourse($urlRelCourse); + $this->addUrls($urlRelCourse); return $this; } diff --git a/src/CoreBundle/Entity/Listener/CourseListener.php b/src/CoreBundle/Entity/Listener/CourseListener.php index c3297d795f..4ae4a9e356 100644 --- a/src/CoreBundle/Entity/Listener/CourseListener.php +++ b/src/CoreBundle/Entity/Listener/CourseListener.php @@ -6,13 +6,10 @@ declare(strict_types=1); namespace Chamilo\CoreBundle\Entity\Listener; -use Chamilo\CoreBundle\Entity\AccessUrl; use Chamilo\CoreBundle\Entity\Course; -use Chamilo\CoreBundle\Repository\Node\CourseRepository; use Chamilo\CoreBundle\Settings\SettingsManager; use Chamilo\CoreBundle\Tool\ToolChain; -use Doctrine\ORM\Event\LifecycleEventArgs; -use Exception; +use Doctrine\ORM\Event\PrePersistEventArgs; /** * Class CourseListener. @@ -28,46 +25,10 @@ class CourseListener ) { } - /** - * This code is executed when a new course is created. - * - * new object : prePersist - * edited object: preUpdate - * - * This function add the course tools to the current course entity - * thanks to the tool chain see src/Chamilo/CourseBundle/ToolChain.php - * - * @throws Exception - */ - public function prePersist(Course $course, LifecycleEventArgs $args): void - { - //error_log('Course listener prePersist'); - /*if ($course) { - // $this->checkLimit($repo, $course, $url); - $this->toolChain->addToolsInCourse($course); - }*/ - } - - public function postPersist(Course $course, LifecycleEventArgs $args): void + public function prePersist(Course $course, PrePersistEventArgs $args): void { ///$this->checkLimit($repo, $course, $url); $this->toolChain->addToolsInCourse($course); - - $args->getObjectManager()->persist($course); - $args->getObjectManager()->flush(); - } - - /** - * This code is executed when a course is updated. - * - * @throws Exception - */ - public function preUpdate(Course $course, LifecycleEventArgs $args): void - { - //error_log('preUpdate'); - /*$url = $course->getCurrentUrl(); - $repo = $args->getEntityManager()->getRepository('ChamiloCoreBundle:Course'); - $this->checkLimit($repo, $course, $url);*/ } /*protected function checkLimit(CourseRepository $repo, Course $course, AccessUrl $url): void