/**
  * 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;
     }
 }
 /**
  * (non-PHPdoc)
  *
  * @see \oat\tao\model\media\MediaManagement::add
  */
 public function add($source, $fileName, $parent)
 {
     if (!file_exists($source)) {
         throw new \tao_models_classes_FileNotFoundException($source);
     }
     $clazz = $this->getOrCreatePath($parent);
     $service = MediaService::singleton();
     $instanceUri = $service->createMediaInstance($source, $clazz->getUri(), $this->lang, $fileName);
     return $this->getFileInfo($instanceUri);
 }
 public function tearDown()
 {
     $this->fileManagerMock = null;
     $ref = new \ReflectionProperty('oat\\taoMediaManager\\model\\fileManagement\\FileManager', 'fileManager');
     $ref->setAccessible(true);
     $ref->setValue(null, null);
     $ref->setAccessible(false);
     $ref = new \ReflectionProperty('tao_models_classes_Service', 'instances');
     $ref->setAccessible(true);
     $ref->setValue(null, array());
     MediaService::singleton()->deleteClass(new \core_kernel_classes_Class($this->classUri));
 }
 /**
  * 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;
     }
 }
 public function testEditMediaInstance()
 {
     $fileTmp = dirname(__DIR__) . '/sample/Italy.png';
     $this->initializeMock($fileTmp);
     $this->fileManagerMock->expects($this->once())->method('deleteFile')->with('MyLink')->willReturn(true);
     $lang = 'EN-en';
     $instanceUri = 'http://myFancyDomain.com/myGreatInstanceUri';
     $instance = new \core_kernel_classes_Class($instanceUri);
     $instance->setPropertyValue(new \core_kernel_classes_Property(MEDIA_LINK), 'MyLink');
     $this->mediaService->editMediaInstance($fileTmp, $instanceUri, $lang);
     $this->assertEquals($lang, $instance->getUniquePropertyValue(new \core_kernel_classes_Property(MEDIA_LANGUAGE)), 'The instance language is wrong');
     // remove what has been done
     $inst = new \core_kernel_classes_Resource($instanceUri);
     $inst->delete();
 }
 /**
  * Validate an xml file, convert file linked inside and store it into media manager
  * @param \core_kernel_classes_Resource $instance the instance to edit
  * @param string $lang language of the shared stimulus
  * @param string $xmlFile File to store
  * @return \common_report_Report
  */
 protected function replaceSharedStimulus($instance, $lang, $xmlFile)
 {
     //if the class does not belong to media classes create a new one with its name (for items)
     $mediaClass = new core_kernel_classes_Class(MediaService::ROOT_CLASS_URI);
     if (!$instance->isInstanceOf($mediaClass)) {
         $report = \common_report_Report::createFailure('The instance ' . $instance->getUri() . ' is not a Media instance');
         return $report;
     }
     SharedStimulusImporter::isValidSharedStimulus($xmlFile);
     $name = basename($xmlFile, '.xml');
     $name .= '.xhtml';
     $filepath = dirname($xmlFile) . '/' . $name;
     \tao_helpers_File::copy($xmlFile, $filepath);
     $service = MediaService::singleton();
     if (!$service->editMediaInstance($filepath, $instance->getUri(), $lang)) {
         $report = \common_report_Report::createFailure(__('Fail to edit Shared Stimulus'));
     } else {
         $report = \common_report_Report::createSuccess(__('Shared Stimulus edited successfully'));
     }
     return $report;
 }
 protected function getClassService()
 {
     return MediaService::singleton();
 }
Exemplo n.º 8
0
 /**
  *
  * @param string $initialVersion
  * @return string $versionUpdatedTo
  */
 public function update($initialVersion)
 {
     $currentVersion = $initialVersion;
     //migrate from 0.1 to 0.1.1
     if ($currentVersion == '0.1') {
         // mediaSources set in 0.2
         $currentVersion = '0.1.1';
     }
     if ($currentVersion == '0.1.1') {
         FileManager::setFileManagementModel(new SimpleFileManagement());
         // mediaSources unset in 0.2
         $currentVersion = '0.1.2';
     }
     if ($currentVersion == '0.1.2') {
         //add alt text to media manager
         $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'alt_text.rdf';
         $adapter = new \tao_helpers_data_GenerisAdapterRdf();
         if ($adapter->import($file)) {
             $currentVersion = '0.1.3';
         } else {
             \common_Logger::w('Import failed for ' . $file);
         }
     }
     if ($currentVersion == '0.1.3') {
         OntologyUpdater::correctModelId(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'alt_text.rdf');
         $currentVersion = '0.1.4';
     }
     if ($currentVersion == '0.1.4') {
         //modify config files due to the new interfaces relation
         $tao = \common_ext_ExtensionsManager::singleton()->getExtensionById('tao');
         $tao->unsetConfig('mediaManagementSources');
         $tao->unsetConfig('mediaBrowserSources');
         TaoMediaService::singleton()->addMediaSource(new MediaSource());
         //modify links in item content
         $service = \taoItems_models_classes_ItemsService::singleton();
         $items = $service->getAllByModel('http://www.tao.lu/Ontologies/TAOItem.rdf#QTI');
         foreach ($items as $item) {
             $itemContent = $service->getItemContent($item);
             $itemContent = preg_replace_callback('/src="mediamanager\\/([^"]+)"/', function ($matches) {
                 $mediaClass = MediaService::singleton()->getRootClass();
                 $medias = $mediaClass->searchInstances(array(MEDIA_LINK => $matches[1]), array('recursive' => true));
                 $media = array_pop($medias);
                 $uri = '';
                 if (!is_null($media) && $media->exists()) {
                     $uri = \tao_helpers_Uri::encode($media->getUri());
                 }
                 return 'src="taomedia://mediamanager/' . $uri . '"';
             }, $itemContent);
             $itemContent = preg_replace_callback('/src="local\\/([^"]+)"/', function ($matches) {
                 return 'src="' . $matches[1] . '"';
             }, $itemContent);
             $service->setItemContent($item, $itemContent);
         }
         $currentVersion = '0.2.0';
     }
     if ($currentVersion === '0.2.0') {
         $accessService = \funcAcl_models_classes_AccessService::singleton();
         //revoke access right to back office
         $backOffice = new \core_kernel_classes_Resource('http://www.tao.lu/Ontologies/TAO.rdf#BackOfficeRole');
         $accessService->revokeExtensionAccess($backOffice, 'taoMediaManager');
         //grant access right to media manager
         $mediaManager = new \core_kernel_classes_Resource('http://www.tao.lu/Ontologies/TAOMedia.rdf#MediaManagerRole');
         $accessService->grantExtensionAccess($mediaManager, 'taoMediaManager');
         $currentVersion = '0.2.1';
     }
     if ($currentVersion === '0.2.1') {
         //include mediamanager into globalmanager
         $mediaManager = new \core_kernel_classes_Resource('http://www.tao.lu/Ontologies/TAOMedia.rdf#MediaManagerRole');
         $globalManager = new \core_kernel_Classes_Resource('http://www.tao.lu/Ontologies/TAO.rdf#GlobalManagerRole');
         \tao_models_classes_RoleService::singleton()->includeRole($globalManager, $mediaManager);
         $currentVersion = '0.2.2';
     }
     if ($currentVersion === '0.2.2') {
         //copy file from /media to data/taoMediaManager/media and delete /media
         $dataPath = FILES_PATH . 'taoMediaManager' . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR;
         $dir = dirname(dirname(__DIR__)) . '/media';
         if (file_exists($dir)) {
             if (\tao_helpers_File::copy($dir, $dataPath)) {
                 $it = new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS);
                 $files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
                 foreach ($files as $file) {
                     if ($file->isDir()) {
                         rmdir($file->getRealPath());
                     } else {
                         unlink($file->getRealPath());
                     }
                 }
                 rmdir($dir);
                 $currentVersion = '0.2.3';
             }
         } else {
             $currentVersion = '0.2.3';
         }
     }
     if ($currentVersion === '0.2.3') {
         $accessService = \funcAcl_models_classes_AccessService::singleton();
         //grant access to item author
         $itemAuthor = new \core_kernel_classes_Resource('http://www.tao.lu/Ontologies/TAOItem.rdf#ItemAuthor');
         $accessService->grantExtensionAccess($itemAuthor, 'taoMediaManager');
         $currentVersion = '0.2.4';
     }
     if ($currentVersion === '0.2.4') {
         $mediaClass = MediaService::singleton()->getRootClass();
         $fileManager = FileManager::getFileManagementModel();
         foreach ($mediaClass->getInstances(true) as $media) {
             $fileLink = $media->getUniquePropertyValue(new \core_kernel_classes_Property(MEDIA_LINK));
             $fileLink = $fileLink instanceof \core_kernel_classes_Resource ? $fileLink->getUri() : (string) $fileLink;
             $filePath = $fileManager->retrieveFile($fileLink);
             $mimeType = \tao_helpers_File::getMimeType($filePath);
             $mimeType = $mimeType === 'application/xhtml+xml' ? 'application/qti+xml' : $mimeType;
             $media->setPropertyValue(new \core_kernel_classes_Property(MEDIA_MIME_TYPE), $mimeType);
         }
         $currentVersion = '0.2.5';
     }
     if ($currentVersion === '0.2.5') {
         $fileManager = FileManager::getFileManagementModel();
         $iterator = new \core_kernel_classes_ResourceIterator(array(MediaService::singleton()->getRootClass()));
         foreach ($iterator as $media) {
             $fileLink = $media->getUniquePropertyValue(new \core_kernel_classes_Property(MEDIA_LINK));
             $fileLink = $fileLink instanceof \core_kernel_classes_Resource ? $fileLink->getUri() : (string) $fileLink;
             $filePath = $fileManager->retrieveFile($fileLink);
             try {
                 SharedStimulusImporter::isValidSharedStimulus($filePath);
                 $media->editPropertyValues(new \core_kernel_classes_Property(MEDIA_MIME_TYPE), 'application/qti+xml');
             } catch (\Exception $e) {
                 $mimeType = \tao_helpers_File::getMimeType($filePath);
                 $media->editPropertyValues(new \core_kernel_classes_Property(MEDIA_MIME_TYPE), $mimeType);
             }
         }
         $currentVersion = '0.3.0';
     }
     $this->skip('0.3.0', '0.5.1');
 }
 public function __construct()
 {
     parent::__construct();
     $this->service = MediaService::singleton();
 }
 protected function createClass($relPath)
 {
     $parentPath = dirname($relPath);
     if (isset($this->directoryMap[$parentPath])) {
         $parentUri = $this->directoryMap[$parentPath];
     } else {
         $parentUri = $this->createClass($parentPath);
     }
     $parentClass = new \core_kernel_classes_Class($parentUri);
     $childClazz = MediaService::singleton()->createSubClass($parentClass, basename($relPath));
     $this->directoryMap[$relPath] = $childClazz->getUri();
     return $childClazz->getUri();
 }
 /**
  * (non-PHPdoc)
  *
  * @see \oat\tao\model\media\MediaManagement::delete
  */
 public function delete($link)
 {
     return MediaService::singleton()->deleteResource(new \core_kernel_classes_Resource(\tao_helpers_Uri::decode($link)));
 }