Passed
Pull Request — master (#5636)
by
unknown
07:08
created

Version20240704120500::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 1
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\ExtraField;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Doctrine\DBAL\Schema\Schema;
12
13
final class Version20240704120500 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Migrate extra fields to duration field in multiple tables';
18
    }
19
20
    public function up(Schema $schema): void
21
    {
22
        $this->migrateStudentPublicationDuration();
23
        $this->migrateAttendanceCalendarDuration();
24
    }
25
26
    public function down(Schema $schema): void
27
    {
28
        // Revert changes if necessary
29
        $this->addSql('UPDATE c_student_publication SET duration = NULL WHERE duration IS NOT NULL');
30
        $this->addSql('UPDATE c_attendance_calendar SET duration = NULL WHERE duration IS NOT NULL');
31
    }
32
33
    private function migrateStudentPublicationDuration(): void
34
    {
35
        $sql = 'SELECT selected_value FROM settings_current WHERE variable = "considered_working_time" AND selected_value IS NOT NULL AND selected_value != "" AND selected_value != "false"';
36
        $selectedValue = $this->connection->fetchOne($sql);
37
38
        if ($selectedValue) {
39
            $sql = 'SELECT s.*, efv.field_value
40
                    FROM c_student_publication s
41
                    INNER JOIN extra_field_values efv ON s.iid = efv.item_id
42
                    INNER JOIN extra_field ef ON efv.field_id = ef.id
43
                    WHERE ef.variable = ? AND ef.item_type = ?';
44
45
            $params = [$selectedValue, ExtraField::WORK_FIELD_TYPE];
46
            $data = $this->connection->fetchAllAssociative($sql, $params);
47
48
            foreach ($data as $item) {
49
                $id = $item['iid'];
50
                $workTime = (int) $item['field_value'];
51
52
                $durationInSeconds = $workTime * 60;
53
54
                $this->addSql("UPDATE c_student_publication SET duration = ? WHERE iid = ?", [$durationInSeconds, $id]);
55
            }
56
        }
57
    }
58
59
    private function migrateAttendanceCalendarDuration(): void
60
    {
61
        $sql = 'SELECT s.*, efv.field_value
62
                FROM c_attendance_calendar s
63
                INNER JOIN extra_field_values efv ON s.iid = efv.item_id
64
                INNER JOIN extra_field ef ON efv.field_id = ef.id
65
                WHERE ef.variable = "duration" AND ef.item_type = ?';
66
67
        $params = [ExtraField::ATTENDANCE_CALENDAR_TYPE];
68
        $data = $this->connection->fetchAllAssociative($sql, $params);
69
70
        foreach ($data as $item) {
71
            $id = $item['iid'];
72
            $duration = $item['field_value'];
73
74
            $matches = [];
75
            $newDuration = null;
76
77
            if (preg_match('/(\d+)([h:](\d+)?)?/', $duration, $matches)) {
78
                $hours = (int)$matches[1];
79
                $minutes = 0;
80
                if (!empty($matches[3])) {
81
                    $minutes = (int)$matches[3];
82
                }
83
                $newDuration = ($hours * 3600) + ($minutes * 60);
84
            }
85
86
            if ($newDuration !== null) {
87
                $this->addSql('UPDATE c_attendance_calendar SET duration = ? WHERE iid = ?', [$newDuration, $id]);
88
            }
89
        }
90
    }
91
}
92