Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 80 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_Export | |
0.00% |
0 / 80 |
|
0.00% |
0 / 8 |
1056 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
342 | |||
| getFormFactory | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isExportable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getNotExportableMessage | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getResourcesToExport | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
30 | |||
| getSecureResourceService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentExporter | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| getAvailableExportHandlers | |
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) 2002-2008 (original work) Public Research Centre Henri Tudor & University of Luxembourg |
| 19 | * (under the |
| 20 | * project TAO & TAO2); |
| 21 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 22 | * (under the project TAO-TRANSFER); |
| 23 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 24 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 25 | * 2013-2018 (update and modification) Open Assessment Technologies SA; |
| 26 | */ |
| 27 | |
| 28 | use oat\generis\model\OntologyAwareTrait; |
| 29 | use oat\oatbox\log\LoggerAwareTrait; |
| 30 | use oat\tao\model\resources\SecureResourceServiceInterface; |
| 31 | use oat\tao\model\task\ExportByHandler; |
| 32 | use oat\tao\model\taskQueue\QueueDispatcher; |
| 33 | use oat\tao\model\taskQueue\TaskLogActionTrait; |
| 34 | |
| 35 | /** |
| 36 | * This controller provide the actions to export and manage exported data |
| 37 | */ |
| 38 | class tao_actions_Export extends tao_actions_CommonModule |
| 39 | { |
| 40 | use TaskLogActionTrait; |
| 41 | use OntologyAwareTrait; |
| 42 | use LoggerAwareTrait; |
| 43 | |
| 44 | /** |
| 45 | * @return mixed |
| 46 | * @throws common_Exception |
| 47 | */ |
| 48 | public function index() |
| 49 | { |
| 50 | $formData = []; |
| 51 | |
| 52 | if ($this->hasRequestParameter('classUri') && trim($this->getRequestParameter('classUri')) !== '') { |
| 53 | $formData['class'] = $this->getClass(tao_helpers_Uri::decode($this->getRequestParameter('classUri'))); |
| 54 | } |
| 55 | |
| 56 | if ( |
| 57 | $this->hasRequestParameter('uri') |
| 58 | && $this->hasRequestParameter('classUri') |
| 59 | && trim($this->getRequestParameter('uri')) !== '' |
| 60 | ) { |
| 61 | $formData['instance'] = $this->getResource(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); |
| 62 | } |
| 63 | |
| 64 | $formData['id'] = $this->getRequestParameter('id'); |
| 65 | |
| 66 | if (!$this->isExportable($formData)) { |
| 67 | $this->setData('message', $this->getNotExportableMessage($formData)); |
| 68 | $this->setView('form/export_error_feedback.tpl', 'tao'); |
| 69 | |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | $handlers = $this->getAvailableExportHandlers(); |
| 74 | $exporter = $this->getCurrentExporter(); |
| 75 | if ($exporter === null) { |
| 76 | throw new common_exception_BadRequest('Exporter is not found'); |
| 77 | } |
| 78 | |
| 79 | $selectedResource = $formData['instance'] ?? $formData['class']; |
| 80 | |
| 81 | if (!$selectedResource) { |
| 82 | throw new common_exception_MissingParameter(); |
| 83 | } |
| 84 | |
| 85 | $formFactory = $this->getFormFactory($handlers, $exporter, $selectedResource, $formData); |
| 86 | $exportForm = $formFactory->getForm(); |
| 87 | $exportForm->setValues(['exportHandler' => get_class($exporter)]); |
| 88 | $this->setData('exportForm', $exportForm->render()); |
| 89 | |
| 90 | // if export form submitted |
| 91 | if ($this->hasRequestParameter('exportChooser_sent') && $this->getRequestParameter('exportChooser_sent') == 1) { |
| 92 | $exportData = $this->getRequestParameters(); |
| 93 | |
| 94 | if (isset($exportData['instances'])) { |
| 95 | $instances = json_decode(urldecode($exportData['instances'])); |
| 96 | unset($exportData['instances']); |
| 97 | |
| 98 | //allow to export complete classes |
| 99 | if (isset($exportData['type']) && $exportData['type'] === 'class') { |
| 100 | $children = [[]]; |
| 101 | foreach ($instances as $instance) { |
| 102 | $class = $this->getClass(tao_helpers_Uri::decode($instance)); |
| 103 | $children[] = $class->getInstances(); |
| 104 | } |
| 105 | $exportData['instances'] = array_merge(...$children); |
| 106 | } else { |
| 107 | foreach ($instances as $instance) { |
| 108 | $exportData['instances'][] = tao_helpers_Uri::decode($instance); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (!empty($exportData['instances'])) { |
| 113 | $this->getSecureResourceService()->validatePermissions($exportData['instances'], ['READ']); |
| 114 | } |
| 115 | } elseif (isset($exportData['exportInstance'])) { |
| 116 | $exportData['exportInstance'] = tao_helpers_Uri::decode($exportData['exportInstance']); |
| 117 | |
| 118 | $this->getSecureResourceService()->validatePermissions([$exportData['exportInstance']], ['READ']); |
| 119 | } |
| 120 | |
| 121 | /** @var QueueDispatcher $queueDispatcher */ |
| 122 | $queueDispatcher = $this->getServiceLocator()->get(QueueDispatcher::SERVICE_ID); |
| 123 | |
| 124 | $task = $queueDispatcher->createTask( |
| 125 | new ExportByHandler(), |
| 126 | [ |
| 127 | ExportByHandler::PARAM_EXPORT_HANDLER => get_class($exporter), |
| 128 | ExportByHandler::PARAM_EXPORT_DATA => $exportData |
| 129 | ], |
| 130 | __('Export "%s" in %s', $selectedResource->getLabel(), $exporter->getLabel()) |
| 131 | ); |
| 132 | |
| 133 | return $this->returnTaskJson($task); |
| 134 | } |
| 135 | |
| 136 | $context = Context::getInstance(); |
| 137 | $this->setData('export_extension', $context->getExtensionName()); |
| 138 | $this->setData('export_module', $context->getModuleName()); |
| 139 | $this->setData('export_action', $context->getActionName()); |
| 140 | |
| 141 | $this->setData('formTitle', __('Export')); |
| 142 | $this->setView('form/export.tpl', 'tao'); |
| 143 | } |
| 144 | |
| 145 | protected function getFormFactory( |
| 146 | array $handlers, |
| 147 | tao_models_classes_export_ExportHandler $exporter, |
| 148 | core_kernel_classes_Resource $selectedResource, |
| 149 | array $formData |
| 150 | ): tao_actions_form_Export { |
| 151 | return new tao_actions_form_Export($handlers, $exporter->getExportForm($selectedResource), $formData); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Is the metadata of the given resource is exportable? |
| 156 | * |
| 157 | * @param array $formData |
| 158 | * |
| 159 | * @return bool |
| 160 | * @author Gyula Szucs, <gyula@taotesting.com> |
| 161 | */ |
| 162 | protected function isExportable(array $formData): bool |
| 163 | { |
| 164 | return true; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Return a message, if the metadata of the resource is not exportable |
| 169 | * |
| 170 | * @return string |
| 171 | * @author Gyula Szucs, <gyula@taotesting.com> |
| 172 | */ |
| 173 | protected function getNotExportableMessage($formData): string |
| 174 | { |
| 175 | return __('Metadata export is not available for the selected resource.'); |
| 176 | } |
| 177 | |
| 178 | protected function getResourcesToExport() |
| 179 | { |
| 180 | $returnValue = []; |
| 181 | if ($this->hasRequestParameter('uri') && trim($this->getRequestParameter('uri')) != '') { |
| 182 | $returnValue[] = $this->getResource(tao_helpers_Uri::decode($this->getRequestParameter('uri'))); |
| 183 | } elseif ($this->hasRequestParameter('classUri') && trim($this->getRequestParameter('classUri')) != '') { |
| 184 | $class = $this->getClass(tao_helpers_Uri::decode($this->getRequestParameter('classUri'))); |
| 185 | $resourceService = $this->getSecureResourceService(); |
| 186 | $returnValue = $resourceService->getAllChildren($class); |
| 187 | } else { |
| 188 | $this->logWarning('No resources to export'); |
| 189 | } |
| 190 | |
| 191 | return $returnValue; |
| 192 | } |
| 193 | |
| 194 | private function getSecureResourceService(): SecureResourceServiceInterface |
| 195 | { |
| 196 | return $this->getServiceLocator()->get(SecureResourceServiceInterface::SERVICE_ID); |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the selected ExportHandler |
| 201 | * |
| 202 | * @return tao_models_classes_export_ExportHandler |
| 203 | * @throws common_Exception |
| 204 | */ |
| 205 | private function getCurrentExporter() |
| 206 | { |
| 207 | if ($this->hasRequestParameter('exportHandler')) { |
| 208 | $exportHandler = $_REQUEST['exportHandler'];//allow method "GET" |
| 209 | if ( |
| 210 | class_exists($exportHandler) |
| 211 | && in_array('tao_models_classes_export_ExportHandler', class_implements($exportHandler)) |
| 212 | ) { |
| 213 | return new $exportHandler(); |
| 214 | } else { |
| 215 | throw new common_Exception('Unknown or incompatible ExporterHandler: \'' . $exportHandler . '\''); |
| 216 | } |
| 217 | } else { |
| 218 | return current($this->getAvailableExportHandlers()); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | /** |
| 223 | * Override this function to add your own custom ExportHandlers |
| 224 | * |
| 225 | * @return \tao_models_classes_export_ExportHandler[] |
| 226 | */ |
| 227 | protected function getAvailableExportHandlers() |
| 228 | { |
| 229 | return [ |
| 230 | new tao_models_classes_export_RdfExporter() |
| 231 | ]; |
| 232 | } |
| 233 | } |