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