Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Publish | |
0.00% |
0 / 31 |
|
0.00% |
0 / 5 |
72 | |
0.00% |
0 / 1 |
| getEventManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| index | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| publish | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
20 | |||
| getTestService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDeliveryFactory | |
0.00% |
0 / 1 |
|
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) 2018 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoDeliveryRdf\controller; |
| 23 | |
| 24 | use common_exception_UserReadableException as UserReadableException; |
| 25 | use Exception; |
| 26 | use oat\generis\model\OntologyRdfs; |
| 27 | use oat\oatbox\event\EventManager; |
| 28 | use oat\tao\model\taskQueue\TaskLogActionTrait; |
| 29 | use oat\taoDeliveryRdf\model\DeliveryFactory; |
| 30 | use oat\taoDeliveryRdf\model\tasks\CompileDelivery; |
| 31 | use oat\taoDeliveryRdf\model\DeliveryAssemblyService; |
| 32 | use RuntimeException; |
| 33 | use tao_helpers_Uri; |
| 34 | use taoTests_models_classes_TestsService as TestsService; |
| 35 | |
| 36 | /** |
| 37 | * Controller to publish delivery by test |
| 38 | * |
| 39 | * @package taoDeliveryRdf |
| 40 | */ |
| 41 | class Publish extends \tao_actions_SaSModule |
| 42 | { |
| 43 | use TaskLogActionTrait; |
| 44 | |
| 45 | /** |
| 46 | * @return EventManager |
| 47 | */ |
| 48 | protected function getEventManager() |
| 49 | { |
| 50 | return $this->getServiceLocator()->get(EventManager::SERVICE_ID); |
| 51 | } |
| 52 | |
| 53 | public function index() |
| 54 | { |
| 55 | $testUri = tao_helpers_Uri::decode($this->getRequestParameter('uri')); |
| 56 | $test = $this->getResource($testUri); |
| 57 | $this->setData('label', $test->getLabel()); |
| 58 | $this->setData('rootClassUri', DeliveryAssemblyService::CLASS_URI); |
| 59 | $this->setData('testUri', $testUri); |
| 60 | $this->setView('Publish/index.tpl'); |
| 61 | } |
| 62 | |
| 63 | public function publish() |
| 64 | { |
| 65 | try { |
| 66 | $parsedBody = $this->getPsrRequest()->getParsedBody(); |
| 67 | $testUri = $parsedBody['testUri'] ?? null; |
| 68 | $classUri = $parsedBody['classUri'] ?? null; |
| 69 | $test = $this->getResource($testUri); |
| 70 | |
| 71 | $testService = $this->getTestService(); |
| 72 | $testItems = $testService->getTestItems($test); |
| 73 | if (empty($testItems)) { |
| 74 | throw new RuntimeException( |
| 75 | __('The test "%s" does not contain any items and cannot be published.', $test->getLabel()) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | $deliveryClass = $this->getClass($classUri); |
| 80 | $deliveryFactoryResources = $this->getDeliveryFactory(); |
| 81 | $initialProperties = $deliveryFactoryResources->getInitialPropertiesFromArray([ |
| 82 | OntologyRdfs::RDFS_LABEL => 'new delivery', |
| 83 | ]); |
| 84 | |
| 85 | return $this->returnTaskJson(CompileDelivery::createTask($test, $deliveryClass, $initialProperties)); |
| 86 | } catch (Exception $e) { |
| 87 | return $this->returnJson([ |
| 88 | 'success' => false, |
| 89 | 'errorMsg' => $e instanceof UserReadableException ? $e->getUserMessage() : $e->getMessage(), |
| 90 | 'errorCode' => $e->getCode(), |
| 91 | ]); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * @return TestsService |
| 97 | */ |
| 98 | private function getTestService() |
| 99 | { |
| 100 | return $this->getServiceLocator()->get(TestsService::class); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * @return DeliveryFactory |
| 105 | */ |
| 106 | private function getDeliveryFactory() |
| 107 | { |
| 108 | return $this->getServiceLocator()->get(DeliveryFactory::SERVICE_ID); |
| 109 | ; |
| 110 | } |
| 111 | } |