/** * Starts the import based on the form * * @param \core_kernel_classes_Class $class * @param \tao_helpers_form_Form $form * @return \common_report_Report $report */ public function import($class, $form) { //as upload may be called multiple times, we remove the session lock as soon as possible session_write_close(); try { $file = $form->getValue('source'); $service = MediaService::singleton(); $classUri = $class->getUri(); if (is_null($this->instanceUri) || $this->instanceUri === $classUri) { //if the file is a zip do a zip import if ($file['type'] !== 'application/zip') { if (!$service->createMediaInstance($file["uploaded_file"], $classUri, \tao_helpers_Uri::decode($form->getValue('lang')), $file["name"])) { $report = \common_report_Report::createFailure(__('Fail to import media')); } else { $report = \common_report_Report::createSuccess(__('Media imported successfully')); } } else { $zipImporter = new ZipImporter(); $report = $zipImporter->import($class, $form); } } else { if ($file['type'] !== 'application/zip') { $service->editMediaInstance($file["uploaded_file"], $this->instanceUri, \tao_helpers_Uri::decode($form->getValue('lang'))); $report = \common_report_Report::createSuccess(__('Media imported successfully')); } else { $report = \common_report_Report::createFailure(__('You can\'t upload a zip file as a media')); } } return $report; } catch (\Exception $e) { $report = \common_report_Report::createFailure($e->getMessage()); return $report; } }
/** * Starts the import based on the form * * @param \core_kernel_classes_Class $class * @param \tao_helpers_form_Form $form * @return \common_report_Report $report */ public function import($class, $form) { //as upload may be called multiple times, we remove the session lock as soon as possible session_write_close(); try { $file = $form->getValue('source'); $service = MediaService::singleton(); $classUri = $class->getUri(); if (is_null($this->instanceUri) || $this->instanceUri === $classUri) { //if the file is a zip do a zip import if ($file['type'] !== 'application/zip') { try { self::isValidSharedStimulus($file['uploaded_file']); $filepath = $file['uploaded_file']; $name = $file['name']; if (!$service->createMediaInstance($filepath, $classUri, \tao_helpers_Uri::decode($form->getValue('lang')), $name, 'application/qti+xml')) { $report = \common_report_Report::createFailure(__('Fail to import Shared Stimulus')); } else { $report = \common_report_Report::createSuccess(__('Shared Stimulus imported successfully')); } } catch (XmlStorageException $e) { // The shared stimulus is not qti compliant, display error $report = \common_report_Report::createFailure($e->getMessage()); } } else { $report = $this->zipImporter->import($class, $form); } } else { if ($file['type'] !== 'application/zip') { self::isValidSharedStimulus($file['uploaded_file']); $filepath = $file['uploaded_file']; if (in_array($file['type'], array('application/xml', 'text/xml'))) { $name = basename($file['name'], 'xml'); $name .= 'xhtml'; $filepath = dirname($file['name']) . '/' . $name; \tao_helpers_File::copy($file['uploaded_file'], $filepath); } if (!$service->editMediaInstance($filepath, $this->instanceUri, \tao_helpers_Uri::decode($form->getValue('lang')))) { $report = \common_report_Report::createFailure(__('Fail to edit shared stimulus')); } else { $report = \common_report_Report::createSuccess(__('Shared Stimulus edited successfully')); } } else { $report = $this->zipImporter->edit(new \core_kernel_classes_Resource($this->instanceUri), $form); } } return $report; } catch (\Exception $e) { $report = \common_report_Report::createFailure($e->getMessage()); return $report; } }
/** * Inits the element to select the importhandler * and takes over the elements of the import form * * @access public * @author Joel Bout, <*****@*****.**> * @return mixed */ public function initElements() { //create the element to select the import format $formatElt = tao_helpers_form_FormFactory::getElement('importHandler', 'Radiobox'); $formatElt->setDescription(__('Choose import format')); $formatElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); // should never happen anyway $importHandlerOptions = array(); foreach ($this->importHandlers as $importHandler) { $importHandlerOptions[get_class($importHandler)] = $importHandler->getLabel(); } $formatElt->setOptions($importHandlerOptions); $classUriElt = tao_helpers_form_FormFactory::getElement('classUri', 'Hidden'); // $classUriElt->setValue($class->getUri()); $this->form->addElement($classUriElt); $classUriElt = tao_helpers_form_FormFactory::getElement('id', 'Hidden'); $this->form->addElement($classUriElt); $this->form->addElement($formatElt); $this->form->createGroup('formats', __('Supported import formats'), array('importHandler')); if (!is_null($this->subForm)) { // load dynamically the method regarding the selected format $this->form->setElements(array_merge($this->form->getElements(), $this->subForm->getElements())); foreach ($this->subForm->getGroups() as $key => $group) { $this->form->createGroup($key, $group['title'], $group['elements'], $group['options']); } } }
/** * @param \core_kernel_classes_Resource $instance * @param \tao_helpers_form_Form $form * @return \common_report_Report */ public function edit($instance, $form) { \helpers_TimeOutHelper::setTimeOutLimit(\helpers_TimeOutHelper::LONG); try { $fileInfo = $form->getValue('source'); $xmlFile = $this->getSharedStimulusFile($fileInfo['uploaded_file']); // throws an exception of invalid SharedStimulusImporter::isValidSharedStimulus($xmlFile); $embeddedFile = $this->embedAssets($xmlFile); $report = $this->replaceSharedStimulus($instance, \tao_helpers_Uri::decode($form->getValue('lang')), $embeddedFile); } catch (\Exception $e) { $report = \common_report_Report::createFailure($e->getMessage()); } \helpers_TimeOutHelper::reset(); return $report; }
/** * Destructor (remove the current form in the static list) * * @access public * @author Cédric Alfonsi, <*****@*****.**> * @return mixed */ public function __destruct() { if (!is_null($this->form)) { //remove the refs of the contained form unset(self::$forms[$this->form->getName()]); } }
/** * Starts the import based on the form * * @param \core_kernel_classes_Class $class * @param \tao_helpers_form_Form $form * @return \common_report_Report */ public function import($class, $form) { //as upload may be called multiple times, we remove the session lock as soon as possible session_write_close(); try { $file = $form->getValue('source'); $resource = new core_kernel_classes_Class($form->getValue('classUri')); // unzip the file try { $directory = $this->extractArchive($file['uploaded_file']); } catch (\Exception $e) { return \common_report_Report::createFailure(__('Unable to extract the archive')); } // get list of directory in order to create classes $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($directory, \RecursiveDirectoryIterator::CURRENT_AS_FILEINFO | \RecursiveDirectoryIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY); $service = MediaService::singleton(); $language = $form->getValue('lang'); $this->directoryMap = array(rtrim($directory, DIRECTORY_SEPARATOR) => $resource->getUri()); /** @var $file \SplFileInfo */ $report = \common_report_Report::createSuccess(__('Media imported successfully')); foreach ($iterator as $file) { if ($file->isFile()) { \common_Logger::i('File ' . $file->getPathname()); if (isset($this->directoryMap[$file->getPath()])) { $classUri = $this->directoryMap[$file->getPath()]; } else { $classUri = $this->createClass($file->getPath()); } $service->createMediaInstance($file->getRealPath(), $classUri, $language, $file->getFilename()); $report->add(\common_report_Report::createSuccess(__('Imported %s', substr($file->getRealPath(), strlen($directory))))); } } return $report; } catch (\Exception $e) { $report = \common_report_Report::createFailure($e->getMessage()); return $report; } }
/** * Short description of method initElements * * @access public * @author Joel Bout, <*****@*****.**> * @return mixed */ public function initElements() { if (count($this->exportHandlers) > 1) { //create the element to select the import format $formatElt = tao_helpers_form_FormFactory::getElement('exportHandler', 'Radiobox'); $formatElt->setDescription(__('Choose export format')); //mandatory field $formatElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); $formatElt->setOptions($this->getFormats()); if (isset($_POST['exportHandler'])) { if (array_key_exists($_POST['exportHandler'], $this->getFormats())) { $formatElt->setValue($_POST['exportHandler']); } } $this->form->addElement($formatElt); $this->form->createGroup('formats', __('Supported export formats'), array('exportHandler')); } if (isset($this->data['instance'])) { $item = $this->data['instance']; if ($item instanceof core_kernel_classes_Resource) { //add an hidden elt for the instance Uri $uriElt = tao_helpers_form_FormFactory::getElement('uri', 'Hidden'); $uriElt->setValue($item->getUri()); $this->form->addElement($uriElt); } } if (isset($this->data['class'])) { $class = $this->data['class']; if ($class instanceof core_kernel_classes_Class) { //add an hidden elt for the class uri $classUriElt = tao_helpers_form_FormFactory::getElement('classUri', 'Hidden'); $classUriElt->setValue($class->getUri()); $this->form->addElement($classUriElt); } } $idElt = tao_helpers_form_FormFactory::getElement('id', 'Hidden'); $this->form->addElement($idElt); foreach ($this->subForm->getElements() as $element) { $this->form->addElement($element); } foreach ($this->subForm->getGroups() as $group) { $this->form->createGroup($group['title'], $group['title'], $group['elements'], $group['options']); } }
/** * test export * * @depends testInitExport * @depends testExportFormCreate * @depends testCreateInstance * @param \taoQtiTest_models_classes_export_TestExport $testExport * @param \tao_helpers_form_Form $form * @param \core_kernel_classes_Resource $qtiTest * @return void */ public function testExportFormSubmit($testExport, $form) { $report = $testExport->export($form->getValues(), $this->outputDir); $this->assertInstanceOf('common_report_Report', $report); $file = $report->getData(); $this->assertInternalType('string', $file); $this->assertFileExists($file); $this->assertStringStartsWith($this->outputDir, $file); $this->assertContains('qti_unit_test', $file); unlink($file); }
/** * test import form validate * * @depends testImportFormCreate * @param \tao_helpers_form_Form $form * @return void */ public function testImportFormValidate($form) { $source = $form->getElement('source')->getRawValue(); $this->assertArrayHasKey('uploaded_file', $source); $value = $form->getElement('import_sent_qti')->getRawValue(); $this->assertEquals(1, $value); }