|
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\ResourceLink; |
|
11
|
|
|
use Chamilo\CoreBundle\Entity\Tool; |
|
12
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
|
13
|
|
|
use Chamilo\CoreBundle\Settings\SettingsManager; |
|
14
|
|
|
use Chamilo\CoreBundle\Tool\ToolChain; |
|
15
|
|
|
use Chamilo\CourseBundle\Entity\CTool; |
|
16
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
17
|
|
|
use Doctrine\ORM\Query; |
|
18
|
|
|
|
|
19
|
|
|
final class Version20250928100200 extends AbstractMigrationChamilo |
|
20
|
|
|
{ |
|
21
|
|
|
public function getDescription(): string |
|
22
|
|
|
{ |
|
23
|
|
|
return 'Seed missing course tools into existing courses (blog tool).'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function up(Schema $schema): void |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var ToolChain $toolChain */ |
|
29
|
|
|
$toolChain = $this->container->get(ToolChain::class); |
|
|
|
|
|
|
30
|
|
|
/** @var SettingsManager $settings */ |
|
31
|
|
|
$settings = $this->container->get(SettingsManager::class); |
|
32
|
|
|
|
|
33
|
|
|
// Ensure global catalog is seeded (tool + resource_types) |
|
34
|
|
|
$toolChain->createTools(); |
|
35
|
|
|
|
|
36
|
|
|
$targetTitle = 'blog'; |
|
37
|
|
|
|
|
38
|
|
|
// Get Tool ID by title (fail fast if not found) |
|
39
|
|
|
$toolId = $this->entityManager |
|
40
|
|
|
->createQuery('SELECT t.id FROM Chamilo\CoreBundle\Entity\Tool t WHERE t.title = :title') |
|
|
|
|
|
|
41
|
|
|
->setParameter('title', $targetTitle) |
|
42
|
|
|
->getOneOrNullResult(Query::HYDRATE_SINGLE_SCALAR); |
|
43
|
|
|
|
|
44
|
|
|
if ($toolId === null) { |
|
45
|
|
|
$this->write('Tool "'.$targetTitle.'" does not exist; aborting migration.'); |
|
46
|
|
|
return; |
|
47
|
|
|
} |
|
48
|
|
|
$toolId = (int) $toolId; |
|
49
|
|
|
|
|
50
|
|
|
$activeOnCreate = $settings->getSetting('course.active_tools_on_create') ?? []; |
|
51
|
|
|
$visible = \in_array($targetTitle, $activeOnCreate, true); |
|
52
|
|
|
$linkVisibility = $visible ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT; |
|
53
|
|
|
|
|
54
|
|
|
$batchSize = self::BATCH_SIZE; |
|
55
|
|
|
$i = 0; |
|
56
|
|
|
|
|
57
|
|
|
// Iterate over course IDs only (no heavy hydration) |
|
58
|
|
|
$q = $this->entityManager->createQuery('SELECT c.id FROM Chamilo\CoreBundle\Entity\Course c'); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($q->toIterable([], Query::HYDRATE_SCALAR) as $row) { |
|
61
|
|
|
$courseId = (int) $row['id']; |
|
62
|
|
|
|
|
63
|
|
|
// Does the course already have this tool? |
|
64
|
|
|
$exists = (int) $this->entityManager |
|
65
|
|
|
->createQuery( |
|
66
|
|
|
'SELECT COUNT(ct.iid) |
|
67
|
|
|
FROM Chamilo\CourseBundle\Entity\CTool ct |
|
68
|
|
|
WHERE ct.title = :title |
|
69
|
|
|
AND ct.course = :course' |
|
70
|
|
|
) |
|
71
|
|
|
->setParameter('title', $targetTitle) |
|
72
|
|
|
->setParameter('course', $this->entityManager->getReference(Course::class, $courseId)) |
|
73
|
|
|
->getSingleScalarResult(); |
|
74
|
|
|
|
|
75
|
|
|
if ($exists > 0) { |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$toolRef = $this->entityManager->getReference(Tool::class, $toolId); |
|
80
|
|
|
$courseRef = $this->entityManager->getReference(Course::class, $courseId); |
|
81
|
|
|
|
|
82
|
|
|
$ctool = (new CTool()) |
|
83
|
|
|
->setTool($toolRef) |
|
84
|
|
|
->setTitle($targetTitle) |
|
85
|
|
|
->setVisibility($visible) |
|
86
|
|
|
->setCourse($courseRef) |
|
87
|
|
|
->setParent($courseRef) |
|
88
|
|
|
->setCreator($courseRef->getCreator()) |
|
89
|
|
|
->addCourseLink($courseRef, null, null, $linkVisibility); |
|
90
|
|
|
|
|
91
|
|
|
$this->entityManager->persist($ctool); |
|
92
|
|
|
|
|
93
|
|
|
if ((++$i % $batchSize) === 0) { |
|
94
|
|
|
$this->entityManager->flush(); |
|
95
|
|
|
$this->entityManager->clear(); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$this->entityManager->flush(); |
|
100
|
|
|
$this->entityManager->clear(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function down(Schema $schema): void {} |
|
104
|
|
|
} |
|
105
|
|
|
|
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.