Passed
Push — master ( 33172e...a587b8 )
by Julito
116:21
created

Version20201216132000::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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\Migrations\AbstractMigrationChamilo;
10
use Chamilo\CourseBundle\Entity\CLp;
11
use Chamilo\CourseBundle\Repository\CLpItemRepository;
12
use Doctrine\DBAL\Connection;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20201216132000 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Migrate c_lp_item to new order';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $container = $this->getContainer();
25
        $doctrine = $container->get('doctrine');
26
        $em = $doctrine->getManager();
27
        /** @var Connection $connection */
28
        $connection = $em->getConnection();
29
30
        $lpItemRepo = $container->get(CLpItemRepository::class);
31
32
        $batchSize = self::BATCH_SIZE;
33
        //$q = $em->createQuery('SELECT lp FROM Chamilo\CourseBundle\Entity\CLp lp WHERE lp.iid = 263 ORDER BY lp.iid');
34
        $q = $em->createQuery('SELECT lp FROM Chamilo\CourseBundle\Entity\CLp lp');
35
        $counter = 1;
36
        /** @var CLp $lp */
37
        foreach ($q->toIterable() as $lp) {
38
            $lpId = $lp->getIid();
39
            error_log("LP #$lpId");
40
41
            /** @var CLp $resource */
42
            //$resource = $lpRepo->find($lpId);
43
            if (!$lp->hasResourceNode()) {
44
                error_log("no resource node");
45
                continue;
46
            }
47
48
            // Root item is created in the previous Migration.
49
            $rootItem = $lpItemRepo->getRootItem($lpId);
50
51
            if (null === $rootItem) {
52
                error_log("no root item");
53
                continue;
54
            }
55
56
            // Migrate c_lp_item
57
            $sql = "SELECT * FROM c_lp_item WHERE lp_id = $lpId AND path <> 'root'
58
                    ORDER BY display_order";
59
            $resultItems = $connection->executeQuery($sql);
60
            $lpItems = $resultItems->fetchAllAssociative();
61
62
            if (empty($lpItems)) {
63
                error_log("no items");
64
                continue;
65
            }
66
67
            $orderList = [];
68
            foreach ($lpItems as $item) {
69
                $object = new \stdClass();
70
                $object->id = $item['iid'];
71
                $object->parent_id = (int) $item['parent_item_id'];
72
                $orderList[] = $object;
73
            }
74
75
            echo '<pre>';
76
            var_dump($lpId, $lpItems);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($lpId, $lpItems) looks like debug code. Are you sure you do not want to remove it?
Loading history...
77
            echo '</pre>';
78
            error_log("save new order");
79
            \learnpath::sortItemByOrderList($rootItem, $orderList, true);
80
            if (($counter % $batchSize) === 0) {
81
                $em->flush();
82
                $em->clear(); // Detaches all objects from Doctrine!
83
            }
84
            $counter++;
85
            $em->flush();
86
        }
87
88
        $em->flush();
89
        $em->clear();
90
    }
91
}
92