Passed
Push — master ( 6304f4...fc13ff )
by Julito
08:01
created

Version20210221082033::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Entity\Course;
8
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
9
use Chamilo\CourseBundle\Repository\CLpRepository;
10
use Chamilo\Kernel;
11
use Doctrine\DBAL\Connection;
12
use Doctrine\DBAL\Schema\Schema;
13
14
class Version20210221082033 extends AbstractMigrationChamilo
15
{
16
    public function getDescription(): string
17
    {
18
        return 'Migrate c_lp images';
19
    }
20
21
    public function up(Schema $schema): void
22
    {
23
        $container = $this->getContainer();
24
        /** @var Kernel $kernel */
25
        $kernel = $container->get('kernel');
26
        $rootPath = $kernel->getProjectDir();
27
        $doctrine = $container->get('doctrine');
28
29
        $em = $doctrine->getManager();
30
        /** @var Connection $connection */
31
        $connection = $em->getConnection();
32
        $lpRepo = $container->get(CLpRepository::class);
33
34
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
35
        /** @var Course $course */
36
        foreach ($q->toIterable() as $course) {
37
            $courseId = $course->getId();
38
            $sql = "SELECT * FROM c_lp WHERE c_id = $courseId
39
                    ORDER BY iid";
40
            $result = $connection->executeQuery($sql);
41
            $items = $result->fetchAllAssociative();
42
            foreach ($items as $itemData) {
43
                $id = $itemData['iid'];
44
                $lp = $lpRepo->find($id);
45
                if ($lp && !empty($lp->getPreviewImage())) {
46
                    $path = $lp->getPreviewImage();
47
                    $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/learning_path/images/'.$path;
48
                    if (file_exists($rootPath)) {
49
                        $this->addLegacyFileToResource($filePath, $lpRepo, $lp, $lp->getIid(), $path);
50
                        $em->persist($lp);
51
                        $em->flush();
52
                    }
53
                }
54
            }
55
56
            $em->flush();
57
            $em->clear();
58
        }
59
    }
60
}
61