Passed
Push — master ( ab20b0...4728a8 )
by Julito
09:22
created

Version20201219113351::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 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
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\CoreBundle\Repository\Node\CourseRepository;
10
use Chamilo\CoreBundle\Repository\Node\UserRepository;
11
use Chamilo\CoreBundle\Repository\SessionRepository;
12
use Chamilo\CourseBundle\Entity\CBlog;
13
use Chamilo\CourseBundle\Repository\CBlogRepository;
14
use Chamilo\CourseBundle\Repository\CGroupRepository;
15
use Chamilo\Kernel;
16
use Doctrine\DBAL\Connection;
17
use Doctrine\DBAL\Schema\Schema;
18
19
final class Version20201219113351 extends AbstractMigrationChamilo
20
{
21
    public function getDescription(): string
22
    {
23
        return 'Migrate c_blog ';
24
    }
25
26
    public function up(Schema $schema): void
27
    {
28
        $container = $this->getContainer();
29
        $doctrine = $container->get('doctrine');
30
        $em = $doctrine->getManager();
31
        /** @var Connection $connection */
32
        $connection = $em->getConnection();
33
34
        $blogRepo = $container->get(CBlogRepository::class);
35
        $courseRepo = $container->get(CourseRepository::class);
36
        $sessionRepo = $container->get(SessionRepository::class);
37
        $groupRepo = $container->get(CGroupRepository::class);
38
        $userRepo = $container->get(UserRepository::class);
39
        /** @var Kernel $kernel */
40
        $kernel = $container->get('kernel');
41
        $rootPath = $kernel->getProjectDir();
42
43
        $admin = $this->getAdmin();
44
45
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
46
        /** @var Course $course */
47
        foreach ($q->toIterable() as $course) {
48
            $courseId = $course->getId();
49
            $course = $courseRepo->find($courseId);
50
51
            $sql = "SELECT * FROM c_blog WHERE c_id = $courseId ORDER BY iid";
52
            $result = $connection->executeQuery($sql);
53
            $items = $result->fetchAllAssociative();
54
            foreach ($items as $itemData) {
55
                $id = $itemData['iid'];
56
                /** @var CBlog $resource */
57
                $resource = $blogRepo->find($id);
58
                if ($resource->hasResourceNode()) {
59
                    continue;
60
                }
61
62
                $result = $this->fixItemProperty(
63
                    'blog_management',
64
                    $blogRepo,
65
                    $course,
66
                    $admin,
67
                    $resource,
68
                    $course
69
                );
70
71
                if (false === $result) {
72
                    continue;
73
                }
74
75
                $em->persist($resource);
76
                $em->flush();
77
            }
78
79
            $em->flush();
80
            $em->clear();
81
82
            $sql = "SELECT * FROM c_blog_attachment WHERE c_id = $courseId ORDER BY iid";
83
            $result = $connection->executeQuery($sql);
84
            $items = $result->fetchAllAssociative();
85
            foreach ($items as $itemData) {
86
                $id = $itemData['iid'];
87
                $blogId = $itemData['blog_id'];
88
                $path = $itemData['path'];
89
                $fileName = $itemData['filename'];
90
                $comment = $itemData['comment'];
91
92
                /** @var CBlog $resource */
93
                $resource = $blogRepo->find($blogId);
94
                if (false === $resource->hasResourceNode()) {
95
                    continue;
96
                }
97
98
                if (!empty($path)) {
99
                    $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/blog/'.$path;
100
                    $this->addLegacyFileToResource($filePath, $blogRepo, $resource, $id, $fileName, $comment);
101
                }
102
103
                $em->persist($resource);
104
                $em->flush();
105
            }
106
107
            $em->flush();
108
            $em->clear();
109
        }
110
    }
111
}
112