Migration: Fix resource migration and file handling for course documents - refs BT#22199

pull/5955/head
Christian Beeznest 11 months ago
parent 7a938e6ff0
commit 70ae32d6de
  1. 30
      src/CoreBundle/Migrations/Schema/V200/Version20230913162700.php
  2. 31
      src/CoreBundle/Migrations/Schema/V200/Version20231022124700.php
  3. 15
      src/CoreBundle/Repository/ResourceRepository.php

@ -32,7 +32,7 @@ final class Version20230913162700 extends AbstractMigrationChamilo
$resourceNodeRepo = $this->container->get(ResourceNodeRepository::class); $resourceNodeRepo = $this->container->get(ResourceNodeRepository::class);
$q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c'); $q = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c');
/*$updateConfigurations = [ $updateConfigurations = [
['table' => 'c_tool_intro', 'field' => 'intro_text'], ['table' => 'c_tool_intro', 'field' => 'intro_text'],
['table' => 'c_course_description', 'field' => 'content'], ['table' => 'c_course_description', 'field' => 'content'],
['table' => 'c_quiz', 'fields' => ['description', 'text_when_finished']], ['table' => 'c_quiz', 'fields' => ['description', 'text_when_finished']],
@ -48,7 +48,7 @@ final class Version20230913162700 extends AbstractMigrationChamilo
['table' => 'c_survey', 'fields' => ['title', 'subtitle']], ['table' => 'c_survey', 'fields' => ['title', 'subtitle']],
['table' => 'c_survey_question', 'fields' => ['survey_question', 'survey_question_comment']], ['table' => 'c_survey_question', 'fields' => ['survey_question', 'survey_question_comment']],
['table' => 'c_survey_question_option', 'field' => 'option_text'], ['table' => 'c_survey_question_option', 'field' => 'option_text'],
];*/ ];
/** @var Course $course */ /** @var Course $course */
foreach ($q->toIterable() as $course) { foreach ($q->toIterable() as $course) {
@ -59,9 +59,9 @@ final class Version20230913162700 extends AbstractMigrationChamilo
continue; continue;
} }
/* foreach ($updateConfigurations as $config) { foreach ($updateConfigurations as $config) {
$this->updateContent($config, $courseDirectory, $courseId, $documentRepo); $this->updateContent($config, $courseDirectory, $courseId, $documentRepo);
}*/ }
$this->updateHtmlContent($courseDirectory, $courseId, $documentRepo, $resourceNodeRepo); $this->updateHtmlContent($courseDirectory, $courseId, $documentRepo, $resourceNodeRepo);
} }
@ -155,13 +155,6 @@ final class Version20230913162700 extends AbstractMigrationChamilo
$documentPath = str_replace('/courses/'.$courseDirectory.'/document/', '/', $videoPath); $documentPath = str_replace('/courses/'.$courseDirectory.'/document/', '/', $videoPath);
error_log('Debugging Replace URLs:');
error_log('Full URL: ' . $fullUrl);
error_log('Video Path: ' . $videoPath);
error_log('Actual Course Directory: ' . $actualCourseDirectory);
error_log('Processed Document Path: ' . $documentPath);
/*
$sql = "SELECT iid, path, resource_node_id FROM c_document WHERE c_id = $courseId AND path LIKE '$documentPath'"; $sql = "SELECT iid, path, resource_node_id FROM c_document WHERE c_id = $courseId AND path LIKE '$documentPath'";
$result = $this->connection->executeQuery($sql); $result = $this->connection->executeQuery($sql);
$documents = $result->fetchAllAssociative(); $documents = $result->fetchAllAssociative();
@ -177,7 +170,7 @@ final class Version20230913162700 extends AbstractMigrationChamilo
$contentText = str_replace($matches[0][$index], $replacement, $contentText); $contentText = str_replace($matches[0][$index], $replacement, $contentText);
} }
} }
}*/ }
} }
return $contentText; return $contentText;
@ -214,9 +207,10 @@ final class Version20230913162700 extends AbstractMigrationChamilo
throw new Exception("Course with ID $courseId not found."); throw new Exception("Course with ID $courseId not found.");
} }
$document = $documentRepo->findCourseResourceByTitle($title, $course->getResourceNode(), $course); $existingDocument = $documentRepo->findResourceByTitleInCourse($title, $course);
if (null !== $document) { if ($existingDocument) {
return $document; error_log("Document '$title' already exists for course {$course->getId()}. Skipping creation.");
return $existingDocument;
} }
if (file_exists($appCourseOldPath) && !is_dir($appCourseOldPath)) { if (file_exists($appCourseOldPath) && !is_dir($appCourseOldPath)) {
@ -235,6 +229,7 @@ final class Version20230913162700 extends AbstractMigrationChamilo
$documentRepo->addFileFromPath($document, $title, $appCourseOldPath); $documentRepo->addFileFromPath($document, $title, $appCourseOldPath);
error_log("Document '$title' successfully created for course $courseId.");
return $document; return $document;
} }
$generalCoursesPath = $this->getUpdateRootPath().'/app/courses/'; $generalCoursesPath = $this->getUpdateRootPath().'/app/courses/';
@ -259,9 +254,10 @@ final class Version20230913162700 extends AbstractMigrationChamilo
return $document; return $document;
} }
throw new Exception('File not found in any location.'); error_log("File '$title' not found for course $courseId. Skipping.");
return null;
} catch (Exception $e) { } catch (Exception $e) {
error_log('Migration error: '.$e->getMessage()); error_log('Error in createNewDocument: ' . $e->getMessage());
return null; return null;
} }

@ -116,10 +116,16 @@ final class Version20231022124700 extends AbstractMigrationChamilo
// Pattern to find and replace cidReq, id_session, and gidReq // Pattern to find and replace cidReq, id_session, and gidReq
$pattern = '/((https?:\/\/[^\/\s]*|)\/[^?\s]+?)\?(.*?)(cidReq=([a-zA-Z0-9_]+))((?:&|&)id_session=([0-9]+))?((?:&|&)gidReq=([0-9]+))?(.*)/i'; $pattern = '/((https?:\/\/[^\/\s]*|)\/[^?\s]+?)\?(.*?)(cidReq=([a-zA-Z0-9_]+))((?:&|&)id_session=([0-9]+))?((?:&|&)gidReq=([0-9]+))?(.*)/i';
try {
$newContent = @preg_replace_callback( $newContent = @preg_replace_callback(
$pattern, $pattern,
function ($matches) { function ($matches) {
$code = $matches[5]; $code = $matches[5] ?? null;
if (!$code) {
error_log('Missing cidReq in URL: ' . $matches[0]);
return $matches[0];
}
$courseId = null; $courseId = null;
$sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1'; $sql = 'SELECT id FROM course WHERE code = :code ORDER BY id DESC LIMIT 1';
@ -131,17 +137,18 @@ final class Version20231022124700 extends AbstractMigrationChamilo
} }
if (null === $courseId) { if (null === $courseId) {
return $matches[0]; // If the courseId is not found, return the original URL. error_log('Course ID not found for cidReq: ' . $code);
return $matches[0];
} }
// Ensure sid and gid are always populated // Ensure sid and gid are always populated
$sessionId = isset($matches[7]) && !empty($matches[7]) ? $matches[7] : '0'; $sessionId = $matches[7] ?? '0';
$groupId = isset($matches[9]) && !empty($matches[9]) ? $matches[9] : '0'; $groupId = $matches[9] ?? '0';
$remainingParams = isset($matches[10]) ? $matches[10] : ''; $remainingParams = $matches[10] ?? '';
// Prepare new URL with updated parameters // Prepare new URL with updated parameters
$newParams = "cid=$courseId&sid=$sessionId&gid=$groupId"; $newParams = "cid=$courseId&sid=$sessionId&gid=$groupId";
$beforeCidReqParams = isset($matches[3]) ? $matches[3] : ''; $beforeCidReqParams = $matches[3] ?? '';
// Ensure other parameters are maintained // Ensure other parameters are maintained
if (!empty($remainingParams)) { if (!empty($remainingParams)) {
@ -150,14 +157,18 @@ final class Version20231022124700 extends AbstractMigrationChamilo
$finalUrl = $matches[1].'?'.$beforeCidReqParams.$newParams; $finalUrl = $matches[1].'?'.$beforeCidReqParams.$newParams;
return str_replace('&', '&', $finalUrl); // Replace any remaining & with & return str_replace('&', '&', $finalUrl);
}, },
$content $content
); );
if (PREG_NO_ERROR !== preg_last_error()) { if (false === $newContent || null === $newContent) {
error_log('Error encountered in preg_replace_callback: '.preg_last_error()); error_log('preg_replace_callback failed for content: ' . substr($content, 0, 500));
$newContent = $content; return $content;
}
} catch (Exception $e) {
error_log('Exception in replaceURLParametersInContent: ' . $e->getMessage());
return $content;
} }
return $newContent; return $newContent;

@ -933,4 +933,19 @@ abstract class ResourceRepository extends ServiceEntityRepository
->getOneOrNullResult() ->getOneOrNullResult()
; ;
} }
public function findResourceByTitleInCourse(
string $title,
Course $course,
?Session $session = null,
?CGroup $group = null
): ?ResourceInterface {
$qb = $this->getResourcesByCourse($course, $session, $group);
$this->addTitleQueryBuilder($title, $qb);
$qb->setMaxResults(1);
return $qb->getQuery()->getOneOrNullResult();
}
} }

Loading…
Cancel
Save