Passed
Push — master ( 719760...59ab2e )
by Angel Fernando Quiroz
18:35
created

Version20231022124700   F

Complexity

Total Complexity 75

Size/Duplication

Total Lines 451
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 295
c 1
b 0
f 0
dl 0
loc 451
rs 2.4
wmc 75

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
B replaceURLParametersInContent() 0 44 6
F up() 0 393 68

How to fix   Complexity   

Complex Class

Complex classes like Version20231022124700 often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Version20231022124700, and based on these observations, apply Extract Interface, too.

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\Migrations\AbstractMigrationChamilo;
11
use Chamilo\CourseBundle\Repository\CDocumentRepository;
12
use Doctrine\DBAL\Connection;
13
use Doctrine\DBAL\Schema\Schema;
14
15
use const PREG_NO_ERROR;
16
17
final class Version20231022124700 extends AbstractMigrationChamilo
18
{
19
    public function getDescription(): string
20
    {
21
        return 'Replace old cidReq url path by new version';
22
    }
23
24
    public function up(Schema $schema): void
25
    {
26
        $container = $this->getContainer();
27
        $em = $this->getEntityManager();
28
29
        /** @var Connection $connection */
30
        $connection = $em->getConnection();
31
        $kernel = $container->get('kernel');
32
        $rootPath = $kernel->getProjectDir();
33
34
        /** @var CDocumentRepository $documentRepo */
35
        $documentRepo = $container->get(CDocumentRepository::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
            $courseDirectory = $course->getDirectory();
43
44
            // Tool intro
45
            $sql = "SELECT * FROM c_tool_intro WHERE c_id = {$courseId} ORDER BY iid";
46
            $result = $connection->executeQuery($sql);
47
            $items = $result->fetchAllAssociative();
48
            if (!empty($items)) {
49
                foreach ($items as $itemData) {
50
                    $originalIntroText = $itemData['intro_text'];
51
                    if (!empty($originalIntroText)) {
52
                        $updatedIntroText = $this->replaceURLParametersInContent($originalIntroText, $connection);
53
                        if ($originalIntroText !== $updatedIntroText) {
54
                            $sql = 'UPDATE c_tool_intro SET intro_text = :newIntroText WHERE iid = :introId';
55
                            $params = [
56
                                'newIntroText' => $updatedIntroText,
57
                                'introId' => $itemData['iid'],
58
                            ];
59
                            $connection->executeQuery($sql, $params);
60
                            error_log('Updated c_tool_intro  cid ='.$courseId);
61
                        }
62
                    }
63
                }
64
            }
65
66
            // Course description
67
            $sql = "SELECT * FROM c_course_description WHERE c_id = {$courseId} ORDER BY iid";
68
            $result = $connection->executeQuery($sql);
69
            $items = $result->fetchAllAssociative();
70
            if (!empty($items)) {
71
                foreach ($items as $itemData) {
72
                    $originalContent = $itemData['content'];
73
                    if (!empty($originalContent)) {
74
                        $updatedContent = $this->replaceURLParametersInContent($originalContent, $connection);
75
                        if ($originalContent !== $updatedContent) {
76
                            $sql = 'UPDATE c_course_description SET content = :newContent WHERE iid = :id';
77
                            $params = [
78
                                'newContent' => $updatedContent,
79
                                'id' => $itemData['iid'],
80
                            ];
81
                            $connection->executeQuery($sql, $params);
82
                            error_log('Updated c_course_description  cid ='.$courseId);
83
                        }
84
                    }
85
                }
86
            }
87
88
            // Quiz
89
            $sql = "SELECT * FROM c_quiz WHERE c_id = {$courseId} ORDER BY iid";
90
            $result = $connection->executeQuery($sql);
91
            $items = $result->fetchAllAssociative();
92
            if (!empty($items)) {
93
                foreach ($items as $itemData) {
94
                    $originalDescription = $itemData['description'];
95
                    if (!empty($originalDescription)) {
96
                        $updatedDescription = $this->replaceURLParametersInContent($originalDescription, $connection);
97
                        if ($originalDescription !== $updatedDescription) {
98
                            $sql = 'UPDATE c_quiz SET description = :newDescription WHERE iid = :id';
99
                            $params = [
100
                                'newDescription' => $updatedDescription,
101
                                'id' => $itemData['iid'],
102
                            ];
103
                            $connection->executeQuery($sql, $params);
104
                            error_log('Updated c_quiz  cid ='.$courseId);
105
                        }
106
                    }
107
108
                    $originalText = $itemData['text_when_finished'];
109
                    if (!empty($originalText)) {
110
                        $updatedText = $this->replaceURLParametersInContent($originalText, $connection);
111
                        if ($originalText !== $updatedText) {
112
                            $sql = 'UPDATE c_quiz SET text_when_finished = :newText WHERE iid = :id';
113
                            $params = [
114
                                'newText' => $updatedText,
115
                                'id' => $itemData['iid'],
116
                            ];
117
                            $connection->executeQuery($sql, $params);
118
                            error_log('Updated c_quiz text_when_finished  cid ='.$courseId);
119
                        }
120
                    }
121
                }
122
            }
123
124
            // Quiz question
125
            $sql = "SELECT * FROM c_quiz_question WHERE c_id = {$courseId} ORDER BY iid";
126
            $result = $connection->executeQuery($sql);
127
            $items = $result->fetchAllAssociative();
128
            if (!empty($items)) {
129
                foreach ($items as $itemData) {
130
                    $originalDescription = $itemData['description'];
131
                    if (!empty($originalDescription)) {
132
                        $updatedDescription = $this->replaceURLParametersInContent($originalDescription, $connection);
133
                        if ($originalDescription !== $updatedDescription) {
134
                            $sql = 'UPDATE c_quiz_question SET description = :newDescription WHERE iid = :id';
135
                            $params = [
136
                                'newDescription' => $updatedDescription,
137
                                'id' => $itemData['iid'],
138
                            ];
139
                            $connection->executeQuery($sql, $params);
140
                            error_log('Updated c_quiz_question  cid ='.$courseId);
141
                        }
142
                    }
143
144
                    $originalQuestion = $itemData['question'];
145
                    if (!empty($originalQuestion)) {
146
                        $updatedQuestion = $this->replaceURLParametersInContent($originalQuestion, $connection);
147
                        if ($originalQuestion !== $updatedQuestion) {
148
                            $sql = 'UPDATE c_quiz_question SET question = :newQuestion WHERE iid = :id';
149
                            $params = [
150
                                'newQuestion' => $updatedQuestion,
151
                                'id' => $itemData['iid'],
152
                            ];
153
                            $connection->executeQuery($sql, $params);
154
                            error_log('Updated c_quiz_question question cid ='.$courseId);
155
                        }
156
                    }
157
                }
158
            }
159
160
            // Quiz answer
161
            $sql = "SELECT * FROM c_quiz_answer WHERE c_id = {$courseId} ORDER BY iid";
162
            $result = $connection->executeQuery($sql);
163
            $items = $result->fetchAllAssociative();
164
            if (!empty($items)) {
165
                foreach ($items as $itemData) {
166
                    $originalAnswer = $itemData['answer'];
167
                    if (!empty($originalAnswer)) {
168
                        $updatedAnswer = $this->replaceURLParametersInContent($originalAnswer, $connection);
169
                        if ($originalAnswer !== $updatedAnswer) {
170
                            $sql = 'UPDATE c_quiz_answer SET answer = :newAnswer WHERE iid = :id';
171
                            $params = [
172
                                'newAnswer' => $updatedAnswer,
173
                                'id' => $itemData['iid'],
174
                            ];
175
                            $connection->executeQuery($sql, $params);
176
                            error_log('Updated c_quiz_answer cid ='.$courseId);
177
                        }
178
                    }
179
180
                    $originalComment = $itemData['comment'];
181
                    if (!empty($originalComment)) {
182
                        $updatedComment = $this->replaceURLParametersInContent($originalComment, $connection);
183
                        if ($originalComment !== $updatedComment) {
184
                            $sql = 'UPDATE c_quiz_answer SET comment = :newComment WHERE iid = :id';
185
                            $params = [
186
                                'newComment' => $updatedComment,
187
                                'id' => $itemData['iid'],
188
                            ];
189
                            $connection->executeQuery($sql, $params);
190
                            error_log('Updated c_quiz_answer comment cid ='.$courseId);
191
                        }
192
                    }
193
                }
194
            }
195
196
            // Student publication
197
            $sql = "SELECT * FROM c_student_publication WHERE c_id = {$courseId} ORDER BY iid";
198
            $result = $connection->executeQuery($sql);
199
            $items = $result->fetchAllAssociative();
200
            if (!empty($items)) {
201
                foreach ($items as $itemData) {
202
                    $originalWorkDescription = $itemData['description'];
203
                    if (!empty($originalWorkDescription)) {
204
                        $updatedWorkDescription = $this->replaceURLParametersInContent($originalWorkDescription, $connection);
205
                        if ($originalWorkDescription !== $updatedWorkDescription) {
206
                            $sql = 'UPDATE c_student_publication SET description = :newDescription WHERE iid = :id';
207
                            $params = [
208
                                'newDescription' => $updatedWorkDescription,
209
                                'id' => $itemData['iid'],
210
                            ];
211
                            $connection->executeQuery($sql, $params);
212
                            error_log('Updated c_student_publication cid ='.$courseId);
213
                        }
214
                    }
215
                }
216
            }
217
218
            // Student publication comment
219
            $sql = "SELECT * FROM c_student_publication_comment WHERE c_id = {$courseId} ORDER BY iid";
220
            $result = $connection->executeQuery($sql);
221
            $items = $result->fetchAllAssociative();
222
            if (!empty($items)) {
223
                foreach ($items as $itemData) {
224
                    $originalWorkComment = $itemData['comment'];
225
                    if (!empty($originalWorkComment)) {
226
                        $updatedWorkComment = $this->replaceURLParametersInContent($originalWorkComment, $connection);
227
                        if ($originalWorkComment !== $updatedWorkComment) {
228
                            $sql = 'UPDATE c_student_publication_comment SET comment = :newComment WHERE iid = :id';
229
                            $params = [
230
                                'newComment' => $updatedWorkComment,
231
                                'id' => $itemData['iid'],
232
                            ];
233
                            $connection->executeQuery($sql, $params);
234
                            error_log('Updated c_student_publication_comment cid ='.$courseId);
235
                        }
236
                    }
237
                }
238
            }
239
240
            // Forum category
241
            $sql = "SELECT * FROM c_forum_category WHERE c_id = {$courseId} ORDER BY iid";
242
            $result = $connection->executeQuery($sql);
243
            $items = $result->fetchAllAssociative();
244
            if (!empty($items)) {
245
                foreach ($items as $itemData) {
246
                    $originalCatComment = $itemData['cat_comment'];
247
                    if (!empty($originalCatComment)) {
248
                        $updatedCatComment = $this->replaceURLParametersInContent($originalCatComment, $connection);
249
                        if ($originalCatComment !== $updatedCatComment) {
250
                            $sql = 'UPDATE c_forum_category SET cat_comment = :newComment WHERE iid = :id';
251
                            $params = [
252
                                'newComment' => $updatedCatComment,
253
                                'id' => $itemData['iid'],
254
                            ];
255
                            $connection->executeQuery($sql, $params);
256
                            error_log('Updated c_forum_category cid ='.$courseId);
257
                        }
258
                    }
259
                }
260
            }
261
262
            // Forum
263
            $sql = "SELECT * FROM c_forum_forum WHERE c_id = {$courseId} ORDER BY iid";
264
            $result = $connection->executeQuery($sql);
265
            $items = $result->fetchAllAssociative();
266
            if (!empty($items)) {
267
                foreach ($items as $itemData) {
268
                    $originalForumComment = $itemData['forum_comment'];
269
                    if (!empty($originalForumComment)) {
270
                        $updatedForumComment = $this->replaceURLParametersInContent($originalForumComment, $connection);
271
                        if ($originalForumComment !== $updatedForumComment) {
272
                            $sql = 'UPDATE c_forum_forum SET forum_comment = :newComment WHERE iid = :id';
273
                            $params = [
274
                                'newComment' => $updatedForumComment,
275
                                'id' => $itemData['iid'],
276
                            ];
277
                            $connection->executeQuery($sql, $params);
278
                            error_log('Updated c_forum_forum cid ='.$courseId);
279
                        }
280
                    }
281
                }
282
            }
283
284
            // Forum post
285
            $sql = "SELECT * FROM c_forum_post WHERE c_id = {$courseId} ORDER BY iid";
286
            $result = $connection->executeQuery($sql);
287
            $items = $result->fetchAllAssociative();
288
            if (!empty($items)) {
289
                foreach ($items as $itemData) {
290
                    $originalPostText = $itemData['post_text'];
291
                    if (!empty($originalPostText)) {
292
                        $updatedPostText = $this->replaceURLParametersInContent($originalPostText, $connection);
293
                        if ($originalPostText !== $updatedPostText) {
294
                            $sql = 'UPDATE c_forum_post SET post_text = :newText WHERE iid = :id';
295
                            $params = [
296
                                'newText' => $updatedPostText,
297
                                'id' => $itemData['iid'],
298
                            ];
299
                            $connection->executeQuery($sql, $params);
300
                            error_log('Updated c_forum_post cid ='.$courseId);
301
                        }
302
                    }
303
                }
304
            }
305
306
            // Glossary
307
            $sql = "SELECT * FROM c_glossary WHERE c_id = {$courseId} ORDER BY iid";
308
            $result = $connection->executeQuery($sql);
309
            $items = $result->fetchAllAssociative();
310
            if (!empty($items)) {
311
                foreach ($items as $itemData) {
312
                    $originalGlossaryDescription = $itemData['description'];
313
                    if (!empty($originalGlossaryDescription)) {
314
                        $updatedGlossaryDescription = $this->replaceURLParametersInContent($originalGlossaryDescription, $connection);
315
                        if ($originalGlossaryDescription !== $updatedGlossaryDescription) {
316
                            $sql = 'UPDATE c_glossary SET description = :newDescription WHERE iid = :id';
317
                            $params = [
318
                                'newDescription' => $updatedGlossaryDescription,
319
                                'id' => $itemData['iid'],
320
                            ];
321
                            $connection->executeQuery($sql, $params);
322
                            error_log('Updated c_glossary cid ='.$courseId);
323
                        }
324
                    }
325
                }
326
            }
327
328
            // Survey
329
            $sql = "SELECT * FROM c_survey WHERE c_id = {$courseId} ORDER BY iid";
330
            $result = $connection->executeQuery($sql);
331
            $items = $result->fetchAllAssociative();
332
            if (!empty($items)) {
333
                foreach ($items as $itemData) {
334
                    $originalSurveyTitle = $itemData['title'];
335
                    if (!empty($originalSurveyTitle)) {
336
                        $updatedSurveyTitle = $this->replaceURLParametersInContent($originalSurveyTitle, $connection);
337
                        if ($originalSurveyTitle !== $updatedSurveyTitle) {
338
                            $sql = 'UPDATE c_survey SET title = :newTitle WHERE iid = :id';
339
                            $params = [
340
                                'newTitle' => $updatedSurveyTitle,
341
                                'id' => $itemData['iid'],
342
                            ];
343
                            $connection->executeQuery($sql, $params);
344
                            error_log('Updated c_survey cid ='.$courseId);
345
                        }
346
                    }
347
348
                    $originalSurveySubTitle = $itemData['subtitle'];
349
                    if (!empty($originalSurveySubTitle)) {
350
                        $updatedSurveySubTitle = $this->replaceURLParametersInContent($originalSurveySubTitle, $connection);
351
                        if ($originalSurveySubTitle !== $updatedSurveySubTitle) {
352
                            $sql = 'UPDATE c_survey SET subtitle = :newSubtitle WHERE iid = :id';
353
                            $params = [
354
                                'newSubtitle' => $updatedSurveySubTitle,
355
                                'id' => $itemData['iid'],
356
                            ];
357
                            $connection->executeQuery($sql, $params);
358
                            error_log('Updated c_survey subtitle cid ='.$courseId);
359
                        }
360
                    }
361
                }
362
            }
363
364
            // Survey question
365
            $sql = "SELECT * FROM c_survey_question WHERE c_id = {$courseId} ORDER BY iid";
366
            $result = $connection->executeQuery($sql);
367
            $items = $result->fetchAllAssociative();
368
            if (!empty($items)) {
369
                foreach ($items as $itemData) {
370
                    $originalSurveyQuestion = $itemData['survey_question'];
371
                    if (!empty($originalSurveyQuestion)) {
372
                        $updatedSurveyQuestion = $this->replaceURLParametersInContent($originalSurveyQuestion, $connection);
373
                        if ($originalSurveyQuestion !== $updatedSurveyQuestion) {
374
                            $sql = 'UPDATE c_survey_question SET survey_question = :newQuestion WHERE iid = :id';
375
                            $params = [
376
                                'newQuestion' => $updatedSurveyQuestion,
377
                                'id' => $itemData['iid'],
378
                            ];
379
                            $connection->executeQuery($sql, $params);
380
                            error_log('Updated c_survey_question cid ='.$courseId);
381
                        }
382
                    }
383
384
                    $originalSurveyQuestionComment = $itemData['survey_question_comment'];
385
                    if (!empty($originalSurveyQuestionComment)) {
386
                        $updatedSurveyQuestionComment = $this->replaceURLParametersInContent($originalSurveyQuestionComment, $connection);
387
                        if ($originalSurveyQuestionComment !== $updatedSurveyQuestionComment) {
388
                            $sql = 'UPDATE c_survey_question SET survey_question_comment = :newComment WHERE iid = :id';
389
                            $params = [
390
                                'newComment' => $updatedSurveyQuestionComment,
391
                                'id' => $itemData['iid'],
392
                            ];
393
                            $connection->executeQuery($sql, $params);
394
                            error_log('Updated c_survey_question survey_question_comment cid ='.$courseId);
395
                        }
396
                    }
397
                }
398
            }
399
400
            // Survey question option
401
            $sql = "SELECT * FROM c_survey_question_option WHERE c_id = {$courseId} ORDER BY iid";
402
            $result = $connection->executeQuery($sql);
403
            $items = $result->fetchAllAssociative();
404
            if (!empty($items)) {
405
                foreach ($items as $itemData) {
406
                    $originalOptionText = $itemData['option_text'];
407
                    if (!empty($originalOptionText)) {
408
                        $updatedOptionText = $this->replaceURLParametersInContent($originalOptionText, $connection);
409
                        if ($originalOptionText !== $updatedOptionText) {
410
                            $sql = 'UPDATE c_survey_question_option SET option_text = :newText WHERE iid = :id';
411
                            $params = [
412
                                'newText' => $updatedOptionText,
413
                                'id' => $itemData['iid'],
414
                            ];
415
                            $connection->executeQuery($sql, $params);
416
                            error_log('Updated c_survey_question_option cid ='.$courseId);
417
                        }
418
                    }
419
                }
420
            }
421
        }
422
    }
423
424
    private function replaceURLParametersInContent($htmlContent, $connection)
425
    {
426
        $pattern = '/((https?:\/\/[^\/\s]*|)\/[^?\s]+?)\?cidReq=([a-zA-Z0-9_]+)(&(?:amp;)?id_session=([0-9]+))(&(?:amp;)?gidReq=([0-9]+))([^"\s]*)/i';
427
428
        // Replace URLs with a callback function.
429
        $newContent = @preg_replace_callback(
430
            $pattern,
431
            function ($matches) use ($connection) {
432
                $code = $matches[3]; // The 'code' is extracted from the captured URL.
433
434
                $courseId = null;
435
                $sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1';
436
                $stmt = $connection->executeQuery($sql, ['code' => $code]);
437
                $course = $stmt->fetch();
438
439
                if ($course) {
440
                    $courseId = $course['id'];
441
                }
442
443
                if (null === $courseId) {
444
                    return $matches[0]; // Complete original URL.
445
                }
446
447
                // Processing the remaining part of the URL.
448
                $remainingParams = '';
449
                if (!empty($matches[8])) {
450
                    $remainingParams = $matches[8];
451
                    if ('&' !== $remainingParams[0]) {
452
                        $remainingParams = '&'.$remainingParams;
453
                    }
454
                }
455
456
                // Reconstructing the URL with the new courseId and adjusted parameters.
457
                return $matches[1].'?cid='.$courseId.'&sid='.$matches[5].'&gid='.$matches[7].$remainingParams;
458
                // Return the new URL.
459
            },
460
            $htmlContent
461
        );
462
463
        if (PREG_NO_ERROR !== preg_last_error()) {
464
            error_log('Error encountered in preg_replace_callback: '.preg_last_error());
465
        }
466
467
        return $newContent;
468
    }
469
}
470