Skip to content

Migration: Ensure resource node and ResourceFile creation for CQuizQuestion with picture - refs BT#22145 #5891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions src/CoreBundle/Migrations/Schema/V200/Version20241025120000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

declare(strict_types=1);

/* For licensing terms, see /license.txt */

namespace Chamilo\CoreBundle\Migrations\Schema\V200;

use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
use Chamilo\CourseBundle\Entity\CDocument;
use Chamilo\CourseBundle\Repository\CDocumentRepository;
use Chamilo\CourseBundle\Repository\CQuizQuestionRepository;
use Doctrine\DBAL\Schema\Schema;

final class Version20241025120000 extends AbstractMigrationChamilo
{
public function getDescription(): string
{
return 'Ensure resource node and ResourceFile creation for CQuizQuestion with picture associations.';
}

public function up(Schema $schema): void
{
$quizQuestionRepo = $this->container->get(CQuizQuestionRepository::class);
$documentRepo = $this->container->get(CDocumentRepository::class);

$questions = $quizQuestionRepo->findAll();

foreach ($questions as $question) {
$courseAdmin = $this->getAdmin();
$pictureId = $question->getPicture();
$course = null;

if (!$question->hasResourceNode()) {
if ($pictureId) {
$document = $this->findDocumentByPictureId($pictureId, $documentRepo);
if ($document && $document->hasResourceNode() && $document->getResourceNode()->getResourceLinks()->first()) {
$course = $document->getResourceNode()->getResourceLinks()->first()->getCourse();
error_log('Creating resource node for question IID ' . $question->getIid());

$resourceNode = $quizQuestionRepo->addResourceNode($question, $courseAdmin, $course);
$this->entityManager->persist($resourceNode);

// Flush here to ensure the resource node is saved
$this->entityManager->flush();
} else {
error_log('No course association for question IID ' . $question->getIid() . ' with document ' . $pictureId);
continue;
}
}
}

if ($question->hasResourceNode() && $pictureId && !$question->getResourceNode()->hasResourceFile()) {
error_log('Existing resource node found for question IID ' . $question->getIid() . ' but no ResourceFile.');

$document = $this->findDocumentByPictureId($pictureId, $documentRepo);
if ($document) {
error_log('Document found for picture ID ' . $pictureId . ' and question IID ' . $question->getIid());

if ($document->hasResourceNode() && !$document->getResourceNode()->hasResourceFile()) {
$course = $document->getResourceNode()->getResourceLinks()->first()->getCourse();
$filePath = $this->getUpdateRootPath() . '/app/courses/' . $course->getDirectory() . '/document/images/' . $document->getTitle();
if (file_exists($filePath)) {
$this->addLegacyFileToResource($filePath, $documentRepo, $document, $document->getIid());
$this->entityManager->persist($document);
$this->entityManager->flush();
error_log('ResourceFile created and flushed for document with IID ' . $document->getIid());
} else {
continue;
}
}

if ($document->getResourceNode()->hasResourceFile()) {
$resourceFile = $document->getResourceNode()->getResourceFiles()->first();
error_log('Resource file ready for question IID ' . $question->getIid() . ': ' . $resourceFile->getOriginalName());

$contents = $documentRepo->getResourceFileContent($document);
$quizQuestionRepo->addFileFromString(
$question,
$resourceFile->getOriginalName(),
$resourceFile->getMimeType(),
$contents
);
$this->entityManager->persist($question);
}
}
}
}

$this->entityManager->flush();
}

private function findDocumentByPictureId(string $pictureId, $documentRepo): ?CDocument
{
if (str_starts_with($pictureId, 'quiz-')) {
return $documentRepo->findOneBy(['title' => $pictureId]) ?: null;
}

return $documentRepo->find($pictureId);
}
}
Loading