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\Entity\Session; |
11
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
12
|
|
|
use Chamilo\CoreBundle\Repository\Node\CourseRepository; |
13
|
|
|
use Chamilo\CourseBundle\Entity\CLp; |
14
|
|
|
use Chamilo\CourseBundle\Repository\CLpRepository; |
15
|
|
|
use Doctrine\DBAL\Connection; |
16
|
|
|
use Doctrine\DBAL\Schema\Schema; |
17
|
|
|
|
18
|
|
|
final class Version20230615213500 extends AbstractMigrationChamilo |
19
|
|
|
{ |
20
|
|
|
public function getDescription(): string |
21
|
|
|
{ |
22
|
|
|
return 'Migrate c_lp to resource node position'; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function up(Schema $schema): void |
26
|
|
|
{ |
27
|
|
|
$container = $this->getContainer(); |
28
|
|
|
$doctrine = $container->get('doctrine'); |
29
|
|
|
$em = $doctrine->getManager(); |
30
|
|
|
|
31
|
|
|
/** @var Connection $connection */ |
32
|
|
|
$connection = $em->getConnection(); |
33
|
|
|
|
34
|
|
|
$lpRepo = $container->get(CLpRepository::class); |
35
|
|
|
$courseRepo = $container->get(CourseRepository::class); |
36
|
|
|
|
37
|
|
|
$q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
38
|
|
|
|
39
|
|
|
/** @var Course $course */ |
40
|
|
|
foreach ($q->toIterable() as $course) { |
41
|
|
|
$courseId = $course->getId(); |
42
|
|
|
|
43
|
|
|
$sql = "SELECT * FROM c_lp WHERE c_id = {$courseId} ORDER BY display_order"; |
44
|
|
|
$result = $connection->executeQuery($sql); |
45
|
|
|
$lps = $result->fetchAllAssociative(); |
46
|
|
|
|
47
|
|
|
foreach ($lps as $lp) { |
48
|
|
|
$lpId = (int) $lp['iid']; |
49
|
|
|
$position = (int) $lp['display_order']; |
50
|
|
|
|
51
|
|
|
/** @var CLp $resource */ |
52
|
|
|
$resource = $lpRepo->find($lpId); |
53
|
|
|
if ($resource->hasResourceNode()) { |
54
|
|
|
$resourceNode = $resource->getResourceNode(); |
55
|
|
|
|
56
|
|
|
$course = $em->find(Course::class, $lp['c_id']); |
57
|
|
|
$session = isset($lp['session_id']) |
58
|
|
|
? $em->find(Session::class, $lp['session_id']) |
59
|
|
|
: null; |
60
|
|
|
|
61
|
|
|
$link = $resourceNode->getResourceLinkByContext($course, $session); |
62
|
|
|
|
63
|
|
|
if ($link) { |
64
|
|
|
$link->setDisplayOrder( |
65
|
|
|
$position > 0 ? $position - 1 : 0 |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$em->flush(); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|