MigrationMoodle: Add task for scorm' scoes files - refs BT#15992

pull/3127/head
Angel Fernando Quiroz Campos 6 years ago
parent 87217f8f46
commit b484376f00
  1. 3
      plugin/migrationmoodle/admin.php
  2. 1
      plugin/migrationmoodle/lang/english.php
  3. 26
      plugin/migrationmoodle/src/Loader/CourseFilesLoader.php
  4. 42
      plugin/migrationmoodle/src/Loader/CourseModulesScormLoader.php
  5. 50
      plugin/migrationmoodle/src/Loader/FilesForScormScoLoader.php
  6. 82
      plugin/migrationmoodle/src/Task/FilesForScormScoesTask.php
  7. 36
      plugin/migrationmoodle/src/Traits/FileFinderTrait.php

@ -73,6 +73,9 @@ $menu = [
'course_modules_scorm' => [
'scorm_scoes',
],
'scorm_scoes' => [
'files_for_scorm_scoes',
],
];
Display::display_header($plugin->get_title());

@ -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';

@ -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;
}
}

@ -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/.";
}
}

@ -0,0 +1,50 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Loader;
use Chamilo\PluginBundle\MigrationMoodle\Traits\FileFinderTrait;
use Symfony\Component\Filesystem\Filesystem;
/**
* Class FilesForScormScoLoader.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Loader
*/
class FilesForScormScoLoader extends CourseFilesLoader
{
use FileFinderTrait;
/**
* @inheritDoc
*/
public function load(array $incomingData)
{
$course = api_get_course_entity($incomingData['c_id']);
try {
$moodleFilePath = $this->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;
}
}

@ -0,0 +1,82 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Task;
use Chamilo\PluginBundle\MigrationMoodle\Extractor\CourseExtractor;
use Chamilo\PluginBundle\MigrationMoodle\Loader\FilesForScormScoLoader;
use Chamilo\PluginBundle\MigrationMoodle\Transformer\BaseTransformer;
use Chamilo\PluginBundle\MigrationMoodle\Transformer\Property\LoadedCourseLookup;
/**
* Class FilesForScormScoesTask.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Task
*/
class FilesForScormScoesTask extends BaseTask
{
/**
* @inheritDoc
*/
public function getExtractConfiguration()
{
return [
'class' => 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,
];
}
}

@ -0,0 +1,36 @@
<?php
/* For licensing terms, see /license.txt */
namespace Chamilo\PluginBundle\MigrationMoodle\Traits;
/**
* Class FileFinderTrait.
*
* @package Chamilo\PluginBundle\MigrationMoodle\Traits\MapTrait
*/
trait FileFinderTrait
{
/**
* @param $contentHash
*
* @throws \Exception
*
* @return string
*/
protected 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;
}
}
Loading…
Cancel
Save