Passed
Push — master ( 131c23...309a4e )
by Angel Fernando Quiroz
07:28
created

Version20201215072920   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 86
dl 0
loc 149
rs 10
c 0
b 0
f 0
wmc 18

4 Methods

Rating   Name   Duplication   Size   Complexity  
C up() 0 115 13
A updateAgendaReminders() 0 13 3
A getDescription() 0 3 1
A down() 0 1 1
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\Course;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
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 Doctrine\DBAL\Schema\Schema;
17
18
final class Version20201215072920 extends AbstractMigrationChamilo
19
{
20
    public function getDescription(): string
21
    {
22
        return 'Migrate c_calendar_event, calendar_event_attachment and update agenda_reminder';
23
    }
24
25
    public function up(Schema $schema): void
26
    {
27
        $eventRepo = $this->container->get(CCalendarEventRepository::class);
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

27
        /** @scrutinizer ignore-call */ 
28
        $eventRepo = $this->container->get(CCalendarEventRepository::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
28
        $eventAttachmentRepo = $this->container->get(CCalendarEventAttachmentRepository::class);
29
        $courseRepo = $this->container->get(CourseRepository::class);
30
31
        $kernel = $this->container->get('kernel');
32
        $rootPath = $kernel->getProjectDir();
33
        $admin = $this->getAdmin();
34
        $oldNewEventMap = [];
35
36
        $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
0 ignored issues
show
Bug introduced by
The method createQuery() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

36
        /** @scrutinizer ignore-call */ 
37
        $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
37
38
        /** @var Course $course */
39
        foreach ($q->toIterable() as $course) {
40
            $courseId = $course->getId();
41
            $course = $courseRepo->find($courseId);
42
43
            $sql = "SELECT * FROM c_calendar_event WHERE c_id = {$courseId}
44
                    ORDER BY iid";
45
            $result = $this->connection->executeQuery($sql);
46
            $events = $result->fetchAllAssociative();
47
            foreach ($events as $eventData) {
48
                $id = $eventData['iid'];
49
                $oldEventId = $id;
50
51
                /** @var CCalendarEvent $event */
52
                $event = $eventRepo->find($id);
53
                if ($event->hasResourceNode()) {
54
                    continue;
55
                }
56
57
                $sql = "SELECT * FROM c_item_property
58
                        WHERE tool = 'calendar_event' AND c_id = {$courseId} AND ref = {$id}";
59
                $result = $this->connection->executeQuery($sql);
60
                $items = $result->fetchAllAssociative();
61
62
                // For some reason this event doesnt have a c_item_property value,
63
                // then we added to the main course and assign the admin as the creator.
64
                if (empty($items)) {
65
                    $items[] = [
66
                        'visibility' => 1,
67
                        'insert_user_id' => $admin->getId(),
68
                        'to_group_id' => 0,
69
                        'session_id' => $eventData['session_id'],
70
                    ];
71
                    $this->fixItemProperty('calendar_event', $eventRepo, $course, $admin, $event, $course, $items);
72
                    $this->entityManager->persist($event);
73
                    $this->entityManager->flush();
74
75
                    continue;
76
                }
77
78
                // Assign parent.
79
                $parent = null;
80
                if (!empty($eventData['parent_event_id'])) {
81
                    $parent = $eventRepo->find($eventData['parent_event_id']);
82
                }
83
                if (null === $parent) {
84
                    $parent = $course;
85
                }
86
87
                if (false === $result) {
88
                    continue;
89
                }
90
91
                $this->fixItemProperty('calendar_event', $eventRepo, $course, $admin, $event, $parent);
92
93
                $this->entityManager->persist($event);
94
                $this->entityManager->flush();
95
96
                $oldNewEventMap[$oldEventId] = $event;
97
            }
98
99
            $sql = "SELECT * FROM c_calendar_event_attachment WHERE c_id = {$courseId}
100
                    ORDER BY iid";
101
            $result = $this->connection->executeQuery($sql);
102
            $attachments = $result->fetchAllAssociative();
103
            foreach ($attachments as $attachmentData) {
104
                $id = $attachmentData['iid'];
105
                $attachmentPath = $attachmentData['path'];
106
                $fileName = $attachmentData['filename'];
107
108
                /** @var CCalendarEventAttachment $attachment */
109
                $attachment = $eventAttachmentRepo->find($id);
110
                if ($attachment->hasResourceNode()) {
111
                    continue;
112
                }
113
                $parent = $attachment->getEvent();
114
                $result = $this->fixItemProperty(
115
                    'calendar_event_attachment',
116
                    $eventRepo,
117
                    $course,
118
                    $admin,
119
                    $attachment,
120
                    $parent
121
                );
122
123
                if (false === $result) {
124
                    continue;
125
                }
126
127
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/calendar/'.$attachmentPath;
128
                error_log('MIGRATIONS :: $filePath -- '.$filePath.' ...');
129
                $this->addLegacyFileToResource($filePath, $eventAttachmentRepo, $attachment, $id, $fileName);
130
                $this->entityManager->persist($attachment);
131
                $this->entityManager->flush();
132
            }
133
        }
134
135
        if ($schema->hasTable('agenda_reminder')) {
136
            $tblAgendaReminder = $schema->getTable('agenda_reminder');
137
138
            if ($tblAgendaReminder->hasColumn('type')) {
139
                $this->updateAgendaReminders($oldNewEventMap);
140
            }
141
        }
142
    }
143
144
    /**
145
     * @param array<int, CCalendarEvent> $oldNewEventMap
146
     */
147
    private function updateAgendaReminders(array $oldNewEventMap): void
148
    {
149
        $result = $this->connection->executeQuery("SELECT * FROM agenda_reminder WHERE type = 'course'");
150
151
        while (($reminder = $result->fetchAssociative()) !== false) {
152
            $oldEventId = $reminder['event_id'];
153
            if (\array_key_exists($oldEventId, $oldNewEventMap)) {
154
                $newEvent = $oldNewEventMap[$oldEventId];
155
                $this->addSql(
156
                    sprintf(
157
                        'UPDATE agenda_reminder SET event_id = %d WHERE id = %d',
158
                        $newEvent->getIid(),
159
                        $reminder['id']
160
                    )
161
                );
162
            }
163
        }
164
    }
165
166
    public function down(Schema $schema): void {}
167
}
168