|
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\AccessUrl; |
|
10
|
|
|
use Chamilo\CoreBundle\Entity\AccessUrlRelCourse; |
|
11
|
|
|
use Chamilo\CoreBundle\Entity\Course; |
|
12
|
|
|
use Chamilo\CoreBundle\Entity\ExtraField; |
|
13
|
|
|
use Chamilo\CoreBundle\Entity\ResourceLink; |
|
14
|
|
|
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; |
|
15
|
|
|
use Chamilo\CoreBundle\Repository\Node\AccessUrlRepository; |
|
16
|
|
|
use Chamilo\CoreBundle\Repository\Node\CourseRepository; |
|
17
|
|
|
use Chamilo\CoreBundle\Repository\Node\UserRepository; |
|
18
|
|
|
use Chamilo\CoreBundle\Repository\SessionRepository; |
|
19
|
|
|
use Chamilo\CourseBundle\Entity\CTool; |
|
20
|
|
|
use Chamilo\CourseBundle\Repository\CToolRepository; |
|
21
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
22
|
|
|
|
|
23
|
|
|
final class Version20201212195011 extends AbstractMigrationChamilo |
|
24
|
|
|
{ |
|
25
|
|
|
public function getDescription(): string |
|
26
|
|
|
{ |
|
27
|
|
|
return 'Migrate courses, c_tool '; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function up(Schema $schema): void |
|
31
|
|
|
{ |
|
32
|
|
|
$courseRepo = $this->container->get(CourseRepository::class); |
|
|
|
|
|
|
33
|
|
|
$sessionRepo = $this->container->get(SessionRepository::class); |
|
34
|
|
|
$toolRepo = $this->container->get(CToolRepository::class); |
|
35
|
|
|
$urlRepo = $this->container->get(AccessUrlRepository::class); |
|
36
|
|
|
$userRepo = $this->container->get(UserRepository::class); |
|
37
|
|
|
|
|
38
|
|
|
$batchSize = self::BATCH_SIZE; |
|
39
|
|
|
$admin = $this->getAdmin(); |
|
40
|
|
|
$adminId = $admin->getId(); |
|
41
|
|
|
|
|
42
|
|
|
// Adding courses to the resource node tree. |
|
43
|
|
|
$urls = $urlRepo->findAll(); |
|
44
|
|
|
|
|
45
|
|
|
/** @var AccessUrl $url */ |
|
46
|
|
|
foreach ($urls as $url) { |
|
47
|
|
|
$counter = 1; |
|
48
|
|
|
|
|
49
|
|
|
/** @var AccessUrl $urlEntity */ |
|
50
|
|
|
$urlEntity = $urlRepo->find($url->getId()); |
|
51
|
|
|
$accessUrlRelCourses = $urlEntity->getCourses(); |
|
52
|
|
|
|
|
53
|
|
|
/** @var AccessUrlRelCourse $accessUrlRelCourse */ |
|
54
|
|
|
foreach ($accessUrlRelCourses as $accessUrlRelCourse) { |
|
55
|
|
|
$course = $accessUrlRelCourse->getCourse(); |
|
56
|
|
|
$course = $courseRepo->find($course->getId()); |
|
57
|
|
|
if ($course->hasResourceNode()) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
$urlEntity = $urlRepo->find($url->getId()); |
|
61
|
|
|
$adminEntity = $userRepo->find($adminId); |
|
62
|
|
|
$courseRepo->addResourceNode($course, $adminEntity, $urlEntity); |
|
63
|
|
|
$this->entityManager->persist($course); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
// Add groups. |
|
66
|
|
|
// $course = $course->getGroups(); |
|
67
|
|
|
if (($counter % $batchSize) === 0) { |
|
68
|
|
|
$this->entityManager->flush(); |
|
69
|
|
|
$this->entityManager->clear(); // Detaches all objects from Doctrine! |
|
70
|
|
|
} |
|
71
|
|
|
$counter++; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->entityManager->flush(); |
|
76
|
|
|
$this->entityManager->clear(); |
|
77
|
|
|
|
|
78
|
|
|
// Special course. |
|
79
|
|
|
$extraFieldType = ExtraField::COURSE_FIELD_TYPE; |
|
80
|
|
|
$sql = "SELECT id FROM extra_field |
|
81
|
|
|
WHERE item_type = $extraFieldType AND variable = 'special_course'"; |
|
82
|
|
|
$result = $this->connection->executeQuery($sql); |
|
83
|
|
|
$extraFieldId = $result->fetchOne(); |
|
84
|
|
|
|
|
85
|
|
|
$specialCourses = []; |
|
86
|
|
|
if (!empty($extraFieldId)) { |
|
87
|
|
|
$extraFieldId = (int) $extraFieldId; |
|
88
|
|
|
$sql = "SELECT DISTINCT(item_id) |
|
89
|
|
|
FROM extra_field_values |
|
90
|
|
|
WHERE field_id = $extraFieldId AND field_value= 1 "; |
|
91
|
|
|
$result = $this->connection->executeQuery($sql); |
|
92
|
|
|
$specialCourses = $result->fetchAllAssociative(); |
|
93
|
|
|
if (!empty($specialCourses)) { |
|
94
|
|
|
$specialCourses = array_map('intval', array_column($specialCourses, 'item_id')); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Migrating c_tool. |
|
99
|
|
|
$q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
|
100
|
|
|
|
|
101
|
|
|
/** @var Course $course */ |
|
102
|
|
|
foreach ($q->toIterable() as $course) { |
|
103
|
|
|
$counter = 1; |
|
104
|
|
|
$courseId = $course->getId(); |
|
105
|
|
|
if (!empty($specialCourses) && \in_array($courseId, $specialCourses, true)) { |
|
106
|
|
|
$this->addSql("UPDATE course SET sticky = 1 WHERE id = $courseId "); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$sql = "SELECT * FROM c_tool |
|
110
|
|
|
WHERE c_id = {$courseId} "; |
|
111
|
|
|
$result = $this->connection->executeQuery($sql); |
|
112
|
|
|
$tools = $result->fetchAllAssociative(); |
|
113
|
|
|
|
|
114
|
|
|
foreach ($tools as $toolData) { |
|
115
|
|
|
/** @var CTool $tool */ |
|
116
|
|
|
$tool = $toolRepo->find($toolData['iid']); |
|
117
|
|
|
if ($tool->hasResourceNode()) { |
|
118
|
|
|
continue; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$course = $courseRepo->find($courseId); |
|
122
|
|
|
$session = null; |
|
123
|
|
|
if (!empty($toolData['session_id'])) { |
|
124
|
|
|
$session = $sessionRepo->find($toolData['session_id']); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$admin = $this->getAdmin(); |
|
128
|
|
|
$tool->setParent($course); |
|
129
|
|
|
$toolRepo->addResourceNode($tool, $admin, $course); |
|
130
|
|
|
$newVisibility = 1 === (int) $toolData['visibility'] ? ResourceLink::VISIBILITY_PUBLISHED : ResourceLink::VISIBILITY_DRAFT; |
|
131
|
|
|
$tool->addCourseLink($course, $session, null, $newVisibility); |
|
132
|
|
|
$this->entityManager->persist($tool); |
|
133
|
|
|
if (($counter % $batchSize) === 0) { |
|
134
|
|
|
$this->entityManager->flush(); |
|
135
|
|
|
$this->entityManager->clear(); // Detaches all objects from Doctrine! |
|
136
|
|
|
} |
|
137
|
|
|
$counter++; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
$this->entityManager->flush(); |
|
141
|
|
|
$this->entityManager->clear(); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
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.