Passed
Push — translations ( 6cd971...bddd68 )
by Yannick
35:39 queued 27:44
created

Version20240323181500::createCCalendarEvent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 12
nc 1
nop 7
dl 0
loc 23
rs 9.8666
c 1
b 0
f 0
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Entity\ResourceLink;
10
use Chamilo\CoreBundle\Entity\User;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Chamilo\CourseBundle\Entity\CCalendarEvent;
13
use DateTime;
14
use DateTimeZone;
15
use Doctrine\DBAL\Schema\Schema;
16
17
class Version20240323181500 extends AbstractMigrationChamilo
18
{
19
    public function getDescription(): string
20
    {
21
        return 'Migrate sys_calendar to c_calendar_event';
22
    }
23
24
    public function up(Schema $schema): void
25
    {
26
        $em = $this->getEntityManager();
27
        $sysCalendars = $this->connection->fetchAllAssociative('SELECT * FROM sys_calendar');
28
29
        $utc = new DateTimeZone('UTC');
30
        $admin = $this->getAdmin();
31
        foreach ($sysCalendars as $sysCalendar) {
32
33
            $calendarEvent = $this->createCCalendarEvent(
34
                $sysCalendar['title'] ?: '-',
35
                $sysCalendar['content'],
36
                $sysCalendar['start_date'] ? new DateTime($sysCalendar['start_date'], $utc) : null,
37
                $sysCalendar['end_date'] ? new DateTime($sysCalendar['end_date'], $utc) : null,
38
                (bool) $sysCalendar['all_day'],
39
                $sysCalendar['color'] ?? '',
40
                $admin
41
            );
42
43
            $em->persist($calendarEvent);
44
            $this->addGlobalResourceLinkToNode($em, $calendarEvent->getResourceNode());
45
        }
46
47
        $em->flush();
48
    }
49
50
    private function createCCalendarEvent(
51
        string $title,
52
        string $content,
53
        ?DateTime $startDate,
54
        ?DateTime $endDate,
55
        bool $allDay,
56
        string $color,
57
        User $creator
58
    ): CCalendarEvent {
59
        $calendarEvent = new CCalendarEvent();
60
        $calendarEvent
61
            ->setTitle($title)
62
            ->setContent($content)
63
            ->setStartDate($startDate)
64
            ->setEndDate($endDate)
65
            ->setAllDay($allDay)
66
            ->setColor($color)
67
            ->setCreator($creator)
68
            ->setResourceName($title)
69
            ->setParentResourceNode($creator->getResourceNode()->getId())
70
        ;
71
72
        return $calendarEvent;
73
    }
74
75
    private function addGlobalResourceLinkToNode($em, $resourceNode): void
76
    {
77
        $globalLink = new ResourceLink();
78
        $globalLink->setCourse(null)
79
            ->setSession(null)
80
            ->setGroup(null)
81
            ->setUser(null);
82
83
        $alreadyHasGlobalLink = false;
84
        foreach ($resourceNode->getResourceLinks() as $existingLink) {
85
            if (null === $existingLink->getCourse() && null === $existingLink->getSession() &&
86
                null === $existingLink->getGroup() && null === $existingLink->getUser()) {
87
                $alreadyHasGlobalLink = true;
88
                break;
89
            }
90
        }
91
92
        if (!$alreadyHasGlobalLink) {
93
            $resourceNode->addResourceLink($globalLink);
94
            $em->persist($globalLink);
95
        }
96
    }
97
98
    public function down(Schema $schema): void
99
    {
100
        // Down migration is not defined, as data migration cannot be easily reverted
101
    }
102
}
103