Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
| QtiPackageExportHandler | |
0.00% |
0 / 52 |
|
0.00% |
0 / 8 |
506 | |
0.00% |
0 / 1 |
| getLabel | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getExportForm | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| export | |
0.00% |
0 / 37 |
|
0.00% |
0 / 1 |
182 | |||
| getFormData | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| createExporter | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getMetadataExporter | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| 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 (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_Error; |
| 26 | use common_report_Report as Report; |
| 27 | use core_kernel_classes_Class; |
| 28 | use core_kernel_classes_Resource; |
| 29 | use DomDocument; |
| 30 | use Exception; |
| 31 | use oat\oatbox\event\EventManagerAwareTrait; |
| 32 | use oat\oatbox\filesystem\FilesystemException; |
| 33 | use oat\oatbox\PhpSerializable; |
| 34 | use oat\oatbox\PhpSerializeStateless; |
| 35 | use oat\oatbox\service\ServiceManager; |
| 36 | use oat\tao\model\resources\SecureResourceServiceInterface; |
| 37 | use oat\taoQtiItem\model\event\QtiItemExportEvent; |
| 38 | use oat\taoQtiItem\model\ItemModel; |
| 39 | use oat\taoQtiItem\model\qti\metadata\exporter\MetadataExporter; |
| 40 | use oat\taoQtiItem\model\qti\metadata\MetadataService; |
| 41 | use tao_helpers_File; |
| 42 | use tao_models_classes_export_ExportHandler; |
| 43 | use taoItems_models_classes_ItemsService; |
| 44 | use ZipArchive; |
| 45 | |
| 46 | /** |
| 47 | * Short description of class oat\taoQtiItem\model\ItemModel |
| 48 | * |
| 49 | * @access public |
| 50 | * @author Joel Bout, <joel@taotesting.com> |
| 51 | * @package taoQTI |
| 52 | */ |
| 53 | class QtiPackageExportHandler implements tao_models_classes_export_ExportHandler, PhpSerializable |
| 54 | { |
| 55 | use PhpSerializeStateless; |
| 56 | use EventManagerAwareTrait; |
| 57 | |
| 58 | /** |
| 59 | * @var MetadataExporter Service to export metadata to IMSManifest |
| 60 | */ |
| 61 | protected $metadataExporter; |
| 62 | |
| 63 | /** |
| 64 | * @return string |
| 65 | */ |
| 66 | public function getLabel() |
| 67 | { |
| 68 | return __('QTI Package 2.1'); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param core_kernel_classes_Resource $resource |
| 73 | * @return \tao_helpers_form_Form |
| 74 | */ |
| 75 | public function getExportForm(core_kernel_classes_Resource $resource) |
| 76 | { |
| 77 | $formData = $this->getFormData($resource); |
| 78 | |
| 79 | return (new Qti21ExportForm($formData))->getForm(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * @param array $formValues |
| 84 | * @param string $destination |
| 85 | * @return Report |
| 86 | * @throws common_exception_Error |
| 87 | */ |
| 88 | public function export($formValues, $destination) |
| 89 | { |
| 90 | if (!isset($formValues['filename'])) { |
| 91 | return Report::createFailure('Missing filename for export using ' . __CLASS__); |
| 92 | } |
| 93 | |
| 94 | if (!isset($formValues['instances'])) { |
| 95 | return Report::createFailure('No instances selected for export using ' . __CLASS__); |
| 96 | } |
| 97 | |
| 98 | $report = Report::createSuccess(); |
| 99 | |
| 100 | if (count($formValues['instances']) > 0) { |
| 101 | $itemService = taoItems_models_classes_ItemsService::singleton(); |
| 102 | |
| 103 | $fileName = $formValues['filename'] . '_' . time() . '.zip'; |
| 104 | $path = tao_helpers_File::concat([$destination, $fileName]); |
| 105 | if (!tao_helpers_File::securityCheck($path, true)) { |
| 106 | throw new Exception('Unauthorized file name'); |
| 107 | } |
| 108 | |
| 109 | $zipArchive = new ZipArchive(); |
| 110 | if ($zipArchive->open($path, ZipArchive::CREATE) !== true) { |
| 111 | throw new Exception('Unable to create archive at ' . $path); |
| 112 | } |
| 113 | |
| 114 | $manifest = null; |
| 115 | foreach ($formValues['instances'] as $instance) { |
| 116 | $item = new core_kernel_classes_Resource($instance); |
| 117 | if ($itemService->hasItemModel($item, [ItemModel::MODEL_URI])) { |
| 118 | $exporter = $this->createExporter($item, $zipArchive, $manifest); |
| 119 | try { |
| 120 | $subReport = $exporter->export(); |
| 121 | $manifest = $exporter->getManifest(); |
| 122 | |
| 123 | $report->add($subReport); |
| 124 | } catch (FilesystemException $e) { |
| 125 | $report->add(Report::createFailure(__('Item "%s" has no xml document', $item->getLabel()))); |
| 126 | } catch (Exception $e) { |
| 127 | $report->add( |
| 128 | Report::createFailure(__('Error to export item %s: %s', $instance, $e->getMessage())) |
| 129 | ); |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | $zipArchive->close(); |
| 135 | $report->setData($path); |
| 136 | $report->setMessage(__('Resource(s) successfully exported.')); |
| 137 | |
| 138 | $subjectUri = isset($formValues['uri']) ? $formValues['uri'] : $formValues['classUri']; |
| 139 | |
| 140 | if (!$report->containsError() && $subjectUri) { |
| 141 | $this->getEventManager()->trigger( |
| 142 | new QtiItemExportEvent(new core_kernel_classes_Resource($subjectUri)) |
| 143 | ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | return $report; |
| 148 | } |
| 149 | |
| 150 | protected function getFormData(core_kernel_classes_Resource $resource): array |
| 151 | { |
| 152 | $formData = []; |
| 153 | |
| 154 | if ($resource instanceof core_kernel_classes_Class) { |
| 155 | $formData['items'] = $this->getResourceService()->getAllChildren($resource); |
| 156 | $formData['file_name'] = $resource->getLabel(); |
| 157 | } else { |
| 158 | $formData['instance'] = $resource; |
| 159 | } |
| 160 | |
| 161 | return $formData; |
| 162 | } |
| 163 | |
| 164 | protected function createExporter($item, ZipArchive $zipArchive, DOMDocument $manifest = null) |
| 165 | { |
| 166 | return new QTIPackedItemExporter($item, $zipArchive, $manifest); |
| 167 | } |
| 168 | |
| 169 | /** |
| 170 | * Get the service to export Metadata |
| 171 | * |
| 172 | * @return MetadataExporter |
| 173 | */ |
| 174 | protected function getMetadataExporter() |
| 175 | { |
| 176 | if (!$this->metadataExporter) { |
| 177 | $this->metadataExporter = $this->getServiceManager()->get(MetadataService::SERVICE_ID)->getExporter(); |
| 178 | } |
| 179 | |
| 180 | return $this->metadataExporter; |
| 181 | } |
| 182 | |
| 183 | protected function getResourceService(): SecureResourceServiceInterface |
| 184 | { |
| 185 | return $this->getServiceManager()->get(SecureResourceServiceInterface::SERVICE_ID); |
| 186 | } |
| 187 | |
| 188 | protected function getServiceManager() |
| 189 | { |
| 190 | return ServiceManager::getServiceManager(); |
| 191 | } |
| 192 | } |