|
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\Migrations\AbstractMigrationChamilo; |
|
10
|
|
|
use Chamilo\CourseBundle\Entity\CDocument; |
|
11
|
|
|
use Chamilo\CourseBundle\Repository\CDocumentRepository; |
|
12
|
|
|
use Chamilo\CourseBundle\Repository\CQuizQuestionRepository; |
|
13
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
14
|
|
|
|
|
15
|
|
|
final class Version20241025120000 extends AbstractMigrationChamilo |
|
16
|
|
|
{ |
|
17
|
|
|
public function getDescription(): string |
|
18
|
|
|
{ |
|
19
|
|
|
return 'Ensure resource node and ResourceFile creation for CQuizQuestion with picture associations.'; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function up(Schema $schema): void |
|
23
|
|
|
{ |
|
24
|
|
|
$quizQuestionRepo = $this->container->get(CQuizQuestionRepository::class); |
|
|
|
|
|
|
25
|
|
|
$documentRepo = $this->container->get(CDocumentRepository::class); |
|
26
|
|
|
|
|
27
|
|
|
$questions = $quizQuestionRepo->findAll(); |
|
28
|
|
|
|
|
29
|
|
|
foreach ($questions as $question) { |
|
30
|
|
|
$courseAdmin = $this->getAdmin(); |
|
31
|
|
|
$pictureId = $question->getPicture(); |
|
32
|
|
|
$course = null; |
|
33
|
|
|
|
|
34
|
|
|
if (!$question->hasResourceNode()) { |
|
35
|
|
|
if ($pictureId) { |
|
36
|
|
|
$document = $this->findDocumentByPictureId($pictureId, $documentRepo); |
|
37
|
|
|
if ($document && $document->hasResourceNode() && $document->getResourceNode()->getResourceLinks()->first()) { |
|
38
|
|
|
$course = $document->getResourceNode()->getResourceLinks()->first()->getCourse(); |
|
39
|
|
|
error_log('Creating resource node for question IID ' . $question->getIid()); |
|
40
|
|
|
|
|
41
|
|
|
$resourceNode = $quizQuestionRepo->addResourceNode($question, $courseAdmin, $course); |
|
42
|
|
|
$this->entityManager->persist($resourceNode); |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
// Flush here to ensure the resource node is saved |
|
45
|
|
|
$this->entityManager->flush(); |
|
46
|
|
|
} else { |
|
47
|
|
|
error_log('No course association for question IID ' . $question->getIid() . ' with document ' . $pictureId); |
|
48
|
|
|
continue; |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($question->hasResourceNode() && $pictureId && !$question->getResourceNode()->hasResourceFile()) { |
|
54
|
|
|
error_log('Existing resource node found for question IID ' . $question->getIid() . ' but no ResourceFile.'); |
|
55
|
|
|
|
|
56
|
|
|
$document = $this->findDocumentByPictureId($pictureId, $documentRepo); |
|
57
|
|
|
if ($document) { |
|
58
|
|
|
error_log('Document found for picture ID ' . $pictureId . ' and question IID ' . $question->getIid()); |
|
59
|
|
|
|
|
60
|
|
|
if ($document->hasResourceNode() && !$document->getResourceNode()->hasResourceFile()) { |
|
61
|
|
|
$course = $document->getResourceNode()->getResourceLinks()->first()->getCourse(); |
|
62
|
|
|
$filePath = $this->getUpdateRootPath() . '/app/courses/' . $course->getDirectory() . '/document/images/' . $document->getTitle(); |
|
63
|
|
|
if (file_exists($filePath)) { |
|
64
|
|
|
$this->addLegacyFileToResource($filePath, $documentRepo, $document, $document->getIid()); |
|
65
|
|
|
$this->entityManager->persist($document); |
|
66
|
|
|
$this->entityManager->flush(); |
|
67
|
|
|
error_log('ResourceFile created and flushed for document with IID ' . $document->getIid()); |
|
68
|
|
|
} else { |
|
69
|
|
|
continue; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
if ($document->getResourceNode()->hasResourceFile()) { |
|
74
|
|
|
$resourceFile = $document->getResourceNode()->getResourceFiles()->first(); |
|
75
|
|
|
error_log('Resource file ready for question IID ' . $question->getIid() . ': ' . $resourceFile->getOriginalName()); |
|
76
|
|
|
|
|
77
|
|
|
$contents = $documentRepo->getResourceFileContent($document); |
|
78
|
|
|
$quizQuestionRepo->addFileFromString( |
|
79
|
|
|
$question, |
|
80
|
|
|
$resourceFile->getOriginalName(), |
|
81
|
|
|
$resourceFile->getMimeType(), |
|
82
|
|
|
$contents |
|
83
|
|
|
); |
|
84
|
|
|
$this->entityManager->persist($question); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->entityManager->flush(); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function findDocumentByPictureId(string $pictureId, $documentRepo): ?CDocument |
|
94
|
|
|
{ |
|
95
|
|
|
if (str_starts_with($pictureId, 'quiz-')) { |
|
96
|
|
|
return $documentRepo->findOneBy(['title' => $pictureId]) ?: null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $documentRepo->find($pictureId); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|
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.