Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
ImportQtiItem | |
0.00% |
0 / 41 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
__invoke | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
30 | |||
jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
createTask | |
0.00% |
0 / 17 |
|
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) 2017 (original work) Open Assessment Technologies SA; |
19 | * |
20 | * |
21 | */ |
22 | |
23 | namespace oat\taoQtiItem\model\tasks; |
24 | |
25 | use oat\oatbox\task\AbstractTaskAction; |
26 | use oat\tao\model\TaoOntology; |
27 | use oat\tao\model\taskQueue\QueueDispatcherInterface; |
28 | use oat\tao\model\taskQueue\Task\TaskInterface; |
29 | use oat\taoQtiItem\model\qti\ImportService; |
30 | use Zend\ServiceManager\ServiceLocatorInterface; |
31 | |
32 | /** |
33 | * Class ImportQtiItem |
34 | * @package oat\taoQtiTest\models\tasks |
35 | * @author Aleh Hutnikau, <hutnikau@1pt.com> |
36 | */ |
37 | class ImportQtiItem extends AbstractTaskAction implements \JsonSerializable |
38 | { |
39 | public const FILE_DIR = 'ImportQtiItemTask'; |
40 | public const PARAM_CLASS_URI = 'class_uri'; |
41 | public const PARAM_FILE = 'file'; |
42 | public const PARAM_GUARDIANS = 'enableMetadataGuardians'; |
43 | public const PARAM_VALIDATORS = 'enableMetadataValidators'; |
44 | public const PARAM_ITEM_MUST_EXIST = 'itemMustExist'; |
45 | public const PARAM_ITEM_MUST_BE_OVERWRITTEN = 'itemMustBeOverwritten'; |
46 | public const PARAM_ITEM_METADATA = 'importMetadata'; |
47 | |
48 | protected $service; |
49 | |
50 | /** |
51 | * @param $params |
52 | * @throws \common_exception_MissingParameter |
53 | * @return \common_report_Report |
54 | */ |
55 | public function __invoke($params) |
56 | { |
57 | if (!isset($params[self::PARAM_FILE])) { |
58 | throw new \common_exception_MissingParameter( |
59 | 'Missing parameter `' . self::PARAM_FILE . '` in ' . self::class |
60 | ); |
61 | } |
62 | |
63 | $file = $this->getFileReferenceSerializer()->unserializeFile($params['file']); |
64 | |
65 | return ImportService::singleton()->importQTIPACKFile( |
66 | $file, |
67 | $this->getClass($params), |
68 | true, |
69 | true, |
70 | true, |
71 | // Continue to support old tasks in the queue |
72 | (isset($params[self::PARAM_GUARDIANS])) ? $params[self::PARAM_GUARDIANS] : true, |
73 | (isset($params[self::PARAM_VALIDATORS])) ? $params[self::PARAM_VALIDATORS] : true, |
74 | (isset($params[self::PARAM_ITEM_MUST_EXIST])) ? $params[self::PARAM_ITEM_MUST_EXIST] : false, |
75 | $params[self::PARAM_ITEM_MUST_BE_OVERWRITTEN] ?? false, |
76 | $params[self::PARAM_ITEM_METADATA] ?? false |
77 | ); |
78 | } |
79 | |
80 | /** |
81 | * @return string |
82 | */ |
83 | public function jsonSerialize() |
84 | { |
85 | return __CLASS__; |
86 | } |
87 | |
88 | /** |
89 | * Create task in queue |
90 | * |
91 | * @param string $packageFile uploaded file path |
92 | * @param \core_kernel_classes_Class $class uploaded file |
93 | * @param ServiceLocatorInterface $serviceManager |
94 | * @param boolean $enableMetadataGuardians |
95 | * @param boolean $enableMetadataValidators |
96 | * @param boolean $itemMustExist |
97 | * @param boolean $itemMustBeOverwritten |
98 | * @return TaskInterface |
99 | */ |
100 | public static function createTask( |
101 | $packageFile, |
102 | \core_kernel_classes_Class $class, |
103 | ServiceLocatorInterface $serviceManager, |
104 | $enableMetadataGuardians = true, |
105 | $enableMetadataValidators = true, |
106 | $itemMustExist = false, |
107 | $itemMustBeOverwritten = false, |
108 | $itemMetadata = false |
109 | ) { |
110 | $action = new self(); |
111 | $action->setServiceLocator($serviceManager); |
112 | |
113 | $fileUri = $action->saveFile($packageFile, basename($packageFile)); |
114 | |
115 | /** @var QueueDispatcherInterface $queueDispatcher */ |
116 | $queueDispatcher = $serviceManager->get(QueueDispatcherInterface::SERVICE_ID); |
117 | |
118 | return $queueDispatcher->createTask( |
119 | $action, |
120 | [ |
121 | self::PARAM_FILE => $fileUri, |
122 | self::PARAM_CLASS_URI => $class->getUri(), |
123 | self::PARAM_GUARDIANS => $enableMetadataGuardians, |
124 | self::PARAM_VALIDATORS => $enableMetadataValidators, |
125 | self::PARAM_ITEM_MUST_EXIST => $itemMustExist, |
126 | self::PARAM_ITEM_MUST_BE_OVERWRITTEN => $itemMustBeOverwritten, |
127 | self::PARAM_ITEM_METADATA => $itemMetadata |
128 | ], |
129 | __('Import QTI ITEM into "%s"', $class->getLabel()) |
130 | ); |
131 | } |
132 | |
133 | /** |
134 | * @param array $taskParams |
135 | * @return \core_kernel_classes_Class |
136 | */ |
137 | private function getClass(array $taskParams) |
138 | { |
139 | $class = null; |
140 | if (isset($taskParams[self::PARAM_CLASS_URI])) { |
141 | $class = new \core_kernel_classes_Class($taskParams[self::PARAM_CLASS_URI]); |
142 | } |
143 | if ($class === null || !$class->exists()) { |
144 | $class = new \core_kernel_classes_Class(TaoOntology::ITEM_CLASS_URI); |
145 | } |
146 | return $class; |
147 | } |
148 | } |