From b484376f00b95b4bf3ebc9e858d2d059de2923b6 Mon Sep 17 00:00:00 2001 From: Angel Fernando Quiroz Campos Date: Wed, 29 Jan 2020 15:06:33 -0500 Subject: [PATCH] MigrationMoodle: Add task for scorm' scoes files - refs BT#15992 --- plugin/migrationmoodle/admin.php | 3 + plugin/migrationmoodle/lang/english.php | 1 + .../src/Loader/CourseFilesLoader.php | 26 +----- .../src/Loader/CourseModulesScormLoader.php | 42 +++++++++- .../src/Loader/FilesForScormScoLoader.php | 50 +++++++++++ .../src/Task/FilesForScormScoesTask.php | 82 +++++++++++++++++++ .../src/Traits/FileFinderTrait.php | 36 ++++++++ 7 files changed, 215 insertions(+), 25 deletions(-) create mode 100644 plugin/migrationmoodle/src/Loader/FilesForScormScoLoader.php create mode 100644 plugin/migrationmoodle/src/Task/FilesForScormScoesTask.php create mode 100644 plugin/migrationmoodle/src/Traits/FileFinderTrait.php diff --git a/plugin/migrationmoodle/admin.php b/plugin/migrationmoodle/admin.php index 4e4113e334..34ae854cd2 100644 --- a/plugin/migrationmoodle/admin.php +++ b/plugin/migrationmoodle/admin.php @@ -73,6 +73,9 @@ $menu = [ 'course_modules_scorm' => [ 'scorm_scoes', ], + 'scorm_scoes' => [ + 'files_for_scorm_scoes', + ], ]; Display::display_header($plugin->get_title()); diff --git a/plugin/migrationmoodle/lang/english.php b/plugin/migrationmoodle/lang/english.php index 9449ca2ba4..12a323986e 100644 --- a/plugin/migrationmoodle/lang/english.php +++ b/plugin/migrationmoodle/lang/english.php @@ -39,3 +39,4 @@ $strings['QuestionMultiChoiceMultipleTask'] = 'Answers for multichoice questions $strings['QuestionsTrueFalseTask'] = 'Answers for truefalse questions'; $strings['CourseModulesScormTask'] = 'Course Scorms'; $strings['ScormScoesTask'] = 'Scorms items'; +$strings['FilesForScormScoesTask'] = 'Files for Scorm items'; diff --git a/plugin/migrationmoodle/src/Loader/CourseFilesLoader.php b/plugin/migrationmoodle/src/Loader/CourseFilesLoader.php index 8b6d9603eb..8c27e2ac2d 100644 --- a/plugin/migrationmoodle/src/Loader/CourseFilesLoader.php +++ b/plugin/migrationmoodle/src/Loader/CourseFilesLoader.php @@ -4,6 +4,7 @@ namespace Chamilo\PluginBundle\MigrationMoodle\Loader; use Chamilo\PluginBundle\MigrationMoodle\Interfaces\LoaderInterface; +use Chamilo\PluginBundle\MigrationMoodle\Traits\FileFinderTrait; /** * Class CourseFilesLoader. @@ -14,6 +15,7 @@ use Chamilo\PluginBundle\MigrationMoodle\Interfaces\LoaderInterface; */ class CourseFilesLoader implements LoaderInterface { + use FileFinderTrait; /** * Load the data and return the ID inserted. @@ -64,28 +66,4 @@ class CourseFilesLoader implements LoaderInterface return $fileData['iid']; } - - /** - * @param $contentHash - * - * @throws \Exception - * - * @return string - */ - private function findFilePath($contentHash) - { - $d1 = substr($contentHash, 0, 2); - $d2 = substr($contentHash, 2, 2); - - $moodleDataPath = '/var/www/moodle/moodledata'; - $moodleDataPath = rtrim($moodleDataPath, ' /'); - - $filePath = "$moodleDataPath/filedir/$d1/$d2/$contentHash"; - - if (!file_exists($filePath)) { - throw new \Exception("File $contentHash not found in $moodleDataPath/filedir"); - } - - return $filePath; - } } diff --git a/plugin/migrationmoodle/src/Loader/CourseModulesScormLoader.php b/plugin/migrationmoodle/src/Loader/CourseModulesScormLoader.php index 1b924e4fb7..9f3c4c1e41 100644 --- a/plugin/migrationmoodle/src/Loader/CourseModulesScormLoader.php +++ b/plugin/migrationmoodle/src/Loader/CourseModulesScormLoader.php @@ -4,6 +4,7 @@ namespace Chamilo\PluginBundle\MigrationMoodle\Loader; use Chamilo\PluginBundle\MigrationMoodle\Interfaces\LoaderInterface; +use Symfony\Component\Filesystem\Filesystem; /** * Class CourseModulesScormLoader. @@ -30,7 +31,7 @@ class CourseModulesScormLoader implements LoaderInterface $courseInfo = api_get_course_info_by_id($incomingData['c_id']); $userId = 1; - $incomingData['path'] = str_replace('.zip', '/.', $incomingData['path']); + $incomingData['path'] = $this->createDirectory($incomingData['name'], $courseInfo['code']); $incomingData['use_max_score'] = $incomingData['use_max_score'] == 100; $incomingData['created_on'] = $incomingData['created_on'] @@ -80,4 +81,43 @@ class CourseModulesScormLoader implements LoaderInterface return $lpId; } + + /** + * @param string $fileName + * + * @return string + */ + public static function generateDirectoryName($fileName) + { + $newDirectory = trim($fileName); + $newDirectory = trim($newDirectory, '/'); + + return api_replace_dangerous_char($newDirectory); + } + + /** + * @param string $name + * @param string $courseCode + * + * @return string + */ + private function createDirectory($name, $courseCode) + { + $courseRelDir = api_get_path(SYS_COURSE_PATH).api_get_course_path($courseCode).'/scorm'; + + $newDirectory = self::generateDirectoryName($name); + + $fullPath = "$courseRelDir/$newDirectory"; + + $fileSystem = new Filesystem(); + + if (!is_dir($fullPath)) { + $fileSystem->mkdir( + $fullPath, + api_get_permissions_for_new_directories() + ); + } + + return "$newDirectory/."; + } } diff --git a/plugin/migrationmoodle/src/Loader/FilesForScormScoLoader.php b/plugin/migrationmoodle/src/Loader/FilesForScormScoLoader.php new file mode 100644 index 0000000000..57e9a7445e --- /dev/null +++ b/plugin/migrationmoodle/src/Loader/FilesForScormScoLoader.php @@ -0,0 +1,50 @@ +findFilePath($incomingData['contenthash']); + } catch (\Exception $exception) { + return 0; + } + + $sysCourseScormPath = api_get_path(SYS_COURSE_PATH).$course->getDirectory().'/scorm'; + $lpDirectory = CourseModulesScormLoader::generateDirectoryName($incomingData['lp_name']); + $lpDirectoryPath = "$sysCourseScormPath/$lpDirectory"; + $fileDirectoryPath = $lpDirectoryPath.$incomingData['filepath']; + $filePath = $fileDirectoryPath.$incomingData['filename']; + + $fileSystem = new Filesystem(); + + if ($incomingData['filepath'] != '/') { + $fileSystem->mkdir( + $fileDirectoryPath, + api_get_permissions_for_new_directories() + ); + } + + $fileSystem->copy($moodleFilePath, $filePath); + + return 0; + } +} diff --git a/plugin/migrationmoodle/src/Task/FilesForScormScoesTask.php b/plugin/migrationmoodle/src/Task/FilesForScormScoesTask.php new file mode 100644 index 0000000000..3b038ceac6 --- /dev/null +++ b/plugin/migrationmoodle/src/Task/FilesForScormScoesTask.php @@ -0,0 +1,82 @@ + CourseExtractor::class, + 'query' => "SELECT + f.id, + f.contenthash, + f.filepath, + f.filename, + f.mimetype, + s.name scorm_name, + cm.course + FROM mdl_files f + INNER JOIN mdl_context ctx ON f.contextid = ctx.id + INNER JOIN mdl_course_modules cm ON ctx.instanceid = cm.id + INNER JOIN mdl_modules m ON cm.module = m.id + INNER JOIN mdl_scorm s ON (cm.course = s.course AND cm.instance = s.id) + WHERE + m.name = 'scorm' + AND ctx.contextlevel = 70 + AND f.filename NOT IN ('.', '..') + AND s.reference != f.filename + AND f.filearea = 'content' + AND f.component = 'mod_scorm' + ORDER BY s.course, s.id", + ]; + } + + /** + * @inheritDoc + */ + /** + * @return array + */ + public function getTransformConfiguration() + { + return [ + 'class' => BaseTransformer::class, + 'map' => [ + 'contenthash' => 'contenthash', + 'filepath' => 'filepath', + 'filename' => 'filename', + 'mimetype' => 'mimetype', + 'c_id' => [ + 'class' => LoadedCourseLookup::class, + 'properties' => ['course'], + ], + 'lp_name' => 'scorm_name', + ], + ]; + } + + /** + * @inheritDoc + */ + public function getLoadConfiguration() + { + return [ + 'class' => FilesForScormScoLoader::class, + ]; + } +} diff --git a/plugin/migrationmoodle/src/Traits/FileFinderTrait.php b/plugin/migrationmoodle/src/Traits/FileFinderTrait.php new file mode 100644 index 0000000000..3e87ca2df9 --- /dev/null +++ b/plugin/migrationmoodle/src/Traits/FileFinderTrait.php @@ -0,0 +1,36 @@ +