Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 48 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ImportQtiTest | |
0.00% |
0 / 48 |
|
0.00% |
0 / 4 |
72 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createTask | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
2 | |||
| getClass | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| 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) 2016-2024 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | namespace oat\taoQtiTest\models\tasks; |
| 22 | |
| 23 | use common_exception_Error; |
| 24 | use common_exception_MissingParameter; |
| 25 | use common_ext_ExtensionException; |
| 26 | use common_report_Report; |
| 27 | use oat\oatbox\service\exception\InvalidServiceManagerException; |
| 28 | use oat\oatbox\task\AbstractTaskAction; |
| 29 | use oat\oatbox\service\ServiceManager; |
| 30 | use oat\tao\model\import\ImporterNotFound; |
| 31 | use oat\tao\model\import\ImportersService; |
| 32 | use oat\tao\model\TaoOntology; |
| 33 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
| 34 | use oat\tao\model\taskQueue\Task\TaskInterface; |
| 35 | use oat\taoQtiTest\models\import\QtiTestImporter; |
| 36 | |
| 37 | /** |
| 38 | * Class ImportQtiTest |
| 39 | * @package oat\taoQtiTest\models\tasks |
| 40 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
| 41 | */ |
| 42 | class ImportQtiTest extends AbstractTaskAction implements \JsonSerializable |
| 43 | { |
| 44 | public const FILE_DIR = 'ImportQtiTestTask'; |
| 45 | public const PARAM_CLASS_URI = 'class_uri'; |
| 46 | public const PARAM_FILE = 'file'; |
| 47 | public const PARAM_ENABLE_GUARDIANS = 'enable_guardians'; |
| 48 | public const PARAM_ENABLE_VALIDATORS = 'enable_validators'; |
| 49 | public const PARAM_ITEM_MUST_EXIST = 'item_must_exist'; |
| 50 | public const PARAM_ITEM_MUST_BE_OVERWRITTEN = 'item_must_be_overwritten'; |
| 51 | public const PARAM_ITEM_CLASS_URI = 'item_class_uri'; |
| 52 | /** |
| 53 | * @deprecated Use oat\taoQtiTest\models\tasks\ImportQtiTest::PARAM_OVERWRITE_TEST_URI instead with the URI of the |
| 54 | * test to be replaced |
| 55 | */ |
| 56 | public const PARAM_OVERWRITE_TEST = 'overwrite_test'; |
| 57 | public const PARAM_OVERWRITE_TEST_URI = 'overwrite_test_uri'; |
| 58 | public const PARAM_PACKAGE_LABEL = 'package_label'; |
| 59 | |
| 60 | protected $service; |
| 61 | |
| 62 | /** |
| 63 | * @param $params |
| 64 | * @return common_report_Report |
| 65 | * @throws common_exception_Error |
| 66 | * @throws common_exception_MissingParameter |
| 67 | * @throws common_ext_ExtensionException |
| 68 | * @throws InvalidServiceManagerException |
| 69 | * @throws ImporterNotFound |
| 70 | */ |
| 71 | public function __invoke($params) |
| 72 | { |
| 73 | if (!isset($params[self::PARAM_FILE])) { |
| 74 | throw new common_exception_MissingParameter( |
| 75 | 'Missing parameter `' . self::PARAM_FILE . '` in ' . self::class |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | \common_ext_ExtensionsManager::singleton()->getExtensionById('taoQtiTest'); |
| 80 | |
| 81 | $file = $this->getFileReferenceSerializer()->unserializeFile($params['file']); |
| 82 | |
| 83 | /** @var ImportersService $importersService */ |
| 84 | $importersService = $this->getServiceManager()->get(ImportersService::SERVICE_ID); |
| 85 | |
| 86 | /** @var QtiTestImporter $importer */ |
| 87 | $importer = $importersService->getImporter(QtiTestImporter::IMPORTER_ID); |
| 88 | |
| 89 | $report = $importer->import( |
| 90 | $file, |
| 91 | $this->getClass($params), |
| 92 | $params[self::PARAM_ENABLE_GUARDIANS] ?? true, |
| 93 | $params[self::PARAM_ENABLE_VALIDATORS] ?? true, |
| 94 | $params[self::PARAM_ITEM_MUST_EXIST] ?? false, |
| 95 | $params[self::PARAM_ITEM_MUST_BE_OVERWRITTEN] ?? false, |
| 96 | $params[self::PARAM_OVERWRITE_TEST] ?? false, |
| 97 | $params[self::PARAM_ITEM_CLASS_URI] ?? null, |
| 98 | $params[self::PARAM_OVERWRITE_TEST_URI] ?? null, |
| 99 | $params[self::PARAM_PACKAGE_LABEL] ?? null, |
| 100 | ); |
| 101 | |
| 102 | return $report; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @return string |
| 107 | */ |
| 108 | public function jsonSerialize() |
| 109 | { |
| 110 | return __CLASS__; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Create task in queue |
| 115 | * @param array $packageFile uploaded file |
| 116 | * @param \core_kernel_classes_Class $class uploaded file |
| 117 | * @param bool $enableGuardians Flag that marks use or not metadata guardians during the import. |
| 118 | * @param bool $enableValidators Flag that marks use or not metadata validators during the import. |
| 119 | * @param bool $itemMustExist Flag to indicate that all items must exist in database (via metadata guardians) to |
| 120 | * make the test import successful. |
| 121 | * @param bool $itemMustBeOverwritten Flag to indicate that items found by metadata guardians will be overwritten. |
| 122 | * @return TaskInterface |
| 123 | */ |
| 124 | public static function createTask( |
| 125 | $packageFile, |
| 126 | \core_kernel_classes_Class $class, |
| 127 | $enableGuardians = true, |
| 128 | $enableValidators = true, |
| 129 | $itemMustExist = false, |
| 130 | $itemMustBeOverwritten = false, |
| 131 | bool $overwriteTest = false, |
| 132 | ?string $itemClassUri = null, |
| 133 | ?string $overwriteTestUri = null, |
| 134 | ?string $packageLabel = null |
| 135 | ) { |
| 136 | $action = new self(); |
| 137 | $action->setServiceLocator(ServiceManager::getServiceManager()); |
| 138 | |
| 139 | $fileUri = $action->saveFile($packageFile['tmp_name'], $packageFile['name']); |
| 140 | |
| 141 | /** @var QueueDispatcherInterface $queueDispatcher */ |
| 142 | $queueDispatcher = ServiceManager::getServiceManager()->get(QueueDispatcherInterface::SERVICE_ID); |
| 143 | |
| 144 | return $queueDispatcher->createTask( |
| 145 | $action, |
| 146 | [ |
| 147 | self::PARAM_FILE => $fileUri, |
| 148 | self::PARAM_CLASS_URI => $class->getUri(), |
| 149 | self::PARAM_ENABLE_GUARDIANS => $enableGuardians, |
| 150 | self::PARAM_ENABLE_VALIDATORS => $enableValidators, |
| 151 | self::PARAM_ITEM_MUST_EXIST => $itemMustExist, |
| 152 | self::PARAM_ITEM_MUST_BE_OVERWRITTEN => $itemMustBeOverwritten, |
| 153 | self::PARAM_OVERWRITE_TEST => $overwriteTest, |
| 154 | self::PARAM_ITEM_CLASS_URI => $itemClassUri, |
| 155 | self::PARAM_OVERWRITE_TEST_URI => $overwriteTestUri, |
| 156 | self::PARAM_PACKAGE_LABEL => $packageLabel, |
| 157 | ], |
| 158 | __('Import QTI TEST into "%s"', $class->getLabel()) |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * @param array $taskParams |
| 164 | * @return \core_kernel_classes_Class |
| 165 | */ |
| 166 | private function getClass(array $taskParams) |
| 167 | { |
| 168 | $class = null; |
| 169 | if (isset($taskParams[self::PARAM_CLASS_URI])) { |
| 170 | $class = new \core_kernel_classes_Class($taskParams[self::PARAM_CLASS_URI]); |
| 171 | } |
| 172 | if ($class === null || !$class->exists()) { |
| 173 | $class = new \core_kernel_classes_Class(TaoOntology::CLASS_URI_TEST); |
| 174 | } |
| 175 | return $class; |
| 176 | } |
| 177 | } |