LP: Fix scorm import

pull/3984/head
Julio 4 years ago
parent f87f9cdfc4
commit 4fd6236531
  1. 23
      public/main/lp/learnpath.class.php
  2. 9
      public/main/lp/lp_content.php
  3. 6
      public/main/lp/lp_upload.php
  4. 1
      public/main/lp/lp_view.php
  5. 215
      public/main/lp/scorm.class.php
  6. 11
      public/main/lp/scormItem.class.php
  7. 7
      public/main/lp/scormOrganization.class.php
  8. 47
      src/CoreBundle/Repository/AssetRepository.php
  9. 25
      tests/CoreBundle/Repository/AssetRepositoryTest.php

@ -121,7 +121,7 @@ class learnpath
$this->encoding = api_get_system_encoding();
$lp_id = 0;
if (null !== $entity) {
$lp_id = (int) $entity->getIid();
$lp_id = $entity->getIid();
}
$course_info = empty($course_info) ? api_get_course_info() : $course_info;
$course_id = (int) $course_info['real_id'];
@ -130,7 +130,7 @@ class learnpath
if (empty($lp_id) || empty($course_id)) {
$this->error = "Parameter is empty: LpId:'$lp_id', courseId: '$lp_id'";
} else {
$this->entity = $entity;
//$this->entity = $entity;
$this->lp_id = $lp_id;
$this->type = $entity->getLpType();
$this->name = stripslashes($entity->getName());
@ -158,7 +158,7 @@ class learnpath
if ($entity->hasAsset()) {
$asset = $entity->getAsset();
$this->scormUrl = Container::getAssetRepository()->getAssetUrl($asset).'/';
$this->scormUrl = Container::getAssetRepository()->getAssetUrl($asset).'/'.$entity->getPath().'/';
}
$this->accumulateScormTime = $entity->getAccumulateWorkTime();
@ -235,7 +235,7 @@ class learnpath
'displayOrder' => Criteria::ASC,
]
);
$items = $this->entity->getItems()->matching($criteria);
$items = $entity->getItems()->matching($criteria);
$lp_item_id_list = [];
foreach ($items as $item) {
$itemId = $item->getIid();
@ -309,7 +309,6 @@ class learnpath
while ($row = Database:: fetch_array($res)) {
$status_list[$row['lp_item_id']] = $row['status'];
}
//echo '<pre>'; var_dump($this->items); echo '</pre>';
foreach ($lp_item_id_list as $item_id) {
if (isset($status_list[$item_id])) {
$status = $status_list[$item_id];
@ -354,7 +353,7 @@ class learnpath
}
}
$this->ordered_items = self::get_flat_ordered_items_list($this->entity, null);
$this->ordered_items = self::get_flat_ordered_items_list($entity, null);
$this->max_ordered_items = 0;
foreach ($this->ordered_items as $index => $dummy) {
if ($index > $this->max_ordered_items && !empty($dummy)) {
@ -453,7 +452,7 @@ class learnpath
->setTitle($title)
->setDescription($description)
->setPath($id)
->setLp($this->entity)
->setLp(api_get_lp_entity($this->get_id()))
->setItemType($type)
->setMaxScore($max_score)
->setMaxTimeAllowed($max_time_allowed)
@ -4671,7 +4670,7 @@ class learnpath
return $return;
}
public function getBuildTree($noWrapper = false, $dropElement = false)
public function getBuildTree($noWrapper = false, $dropElement = false): string
{
$mainUrl = api_get_path(WEB_CODE_PATH).'lp/lp_controller.php?'.api_get_cidreq();
@ -4778,7 +4777,7 @@ class learnpath
);
$url = $mainUrl.'&view=build&id='.$itemId.'&lp_id='.$lpId;
$preRequisitiesIcon = Display::url(
$preRequisitesIcon = Display::url(
Display::return_icon(
'accept.png',
get_lang('Prerequisites'),
@ -4841,7 +4840,7 @@ class learnpath
'div',
"<div class=\"btn-group btn-group-sm\">
$editIcon
$preRequisitiesIcon
$preRequisitesIcon
$orderIcons
$deleteIcon
</div>",
@ -4859,6 +4858,7 @@ class learnpath
;
},
];
$tree = $lpItemRepo->childrenHierarchy($itemRoot, false, $options);
if (empty($tree) && $dropElement) {
@ -6439,7 +6439,8 @@ class learnpath
public function display_lp_prerequisites_list(FormValidator $form)
{
$lp_id = $this->lp_id;
$prerequisiteId = $this->entity->getPrerequisite();
$lp = api_get_lp_entity($lp_id);
$prerequisiteId = $lp->getPrerequisite();
$repo = Container::getLpRepository();
$qb = $repo->findAllByCourse(api_get_course_entity(), api_get_session_entity());

@ -2,6 +2,7 @@
/* For licensing terms, see /license.txt */
use Chamilo\CourseBundle\Entity\CLp;
use ChamiloSession as Session;
/**
@ -52,7 +53,7 @@ if ($dir) {
$src = 'blank.php';
} else {
switch ($lpType) {
case 1:
case CLp::LP_TYPE:
$learnPath->stop_previous_item();
$prerequisiteCheck = $learnPath->prerequisites_match($lpItemId);
if (true === $prerequisiteCheck) {
@ -76,7 +77,7 @@ if ($dir) {
}
$src = 'blank.php?'.api_get_cidreq().'&error=prerequisites&prerequisite_message='.Security::remove_XSS($learnPath->error);
break;
case 2:
case CLp::SCORM_TYPE:
$learnPath->stop_previous_item();
$prerequisiteCheck = $learnPath->prerequisites_match($lpItemId);
@ -87,7 +88,7 @@ if ($dir) {
$src = 'blank.php?'.api_get_cidreq().'&error=prerequisites&prerequisite_message='.Security::remove_XSS($learnPath->error);
}
break;
case 3:
case CLp::AICC_TYPE:
// save old if asset
$learnPath->stop_previous_item(); // save status manually if asset
$prerequisiteCheck = $learnPath->prerequisites_match($lpItemId);
@ -98,8 +99,6 @@ if ($dir) {
$src = 'blank.php';
}
break;
case 4:
break;
}
}

@ -90,7 +90,7 @@ if (isset($_POST) && $is_error) {
);
if (!empty($scorm->manifestToString)) {
$scorm->parse_manifest();
$lp = $scorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
$lp = $scorm->import_manifest(api_get_course_int_id(), $_REQUEST['use_max_score']);
if ($lp) {
$lp
->setContentLocal($proximity)
@ -172,8 +172,8 @@ if (isset($_POST) && $is_error) {
$entity = $oScorm->getEntity();
$manifest = $oScorm->import_local_package($s, $current_dir);
if (!empty($manifest)) {
$oScorm->parse_manifest($manifest);
$oScorm->import_manifest(api_get_course_id(), $_REQUEST['use_max_score']);
$oScorm->parse_manifest();
$oScorm->import_manifest(api_get_course_int_id(), $_REQUEST['use_max_score']);
Display::addFlash(Display::return_message(get_lang('File upload succeeded!')));
}

@ -244,6 +244,7 @@ if (!isset($src)) {
$oLP->stop_previous_item(); // save status manually if asset
$htmlHeadXtra[] = '<script src="scorm_api.php"></script>';
$preReqCheck = $oLP->prerequisites_match($lp_item_id);
if (true === $preReqCheck) {
$src = $oLP->get_link('http', $lp_item_id, $get_toc_list);
$oLP->start_current_item(); // starts time counter manually if asset

@ -220,94 +220,11 @@ class scorm extends learnpath
}
// End parsing using PHP5 DOMXML methods.
} else {
if ($this->debug > 1) {
error_log('Could not open/read file '.$file);
}
$this->set_error_msg("File $file could not be read");
$this->set_error_msg("File could not be read");
return null;
}
// @todo implement learnpath_fix_xerte_template
$fixTemplate = api_get_configuration_value('learnpath_fix_xerte_template');
$proxyPath = api_get_configuration_value('learnpath_proxy_url');
/*if ($fixTemplate && !empty($proxyPath)) {
// Check organisations:
if (isset($this->manifest['organizations'])) {
foreach ($this->manifest['organizations'] as $data) {
if (false !== strpos(strtolower($data), 'xerte')) {
// Check if template.xml exists:
$templatePath = str_replace('imsmanifest.xml', 'template.xml', $file);
if (file_exists($templatePath) && is_file($templatePath)) {
$templateContent = file_get_contents($templatePath);
$find = [
'href="www.',
'href="https://',
'href="http://',
'url="www.',
'pdfs/download.php?',
];
$replace = [
'href="http://www.',
'target = "_blank" href="'.$proxyPath.'?type=link&src=https://',
'target = "_blank" href="'.$proxyPath.'?type=link&src=http://',
'url="http://www.',
'pdfs/download.php&',
];
$templateContent = str_replace($find, $replace, $templateContent);
file_put_contents($templatePath, $templateContent);
}
// Fix link generation:
$linkPath = str_replace('imsmanifest.xml', 'models_html5/links.html', $file);
if (file_exists($linkPath) && is_file($linkPath)) {
$linkContent = file_get_contents($linkPath);
$find = [
':this.getAttribute("url")',
];
$replace = [
':"'.$proxyPath.'?type=link&src=" + this.getAttribute("url")',
];
$linkContent = str_replace($find, $replace, $linkContent);
file_put_contents($linkPath, $linkContent);
}
// Fix iframe generation
$framePath = str_replace('imsmanifest.xml', 'models_html5/embedDiv.html', $file);
if (file_exists($framePath) && is_file($framePath)) {
$content = file_get_contents($framePath);
$find = [
'$iFrameHolder.html(iFrameTag);',
];
$replace = [
'iFrameTag = \'<a target ="_blank" href="'.$proxyPath.'?type=link&src=\'+ pageSrc + \'">Open website. <img width="16px" src="'.Display::returnIconPath('link-external.png').'"></a>\'; $iFrameHolder.html(iFrameTag); ',
];
$content = str_replace($find, $replace, $content);
file_put_contents($framePath, $content);
}
// Fix new window generation
$newWindowPath = str_replace('imsmanifest.xml', 'models_html5/newWindow.html', $file);
if (file_exists($newWindowPath) && is_file($newWindowPath)) {
$content = file_get_contents($newWindowPath);
$find = [
'var src = x_currentPageXML',
];
$replace = [
'var src = "'.$proxyPath.'?type=link&src=" + x_currentPageXML',
];
$content = str_replace($find, $replace, $content);
file_put_contents($newWindowPath, $content);
}
}
}
}
}*/
// TODO: Close the DOM handler.
return $this->manifest;
}
@ -315,30 +232,29 @@ class scorm extends learnpath
/**
* Import the scorm object (as a result from the parse_manifest function) into the database structure.
*
* @param string $courseCode
* @param int $userMaxScore
* @param int $sessionId
* @param int $courseId
* @param int $userMaxScore
* @param int $sessionId
*
* @return CLp|null
*/
public function import_manifest($courseCode, $userMaxScore = 1, $sessionId = 0)
public function import_manifest($courseId, $userMaxScore = 1, $sessionId = 0)
{
if ($this->debug > 0) {
error_log('Entered import_manifest('.$courseCode.')', 0);
error_log('Entered import_manifest('.$courseId.')', 0);
}
$courseInfo = api_get_course_info($courseCode);
$courseId = $courseInfo['real_id'];
$course = api_get_course_entity($courseId);
// Get table names.
$lpTable = Database::get_course_table(TABLE_LP_MAIN);
$lpItemTable = Database::get_course_table(TABLE_LP_ITEM);
$userMaxScore = (int) $userMaxScore;
$sessionId = empty($sessionId) ? api_get_session_id() : (int) $sessionId;
$repo = Container::getLpRepository();
$em = Database::getManager();
$lpItemRepo = Container::getLpItemRepository();
$lp = null;
foreach ($this->organizations as $id => $dummy) {
/** @var scormOrganization $oOrganization */
$oOrganization = &$this->organizations[$id];
// Prepare and execute insert queries:
// -for learnpath
@ -353,11 +269,10 @@ class scorm extends learnpath
}*/
$dsp = 1;
$courseEntity = api_get_course_entity($courseInfo['real_id']);
$myname = api_utf8_decode($oOrganization->get_name());
$name = $oOrganization->get_name();
$lp = (new CLp())
->setLpType(CLp::SCORM_TYPE)
->setName($myname)
->setName($name)
->setRef($oOrganization->get_ref())
->setPath($this->subdir)
->setDefaultEncoding($this->manifest_encoding)
@ -365,11 +280,11 @@ class scorm extends learnpath
->setDisplayOrder($dsp)
->setUseMaxScore($userMaxScore)
->setAsset($this->asset)
->setParent($courseEntity)
->addCourseLink($courseEntity, api_get_session_entity())
->setParent($course)
->addCourseLink($course, api_get_session_entity($sessionId))
;
$repo->create($lp);
$repo->createLp($lp);
$lp_id = $lp->getIid();
@ -425,13 +340,13 @@ class scorm extends learnpath
if (empty($title)) {
$title = get_lang('Untitled');
}
$parentEntity = null;
$parentEntity = $lpItemRepo->getRootItem($lp_id);
if (!empty($parent)) {
$parentEntity = $em->getRepository(CLpItem::class)->find($parent);
$parentEntity = $lpItemRepo->find($parent);
}
$lpItem = new CLpItem();
$lpItem
$lpItem = (new CLpItem())
->setTitle($title)
->setItemType($type)
->setRef($item['identifier'])
@ -439,10 +354,10 @@ class scorm extends learnpath
->setMinScore(0)
->setMaxScore($max_score)
->setParent($parentEntity)
->setPreviousItemId($previous)
->setNextItemId(0)
//->setPreviousItemId($previous)
//->setNextItemId(0)
->setPrerequisite($item['prerequisites'])
->setDisplayOrder($item['rel_order'])
//->setDisplayOrder($item['rel_order'])
->setLaunchData($item['datafromlms'])
->setParameters($item['parameters'])
->setLp($lp)
@ -455,21 +370,20 @@ class scorm extends learnpath
if (!empty($item['maxtimeallowed'])) {
$lpItem->setMaxTimeAllowed($item['maxtimeallowed']);
}
$em->persist($lpItem);
$em->flush();
$lpItemRepo->create($lpItem);
$item_id = $lpItem->getIid();
if ($item_id) {
/*if ($item_id) {
// Now update previous item to change next_item_id.
$upd = "UPDATE $lpItemTable SET next_item_id = $item_id
WHERE iid = $previous";
Database::query($upd);
// Update previous item id.
$previous = $item_id;
}
}*/
// Code for indexing, now only index specific fields like terms and the title.
if (!empty($_POST['index_document'])) {
/*if (!empty($_POST['index_document'])) {
$di = new ChamiloIndexer();
isset($_POST['language']) ? $lang = Database::escape_string($_POST['language']) : $lang = 'english';
$di->connectDb(null, null, $lang);
@ -492,12 +406,12 @@ class scorm extends learnpath
$body_to_index = $all_specific_terms.' '.$title;
$ic_slide->addValue("content", $body_to_index);
// TODO: Add a comment to say terms separated by commas.
$courseid = api_get_course_id();
$ic_slide->addCourseId($courseid);
$ic_slide->addCourseId($courseId);
$ic_slide->addToolId(TOOL_LEARNPATH);
// TODO: Unify with other lp types.
$xapian_data = [
SE_COURSE_ID => $courseid,
SE_COURSE_ID => $courseId,
SE_TOOL_ID => TOOL_LEARNPATH,
SE_DATA => ['lp_id' => $lp_id, 'lp_item' => $previous, 'document_id' => ''],
SE_USER => api_get_user_id(),
@ -511,10 +425,10 @@ class scorm extends learnpath
$tbl_se_ref = Database::get_main_table(TABLE_MAIN_SEARCH_ENGINE_REF);
$sql = 'INSERT INTO %s (id, course_code, tool_id, ref_id_high_level, ref_id_second_level, search_did)
VALUES (NULL , \'%s\', \'%s\', %s, %s, %s)';
$sql = sprintf($sql, $tbl_se_ref, $courseCode, TOOL_LEARNPATH, $lp_id, $previous, $did);
$sql = sprintf($sql, $tbl_se_ref, $course->getCode(), TOOL_LEARNPATH, $lp_id, $previous, $did);
Database::query($sql);
}
}
}*/
}
}
@ -559,6 +473,7 @@ class scorm extends learnpath
$lpToCheck = null,
$allowHtaccess = false
) {
$this->debug= 100;
if ($this->debug) {
error_log(
'In scorm::import_package('.print_r($zipFileInfo, true).',"'.$currentDir.'") method'
@ -568,13 +483,6 @@ class scorm extends learnpath
$zipFilePath = $zipFileInfo['tmp_name'];
$zipFileName = $zipFileInfo['name'];
if ($this->debug > 1) {
error_log(
'import_package() - zip file path = '.$zipFilePath.', zip file name = '.$zipFileName,
0
);
}
$currentDir = api_replace_dangerous_char(trim($currentDir)); // Current dir we are in, inside scorm/
if ($this->debug > 1) {
@ -589,7 +497,8 @@ class scorm extends learnpath
$this->zipname = $fileBaseName; // Save for later in case we don't have a title.
$newDir = api_replace_dangerous_char(trim($fileBaseName));
$this->subdir = $newDir;
if ($this->debug > 1) {
if ($this->debug) {
error_log('$zipFileName: '.$zipFileName);
error_log('Received zip file name: '.$zipFilePath);
error_log("subdir is first set to : ".$this->subdir);
error_log("base file name is : ".$fileBaseName);
@ -598,7 +507,6 @@ class scorm extends learnpath
$zipFile = new ZipFile();
$zipFile->openFile($zipFilePath);
$zipContentArray = $zipFile->getEntries();
$packageType = '';
$manifestList = [];
// The following loop should be stopped as soon as we found the right imsmanifest.xml (how to recognize it?).
@ -612,7 +520,7 @@ class scorm extends learnpath
} elseif (stristr($fileName, 'imsmanifest.xml')) {
if ($fileName == basename($fileName)) {
} else {
if ($this->debug > 2) {
if ($this->debug) {
error_log("subdir is now ".$this->subdir);
}
}
@ -633,12 +541,17 @@ class scorm extends learnpath
}
}
$firstDir = $this->subdir;
$this->subdir .= '/'.dirname($shortestPath); // Do not concatenate because already done above.
if ($this->debug) {
error_log("subdir is now (2): ".$this->subdir);
}
$this->manifestToString = $zipFile->getEntryContents($shortestPath);
if ($this->debug) {
error_log("Package type is now: '$packageType'");
}
if ('' === $packageType) {
Display::addFlash(
Display::return_message(get_lang('This is not a valid SCORM ZIP file !'))
@ -695,58 +608,20 @@ class scorm extends learnpath
if ($request->files->has('user_file')) {
$uploadFile = $request->files->get('user_file');
}
$em = Database::getManager();
$repo = Container::getAssetRepository();
$asset = (new Asset())
->setCategory(Asset::SCORM)
->setTitle($zipFileName)
->setFile($uploadFile)
->setCompressed(true)
;
$em->persist($asset);
$em->flush();
$repo->update($asset);
// 2. Unzip file
$repo = Container::getAssetRepository();
$repo->unZipFile($asset, $zipFile);
$repo->unZipFile($asset, $firstDir);
$this->asset = $asset;
/*if (is_dir($courseSysDir.$newDir) ||
@mkdir(
$courseSysDir.$newDir,
api_get_permissions_for_new_directories()
)
) {
// PHP method - slower...
if ($this->debug >= 1) {
error_log('Changing dir to '.$courseSysDir.$newDir);
}
chdir($courseSysDir.$newDir);
$callBack = 'clean_up_files_in_zip';
if ($allowHtaccess) {
$callBack = 'cleanZipFilesAllowHtaccess';
}
if (api_get_configuration_value('skip_scorm_package_clean_up')) {
$callBack = 'cleanZipFilesNoRename';
}
$zipFile->extract(
PCLZIP_CB_PRE_EXTRACT,
$callBack
);
if (!empty($newDir)) {
$newDir = $newDir.'/';
}
api_chmod_R($courseSysDir.$newDir, api_get_permissions_for_new_directories());
} else {
return false;
}*/
//return $courseSysDir.$newDir.$manifest;
return $asset;
}
@ -945,7 +820,7 @@ class scorm extends learnpath
if ($this->debug > 1) {
error_log('In scorm::reimport_manifest() - Importing manifest '.$manifest_file);
}
$this->import_manifest($this->cc);
$this->import_manifest(api_get_course_int_id());
} else {
if ($this->debug > 0) {
error_log('In scorm::reimport_manifest() - Could not find manifest file at '.$manifest_file);

@ -1,4 +1,5 @@
<?php
/* For licensing terms, see /license.txt */
/**
@ -15,13 +16,14 @@ class scormItem extends learnpathItem
public $isvisible = '';
public $parameters = '';
public $title = '';
/** @var array|scormItem[] */
public $sub_items = [];
public $metadata;
//public $prerequisites = ''; - defined in learnpathItem.class.php
// Modified by Ivan Tcholakov, 06-FEB-2010.
//public $max_time_allowed = ''; //should be something like HHHH:MM:SS.SS
public $max_time_allowed = '00:00:00';
public $timelimitaction = '';
public $datafromlms = '';
public $mastery_score = '';
@ -35,13 +37,13 @@ class scormItem extends learnpathItem
* @param mixed $element Depending on the type given, DB id for the lp_item or reference to the DOM element
* @param int $course_id
*/
public function __construct($type = 'manifest', &$element, $course_id = 0)
public function __construct($type = 'manifest', &$element = null)
{
if (isset($element)) {
// Parsing using PHP5 DOMXML methods.
switch ($type) {
case 'db':
parent::__construct($element, api_get_user_id(), $course_id);
parent::__construct($element, api_get_user_id());
$this->scorm_contact = false;
// TODO: Implement this way of metadata object creation.
break;
@ -137,7 +139,6 @@ class scormItem extends learnpathItem
}
}
}
// End parsing using PHP5 DOMXML methods.
}
}
@ -162,7 +163,7 @@ class scormItem extends learnpathItem
'maxtimeallowed' => $this->max_time_allowed,
'metadata' => $this->metadata,
'parameters' => $this->parameters,
'prerequisites' => (!empty($this->prereq_string) ? $this->prereq_string : ''),
'prerequisites' => !empty($this->prereq_string) ? $this->prereq_string : '',
'rel_order' => $rel_order,
'timelimitaction' => $this->timelimitaction,
'title' => $this->title,

@ -1,13 +1,12 @@
<?php
/* For licensing terms, see /license.txt */
/**
* Container for the scormOrganization class.
*
* @author Yannick Warnier <ywarnier@beeznest.org>
*/
/**
* Class defining the <organization> tag in an imsmanifest.xml file.
*/
class scormOrganization
@ -15,6 +14,7 @@ class scormOrganization
public $identifier = '';
public $structure = '';
public $title = '';
/** @var array|scormItem[] */
public $items = [];
public $metadata;
@ -26,7 +26,7 @@ class scormOrganization
* @param string Type of construction needed ('db' or 'manifest', default = 'manifest')
* @param mixed Depending on the type given, DB id for the lp_item or reference to the DOM element
*/
public function __construct($type = 'manifest', &$element, $scorm_charset = 'UTF-8')
public function __construct($type = 'manifest', &$element = null)
{
if (isset($element)) {
// Parsing using PHP5 DOMXML methods.
@ -91,7 +91,6 @@ class scormOrganization
}
}
}
// End parsing using PHP5 DOMXML methods.
}
}

@ -10,6 +10,7 @@ use Chamilo\CoreBundle\Component\Utils\CreateUploadedFile;
use Chamilo\CoreBundle\Entity\Asset;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Exception;
use League\Flysystem\FilesystemOperator;
use PhpZip\ZipFile;
use Symfony\Component\HttpFoundation\File\UploadedFile;
@ -44,29 +45,41 @@ class AssetRepository extends ServiceEntityRepository
return $this->filesystem;
}
public function unZipFile(Asset $asset, ZipFile $zipFile): void
public function unZipFile(Asset $asset, string $addFolder = ''): void
{
$folder = '/'.$asset->getCategory().'/'.$asset->getTitle();
if (!empty($addFolder)) {
$folder .= '/'.$addFolder;
}
$fs = $this->getFileSystem();
$file = $this->getStorage()->resolveUri($asset);
if (!$fs->fileExists($file)) {
throw new Exception('file not found');
}
if ($fs->fileExists($folder)) {
$list = $zipFile->getEntries();
foreach ($list as $item) {
$name = $item->getName();
if ($fs->fileExists($folder.'/'.$name)) {
continue;
}
$stream = $fs->readStream($file);
$zipFile = new ZipFile();
$zipFile->openFromStream($stream);
if ($item->isDirectory()) {
$fs->createDirectory($folder.'/'.$name);
$list = $zipFile->getEntries();
foreach ($list as $item) {
$name = $item->getName();
error_log('final: '.$folder.'/'.$name);
if ($fs->fileExists($folder.'/'.$name)) {
continue;
}
continue;
}
if ($item->isDirectory()) {
$fs->createDirectory($folder.'/'.$name);
$content = $zipFile->getEntryContents($name);
$fs->write($folder.'/'.$name, $content);
continue;
}
$content = $zipFile->getEntryContents($name);
$fs->write($folder.'/'.$name, $content);
}
}
@ -79,11 +92,11 @@ class AssetRepository extends ServiceEntityRepository
$fs = $this->getFileSystem();
$file = $this->getStorage()->resolveUri($asset);
if ($fs->fileExists($file)) {
return $this->getFileSystem()->read($file);
if (!$fs->fileExists($file)) {
return '';
}
return '';
return $this->getFileSystem()->read($file);
}
public function getFolder(Asset $asset): ?string

@ -111,6 +111,31 @@ class AssetRepositoryTest extends AbstractApiTest
$this->assertResponseIsSuccessful();
}
public function testUnZipFileSubFolder(): void
{
$client = static::createClient();
$assetRepo = self::getContainer()->get(AssetRepository::class);
$this->assertSame(0, $assetRepo->count([]));
$file = $this->getUploadedZipFile();
$asset = (new Asset())
->setTitle('my file')
->setCategory(Asset::SCORM)
->setFile($file)
->setCompressed(true)
;
$assetRepo->update($asset);
$this->assertHasNoEntityViolations($asset);
$subDir = 'test/test';
$assetRepo->unZipFile($asset, $subDir);
$url = $assetRepo->getAssetUrl($asset);
$client->request('GET', $url.'/'.$subDir.'/logo.png');
$this->assertResponseIsSuccessful();
}
public function testDelete(): void
{
self::bootKernel();

Loading…
Cancel
Save