Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 58 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
| tao_actions_Import | |
0.00% |
0 / 58 |
|
0.00% |
0 / 11 |
342 | |
0.00% |
0 / 1 |
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| index | |
0.00% |
0 / 23 |
|
0.00% |
0 / 1 |
6 | |||
| getFormTitle | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentImporter | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
20 | |||
| getAvailableImportHandlers | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| getImportByHandler | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getCurrentClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getValidators | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getImportHandlerServiceIdMap | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createTaskData | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
6 | |||
| getTaskParams | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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 project TAO & TAO2); |
| 20 | * 2008-2010 (update and modification) Deutsche Institut für Internationale Pädagogische Forschung |
| 21 | * (under the project TAO-TRANSFER); |
| 22 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 23 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 24 | * 2013-2018 (update and modification) Open Assessment Technologies SA; |
| 25 | */ |
| 26 | |
| 27 | use oat\oatbox\event\EventManager; |
| 28 | use oat\tao\model\import\TaskParameterProviderInterface; |
| 29 | use oat\tao\model\task\ImportByHandler; |
| 30 | use oat\tao\model\taskQueue\QueueDispatcher; |
| 31 | use oat\tao\model\taskQueue\TaskLogActionTrait; |
| 32 | use oat\generis\model\OntologyAwareTrait; |
| 33 | |
| 34 | /** |
| 35 | * This controller provide the actions to import resources |
| 36 | * |
| 37 | * @author CRP Henri Tudor - TAO Team - {@link http://www.tao.lu} |
| 38 | * @license GPLv2 http://www.opensource.org/licenses/gpl-2.0.php |
| 39 | * @package tao |
| 40 | * |
| 41 | */ |
| 42 | class tao_actions_Import extends tao_actions_CommonModule |
| 43 | { |
| 44 | use TaskLogActionTrait; |
| 45 | use OntologyAwareTrait; |
| 46 | |
| 47 | /** |
| 48 | * @var tao_models_classes_import_ImportHandler[] |
| 49 | */ |
| 50 | private $availableHandlers = []; |
| 51 | |
| 52 | /** |
| 53 | * @return EventManager |
| 54 | */ |
| 55 | protected function getEventManager() |
| 56 | { |
| 57 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * initialize the classUri and execute the upload action |
| 62 | * |
| 63 | * @requiresRight id WRITE |
| 64 | * @requiresRight classUri WRITE |
| 65 | */ |
| 66 | public function index() |
| 67 | { |
| 68 | $importer = $this->getCurrentImporter(); |
| 69 | |
| 70 | $this->propagate($importer); |
| 71 | |
| 72 | $formContainer = new tao_actions_form_Import( |
| 73 | $importer, |
| 74 | $this->getAvailableImportHandlers(), |
| 75 | $this->getCurrentClass() |
| 76 | ); |
| 77 | $importForm = $formContainer->getForm(); |
| 78 | |
| 79 | if ($importForm->isSubmited()) { |
| 80 | /** @var QueueDispatcher $queueDispatcher */ |
| 81 | $queueDispatcher = $this->getServiceLocator()->get(QueueDispatcher::SERVICE_ID); |
| 82 | |
| 83 | $task = $queueDispatcher->createTask( |
| 84 | $this->getImportByHandler(), |
| 85 | $this->createTaskData($importer, $importForm), |
| 86 | __('Import a %s into "%s"', $importer->getLabel(), $this->getCurrentClass()->getLabel()) |
| 87 | ); |
| 88 | |
| 89 | return $this->returnTaskJson($task); |
| 90 | } |
| 91 | |
| 92 | $context = Context::getInstance(); |
| 93 | $this->setData('import_extension', $context->getExtensionName()); |
| 94 | $this->setData('import_module', $context->getModuleName()); |
| 95 | $this->setData('import_action', $context->getActionName()); |
| 96 | |
| 97 | $this->setData('myForm', $importForm->render()); |
| 98 | $this->setData('formTitle', $this->getFormTitle()); |
| 99 | $this->setView('form/import.tpl', 'tao'); |
| 100 | } |
| 101 | |
| 102 | protected function getFormTitle(): string |
| 103 | { |
| 104 | return __('Import'); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Returns the currently selected import handler |
| 109 | * or the import handler to use by default |
| 110 | * |
| 111 | * @return tao_models_classes_import_ImportHandler |
| 112 | */ |
| 113 | protected function getCurrentImporter() |
| 114 | { |
| 115 | $handlers = $this->getAvailableImportHandlers(); |
| 116 | |
| 117 | if ($this->hasRequestParameter('importHandler')) { |
| 118 | foreach ($handlers as $importHandler) { |
| 119 | if (get_class($importHandler) == $_POST['importHandler']) { |
| 120 | return $importHandler; |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | $availableImportHandlers = $this->getAvailableImportHandlers(); |
| 126 | $currentImporter = reset($availableImportHandlers); |
| 127 | |
| 128 | return $currentImporter; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Gets the available import handlers for this module |
| 133 | * Should be overwritten by extensions that want to provide additional ImportHandlers |
| 134 | * |
| 135 | * @return tao_models_classes_import_ImportHandler[] |
| 136 | */ |
| 137 | protected function getAvailableImportHandlers() |
| 138 | { |
| 139 | if (empty($this->availableHandlers)) { |
| 140 | $this->availableHandlers = [ |
| 141 | new tao_models_classes_import_RdfImporter(), |
| 142 | new tao_models_classes_import_CsvImporter() |
| 143 | ]; |
| 144 | } |
| 145 | |
| 146 | return $this->availableHandlers; |
| 147 | } |
| 148 | |
| 149 | protected function getImportByHandler(): ImportByHandler |
| 150 | { |
| 151 | return new ImportByHandler(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Helper to get the selected class, needs to be passed as hidden field in the form |
| 156 | */ |
| 157 | protected function getCurrentClass() |
| 158 | { |
| 159 | return $this->getClass(tao_helpers_Uri::decode($this->getRequestParameter('classUri'))); |
| 160 | } |
| 161 | |
| 162 | protected function getValidators() |
| 163 | { |
| 164 | return []; |
| 165 | } |
| 166 | |
| 167 | protected function getImportHandlerServiceIdMap(): array |
| 168 | { |
| 169 | return []; |
| 170 | } |
| 171 | |
| 172 | private function createTaskData($importer, $importForm): array |
| 173 | { |
| 174 | $data = [ |
| 175 | ImportByHandler::PARAM_PARENT_CLASS => $this->getCurrentClass()->getUri(), |
| 176 | ImportByHandler::PARAM_OWNER => common_session_SessionManager::getSession()->getUser()->getIdentifier(), |
| 177 | ImportByHandler::PARAM_FORM_VALUES => $this->getTaskParams($importer, $importForm), |
| 178 | ]; |
| 179 | |
| 180 | $map = $this->getImportHandlerServiceIdMap(); |
| 181 | $importerClass = get_class($importer); |
| 182 | |
| 183 | if (isset($map[$importerClass])) { |
| 184 | $data[ImportByHandler::PARAM_IMPORT_HANDLER_SERVICE_ID] = $map[$importerClass]; |
| 185 | |
| 186 | return $data; |
| 187 | } |
| 188 | |
| 189 | $data[ImportByHandler::PARAM_IMPORT_HANDLER] = $importerClass; |
| 190 | |
| 191 | return $data; |
| 192 | } |
| 193 | |
| 194 | private function getTaskParams($importer, $importForm): array |
| 195 | { |
| 196 | return $importer instanceof TaskParameterProviderInterface |
| 197 | ? $importer->getTaskParameters($importForm) |
| 198 | : []; |
| 199 | } |
| 200 | } |