Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 41 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
ApipPackageExportHandler | |
0.00% |
0 / 41 |
|
0.00% |
0 / 5 |
272 | |
0.00% |
0 / 1 |
getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getExportForm | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
export | |
0.00% |
0 / 33 |
|
0.00% |
0 / 1 |
132 | |||
getResourceService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getServiceManager | |
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) 2014-2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | * |
21 | */ |
22 | |
23 | namespace oat\taoQtiItem\model\Export; |
24 | |
25 | use common_Exception; |
26 | use common_exception_Error; |
27 | use common_report_Report as Report; |
28 | use oat\oatbox\event\EventManagerAwareTrait; |
29 | use oat\oatbox\PhpSerializable; |
30 | use oat\oatbox\PhpSerializeStateless; |
31 | use oat\oatbox\service\ServiceManager; |
32 | use oat\tao\model\resources\SecureResourceServiceInterface; |
33 | use oat\taoQtiItem\model\event\QtiItemExportEvent; |
34 | use oat\taoQtiItem\model\ItemModel; |
35 | use tao_helpers_form_Form; |
36 | use tao_models_classes_export_ExportHandler; |
37 | use core_kernel_classes_Resource; |
38 | use core_kernel_classes_Class; |
39 | use taoItems_models_classes_ItemsService; |
40 | use tao_helpers_File; |
41 | use Exception; |
42 | use ZipArchive; |
43 | use common_Logger; |
44 | |
45 | /** |
46 | * Apip Package Export Handler. |
47 | * |
48 | * @author Jérôme Bogaerts <jerome@taotesting.com> |
49 | */ |
50 | class ApipPackageExportHandler implements tao_models_classes_export_ExportHandler, PhpSerializable |
51 | { |
52 | use PhpSerializeStateless; |
53 | use EventManagerAwareTrait; |
54 | |
55 | /** |
56 | * @return string |
57 | */ |
58 | public function getLabel() |
59 | { |
60 | return __('APIP Content Package'); |
61 | } |
62 | |
63 | /** |
64 | * @param core_kernel_classes_Resource $resource |
65 | * |
66 | * @return tao_helpers_form_Form |
67 | * @throws common_Exception |
68 | * @throws common_exception_Error |
69 | */ |
70 | public function getExportForm(core_kernel_classes_Resource $resource) |
71 | { |
72 | if ($resource instanceof core_kernel_classes_Class) { |
73 | $formData['items'] = $this->getResourceService()->getAllChildren($resource); |
74 | $formData['file_name'] = $resource->getLabel(); |
75 | } else { |
76 | $formData = ['instance' => $resource]; |
77 | } |
78 | |
79 | return (new ApipExportForm($formData))->getForm(); |
80 | } |
81 | |
82 | /** |
83 | * @param array $formValues |
84 | * @param string $destination |
85 | * @return string |
86 | * @throws common_exception_Error |
87 | */ |
88 | public function export($formValues, $destination) |
89 | { |
90 | $report = Report::createSuccess(); |
91 | |
92 | if (isset($formValues['filename'])) { |
93 | $instances = $formValues['instances']; |
94 | |
95 | if (count($instances) > 0) { |
96 | $itemService = taoItems_models_classes_ItemsService::singleton(); |
97 | |
98 | $fileName = $formValues['filename'] . '_' . time() . '.zip'; |
99 | $path = tao_helpers_File::concat([$destination, $fileName]); |
100 | if (!tao_helpers_File::securityCheck($path, true)) { |
101 | throw new Exception('Unauthorized file name'); |
102 | } |
103 | |
104 | $zipArchive = new ZipArchive(); |
105 | if ($zipArchive->open($path, ZipArchive::CREATE) !== true) { |
106 | throw new Exception('Unable to create archive at ' . $path); |
107 | } |
108 | |
109 | $manifest = null; |
110 | foreach ($instances as $instance) { |
111 | $item = new core_kernel_classes_Resource($instance); |
112 | if ($itemService->hasItemModel($item, [ItemModel::MODEL_URI])) { |
113 | $exporter = new QTIPackedItemExporter($item, $zipArchive, $manifest); |
114 | |
115 | try { |
116 | $exporter->export(['apip' => true]); |
117 | $manifest = $exporter->getManifest(); |
118 | } catch (\Exception $e) { |
119 | $report = Report::createFailure( |
120 | 'Error to export item "' . $instance . '": ' . $e->getMessage() |
121 | ); |
122 | } |
123 | } |
124 | } |
125 | |
126 | $zipArchive->close(); |
127 | |
128 | $subjectUri = isset($formValues['uri']) ? $formValues['uri'] : $formValues['classUri']; |
129 | |
130 | if ($path && $subjectUri) { |
131 | $report->setData($path); |
132 | $report->setMessage(__('Apip Package successfully exported.')); |
133 | |
134 | $this->getEventManager()->trigger( |
135 | new QtiItemExportEvent(new core_kernel_classes_Resource($subjectUri)) |
136 | ); |
137 | } |
138 | } |
139 | } else { |
140 | $report = Report::createFailure('Missing filename for export using ' . __CLASS__); |
141 | } |
142 | |
143 | return $report; |
144 | } |
145 | |
146 | protected function getResourceService(): SecureResourceServiceInterface |
147 | { |
148 | return $this->getServiceManager()->get(SecureResourceServiceInterface::SERVICE_ID); |
149 | } |
150 | |
151 | protected function getServiceManager() |
152 | { |
153 | return ServiceManager::getServiceManager(); |
154 | } |
155 | } |