-
Notifications
You must be signed in to change notification settings - Fork 529
Migration: Migrate SCORM packages into asset system - refs BT#22590 #6259
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
134 changes: 134 additions & 0 deletions
134
src/CoreBundle/Migrations/Schema/V200/Version20250429144100.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /* For licensing terms, see /license.txt */ | ||
|
|
||
| namespace Chamilo\CoreBundle\Migrations\Schema\V200; | ||
|
|
||
| use Chamilo\CoreBundle\Entity\Asset; | ||
| use Chamilo\CoreBundle\Repository\AssetRepository; | ||
| use Chamilo\CourseBundle\Entity\CLp; | ||
| use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo; | ||
| use Chamilo\CourseBundle\Repository\CLpRepository; | ||
| use Doctrine\DBAL\Schema\Schema; | ||
| use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
|
|
||
| final class Version20250429144100 extends AbstractMigrationChamilo | ||
| { | ||
| public function getDescription(): string | ||
| { | ||
| return 'Migrate SCORM packages into asset system.'; | ||
| } | ||
|
|
||
| public function up(Schema $schema): void | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function |
||
| { | ||
| $this->log('Starting SCORM migration...'); | ||
|
|
||
| $assetRepo = $this->container->get( AssetRepository::class); | ||
| $lpRepo = $this->container->get(CLpRepository::class);; | ||
|
|
||
| $courses = $this->entityManager->createQuery('SELECT c FROM Chamilo\CoreBundle\Entity\Course c')->toIterable(); | ||
|
|
||
| foreach ($courses as $course) { | ||
| $courseId = $course->getId(); | ||
| $courseDir = $course->getDirectory(); | ||
| $this->log("Processing course ID: $courseId - Directory: $courseDir"); | ||
|
|
||
| $scorms = $lpRepo->createQueryBuilder('lp') | ||
| ->join('lp.resourceNode', 'rn') | ||
| ->join('rn.resourceLinks', 'rl') | ||
| ->where('lp.lpType = :type') | ||
| ->andWhere('rl.course = :course') | ||
| ->setParameter('type', CLp::SCORM_TYPE) | ||
| ->setParameter('course', $course) | ||
| ->getQuery() | ||
| ->getResult(); | ||
|
|
||
| if (empty($scorms)) { | ||
| $this->log("No SCORMs found for course $courseDir"); | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($scorms as $lp) { | ||
| $lpId = $lp->getIid(); | ||
| $path = rtrim($lp->getPath(), '/.'); | ||
|
|
||
| $this->log("Processing SCORM LP id=$lpId path=$path"); | ||
|
|
||
| $folderPath = $this->getUpdateRootPath()."/app/courses/$courseDir/scorm/$path"; | ||
|
|
||
| if (!is_dir($folderPath)) { | ||
| $this->log("SCORM folder not found: $folderPath"); | ||
| continue; | ||
| } | ||
|
|
||
| $zipName = basename($path).'.zip'; | ||
| $tmpZipPath = "/tmp/$zipName"; | ||
|
|
||
| if (!file_exists($tmpZipPath)) { | ||
| $this->log("Zipping SCORM folder: $folderPath -> $tmpZipPath"); | ||
| $this->zipFolder($folderPath, $tmpZipPath); | ||
| } | ||
|
|
||
| if (!file_exists($tmpZipPath)) { | ||
| $this->log("Failed to create zip: $tmpZipPath"); | ||
| continue; | ||
| } | ||
|
|
||
| $file = new UploadedFile($tmpZipPath, $zipName, 'application/zip', null, true); | ||
|
|
||
| $asset = new Asset(); | ||
| $asset->setCategory(Asset::SCORM) | ||
| ->setTitle($zipName) | ||
| ->setFile($file) | ||
| ->setCompressed(true); | ||
|
|
||
| $assetRepo->update($asset); | ||
|
|
||
| $this->log("Asset created: id=".$asset->getId()); | ||
|
|
||
| $assetRepo->unZipFile($asset, basename($path)); | ||
| $this->log("Asset unzipped for: ".$asset->getTitle()); | ||
|
|
||
| $lp->setAsset($asset); | ||
| $lp->setPath(basename($path).'/.'); | ||
| $this->entityManager->persist($lp); | ||
| $this->entityManager->flush(); | ||
|
|
||
| $this->log("LP updated id=$lpId linked to asset_id=".$asset->getId()); | ||
| } | ||
| } | ||
|
|
||
| $this->log('Finished SCORM migration.'); | ||
| } | ||
|
|
||
| private function zipFolder(string $folderPath, string $zipPath): void | ||
| { | ||
| $zip = new \ZipArchive(); | ||
| if ($zip->open($zipPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE) !== true) { | ||
| $this->log("Cannot create ZIP file: $zipPath"); | ||
| return; | ||
| } | ||
|
|
||
| $files = new \RecursiveIteratorIterator( | ||
| new \RecursiveDirectoryIterator($folderPath, \RecursiveDirectoryIterator::SKIP_DOTS), | ||
| \RecursiveIteratorIterator::LEAVES_ONLY | ||
| ); | ||
|
|
||
| foreach ($files as $file) { | ||
| $filePath = $file->getRealPath(); | ||
| $relativePath = substr($filePath, strlen($folderPath) + 1); | ||
| $zip->addFile($filePath, $relativePath); | ||
| } | ||
|
|
||
| $zip->close(); | ||
| } | ||
|
|
||
| private function log(string $message): void | ||
| { | ||
| error_log('[SCORM Migration] ' . $message); | ||
| } | ||
|
|
||
| public function down(Schema $schema): void {} | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Method
uphas 58 lines of code (exceeds 25 allowed). Consider refactoring.