Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 64 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| FileImporter | |
0.00% |
0 / 64 |
|
0.00% |
0 / 6 |
420 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getForm | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| import | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
240 | |||
| getTaskParameters | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
| getZipImporter | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2014 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoMediaManager\model; |
| 24 | |
| 25 | use common_report_Report as Report; |
| 26 | use oat\oatbox\log\LoggerAwareTrait; |
| 27 | use oat\oatbox\log\TaoLoggerAwareInterface; |
| 28 | use tao_helpers_form_Form as Form; |
| 29 | use oat\tao\model\import\ImportHandlerHelperTrait; |
| 30 | use oat\tao\model\import\TaskParameterProviderInterface; |
| 31 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 32 | |
| 33 | /** |
| 34 | * Service methods to manage the Media |
| 35 | * |
| 36 | * @access public |
| 37 | * @author Antoine Robin, <antoine.robin@vesperiagroup.com> |
| 38 | * @package taoMediaManager |
| 39 | */ |
| 40 | class FileImporter implements |
| 41 | \tao_models_classes_import_ImportHandler, |
| 42 | ServiceLocatorAwareInterface, |
| 43 | TaskParameterProviderInterface, |
| 44 | TaoLoggerAwareInterface |
| 45 | { |
| 46 | use ImportHandlerHelperTrait { |
| 47 | getTaskParameters as getDefaultTaskParameters; |
| 48 | } |
| 49 | use LoggerAwareTrait; |
| 50 | |
| 51 | private $instanceUri; |
| 52 | |
| 53 | public function __construct($instanceUri = null) |
| 54 | { |
| 55 | $this->instanceUri = $instanceUri; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Returns a textual description of the import format |
| 60 | * |
| 61 | * @return string |
| 62 | */ |
| 63 | public function getLabel() |
| 64 | { |
| 65 | return __('File'); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Returns a form in order to prepare the import |
| 70 | * if the import is from a file, the form should include the file element |
| 71 | * |
| 72 | * @return Form |
| 73 | */ |
| 74 | public function getForm() |
| 75 | { |
| 76 | return (new FileImportForm($this->instanceUri)) |
| 77 | ->getForm(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * @param \core_kernel_classes_Class $class |
| 82 | * @param Form|array $form |
| 83 | * @param string|null $userId owner of the resource |
| 84 | * @return Report |
| 85 | * @throws \common_exception_Error |
| 86 | */ |
| 87 | public function import($class, $form, $userId = null) |
| 88 | { |
| 89 | $uploadedFile = $this->fetchUploadedFile($form); |
| 90 | |
| 91 | try { |
| 92 | $service = MediaService::singleton(); |
| 93 | $classUri = $class->getUri(); |
| 94 | |
| 95 | if (!$form instanceof Form && !is_array($form)) { |
| 96 | throw new \InvalidArgumentException('Import form should be either a Form object or an array.'); |
| 97 | } |
| 98 | |
| 99 | $instanceUri = $form instanceof Form |
| 100 | ? $form->getValue('instanceUri') |
| 101 | : (isset($form['instanceUri']) ? $form['instanceUri'] : null); |
| 102 | |
| 103 | $fileInfo = $form instanceof Form |
| 104 | ? $form->getValue('source') |
| 105 | : $form['source']; |
| 106 | |
| 107 | // importing new media |
| 108 | if (!$instanceUri || $instanceUri === $classUri) { |
| 109 | //if the file is a zip do a zip import |
| 110 | if (!\helpers_File::isZipMimeType($fileInfo['type'])) { |
| 111 | $mediaResourceUri = $service->createMediaInstance( |
| 112 | $uploadedFile, |
| 113 | $classUri, |
| 114 | \tao_helpers_Uri::decode($form instanceof Form ? $form->getValue('lang') : $form['lang']), |
| 115 | $fileInfo['name'], |
| 116 | null, |
| 117 | $userId |
| 118 | ); |
| 119 | |
| 120 | if (!$mediaResourceUri) { |
| 121 | $report = Report::createFailure(__('Fail to import media')); |
| 122 | $report->setData(['uriResource' => '']); |
| 123 | } else { |
| 124 | $report = Report::createSuccess(__('Media imported successfully')); |
| 125 | $report->add(Report::createSuccess( |
| 126 | __('Imported %s', $fileInfo['name']), |
| 127 | // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl |
| 128 | ['uriResource' => $mediaResourceUri] |
| 129 | )); |
| 130 | } |
| 131 | } else { |
| 132 | $report = $this->getZipImporter()->import($class, $form, $userId); |
| 133 | } |
| 134 | } else { |
| 135 | // editing existing media |
| 136 | if (!\helpers_File::isZipMimeType($fileInfo['type'])) { |
| 137 | $service->editMediaInstance( |
| 138 | $uploadedFile, |
| 139 | $instanceUri, |
| 140 | \tao_helpers_Uri::decode($form instanceof Form ? $form->getValue('lang') : $form['lang']), |
| 141 | $userId |
| 142 | ); |
| 143 | $report = Report::createSuccess(__('Media imported successfully')); |
| 144 | $report->add(Report::createSuccess( |
| 145 | __('Edited %s', $fileInfo['name']), |
| 146 | // 'uriResource' key is needed by javascript in tao/views/templates/form/import.tpl |
| 147 | ['uriResource' => $instanceUri] |
| 148 | )); |
| 149 | } else { |
| 150 | $report = Report::createFailure(__('You can\'t upload a zip file as a media')); |
| 151 | $report->setData(['uriResource' => $instanceUri]); |
| 152 | } |
| 153 | } |
| 154 | } catch (\Exception $e) { |
| 155 | $message = $e instanceof \common_exception_UserReadableException |
| 156 | ? $e->getUserMessage() |
| 157 | : __('An error has occurred. Please contact your administrator.'); |
| 158 | $report = Report::createFailure($message); |
| 159 | $this->logError($e->getMessage()); |
| 160 | $report->setData(['uriResource' => '']); |
| 161 | } |
| 162 | |
| 163 | $this->getUploadService()->remove($uploadedFile); |
| 164 | |
| 165 | return $report; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Defines the task parameters to be stored for later use. |
| 170 | * |
| 171 | * @param Form $form |
| 172 | * @return array |
| 173 | */ |
| 174 | public function getTaskParameters(Form $form) |
| 175 | { |
| 176 | return array_merge( |
| 177 | $form->getValues(), |
| 178 | $this->getDefaultTaskParameters($form) |
| 179 | ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * Get the zip importer for shared stimulus |
| 184 | * |
| 185 | * @return SharedStimulusPackageImporter |
| 186 | */ |
| 187 | protected function getZipImporter() |
| 188 | { |
| 189 | $zipImporter = new SharedStimulusPackageImporter(); |
| 190 | $zipImporter->setServiceLocator($this->getServiceLocator()); |
| 191 | return $zipImporter; |
| 192 | } |
| 193 | } |