Passed
Push — master ( 70291d...363d10 )
by Julito
10:16
created

Version20210930130343   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 75
dl 0
loc 125
rs 10
c 3
b 0
f 1
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
C up() 0 111 12
A down() 0 2 1
1
<?php
2
3
declare(strict_types=1);
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\SessionRepository;
11
use Chamilo\CoreBundle\Repository\ToolRepository;
12
use Chamilo\CourseBundle\Entity\CTool;
13
use Chamilo\CourseBundle\Entity\CToolIntro;
14
use Chamilo\CourseBundle\Repository\CToolIntroRepository;
15
use Chamilo\CourseBundle\Repository\CToolRepository;
16
use Doctrine\DBAL\Schema\Schema;
17
18
final class Version20210930130343 extends AbstractMigrationChamilo
19
{
20
    public function getDescription(): string
21
    {
22
        return 'CToolIntro';
23
    }
24
25
    public function up(Schema $schema): void
26
    {
27
        $container = $this->getContainer();
28
        $em = $this->getEntityManager();
29
        $connection = $em->getConnection();
30
31
        $introRepo = $container->get(CToolIntroRepository::class);
32
        $cToolRepo = $container->get(CToolRepository::class);
33
        $toolRepo = $container->get(ToolRepository::class);
34
35
        $sessionRepo = $container->get(SessionRepository::class);
36
        $courseRepo = $container->get(CourseRepository::class);
37
38
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
39
40
        /** @var Course $course */
41
        foreach ($q->toIterable() as $course) {
42
            $courseId = $course->getId();
43
            $sql = "SELECT * FROM c_tool_intro WHERE c_id = {$courseId}
44
                    ORDER BY iid";
45
            $result = $connection->executeQuery($sql);
46
            $items = $result->fetchAllAssociative();
47
48
            if (empty($items)) {
49
                $admin = $this->getAdmin();
50
                $tool = $toolRepo->findOneBy(['name' => 'course_homepage']);
51
                $cTool = (new CTool())
52
                    ->setName('course_homepage')
53
                    ->setCourse($course)
54
                    ->setTool($tool)
55
                    ->setCreator($admin)
56
                    ->setParent($course)
57
                    ->addCourseLink($course)
58
                ;
59
                $em->persist($cTool);
60
                $em->flush();
61
62
                continue;
63
            }
64
65
            foreach ($items as $itemData) {
66
                $id = $itemData['iid'];
67
                $sessionId = (int) $itemData['session_id'];
68
                $toolName = $itemData['id'];
69
70
                /** @var CToolIntro $intro */
71
                $intro = $introRepo->find($id);
72
73
                if ($intro->hasResourceNode()) {
74
                    continue;
75
                }
76
77
                $session = null;
78
                if (!empty($sessionId)) {
79
                    $session = $sessionRepo->find($sessionId);
80
                }
81
82
                $admin = $this->getAdmin();
83
84
                $cTool = null;
85
                if ('course_homepage' === $toolName) {
86
                    $tool = $toolRepo->findOneBy(['name' => $toolName]);
87
88
                    if (null === $tool) {
89
                        continue;
90
                    }
91
92
                    $course = $courseRepo->find($courseId);
93
94
                    $cTool = (new CTool())
95
                        ->setName('course_homepage')
96
                        ->setCourse($course)
97
                        ->setSession($session)
98
                        ->setTool($tool)
99
                        ->setCreator($admin)
100
                        ->setParent($course)
101
                        ->addCourseLink($course, $session)
102
                    ;
103
                    $em->persist($cTool);
104
                } else {
105
                    $cTool = $cToolRepo->findCourseResourceByTitle($toolName, $course->getResourceNode(), $course, $session);
106
                }
107
108
                if (null === $cTool) {
109
                    continue;
110
                }
111
112
                $intro
113
                    ->setParent($course)
114
                    ->setCourseTool($cTool)
115
                ;
116
                $introRepo->addResourceNode($intro, $admin, $course);
117
                $intro->addCourseLink($course, $session);
118
119
                $em->persist($intro);
120
                $em->flush();
121
            }
122
123
            $em->flush();
124
            $em->clear();
125
126
            $table = $schema->getTable('c_tool_intro');
127
128
            if ($table->hasColumn('c_tool_id')) {
129
                if (!$table->hasForeignKey('FK_D705267B1DF6B517')) {
130
                    $this->addSql(
131
                        'ALTER TABLE c_tool_intro ADD CONSTRAINT FK_D705267B1DF6B517 FOREIGN KEY (c_tool_id) REFERENCES c_tool (iid);'
132
                    );
133
                }
134
                if (!$table->hasIndex('IDX_D705267B1DF6B517')) {
135
                    $this->addSql('CREATE INDEX IDX_D705267B1DF6B517 ON c_tool_intro (c_tool_id);');
136
                }
137
            }
138
        }
139
    }
140
141
    public function down(Schema $schema): void
142
    {
143
    }
144
}
145