Passed
Push — master ( b58c60...cd85be )
by Julito
08:44
created

Version20201215072918   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 78
c 1
b 0
f 0
dl 0
loc 118
rs 10
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A down() 0 2 1
C up() 0 105 11
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository;
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\CCalendarEvent;
13
use Chamilo\CourseBundle\Entity\CCalendarEventAttachment;
14
use Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository;
15
use Chamilo\CourseBundle\Repository\CCalendarEventRepository;
16
use Chamilo\CourseBundle\Repository\CGroupRepository;
17
use Chamilo\Kernel;
18
use Doctrine\DBAL\Connection;
19
use Doctrine\DBAL\Schema\Schema;
20
21
final class Version20201215072918 extends AbstractMigrationChamilo
22
{
23
    public function getDescription(): string
24
    {
25
        return 'Migrate c_calendar_event, calendar_event_attachment';
26
    }
27
28
    public function up(Schema $schema): void
29
    {
30
        $container = $this->getContainer();
31
        $doctrine = $container->get('doctrine');
32
        $em = $doctrine->getManager();
33
        /** @var Connection $connection */
34
        $connection = $em->getConnection();
35
36
        $urlRepo = $container->get(AccessUrlRepository::class);
37
        $eventRepo = $container->get(CCalendarEventRepository::class);
38
        $eventAttachmentRepo = $container->get(CCalendarEventAttachmentRepository::class);
39
        $courseRepo = $container->get(CourseRepository::class);
40
        $sessionRepo = $container->get(SessionRepository::class);
41
        $groupRepo = $container->get(CGroupRepository::class);
42
        $userRepo = $container->get(UserRepository::class);
43
44
        /** @var Kernel $kernel */
45
        $kernel = $container->get('kernel');
46
        $rootPath = $kernel->getProjectDir();
47
        $admin = $this->getAdmin();
48
        $urls = $urlRepo->findAll();
49
50
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
51
        foreach ($q->toIterable() as $course) {
52
            $counter = 1;
53
            $courseId = $course->getId();
54
            $course = $courseRepo->find($courseId);
55
56
            $sql = "SELECT * FROM c_calendar_event WHERE c_id = $courseId
57
                    ORDER BY iid";
58
            $result = $connection->executeQuery($sql);
59
            $events = $result->fetchAllAssociative();
60
            foreach ($events as $eventData) {
61
                $id = $eventData['iid'];
62
                /** @var CCalendarEvent $event */
63
                $event = $eventRepo->find($id);
64
                if ($event->hasResourceNode()) {
65
                    continue;
66
                }
67
68
                $sql = "SELECT * FROM c_item_property
69
                        WHERE tool = 'calendar_event' AND c_id = $courseId AND ref = $id";
70
                $result = $connection->executeQuery($sql);
71
                $items = $result->fetchAllAssociative();
72
73
                // For some reason this event doesnt have a c_item_property value, then we added to the main course.
74
                if (empty($items)) {
75
                    $items[] = [
76
                        'visibility' => 1,
77
                        'insert_user_id' => $admin->getId(),
78
                        'to_group_id' => 0,
79
                        'session_id' => $eventData['session_id'],
80
                    ];
81
                    $this->fixItemProperty($eventRepo, $course, $admin, $event, $course, $items);
82
                    $em->persist($event);
83
                    $em->flush();
84
                    continue;
85
                }
86
                $parent = null;
87
                if (!empty($eventData['parent_event_id'])) {
88
                    $parent = $eventRepo->find($eventData['parent_event_id']);
89
                }
90
                if (null === $parent) {
91
                    $parent = $course;
92
                }
93
                $result = $this->fixItemProperty('calendar_event', $eventRepo, $course, $admin, $event, $parent);
94
95
                if (false === $result) {
96
                    continue;
97
                }
98
                $em->persist($event);
99
                $em->flush();
100
            }
101
102
            $sql = "SELECT * FROM c_calendar_event_attachment WHERE c_id = $courseId
103
                    ORDER BY iid";
104
            $result = $connection->executeQuery($sql);
105
            $attachments = $result->fetchAllAssociative();
106
            foreach ($attachments as $attachmentData) {
107
                $id = $attachmentData['iid'];
108
                $attachmentPath = $attachmentData['path'];
109
                $fileName = $attachmentData['filename'];
110
                /** @var CCalendarEventAttachment $attachment */
111
                $attachment = $eventAttachmentRepo->find($id);
112
                if ($attachment->hasResourceNode()) {
113
                    continue;
114
                }
115
                $parent = $attachment->getEvent();
116
                $result = $this->fixItemProperty(
117
                    'calendar_event_attachment',
118
                    $eventRepo,
119
                    $course,
120
                    $admin,
121
                    $attachment,
122
                    $parent
123
                );
124
125
                if (false === $result) {
126
                    continue;
127
                }
128
129
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/calendar/'.$attachmentPath;
130
                $this->addLegacyFileToResource($filePath, $eventAttachmentRepo, $attachment, $id, $fileName);
131
                $em->persist($attachment);
132
                $em->flush();
133
            }
134
        }
135
    }
136
137
    public function down(Schema $schema): void
138
    {
139
    }
140
}
141