Passed
Push — master ( b9a35c...ab20b0 )
by Julito
15:41
created

Version20201216124011::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\Entity\ResourceLink;
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
11
use Chamilo\CoreBundle\Repository\Node\UserRepository;
12
use Chamilo\CoreBundle\Repository\SessionRepository;
13
use Chamilo\CourseBundle\Entity\CStudentPublication;
14
use Chamilo\CourseBundle\Entity\CStudentPublicationComment;
15
use Chamilo\CourseBundle\Entity\CStudentPublicationCorrection;
16
use Chamilo\CourseBundle\Repository\CGroupRepository;
17
use Chamilo\CourseBundle\Repository\CStudentPublicationCommentRepository;
18
use Chamilo\CourseBundle\Repository\CStudentPublicationCorrectionRepository;
19
use Chamilo\CourseBundle\Repository\CStudentPublicationRepository;
20
use Chamilo\Kernel;
21
use Doctrine\DBAL\Connection;
22
use Doctrine\DBAL\Schema\Schema;
23
24
final class Version20201216124011 extends AbstractMigrationChamilo
25
{
26
    public function getDescription(): string
27
    {
28
        return 'Migrate c_student_publication';
29
    }
30
31
    public function up(Schema $schema): void
32
    {
33
        $container = $this->getContainer();
34
        $doctrine = $container->get('doctrine');
35
        $em = $doctrine->getManager();
36
        /** @var Connection $connection */
37
        $connection = $em->getConnection();
38
39
        $studentPublicationRepo = $container->get(CStudentPublicationRepository::class);
40
        $studentPublicationCommentRepo = $container->get(CStudentPublicationCommentRepository::class);
41
        $studentPublicationCorrectionRepo = $container->get(CStudentPublicationCorrectionRepository::class);
42
43
        $courseRepo = $container->get(CourseRepository::class);
44
        $sessionRepo = $container->get(SessionRepository::class);
45
        $groupRepo = $container->get(CGroupRepository::class);
46
        $userRepo = $container->get(UserRepository::class);
47
        /** @var Kernel $kernel */
48
        $kernel = $container->get('kernel');
49
        $rootPath = $kernel->getProjectDir();
50
51
        $admin = $this->getAdmin();
52
53
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
54
        /** @var Course $course */
55
        foreach ($q->toIterable() as $course) {
56
            $courseId = $course->getId();
57
            $course = $courseRepo->find($courseId);
58
59
            // Assignments folders
60
            $sql = "SELECT * FROM c_student_publication WHERE contains_file = 0 AND c_id = $courseId
61
                    ORDER BY iid";
62
            $result = $connection->executeQuery($sql);
63
            $items = $result->fetchAllAssociative();
64
            foreach ($items as $itemData) {
65
                $id = $itemData['iid'];
66
                /** @var CStudentPublication $resource */
67
                $resource = $studentPublicationRepo->find($id);
68
                if ($resource->hasResourceNode()) {
69
                    continue;
70
                }
71
72
                $result = $this->fixItemProperty(
73
                    'work',
74
                    $studentPublicationRepo,
75
                    $course,
76
                    $admin,
77
                    $resource,
78
                    $course
79
                );
80
81
                if (false === $result) {
82
                    continue;
83
                }
84
85
                $em->persist($resource);
86
                $em->flush();
87
            }
88
89
            $em->flush();
90
            $em->clear();
91
92
            // Assignments files
93
            $sql = "SELECT * FROM c_student_publication
94
                    WHERE contains_file = 1 AND c_id = $courseId
95
                    ORDER BY iid";
96
            $result = $connection->executeQuery($sql);
97
            $items = $result->fetchAllAssociative();
98
            foreach ($items as $itemData) {
99
                $id = $itemData['iid'];
100
                $path = $itemData['url'];
101
                $title = $itemData['title'];
102
                $parentId = $itemData['parent_id'];
103
                /** @var CStudentPublication $resource */
104
                $resource = $studentPublicationRepo->find($id);
105
                if ($resource->hasResourceNode()) {
106
                    continue;
107
                }
108
109
                $parent = $studentPublicationRepo->find($parentId);
110
111
                $result = $this->fixItemProperty(
112
                    'work',
113
                    $studentPublicationRepo,
114
                    $course,
115
                    $admin,
116
                    $resource,
117
                    $parent
118
                );
119
120
                if (false === $result) {
121
                    continue;
122
                }
123
124
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$path;
125
                $this->addLegacyFileToResource($filePath, $studentPublicationRepo, $resource, $id, $title);
126
                $em->persist($resource);
127
                $em->flush();
128
            }
129
130
            $em->flush();
131
            $em->clear();
132
133
            $admin = $this->getAdmin();
134
135
            // Corrections.
136
            $sql = "SELECT * FROM c_student_publication
137
                    WHERE
138
                          (title_correction <> '' OR title_correction IS NOT NULL) AND
139
                          c_id = $courseId
140
                    ORDER BY iid";
141
            $result = $connection->executeQuery($sql);
142
            $items = $result->fetchAllAssociative();
143
            foreach ($items as $itemData) {
144
                $id = $itemData['iid'];
145
                $title = $itemData['title_correction'];
146
                $path = $itemData['url_correction'];
147
148
                $course = $courseRepo->find($courseId);
149
                /** @var CStudentPublication $studentPublication */
150
                $studentPublication = $studentPublicationRepo->find($id);
151
                $correction = $studentPublication->getCorrection();
152
                if (null !== $correction) {
153
                    continue;
154
                }
155
156
                $correction = new CStudentPublicationCorrection();
157
                $correction->setTitle($title);
158
                $correction->setParent($studentPublication);
159
                $studentPublicationCorrectionRepo->addResourceNode($correction, $admin, $studentPublication);
160
                $em->persist($correction);
161
162
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$path;
163
                $this->addLegacyFileToResource($filePath, $studentPublicationCorrectionRepo, $correction, null, $title);
164
                $em->persist($correction);
165
                $em->flush();
166
            }
167
168
            $em->flush();
169
            $em->clear();
170
171
            // Comments
172
            $sql = "SELECT * FROM c_student_publication_comment WHERE c_id = $courseId
173
                    ORDER BY iid";
174
            $result = $connection->executeQuery($sql);
175
            $items = $result->fetchAllAssociative();
176
            foreach ($items as $itemData) {
177
                $id = $itemData['iid'];
178
                $file = $itemData['file'];
179
                $workId = $itemData['work_id'];
180
                /** @var CStudentPublicationComment $resource */
181
                $resource = $studentPublicationCommentRepo->find($id);
182
                if ($resource->hasResourceNode()) {
183
                    continue;
184
                }
185
                /** @var CStudentPublication $parent */
186
                $parent = $studentPublicationRepo->find($workId);
187
                $sql = "SELECT * FROM c_student_publication WHERE c_id = $courseId AND id = $workId";
188
                $result = $connection->executeQuery($sql);
189
                $work = $result->fetchAssociative();
190
                if (empty($work)) {
191
                    continue;
192
                }
193
                $session = empty($work['session_id']) ? null : $sessionRepo->find($work['session_id']);
194
                $group = empty($work['post_group_id']) ? null : $groupRepo->find($work['post_group_id']);
195
196
                $resource->setParent($parent);
197
                $resource->addCourseLink($course, $session, $group, ResourceLink::VISIBILITY_PUBLISHED);
198
199
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/'.$file;
200
                $this->addLegacyFileToResource($filePath, $studentPublicationRepo, $resource, $id, $title);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $title does not seem to be defined for all execution paths leading up to this point.
Loading history...
201
                $em->persist($resource);
202
                $em->flush();
203
            }
204
        }
205
    }
206
}
207