Passed
Push — master ( c223c8...be9e8f )
by
unknown
09:35
created

Version20201216110722   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 38
c 1
b 1
f 0
dl 0
loc 78
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
B up() 0 71 6
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\CAttendance;
13
use Chamilo\CourseBundle\Entity\CAttendanceCalendar;
14
use Chamilo\CourseBundle\Repository\CAttendanceRepository;
15
use Doctrine\DBAL\Schema\Schema;
16
17
final class Version20201216110722 extends AbstractMigrationChamilo
18
{
19
    public function getDescription(): string
20
    {
21
        return 'Migrate c_attendance';
22
    }
23
24
    public function up(Schema $schema): void
25
    {
26
        $attendanceRepo = $this->container->get(CAttendanceRepository::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

26
        /** @scrutinizer ignore-call */ 
27
        $attendanceRepo = $this->container->get(CAttendanceRepository::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...
27
        // $attendanceRepo = $container->get(CAttendanceCalendar::class);
28
        $courseRepo = $this->container->get(CourseRepository::class);
29
30
        $admin = $this->getAdmin();
31
32
        $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

32
        /** @scrutinizer ignore-call */ 
33
        $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...
33
34
        // The title of the attendance table is used to create the slug in the resource_node tbale.
35
        // Before creating a new registry in resource_node, Doctrine is looking for all the registries that start with the same name
36
        // It then find the last one depending on the name and create new one with the "title-number" 
37
        // where title is the title of the attendance and the number is the max number all ready existing + 1
38
        // Since in Chamilo 1.11.x the name of the first attendance is created automatically we have a lot of attendance with the same name
39
        // This make the migration to take a really long time if we have many attendance because this process is taking more and more time
40
        // when creating more registry with the same name.
41
        // So to avoid this process taking so much time :
42
        // * Save temporarly the title and the id of all the attendance in a PHP Array
43
        // * we modify the title of the attendance to make it unique during the migration by adding the iid at the end
44
        // * We then process the migration
45
        // * At the end we restore the title without the iid and also the resource_node.title
46
47
        $sql = "SELECT iid, title FROM c_attendance";
48
        $result = $this->connection->executeQuery($sql);
49
        $attendancesBackup = $result->fetchAllAssociative();
50
        $sqlUpdateTitle = "UPDATE c_attendance SET title = CONCAT(title, '-', iid)";
51
        $resultUpdate = $this->connection->executeQuery($sqlUpdateTitle);
52
53
        /** @var Course $course */
54
        foreach ($q->toIterable() as $course) {
55
            $courseId = $course->getId();
56
            $course = $courseRepo->find($courseId);
57
58
            // c_thematic.
59
            $sql = "SELECT * FROM c_attendance WHERE c_id = {$courseId}
60
                    ORDER BY iid";
61
            $result = $this->connection->executeQuery($sql);
62
            $items = $result->fetchAllAssociative();
63
            foreach ($items as $itemData) {
64
                $id = $itemData['iid'];
65
66
                /** @var CAttendance $resource */
67
                $resource = $attendanceRepo->find($id);
68
                if ($resource->hasResourceNode()) {
69
                    continue;
70
                }
71
72
                $result = $this->fixItemProperty(
73
                    'attendance',
74
                    $attendanceRepo,
75
                    $course,
76
                    $admin,
77
                    $resource,
78
                    $course
79
                );
80
81
                if (false === $result) {
82
                    continue;
83
                }
84
85
                $this->entityManager->persist($resource);
86
                $this->entityManager->flush();
87
            }
88
        }
89
        // Restoring attendance title and resource_node title
90
        foreach ($attendancesBackup as $attendance) {
91
            $sqlRestoreAttendance = "UPDATE c_attendance SET title = '{$attendance['title']}' where iid = {$attendance['iid']}";
92
            $resultUpdate = $this->connection->executeQuery($sqlRestoreAttendance);
93
            $sqlUpdateResourceNode = "UPDATE resource_node SET title = '{$attendance['title']}' where id in (SELECT resource_node_id FROM c_attendance where iid = {$attendance['iid']})";
94
            $resultUpdate = $this->connection->executeQuery($sqlUpdateResourceNode);
95
        }
96
    }
97
}
98