Conditions | 21 |
Paths | 882 |
Total Lines | 225 |
Code Lines | 138 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
30 | public function up(Schema $schema): void |
||
31 | { |
||
32 | $container = $this->getContainer(); |
||
33 | $doctrine = $container->get('doctrine'); |
||
34 | $em = $doctrine->getManager(); |
||
35 | /** @var Connection $connection */ |
||
36 | $connection = $em->getConnection(); |
||
37 | |||
38 | $documentRepo = $container->get(CDocumentRepository::class); |
||
39 | $courseRepo = $container->get(CourseRepository::class); |
||
40 | $attemptRepo = $em->getRepository(TrackEAttempt::class); |
||
41 | |||
42 | /** @var Kernel $kernel */ |
||
43 | $kernel = $container->get('kernel'); |
||
44 | $rootPath = $kernel->getProjectDir(); |
||
45 | |||
46 | $batchSize = self::BATCH_SIZE; |
||
47 | |||
48 | // Migrate teacher exercise audio. |
||
49 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
50 | /** @var Course $course */ |
||
51 | foreach ($q->toIterable() as $course) { |
||
52 | $courseId = $course->getId(); |
||
53 | $sql = "SELECT iid, path |
||
54 | FROM c_document |
||
55 | WHERE |
||
56 | c_id = $courseId AND |
||
57 | path LIKE '/../exercises/teacher_audio%' |
||
58 | "; |
||
59 | $result = $connection->executeQuery($sql); |
||
60 | $documents = $result->fetchAllAssociative(); |
||
61 | |||
62 | foreach ($documents as $documentData) { |
||
63 | $documentId = $documentData['iid']; |
||
64 | $path = $documentData['path']; |
||
65 | |||
66 | $path = str_replace('//', '/', $path); |
||
67 | $path = str_replace('/../exercises/teacher_audio/', '', $path); |
||
68 | |||
69 | $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/exercises/teacher_audio/'.$path; |
||
70 | |||
71 | if ($this->fileExists($filePath)) { |
||
72 | preg_match('#/(.*)/#', '/'.$path, $matches); |
||
73 | if (isset($matches[1]) && !empty($matches[1])) { |
||
74 | $attemptId = $matches[1]; |
||
75 | /** @var TrackEAttempt $attempt */ |
||
76 | $attempt = $attemptRepo->find($attemptId); |
||
77 | if (null !== $attempt) { |
||
78 | if ($attempt->getAttemptFeedbacks()->count() > 0) { |
||
79 | continue; |
||
80 | } |
||
81 | |||
82 | $fileName = basename($filePath); |
||
83 | $mimeType = mime_content_type($filePath); |
||
84 | $file = new UploadedFile($filePath, $fileName, $mimeType, null, true); |
||
85 | $asset = (new Asset()) |
||
86 | ->setCategory(Asset::EXERCISE_FEEDBACK) |
||
87 | ->setTitle($fileName) |
||
88 | ->setFile($file) |
||
89 | ; |
||
90 | $em->persist($asset); |
||
91 | $em->flush(); |
||
92 | |||
93 | $attemptFile = (new AttemptFile()) |
||
94 | ->setAsset($asset) |
||
95 | ; |
||
96 | $attempt->addAttemptFile($attemptFile); |
||
97 | $em->persist($attemptFile); |
||
98 | $em->flush(); |
||
99 | |||
100 | $sql = "UPDATE c_document |
||
101 | SET comment = 'skip_migrate' |
||
102 | WHERE iid = $documentId |
||
103 | "; |
||
104 | $connection->executeQuery($sql); |
||
105 | } |
||
106 | } |
||
107 | } |
||
108 | } |
||
109 | $em->flush(); |
||
110 | $em->clear(); |
||
111 | } |
||
112 | |||
113 | $em->flush(); |
||
114 | $em->clear(); |
||
115 | |||
116 | // Migrate student exercise audio |
||
117 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
118 | /** @var Course $course */ |
||
119 | foreach ($q->toIterable() as $course) { |
||
120 | $courseId = $course->getId(); |
||
121 | |||
122 | $sql = "SELECT iid, path |
||
123 | FROM c_document |
||
124 | WHERE |
||
125 | c_id = $courseId AND |
||
126 | path NOT LIKE '/../exercises/teacher_audio%' AND |
||
127 | path LIKE '/../exercises/%' |
||
128 | "; |
||
129 | $result = $connection->executeQuery($sql); |
||
130 | $documents = $result->fetchAllAssociative(); |
||
131 | |||
132 | foreach ($documents as $documentData) { |
||
133 | $documentId = $documentData['iid']; |
||
134 | $path = $documentData['path']; |
||
135 | |||
136 | $path = str_replace('//', '/', $path); |
||
137 | $path = str_replace('/../exercises/', '', $path); |
||
138 | |||
139 | $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/exercises/'.$path; |
||
140 | if ($this->fileExists($filePath)) { |
||
141 | $fileName = basename($filePath); |
||
142 | preg_match('#/(.*)/(.*)/(.*)/(.*)/#', '/'.$path, $matches); |
||
143 | $sessionId = $matches[1] ?? 0; |
||
144 | $exerciseId = $matches[2] ?? 0; |
||
145 | $questionId = $matches[3] ?? 0; |
||
146 | $userId = $matches[4] ?? 0; |
||
147 | |||
148 | /** @var TrackEAttempt $attempt */ |
||
149 | $attempt = $attemptRepo->findOneBy([ |
||
150 | 'user' => $userId, |
||
151 | 'questionId' => $questionId, |
||
152 | 'filename' => $fileName |
||
153 | ]); |
||
154 | if (null !== $attempt) { |
||
155 | if ($attempt->getAttemptFiles()->count() > 0) { |
||
156 | continue; |
||
157 | } |
||
158 | |||
159 | $mimeType = mime_content_type($filePath); |
||
160 | $file = new UploadedFile($filePath, $fileName, $mimeType, null, true); |
||
161 | $asset = (new Asset()) |
||
162 | ->setCategory(Asset::EXERCISE_ATTEMPT) |
||
163 | ->setTitle($fileName) |
||
164 | ->setFile($file) |
||
165 | ; |
||
166 | $em->persist($asset); |
||
167 | $em->flush(); |
||
168 | |||
169 | $attemptFile = (new AttemptFile()) |
||
170 | ->setAsset($asset) |
||
171 | ; |
||
172 | $attempt->addAttemptFile($attemptFile); |
||
173 | $em->persist($attemptFile); |
||
174 | $em->flush(); |
||
175 | |||
176 | $sql = "UPDATE c_document |
||
177 | SET comment = 'skip_migrate' |
||
178 | WHERE iid = $documentId |
||
179 | "; |
||
180 | $connection->executeQuery($sql); |
||
181 | } |
||
182 | } |
||
183 | } |
||
184 | $em->flush(); |
||
185 | $em->clear(); |
||
186 | } |
||
187 | |||
188 | $em->flush(); |
||
189 | $em->clear(); |
||
190 | |||
191 | // Migrate normal documents. |
||
192 | $q = $em->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); |
||
193 | /** @var Course $course */ |
||
194 | foreach ($q->toIterable() as $course) { |
||
195 | $counter = 1; |
||
196 | $courseId = $course->getId(); |
||
197 | |||
198 | $sql = "SELECT iid, path FROM c_document |
||
199 | WHERE |
||
200 | c_id = {$courseId} AND |
||
201 | (comment IS NULL OR comment <> 'skip_migrate') |
||
202 | ORDER BY filetype DESC, path"; |
||
203 | $result = $connection->executeQuery($sql); |
||
204 | $documents = $result->fetchAllAssociative(); |
||
205 | foreach ($documents as $documentData) { |
||
206 | $documentId = $documentData['iid']; |
||
207 | $documentPath = $documentData['path']; |
||
208 | $course = $courseRepo->find($courseId); |
||
209 | |||
210 | /** @var CDocument $document */ |
||
211 | $document = $documentRepo->find($documentId); |
||
212 | if ($document->hasResourceNode()) { |
||
213 | continue; |
||
214 | } |
||
215 | |||
216 | $parent = null; |
||
217 | if ('.' !== \dirname($documentPath)) { |
||
218 | $parentId = (int) DocumentManager::get_document_id( |
||
219 | [ |
||
220 | 'real_id' => $courseId, |
||
221 | ], |
||
222 | \dirname($documentPath) |
||
223 | ); |
||
224 | if (!empty($parentId)) { |
||
225 | $parent = $documentRepo->find($parentId); |
||
226 | } |
||
227 | } |
||
228 | |||
229 | if (null === $parent) { |
||
230 | $parent = $course; |
||
231 | } |
||
232 | $admin = $this->getAdmin(); |
||
233 | $result = $this->fixItemProperty('document', $documentRepo, $course, $admin, $document, $parent); |
||
234 | |||
235 | if (false === $result) { |
||
236 | continue; |
||
237 | } |
||
238 | |||
239 | $filePath = $rootPath.'/app/courses/'.$course->getDirectory().'/document/'.$documentPath; |
||
240 | $this->addLegacyFileToResource($filePath, $documentRepo, $document, $documentId); |
||
241 | $em->persist($document); |
||
242 | |||
243 | if (($counter % $batchSize) === 0) { |
||
244 | $em->flush(); |
||
245 | $em->clear(); |
||
246 | } |
||
247 | $counter++; |
||
248 | } |
||
249 | $em->flush(); |
||
250 | $em->clear(); |
||
251 | } |
||
252 | |||
253 | $em->flush(); |
||
254 | $em->clear(); |
||
255 | } |
||
261 |