/**
  * Import file entry point by using $this->service
  * Check POST method & get valid uploaded file
  */
 public function import()
 {
     try {
         // Check if it's post method
         if ($this->getRequestMethod() != Request::HTTP_POST) {
             throw new \common_exception_NotImplemented('Only post method is accepted to import Qti package.');
         }
         // Get valid package parameter
         $package = $this->getUploadedPackage();
         // Call service to import package
         \helpers_TimeOutHelper::setTimeOutLimit(\helpers_TimeOutHelper::LONG);
         $report = ImportService::singleton()->importQTIPACKFile($package, $this->getDestinationClass());
         \helpers_TimeOutHelper::reset();
         \tao_helpers_File::remove($package);
         if ($report->getType() !== \common_report_Report::TYPE_SUCCESS) {
             $this->returnFailure(new \common_Exception(__("An unexpected error occured during the import of the IMS QTI Item Package.")));
         } else {
             $itemIds = [];
             /** @var \common_report_Report $subReport */
             foreach ($report as $subReport) {
                 $itemIds[] = $subReport->getData()->getUri();
             }
             $this->returnSuccess(array('items' => $itemIds));
         }
     } catch (ExtractException $e) {
         $this->returnFailure(new \common_Exception(__('The ZIP archive containing the IMS QTI Item cannot be extracted.')));
     } catch (ParsingException $e) {
         $this->returnFailure(new \common_Exception(__('The ZIP archive does not contain an imsmanifest.xml file or is an invalid ZIP archive.')));
     } catch (\Exception $e) {
         $this->returnFailure($e);
     }
 }
 /**
  * (non-PHPdoc)
  * @see tao_models_classes_import_ImportHandler::import()
  */
 public function import($class, $form)
 {
     $fileInfo = $form->getValue('source');
     $rollbackInfo = $form->getValue('rollback');
     if (isset($fileInfo['uploaded_file'])) {
         $uploadedFile = $fileInfo['uploaded_file'];
         //test versioning
         $repository = null;
         if (common_Utils::isUri($form->getValue('repository'))) {
             $repository = new core_kernel_versioning_Repository($form->getValue('repository'));
             if (!$repository->exists()) {
                 $repository = null;
             }
         }
         helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::LONG);
         //the zip extraction is a long process that can exced the 30s timeout
         try {
             $importService = ImportService::singleton();
             $rollbackOnError = in_array('error', $rollbackInfo);
             $rollbackOnWarning = in_array('warning', $rollbackInfo);
             $report = $importService->importQTIPACKFile($uploadedFile, $class, true, $repository, $rollbackOnError, $rollbackOnWarning);
         } catch (ExtractException $e) {
             $report = common_report_Report::createFailure(__('The ZIP archive containing the IMS QTI Item cannot be extracted.'));
         } catch (ParsingException $e) {
             $report = common_report_Report::createFailure(__('The ZIP archive does not contain an imsmanifest.xml file or is an invalid ZIP archive.'));
         } catch (Exception $e) {
             $report = common_report_Report::createFailure(__("An unexpected error occured during the import of the IMS QTI Item Package."));
         }
         helpers_TimeOutHelper::reset();
         tao_helpers_File::remove($uploadedFile);
     } else {
         throw new common_exception_Error('No source file for import');
     }
     return $report;
 }
 public function testLomIdentifierGuardian()
 {
     $itemClass = \taoItems_models_classes_ItemsService::singleton()->getRootClass();
     $samplePath = dirname(__FILE__) . '/../samples/metadata/metadataGuardians/lomidentifieritem.zip';
     $report = \oat\taoQtiItem\model\qti\ImportService::singleton()->importQTIPACKFile($samplePath, $itemClass, true);
     // Report must contain an information message.
     $this->assertTrue($report->contains(\common_report_Report::TYPE_INFO));
     $this->assertEquals(1, count($report->getInfos()));
 }
 public static function setUpBeforeClass()
 {
     $itemClass = \taoItems_models_classes_ItemsService::singleton()->getRootClass();
     // Register Metadata ClassLookup.
     \oat\taoQtiItem\model\qti\Service::singleton()->getMetadataRegistry()->registerMetadataClassLookup('oat\\taoQtiItem\\model\\qti\\metadata\\classLookups\\LabelClassLookup');
     // Register Metadata Extractor.
     \oat\taoQtiItem\model\qti\Service::singleton()->getMetadataRegistry()->registerMetadataExtractor('oat\\taoQtiItem\\model\\qti\\metadata\\imsManifest\\ImsManifestMetadataExtractor');
     // Create fake class.
     \core_kernel_classes_ClassFactory::createSubClass($itemClass, 'mytestclasslabel', 'mytestclasslabel', 'http://www.test.com#mytestclass');
     // Import myTestClassLabel sample...
     $samplePath = dirname(__FILE__) . '/../samples/metadata/metadataClassLookups/mytestclasslabel.zip';
     $report = \oat\taoQtiItem\model\qti\ImportService::singleton()->importQTIPACKFile($samplePath, $itemClass, true);
     $successes = $report->getSuccesses();
     self::$itemResource = $successes[0]->getData();
 }
 public static function generateItems($count = 100)
 {
     // load QTI constants
     \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiItem');
     $ext = \common_ext_ExtensionsManager::singleton()->getExtensionById('taoDevTools');
     $generationId = NameGenerator::generateRandomString(4);
     $topClass = new \core_kernel_classes_Class(TAO_ITEM_CLASS);
     $class = $topClass->createSubClass('Generation ' . $generationId);
     $fileClass = new \core_kernel_classes_Class('http://www.tao.lu/Ontologies/generis.rdf#File');
     $sampleFile = $ext->getDir() . 'data/items/sampleItem.xml';
     helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::LONG);
     for ($i = 0; $i < $count; $i++) {
         $report = ImportService::singleton()->importQTIFile($sampleFile, $class, false);
         $item = $report->getData();
         $item->setLabel(NameGenerator::generateTitle());
     }
     helpers_TimeOutHelper::reset();
     return $class;
 }
 public function testResourceManager()
 {
     $itemClass = taoItems_models_classes_ItemsService::singleton()->getRootClass();
     $report = ImportService::singleton()->importQTIPACKFile(dirname(__FILE__) . '/samples/package/QTI/package.zip', $itemClass);
     $items = array();
     foreach ($report as $itemReport) {
         $data = $itemReport->getData();
         if (!is_null($data)) {
             $items[] = $data;
         }
     }
     $this->assertEquals(1, count($items));
     $item = current($items);
     $this->assertIsA($item, 'core_kernel_classes_Resource');
     $this->assertTrue($item->exists());
     $rm = new taoItems_helpers_ResourceManager(array('item' => $item, 'lang' => DEFAULT_LANG));
     $data = $rm->getDirectory();
     $this->assertTrue(is_array($data));
     $this->assertTrue(isset($data['path']));
     $this->assertEquals('/', $data['path']);
     $this->assertTrue(isset($data['children']));
     $children = $data['children'];
     $this->assertEquals(2, count($children));
     $file = null;
     $dir = null;
     foreach ($children as $child) {
         if (isset($child['path'])) {
             $dir = $child;
         }
         if (isset($child['name'])) {
             $file = $child;
         }
     }
     $this->assertEquals("qti.xml", $file['name']);
     $this->assertEquals("application/xml", $file['mime']);
     $this->assertTrue($file['size'] > 0);
     $this->assertEquals("/images", $dir['path']);
     $this->assertEquals(ROOT_URL, substr($dir['url'], 0, strlen(ROOT_URL)));
     taoItems_models_classes_ItemsService::singleton()->deleteItem($item);
     $this->assertFalse($item->exists());
 }
 public function testResourceManager()
 {
     $itemClass = taoItems_models_classes_ItemsService::singleton()->getRootClass();
     $report = ImportService::singleton()->importQTIPACKFile(dirname(__FILE__) . '/samples/package/QTI/package.zip', $itemClass);
     $items = array();
     foreach ($report as $itemReport) {
         $data = $itemReport->getData();
         if (!is_null($data)) {
             $items[] = $data;
         }
     }
     $this->assertEquals(1, count($items));
     $item = current($items);
     $this->assertIsA($item, 'core_kernel_classes_Resource');
     $this->assertTrue($item->exists());
     $rm = new LocalItemSource(array('item' => $item, 'lang' => DEFAULT_LANG));
     $data = $rm->getDirectory();
     $this->assertTrue(is_array($data));
     $this->assertTrue(isset($data['path']));
     $this->assertEquals('/', $data['path']);
     $this->assertTrue(isset($data['children']));
     $children = $data['children'];
     $this->assertEquals(3, count($children));
     $check = array('/images/', '/style/');
     $file = null;
     $dir = null;
     foreach ($children as $child) {
         if (isset($child['path'])) {
             $this->assertContains($child['path'], $check);
         }
         if (isset($child['name'])) {
             $file = $child;
         }
     }
     $this->assertEquals("qti.xml", $file['name']);
     $this->assertTrue(strpos($file['mime'], '/xml') !== false);
     //can be 'application/xml' or 'text/xml'
     $this->assertTrue($file['size'] > 0);
     taoItems_models_classes_ItemsService::singleton()->deleteItem($item);
     $this->assertFalse($item->exists());
 }
 /**
  * @depends testImport
  * @param $item
  * @throws Exception
  * @throws \oat\taoQtiItem\model\qti\exception\ExtractException
  * @throws \oat\taoQtiItem\model\qti\exception\ParsingException
  * @return mixed
  */
 public function testExport($item)
 {
     $itemClass = $this->itemService->getRootClass();
     list($path, $manifest) = $this->createZipArchive($item);
     $report = $this->importService->importQTIPACKFile($path, $itemClass);
     $this->assertEquals(\common_report_Report::TYPE_SUCCESS, $report->getType());
     $items = array();
     foreach ($report as $itemReport) {
         $data = $itemReport->getData();
         if (!is_null($data)) {
             $items[] = $data;
         }
     }
     $this->assertEquals(1, count($items));
     $item2 = current($items);
     $this->assertInstanceOf('\\core_kernel_classes_Resource', $item);
     $this->assertTrue($item->exists());
     $this->assertEquals($item->getLabel(), $item2->getLabel());
     $this->removeItem($item2);
     return $manifest;
 }
 /**
  * (non-PHPdoc)
  * @see tao_models_classes_import_ImportHandler::import()
  */
 public function import($class, $form)
 {
     $fileInfo = $form->getValue('source');
     if (isset($fileInfo['uploaded_file'])) {
         $uploadedFile = $fileInfo['uploaded_file'];
         try {
             $importService = ImportService::singleton();
             $report = $importService->importQTIFile($uploadedFile, $class, true, null);
         } catch (UnsupportedQtiElement $e) {
             $report = common_report_Report::createFailure(__('The "%s" QTI component is not supported.', $e->getType()));
         } catch (ParsingException $e) {
             $report = common_report_Report::createFailure(__("The validation of the imported QTI item failed. The system returned the following error:%s\n", $e->getMessage()));
         } catch (common_Exception $e) {
             $report = common_report_Report::createFailure(__("An unexpected error occured during the import of the QTI Item. The system returned the following error:", $e->getMessage()));
         }
         @unlink($uploadedFile);
     } else {
         throw new common_exception_Error('No source file for import');
     }
     return $report;
 }
 /**
  * Import a QTI Test and its dependent Items into the TAO Platform.
  *
  * @param core_kernel_classes_Class $targetClass The RDFS Class where Ontology resources must be created.
  * @param oat\taoQtiItem\model\qti\Resource $qtiTestResource The QTI Test Resource representing the IMS QTI Test to be imported.
  * @param taoQtiTest_models_classes_ManifestParser $manifestParser The parser used to retrieve the IMS Manifest.
  * @param string $folder The absolute path to the folder where the IMS archive containing the test content
  * @return common_report_Report A report about how the importation behaved.
  */
 protected function importTest(core_kernel_classes_Class $targetClass, Resource $qtiTestResource, taoQtiTest_models_classes_ManifestParser $manifestParser, $folder)
 {
     $itemImportService = ImportService::singleton();
     $itemService = taoItems_models_classes_ItemsService::singleton();
     $testClass = $targetClass;
     // Create an RDFS resource in the knowledge base that will hold
     // the information about the imported QTI Test.
     $testResource = $this->createInstance($testClass);
     $qtiTestModelResource = new core_kernel_classes_Resource(INSTANCE_TEST_MODEL_QTI);
     $modelProperty = new core_kernel_classes_Property(PROPERTY_TEST_TESTMODEL);
     $testResource->editPropertyValues($modelProperty, $qtiTestModelResource);
     // Create the report that will hold information about the import
     // of $qtiTestResource in TAO.
     $report = new common_report_Report(common_report_Report::TYPE_INFO);
     // The class where the items that belong to the test will be imported.
     $itemClass = new core_kernel_classes_Class(TAO_ITEM_CLASS);
     $targetClass = $itemClass->createSubClass($testResource->getLabel());
     // Load and validate the manifest
     $qtiManifestParser = new taoQtiTest_models_classes_ManifestParser($folder . 'imsmanifest.xml');
     $qtiManifestParser->validate();
     // Prepare Metadata mechanisms.
     $metadataMapping = oat\taoQtiItem\model\qti\Service::singleton()->getMetadataRegistry()->getMapping();
     $metadataInjectors = array();
     $metadataGuardians = array();
     $metadataClassLookups = array();
     $metadataValues = array();
     $domManifest = new DOMDocument('1.0', 'UTF-8');
     $domManifest->load($folder . 'imsmanifest.xml');
     foreach ($metadataMapping['injectors'] as $injector) {
         $metadataInjectors[] = new $injector();
     }
     foreach ($metadataMapping['guardians'] as $guardian) {
         $metadataGuardians[] = new $guardian();
     }
     foreach ($metadataMapping['classLookups'] as $classLookup) {
         $metadataClassLookups[] = new $classLookup();
     }
     foreach ($metadataMapping['extractors'] as $extractor) {
         $metadataExtractor = new $extractor();
         $metadataValues = array_merge($metadataValues, $metadataExtractor->extract($domManifest));
     }
     // Set up $report with useful information for client code (especially for rollback).
     $reportCtx = new stdClass();
     $reportCtx->manifestResource = $qtiTestResource;
     $reportCtx->rdfsResource = $testResource;
     $reportCtx->itemClass = $targetClass;
     $reportCtx->items = array();
     $report->setData($reportCtx);
     // Expected test.xml file location.
     $expectedTestFile = $folder . str_replace('/', DIRECTORY_SEPARATOR, $qtiTestResource->getFile());
     // Already imported test items (qti xml file paths).
     $alreadyImportedTestItemFiles = array();
     // -- Check if the file referenced by the test QTI resource exists.
     if (is_readable($expectedTestFile) === false) {
         $report->add(common_report_Report::createFailure(__('No file found at location "%s".', $qtiTestResource->getFile())));
     } else {
         // -- Load the test in a QTISM flavour.
         $testDefinition = new XmlDocument();
         try {
             $testDefinition->load($expectedTestFile, true);
             // -- Load all items related to test.
             $itemError = false;
             // discover test's base path.
             $dependencies = taoQtiTest_helpers_Utils::buildAssessmentItemRefsTestMap($testDefinition, $manifestParser, $folder);
             if (count($dependencies['items']) > 0) {
                 foreach ($dependencies['items'] as $assessmentItemRefId => $qtiDependency) {
                     if ($qtiDependency !== false) {
                         if (Resource::isAssessmentItem($qtiDependency->getType())) {
                             $resourceIdentifier = $qtiDependency->getIdentifier();
                             // Check if the item is already stored in the bank.
                             foreach ($metadataGuardians as $guardian) {
                                 if (isset($metadataValues[$resourceIdentifier]) === true) {
                                     if (($guard = $guardian->guard($metadataValues[$resourceIdentifier])) !== false) {
                                         common_Logger::i("Item with identifier '{$resourceIdentifier}' already in Item Bank.");
                                         $msg = __('The IMS QTI Item referenced as "%s" in the IMS Manifest file was already stored in the Item Bank.', $resourceIdentifier);
                                         $report->add(common_report_Report::createInfo($msg, $guard));
                                         $reportCtx->items[$assessmentItemRefId] = $guard;
                                         // Simply do not import again.
                                         continue 2;
                                     }
                                 }
                             }
                             // Determine target class from metadata, if possible.
                             // This is applied to items, not for test definitions.
                             // The test definitions' target class will not be affected
                             // by class lookups.
                             $lookupTargetClass = false;
                             foreach ($metadataClassLookups as $classLookup) {
                                 if (isset($metadataValues[$resourceIdentifier]) === true) {
                                     if (($lookupTargetClass = $classLookup->lookup($metadataValues[$resourceIdentifier])) !== false) {
                                         break;
                                     }
                                 }
                             }
                             $qtiFile = $folder . str_replace('/', DIRECTORY_SEPARATOR, $qtiDependency->getFile());
                             // Skip if $qtiFile already imported (multiple assessmentItemRef "hrefing" the same file).
                             if (array_key_exists($qtiFile, $alreadyImportedTestItemFiles) === false) {
                                 $isApip = $qtiDependency->getType() === 'imsqti_apipitem_xmlv2p1';
                                 $itemReport = $itemImportService->importQtiItem($folder, $qtiDependency, $lookupTargetClass !== false ? $lookupTargetClass : $targetClass, $isApip, $dependencies['dependencies']);
                                 $rdfItem = $itemReport->getData();
                                 if ($rdfItem) {
                                     $reportCtx->items[$assessmentItemRefId] = $rdfItem;
                                     $alreadyImportedTestItemFiles[$qtiFile] = $rdfItem;
                                     $itemReport->setMessage(__('IMS QTI Item referenced as "%s" in the IMS Manifest file successfully imported.', $qtiDependency->getIdentifier()));
                                 } else {
                                     $itemReport->setType(common_report_Report::TYPE_ERROR);
                                     $itemReport->setMessage(__('IMS QTI Item referenced as "%s" in the IMS Manifest file could not be imported.', $qtiDependency->getIdentifier()));
                                     $itemError = $itemError === false ? true : $itemError;
                                 }
                                 $report->add($itemReport);
                             } else {
                                 $reportCtx->items[$assessmentItemRefId] = $alreadyImportedTestItemFiles[$qtiFile];
                             }
                         }
                     } else {
                         $msg = __('The dependency to the IMS QTI AssessmentItemRef "%s" in the IMS Manifest file could not be resolved.', $assessmentItemRefId);
                         $report->add(common_report_Report::createFailure($msg));
                         $itemError = $itemError === false ? true : $itemError;
                     }
                 }
                 // If items did not produce errors, we import the test definition.
                 if ($itemError === false) {
                     common_Logger::i('Importing test...');
                     // Second step is to take care of the test definition and the related media (auxiliary files).
                     // 1. Import test definition (i.e. the QTI-XML Test file).
                     $testContent = $this->importTestDefinition($testResource, $testDefinition, $qtiTestResource, $reportCtx->items, $folder, $report);
                     if ($testContent !== false) {
                         // 2. Import test auxilliary files (e.g. stylesheets, images, ...).
                         $this->importTestAuxiliaryFiles($testContent, $qtiTestResource, $folder, $report);
                         // 3. Give meaningful names to resources.
                         $testTitle = $testDefinition->getDocumentComponent()->getTitle();
                         $testResource->setLabel($testDefinition->getDocumentComponent()->getTitle());
                         $targetClass->setLabel($testDefinition->getDocumentComponent()->getTitle());
                         // 4. if $targetClass does not contain any instances (because everything resolved by class lookups),
                         // Just delete it.
                         if ($targetClass->countInstances() == 0) {
                             $targetClass->delete();
                         }
                     }
                 } else {
                     $msg = __("One or more dependent IMS QTI Items could not be imported.");
                     $report->add(common_report_Report::createFailure($msg));
                 }
             } else {
                 // No depencies found (i.e. no item resources bound to the test).
                 $msg = __("No reference to any IMS QTI Item found.");
                 $report->add(common_report_Report::createFailure($msg));
             }
         } catch (StorageException $e) {
             // Source of the exception = $testDefinition->load()
             // What is the reason ?
             $finalErrorString = '';
             $eStrs = array();
             if (($libXmlErrors = $e->getErrors()) !== null) {
                 foreach ($libXmlErrors as $libXmlError) {
                     $eStrs[] = __('XML error at line %1$d column %2$d "%3$s".', $libXmlError->line, $libXmlError->column, trim($libXmlError->message));
                 }
             }
             $finalErrorString = implode("\n", $eStrs);
             if (empty($finalErrorString) === true) {
                 // Not XML malformation related. No info from LibXmlErrors extracted.
                 if (($previous = $e->getPrevious()) != null) {
                     // Useful information could be found here.
                     $finalErrorString = $previous->getMessage();
                     if ($previous instanceof UnmarshallingException) {
                         $domElement = $previous->getDOMElement();
                         $finalErrorString = __('Inconsistency at line %1d:', $domElement->getLineNo()) . ' ' . $previous->getMessage();
                     }
                 } else {
                     $finalErrorString = __("Unknown error.");
                 }
             }
             $msg = __("Error found in the IMS QTI Test:\n%s", $finalErrorString);
             $report->add(common_report_Report::createFailure($msg));
         }
     }
     if ($report->containsError() === false) {
         $report->setType(common_report_Report::TYPE_SUCCESS);
         $msg = __("IMS QTI Test referenced as \"%s\" in the IMS Manifest file successfully imported.", $qtiTestResource->getIdentifier());
         $report->setMessage($msg);
     } else {
         $report->setType(common_report_Report::TYPE_ERROR);
         $msg = __("The IMS QTI Test referenced as \"%s\" in the IMS Manifest file could not be imported.", $qtiTestResource->getIdentifier());
         $report->setMessage($msg);
     }
     return $report;
 }
 /**
  * Import a QTI Test and its dependent Items into the TAO Platform.
  *
  * @param core_kernel_classes_Class $targetClass The RDFS Class where Ontology resources must be created.
  * @param oat\taoQtiItem\model\qti\Resource $qtiTestResource The QTI Test Resource representing the IMS QTI Test to be imported.
  * @param taoQtiTest_models_classes_ManifestParser $manifestParser The parser used to retrieve the IMS Manifest.
  * @param string $folder The absolute path to the folder where the IMS archive containing the test content
  * @return common_report_Report A report about how the importation behaved.
  */
 protected function importTest(core_kernel_classes_Class $targetClass, Resource $qtiTestResource, taoQtiTest_models_classes_ManifestParser $manifestParser, $folder)
 {
     $itemImportService = ImportService::singleton();
     $itemService = taoItems_models_classes_ItemsService::singleton();
     $testClass = $targetClass;
     // Create an RDFS resource in the knowledge base that will hold
     // the information about the imported QTI Test.
     $testResource = $this->createInstance($testClass);
     $qtiTestModelResource = new core_kernel_classes_Resource(INSTANCE_TEST_MODEL_QTI);
     $modelProperty = new core_kernel_classes_Property(PROPERTY_TEST_TESTMODEL);
     $testResource->editPropertyValues($modelProperty, $qtiTestModelResource);
     // Create the report that will hold information about the import
     // of $qtiTestResource in TAO.
     $report = new common_report_Report(common_report_Report::TYPE_INFO);
     // The class where the items that belong to the test will be imported.
     $itemClass = new core_kernel_classes_Class(TAO_ITEM_CLASS);
     $targetClass = $itemClass->createSubClass($testResource->getLabel());
     // Load and validate the manifest
     $qtiManifestParser = new taoQtiTest_models_classes_ManifestParser($folder . 'imsmanifest.xml');
     $qtiManifestParser->validate();
     // Set up $report with useful information for client code (especially for rollback).
     $reportCtx = new stdClass();
     $reportCtx->manifestResource = $qtiTestResource;
     $reportCtx->rdfsResource = $testResource;
     $reportCtx->itemClass = $targetClass;
     $reportCtx->items = array();
     $report->setData($reportCtx);
     // Expected test.xml file location.
     $expectedTestFile = $folder . str_replace('/', DIRECTORY_SEPARATOR, $qtiTestResource->getFile());
     // Already imported test items (qti xml file paths).
     $alreadyImportedTestItemFiles = array();
     // -- Check if the file referenced by the test QTI resource exists.
     if (is_readable($expectedTestFile) === false) {
         $report->add(common_report_Report::createFailure(__('No file found at location "%s".', $qtiTestResource->getFile())));
     } else {
         // -- Load the test in a QTISM flavour.
         $testDefinition = new XmlDocument();
         try {
             $testDefinition->load($expectedTestFile, true);
             // -- Load all items related to test.
             $itemError = false;
             // discover test's base path.
             $dependencies = taoQtiTest_helpers_Utils::buildAssessmentItemRefsTestMap($testDefinition, $manifestParser, $folder);
             if (count($dependencies) > 0) {
                 foreach ($dependencies as $assessmentItemRefId => $qtiDependency) {
                     if ($qtiDependency !== false) {
                         if (Resource::isAssessmentItem($qtiDependency->getType())) {
                             $qtiFile = $folder . str_replace('/', DIRECTORY_SEPARATOR, $qtiDependency->getFile());
                             // Skip if $qtiFile already imported (multiple assessmentItemRef "hrefing" the same file).
                             if (array_key_exists($qtiFile, $alreadyImportedTestItemFiles) === false) {
                                 $itemReport = $itemImportService->importQTIFile($qtiFile, $targetClass);
                                 $rdfItem = $itemReport->getData();
                                 if ($rdfItem) {
                                     $itemPath = taoItems_models_classes_ItemsService::singleton()->getItemFolder($rdfItem);
                                     foreach ($qtiDependency->getAuxiliaryFiles() as $auxResource) {
                                         // $auxResource is a relativ URL, so we need to replace the slashes with directory separators
                                         $auxPath = $folder . str_replace('/', DIRECTORY_SEPARATOR, $auxResource);
                                         // does the file referenced by $auxPath exist?
                                         if (is_readable($auxPath) === true) {
                                             $relPath = helpers_File::getRelPath($qtiFile, $auxPath);
                                             $destPath = $itemPath . $relPath;
                                             tao_helpers_File::copy($auxPath, $destPath, true);
                                         } else {
                                             $msg = __('Auxiliary file not found at location "%s".', $auxResource);
                                             $itemReport->add(new common_report_Report(common_report_Report::TYPE_WARNING, $msg));
                                         }
                                     }
                                     $reportCtx->items[$assessmentItemRefId] = $rdfItem;
                                     $alreadyImportedTestItemFiles[$qtiFile] = $rdfItem;
                                     $itemReport->setMessage(__('IMS QTI Item referenced as "%s" in the IMS Manifest file successfully imported.', $qtiDependency->getIdentifier()));
                                 } else {
                                     $itemReport->setType(common_report_Report::TYPE_ERROR);
                                     $itemReport->setMessage(__('IMS QTI Item referenced as "%s" in the IMS Manifest file could not be imported.', $qtiDependency->getIdentifier()));
                                     $itemError = $itemError === false ? true : $itemError;
                                 }
                                 $report->add($itemReport);
                             } else {
                                 $reportCtx->items[$assessmentItemRefId] = $alreadyImportedTestItemFiles[$qtiFile];
                             }
                         }
                     } else {
                         $msg = __('The dependency to the IMS QTI AssessmentItemRef "%s" in the IMS Manifest file could not be resolved.', $assessmentItemRefId);
                         $report->add(common_report_Report::createFailure($msg));
                         $itemError = $itemError === false ? true : $itemError;
                     }
                 }
                 // If items did not produce errors, we import the test definition.
                 if ($itemError === false) {
                     common_Logger::i('Importing test...');
                     // Second step is to take care of the test definition and the related media (auxiliary files).
                     // 1. Import test definition (i.e. the QTI-XML Test file).
                     $testContent = $this->importTestDefinition($testResource, $testDefinition, $qtiTestResource, $reportCtx->items, $folder, $report);
                     if ($testContent !== false) {
                         // 2. Import test auxilliary files (e.g. stylesheets, images, ...).
                         $this->importTestAuxiliaryFiles($testContent, $qtiTestResource, $folder, $report);
                         // 3. Give meaningful names to resources.
                         $testTitle = $testDefinition->getDocumentComponent()->getTitle();
                         $testResource->setLabel($testDefinition->getDocumentComponent()->getTitle());
                         $targetClass->setLabel($testDefinition->getDocumentComponent()->getTitle());
                     }
                 } else {
                     $msg = __("One or more dependent IMS QTI Items could not be imported.");
                     $report->add(common_report_Report::createFailure($msg));
                 }
             } else {
                 // No depencies found (i.e. no item resources bound to the test).
                 $msg = __("No reference to any IMS QTI Item found.");
                 $report->add(common_report_Report::createFailure($msg));
             }
         } catch (StorageException $e) {
             // Source of the exception = $testDefinition->load()
             // What is the reason ?
             $finalErrorString = '';
             $eStrs = array();
             if (($libXmlErrors = $e->getErrors()) !== null) {
                 foreach ($libXmlErrors as $libXmlError) {
                     $eStrs[] = __('XML error at line %1$d column %2$d "%3$s".', $libXmlError->line, $libXmlError->column, trim($libXmlError->message));
                 }
             }
             $finalErrorString = implode("\n", $eStrs);
             if (empty($finalErrorString) === true) {
                 // Not XML malformation related. No info from LibXmlErrors extracted.
                 if (($previous = $e->getPrevious()) != null) {
                     // Useful information could be found here.
                     $finalErrorString = $previous->getMessage();
                     if ($previous instanceof UnmarshallingException) {
                         $domElement = $previous->getDOMElement();
                         $finalErrorString = __('Inconsistency at line %1d:', $domElement->getLineNo()) . ' ' . $previous->getMessage();
                     }
                 } else {
                     $finalErrorString = __("Unknown error.");
                 }
             }
             $msg = __("Error found in the IMS QTI Test:\n%s", $finalErrorString);
             $report->add(common_report_Report::createFailure($msg));
         }
     }
     if ($report->containsError() === false) {
         $report->setType(common_report_Report::TYPE_SUCCESS);
         $msg = __("IMS QTI Test referenced as \"%s\" in the IMS Manifest file successfully imported.", $qtiTestResource->getIdentifier());
         $report->setMessage($msg);
     } else {
         $report->setType(common_report_Report::TYPE_ERROR);
         $msg = __("The IMS QTI Test referenced as \"%s\" in the IMS Manifest file could not be imported.", $qtiTestResource->getIdentifier());
         $report->setMessage($msg);
     }
     return $report;
 }
<?php

/*  
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; under version 2
 * of the License (non-upgradable).
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Copyright (c) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
 *               
 * 
 */
use oat\taoQtiItem\model\qti\ImportService;
$itemClass = taoItems_models_classes_ItemsService::singleton()->getRootClass();
$file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'qtiv2p1Examples.zip';
$service = ImportService::singleton();
try {
    $service->importQTIPACKFile($file, $itemClass, false);
} catch (Exception $e) {
    common_Logger::e('Error Occurs when importing Qti Exemples ' . $e->getMessage());
    throw $e;
}