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