Passed
Push — master ( b88e45...b1cf97 )
by Angel Fernando Quiroz
07:42
created

Version20201215072918::updateAgendaReminders()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
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\AgendaReminder;
10
use Chamilo\CoreBundle\Entity\Course;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Chamilo\CoreBundle\Repository\Node\CourseRepository;
13
use Chamilo\CourseBundle\Entity\CCalendarEvent;
14
use Chamilo\CourseBundle\Entity\CCalendarEventAttachment;
15
use Chamilo\CourseBundle\Repository\CCalendarEventAttachmentRepository;
16
use Chamilo\CourseBundle\Repository\CCalendarEventRepository;
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 and update agenda_reminder';
26
    }
27
28
    public function up(Schema $schema): void
29
    {
30
        $container = $this->getContainer();
31
        $doctrine = $container->get('doctrine');
32
        $em = $doctrine->getManager();
33
34
        /** @var Connection $connection */
35
        $connection = $em->getConnection();
36
37
        $eventRepo = $container->get(CCalendarEventRepository::class);
38
        $eventAttachmentRepo = $container->get(CCalendarEventAttachmentRepository::class);
39
        $courseRepo = $container->get(CourseRepository::class);
40
41
        /** @var Kernel $kernel */
42
        $kernel = $container->get('kernel');
43
        $rootPath = $kernel->getProjectDir();
44
        $admin = $this->getAdmin();
45
        $oldNewEventIdMap = [];
46
47
        $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
48
49
        /** @var Course $course */
50
        foreach ($q->toIterable() as $course) {
51
            $courseId = $course->getId();
52
            $course = $courseRepo->find($courseId);
53
54
            $sql = "SELECT * FROM c_calendar_event WHERE c_id = {$courseId}
55
                    ORDER BY iid";
56
            $result = $connection->executeQuery($sql);
57
            $events = $result->fetchAllAssociative();
58
            foreach ($events as $eventData) {
59
                $id = $eventData['iid'];
60
                $oldEventId = $id;
61
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,
74
                // then we added to the main course and assign the admin as the creator.
75
                if (empty($items)) {
76
                    $items[] = [
77
                        'visibility' => 1,
78
                        'insert_user_id' => $admin->getId(),
79
                        'to_group_id' => 0,
80
                        'session_id' => $eventData['session_id'],
81
                    ];
82
                    $this->fixItemProperty('calendar_event', $eventRepo, $course, $admin, $event, $course, $items);
83
                    $em->persist($event);
84
                    $em->flush();
85
86
                    continue;
87
                }
88
89
                // Assign parent.
90
                $parent = null;
91
                if (!empty($eventData['parent_event_id'])) {
92
                    $parent = $eventRepo->find($eventData['parent_event_id']);
93
                }
94
                if (null === $parent) {
95
                    $parent = $course;
96
                }
97
98
                if (false === $result) {
99
                    continue;
100
                }
101
102
                $this->fixItemProperty('calendar_event', $eventRepo, $course, $admin, $event, $parent);
103
104
                $em->persist($event);
105
                $em->flush();
106
107
                $newEventId = $event->getId();
0 ignored issues
show
Bug introduced by
The method getId() does not exist on Chamilo\CourseBundle\Entity\CCalendarEvent. Did you maybe mean getIid()? ( Ignorable by Annotation )

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

107
                /** @scrutinizer ignore-call */ 
108
                $newEventId = $event->getId();

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...
108
                $oldNewEventIdMap[$oldEventId] = $newEventId;
109
            }
110
111
            $sql = "SELECT * FROM c_calendar_event_attachment WHERE c_id = {$courseId}
112
                    ORDER BY iid";
113
            $result = $connection->executeQuery($sql);
114
            $attachments = $result->fetchAllAssociative();
115
            foreach ($attachments as $attachmentData) {
116
                $id = $attachmentData['iid'];
117
                $attachmentPath = $attachmentData['path'];
118
                $fileName = $attachmentData['filename'];
119
120
                /** @var CCalendarEventAttachment $attachment */
121
                $attachment = $eventAttachmentRepo->find($id);
122
                if ($attachment->hasResourceNode()) {
123
                    continue;
124
                }
125
                $parent = $attachment->getEvent();
126
                $result = $this->fixItemProperty(
127
                    'calendar_event_attachment',
128
                    $eventRepo,
129
                    $course,
130
                    $admin,
131
                    $attachment,
132
                    $parent
133
                );
134
135
                if (false === $result) {
136
                    continue;
137
                }
138
139
                $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/upload/calendar/'.$attachmentPath;
140
                error_log('MIGRATIONS :: $filePath -- '.$filePath.' ...');
141
                $this->addLegacyFileToResource($filePath, $eventAttachmentRepo, $attachment, $id, $fileName);
142
                $em->persist($attachment);
143
                $em->flush();
144
            }
145
        }
146
147
        $this->updateAgendaReminders($oldNewEventIdMap, $em);
148
    }
149
150
    private function updateAgendaReminders($oldNewEventIdMap, $em): void
151
    {
152
        $reminders = $em->getRepository(AgendaReminder::class)->findBy(['type' => 'course']);
153
        foreach ($reminders as $reminder) {
154
            $oldEventId = $reminder->getEventId();
155
            if (array_key_exists($oldEventId, $oldNewEventIdMap)) {
156
                $newEventId = $oldNewEventIdMap[$oldEventId];
157
                $reminder->setEventId($newEventId);
158
                $em->persist($reminder);
159
            }
160
        }
161
        $em->flush();
162
    }
163
164
    public function down(Schema $schema): void {}
165
}
166