Browse Source

BGC-192 @fixed image des sequences

raphael 3 years ago
parent
commit
74e8fc791b

+ 4
- 3
assets/exclusive/js/back-office/learning_path/learning_path_editor.js View File

@@ -1188,9 +1188,10 @@ function showFormEditSequenceItem(action, sequenceItem) {
1188 1188
 
1189 1189
         // Dévérouillage de la page
1190 1190
         $(document).phxUnlock();
1191
-        
1192
-        // Envoi event pour informer que le formulaire est affiché
1193
-        sequenceItem.trigger("logiprolearningpathsequenceformopen");
1191
+        if (sequenceItem)
1192
+        {
1193
+            sequenceItem.trigger("logiprolearningpathsequenceformopen");
1194
+        }
1194 1195
     });
1195 1196
 }
1196 1197
 

+ 0
- 5
config/services.yaml View File

@@ -120,11 +120,6 @@ services:
120 120
     tags:
121 121
         - { name: twig.extension }
122 122
 
123
-  # Service de duplication des entités
124
-  Logipro\Phoenix\Service\EntityDuplicator:
125
-    public: true
126
-    arguments: ['@service_container']
127
-
128 123
   Logipro\Phoenix\EntityListener\ImageListener:
129 124
 
130 125
   phoenix.bundle.learningpath:

+ 40
- 58
src/Phoenix/Service/EntityDuplicator.php View File

@@ -9,36 +9,40 @@ use Logipro\Phoenix\Entity\SequenceItem\ActivityPackage;
9 9
 use Logipro\Phoenix\Entity\SequenceItem\SequenceItem;
10 10
 use Logipro\Phoenix\Exception\PhoenixException;
11 11
 use Logipro\Phoenix\Package\Common\AbstractPackageFile;
12
-use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13
-use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14
-use Symfony\Component\DependencyInjection\ContainerInterface;
15 12
 use Logipro\Phoenix\Entity\Session\Session;
16 13
 use Symfony\Component\Security\Core\Security;
17 14
 use Logipro\Phoenix\Entity\ProgramItem;
18 15
 use Doctrine\Common\Collections\ArrayCollection;
16
+use Logipro\Bundle\SCORMBundle\Services\ScormEngine;
17
+use Symfony\Component\Translation\TranslatorInterface;
18
+use Doctrine\ORM\EntityManagerInterface;
19 19
 
20 20
 /**
21 21
  * Service de duplication des entités.
22 22
  */
23
-class EntityDuplicator implements ContainerAwareInterface
23
+class EntityDuplicator
24 24
 {
25
-    use ContainerAwareTrait;
26
-
27 25
     private $security;
28 26
     private $fileUploader;
27
+    private $entityManager;
28
+    private $translator;
29
+    private $scormEngine;
29 30
 
30 31
     /**
31 32
      * Constructeur.
32
-     * @param ContainerInterface $container
33 33
      */
34 34
     public function __construct(
35
-        ContainerInterface $container,
36 35
         Security $security,
37
-        FileUploader $fileUploader
36
+        FileUploader $fileUploader,
37
+        EntityManagerInterface $entityManager,
38
+        TranslatorInterface $translator,
39
+        ScormEngine $scormEngine
38 40
     ) {
39
-        $this->setContainer($container);
40 41
         $this->security = $security;
41 42
         $this->fileUploader = $fileUploader;
43
+        $this->entityManager = $entityManager;
44
+        $this->translator = $translator;
45
+        $this->scormEngine = $scormEngine;
42 46
     }
43 47
 
44 48
     /**
@@ -49,29 +53,7 @@ class EntityDuplicator implements ContainerAwareInterface
49 53
      */
50 54
     public function duplicateLearningPath(LearningPath $learningPath, Session $session = null, bool $isTemplate = false): LearningPath
51 55
     {
52
-        $em = $this->getDoctrine()->getManager();
53
-
54
-        $newLearningPath = null;
55
-
56
-        // Si c'est une duplication de template
57
-        if ($isTemplate) {
58
-            $title = '[copie] ' . $learningPath->getTitle();
59
-            $newLearningPath = $this->cloneLearningPath($learningPath, true);
60
-            $newLearningPath->setTitle($title);
61
-        } else {
62
-            $newLearningPath = $this->cloneLearningPath($learningPath);
63
-        }
64
-
65
-        $autor = $this->security->getUser();
66
-        $newLearningPath->setCreatingUser($autor);
67
-
68
-        if ($session) {
69
-            $newLearningPath->setSession($session);
70
-            $session->setLearningPath($newLearningPath);
71
-        }
72
-
73
-        $em->persist($newLearningPath);
74
-        $em->flush();
56
+        $newLearningPath = $this->cloneLearningPath($learningPath, $session, $isTemplate);
75 57
 
76 58
         // Duplication des cours au niveau du moteur SCORM
77 59
         $this->duplicateScormCoursesFromPaths($learningPath, $newLearningPath);
@@ -79,7 +61,7 @@ class EntityDuplicator implements ContainerAwareInterface
79 61
         return $newLearningPath;
80 62
     }
81 63
 
82
-    private function cloneLearningPath(LearningPath $learningPath, bool $isTemplate = false): LearningPath
64
+    protected function cloneLearningPath(LearningPath $learningPath, Session $session = null, bool $isTemplate = false): LearningPath
83 65
     {
84 66
         //FIXME Peut-on copier un parcours qui n'est pas un gabarit ?
85 67
         if (!$learningPath->isTemplate()) {
@@ -89,6 +71,9 @@ class EntityDuplicator implements ContainerAwareInterface
89 71
         // Clonage du parcours (et de ses éléments enfants en cascade)
90 72
         $pathCopy = clone $learningPath;
91 73
 
74
+        $autor = $this->security->getUser();
75
+        $pathCopy->setCreatingUser($autor);
76
+
92 77
         // Copie les séquences
93 78
         $sequences = new ArrayCollection();
94 79
         foreach ($learningPath->getSequences() as $sequence) {
@@ -112,10 +97,22 @@ class EntityDuplicator implements ContainerAwareInterface
112 97
         $pathCopy->setTemplate($learningPath);
113 98
         $pathCopy->setIsTemplate($isTemplate);
114 99
 
100
+        if ($isTemplate) {
101
+            $pathCopy->setTitle('[copie] ' . $learningPath->getTitle());
102
+        }
103
+
104
+        if ($session) {
105
+            $pathCopy->setSession($session);
106
+            $session->setLearningPath($pathCopy);
107
+        }
108
+
109
+        $this->entityManager->persist($pathCopy);
110
+        $this->entityManager->flush();
111
+
115 112
         return $pathCopy;
116 113
     }
117 114
 
118
-    private function cloneSequence(Sequence $sequence): Sequence
115
+    protected function cloneSequence(Sequence $sequence): Sequence
119 116
     {
120 117
         $sequenceCopy = clone $sequence;
121 118
         // Copie les éléments
@@ -144,13 +141,13 @@ class EntityDuplicator implements ContainerAwareInterface
144 141
         return $sequenceCopy;
145 142
     }
146 143
 
147
-    private function cloneProgramItem(ProgramItem $programItem): ProgramItem
144
+    protected function cloneProgramItem(ProgramItem $programItem): ProgramItem
148 145
     {
149 146
         // FIXME à corriger
150 147
         return clone $programItem;
151 148
     }
152 149
 
153
-    private function cloneSequenceItem(SequenceItem $sequenceItem): SequenceItem
150
+    protected function cloneSequenceItem(SequenceItem $sequenceItem): SequenceItem
154 151
     {
155 152
         // FIXME à corriger
156 153
         return clone $sequenceItem;
@@ -168,14 +165,13 @@ class EntityDuplicator implements ContainerAwareInterface
168 165
     {
169 166
         // Vérification de l'existance des parcours
170 167
         if (empty($originalPath) || empty($newPath)) {
171
-            throw new PhoenixException($this->container->get('translator')->trans('exception_duplicator_no_learning_path'));
168
+            throw new PhoenixException($this->translator->trans('exception_duplicator_no_learning_path'));
172 169
         }
173 170
 
174
-        $em = $this->getDoctrine()->getManager();
175
-        $sequenceRepository = $em->getRepository(Sequence::class);
171
+        $sequenceRepository = $this->entityManager->getRepository(Sequence::class);
176 172
 
177 173
         // Récupération du service ScormEngine
178
-        $scormEngine = $this->container->get('scorm.services.engine');
174
+        $scormEngine = $this->scormEngine;
179 175
 
180 176
         // On récupère les séquences par repository avec classement croissant des id
181 177
         $originalSequences = $sequenceRepository->findBy(array('learningPath' => $originalPath), array('sequenceId' => 'asc'));
@@ -183,7 +179,7 @@ class EntityDuplicator implements ContainerAwareInterface
183 179
 
184 180
         // Vérification de l'existance des séquences
185 181
         if (empty($originalSequences) || empty($newSequences)) {
186
-            throw new PhoenixException($this->container->get('translator')->trans('exception_duplicator_no_sequences'));
182
+            throw new PhoenixException($this->translator->trans('exception_duplicator_no_sequences'));
187 183
         }
188 184
 
189 185
         // Pour chaque Sequence
@@ -197,7 +193,7 @@ class EntityDuplicator implements ContainerAwareInterface
197 193
 
198 194
             // Vérification de l'existance des items
199 195
             if (empty($originalItems) || empty($newItems)) {
200
-                throw new PhoenixException($this->container->get('translator')->trans('exception_duplicator_no_items'));
196
+                throw new PhoenixException($this->translator->trans('exception_duplicator_no_items'));
201 197
             }
202 198
 
203 199
             // Pour chaque item
@@ -208,24 +204,10 @@ class EntityDuplicator implements ContainerAwareInterface
208 204
                     $new  = $newItems[$j];
209 205
                     $response = $scormEngine->duplicateCourse($original->getSequenceItemId(), $new->getSequenceItemId());
210 206
                     if (400 == $response['code']) {
211
-                        throw new PhoenixException($this->container->get('translator')->trans('exception_duplicator_engine_course'));
207
+                        throw new PhoenixException($this->translator->trans('exception_duplicator_engine_course'));
212 208
                     }
213 209
                 }
214 210
             }
215 211
         }
216 212
     }
217
-
218
-    /**
219
-     *
220
-     * @throws \LogicException
221
-     * @return ManagerRegistry
222
-     */
223
-    protected function getDoctrine(): ManagerRegistry
224
-    {
225
-        if (!$this->container->has('doctrine')) {
226
-            throw new \LogicException('The DoctrineBundle is not registered in your application. Try running "composer require symfony/orm-pack".');
227
-        }
228
-
229
-        return $this->container->get('doctrine');
230
-    }
231 213
 }

+ 15
- 10
src/Phoenix/Service/FileUploader.php View File

@@ -175,20 +175,25 @@ implements FileUploaderInterface
175 175
 		return uniqid() . '.' . pathinfo($oldFileName, PATHINFO_EXTENSION);
176 176
 	}
177 177
 
178
-	public function cloneFile(AbstractEntity $entity, string $propertyName): string
178
+	public function cloneFile(AbstractEntity $entity, string $propertyName)
179 179
 	{
180 180
 		$filesystem = new Filesystem();
181 181
 		$oldFileName = $this->getEntityValue($entity, $propertyName);
182
-		$newFileName = $this->generateNewName($oldFileName);
183
-
184
-		try {
185
-			$filesystem->copy(
186
-				$this->buildUploadPublicPath($oldFileName, $entity, $propertyName),
187
-				$this->buildUploadPublicPath($newFileName, $entity, $propertyName)
188
-			);
189
-		} catch (\Exception $exp) {
190
-			dd($exp->getMessage());
182
+		$newFileName = null;
183
+		if ($oldFileName)
184
+		{
185
+			$newFileName = $this->generateNewName($oldFileName);
186
+
187
+			try {
188
+				$filesystem->copy(
189
+					$this->buildUploadPublicPath($oldFileName, $entity, $propertyName),
190
+					$this->buildUploadPublicPath($newFileName, $entity, $propertyName)
191
+				);
192
+			} catch (\Exception $exp) {
193
+				dd($exp->getMessage());
194
+			}
191 195
 		}
196
+		
192 197
 
193 198
 		return $newFileName;
194 199
 	}

+ 15
- 0
tests/Phoenix/Service/EntityDuplicatorForTest.php View File

@@ -0,0 +1,15 @@
1
+<?php
2
+
3
+namespace Logipro\Phoenix\tests\Phoenix\Service;
4
+
5
+use Logipro\Phoenix\Service\EntityDuplicator;
6
+use Logipro\Phoenix\Entity\LearningPath;
7
+use Logipro\Phoenix\Entity\Session\Session;
8
+
9
+class EntityDuplicatorForTest extends EntityDuplicator
10
+{
11
+    public function cloneLearningPath(LearningPath $learningPath, Session $session = null, bool $isTemplate = false): LearningPath
12
+    {
13
+        return parent::cloneLearningPath($learningPath, $session, $isTemplate);
14
+    }
15
+}

+ 65
- 0
tests/Phoenix/Service/EntityDuplicatorTest.php View File

@@ -0,0 +1,65 @@
1
+<?php
2
+
3
+namespace Logipro\Phoenix\tests\Phoenix\Service;
4
+
5
+use PHPUnit\Framework\TestCase;
6
+use Logipro\Phoenix\Entity\LearningPath;
7
+use Symfony\Component\DependencyInjection\ContainerInterface;
8
+use Symfony\Component\Security\Core\Security;
9
+use Logipro\Phoenix\Service\FileUploader;
10
+use Logipro\Phoenix\Repository\SequenceRepository;
11
+use Logipro\Bundle\SCORMBundle\Services\ScormEngine;
12
+use Symfony\Component\Translation\TranslatorInterface;
13
+use Logipro\Phoenix\Exception\PhoenixException;
14
+use Logipro\Phoenix\Entity\Sequence;
15
+use Logipro\Phoenix\tests\Phoenix\Service\EntityDuplicatorForTest;
16
+use Doctrine\ORM\EntityManagerInterface;
17
+
18
+/**
19
+ * Service de duplication des entités.
20
+ */
21
+class EntityDuplicatEntityDuplicatorTest extends TestCase
22
+{
23
+    /**
24
+     * test si le nom de l'image d'une sequence dupliqué est différent après le clonnage
25
+     * et si le nom est donné par le service fileUploader
26
+     */
27
+    public function testDuplicateLearningPathCopySequenceImage()
28
+    {
29
+        $securityMock = $this->createMock(Security::class);
30
+        $fileUploaderMock = $this->createMock(FileUploader::class);
31
+
32
+        $newName = 'newName';
33
+        $fileUploaderMock->method('cloneFile')->willReturn($newName);
34
+
35
+        $entityManagerMock = $this->createMock(EntityManagerInterface::class);
36
+        $repositoryMock = $this->createMock(SequenceRepository::class);
37
+        $translatorMock = $this->createMock(TranslatorInterface::class);
38
+        $scormEngineMock = $this->createMock(ScormEngine::class);
39
+
40
+        $learningPath = new LearningPath();
41
+        $learningPath->setIsTemplate(true);
42
+
43
+        $sequence = new Sequence();
44
+        $sequence->setImage('oldName');
45
+        $learningPath->setSequences([
46
+            $sequence
47
+        ]);
48
+        $service = new EntityDuplicatorForTest(
49
+            $securityMock,
50
+            $fileUploaderMock,
51
+            $entityManagerMock,
52
+            $translatorMock,
53
+            $scormEngineMock
54
+        );
55
+
56
+        $learningPathCopy = $service->cloneLearningPath($learningPath);
57
+
58
+        $sequencesClone = $learningPathCopy->getSequences();
59
+        $firstSequenceClone = $sequencesClone->first();
60
+        $this->assertSame(
61
+            $firstSequenceClone->getImage(),
62
+            $newName
63
+        );
64
+    }
65
+}

Loading…
Cancel
Save