Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ItemMetadataByClassExportHandler | |
0.00% |
0 / 34 |
|
0.00% |
0 / 6 |
240 | |
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 / 16 |
|
0.00% |
0 / 1 |
42 | |||
| getInstances | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| getClassToExport | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 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) 2016 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | namespace oat\taoQtiItem\model\Export; |
| 23 | |
| 24 | use common_report_Report as Report; |
| 25 | use core_kernel_classes_Resource; |
| 26 | use oat\generis\model\OntologyAwareTrait; |
| 27 | use oat\oatbox\event\EventManagerAwareTrait; |
| 28 | use oat\oatbox\service\ServiceManager; |
| 29 | use oat\taoQtiItem\model\event\QtiItemMetadataExportEvent; |
| 30 | use oat\taoQtiItem\model\flyExporter\extractor\ExtractorException; |
| 31 | use oat\taoQtiItem\model\flyExporter\simpleExporter\ItemExporter; |
| 32 | use oat\taoQtiItem\model\flyExporter\simpleExporter\SimpleExporter; |
| 33 | use oat\taoQtiItem\model\ItemModel; |
| 34 | use oat\oatbox\PhpSerializable; |
| 35 | use oat\oatbox\PhpSerializeStateless; |
| 36 | |
| 37 | class ItemMetadataByClassExportHandler implements \tao_models_classes_export_ExportHandler, PhpSerializable |
| 38 | { |
| 39 | use OntologyAwareTrait; |
| 40 | use PhpSerializeStateless; |
| 41 | use EventManagerAwareTrait; |
| 42 | |
| 43 | /** |
| 44 | * Get label form |
| 45 | * |
| 46 | * @return string |
| 47 | */ |
| 48 | public function getLabel() |
| 49 | { |
| 50 | return __('QTI Metadata'); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Create MetadataExporterForm with class uri |
| 55 | * |
| 56 | * @param core_kernel_classes_Resource $resource |
| 57 | * @return \tao_helpers_form_Form |
| 58 | */ |
| 59 | public function getExportForm(core_kernel_classes_Resource $resource) |
| 60 | { |
| 61 | if ($resource instanceof \core_kernel_classes_Class) { |
| 62 | $formData = ['class' => $resource]; |
| 63 | } else { |
| 64 | $formData = ['class' => $this->getClass($resource)]; |
| 65 | } |
| 66 | $form = new MetadataExporterForm($formData); |
| 67 | |
| 68 | return $form->getForm(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * @param array $formValues |
| 73 | * @param string $destination |
| 74 | * @return Report |
| 75 | * @throws \common_exception_BadRequest |
| 76 | */ |
| 77 | public function export($formValues, $destination) |
| 78 | { |
| 79 | $report = Report::createSuccess(); |
| 80 | |
| 81 | if (isset($formValues['filename']) && isset($formValues['classUri'])) { |
| 82 | $classToExport = $this->getClassToExport($formValues['classUri']); |
| 83 | |
| 84 | if ($classToExport->exists()) { |
| 85 | try { |
| 86 | $fileName = $formValues['filename'] . '_' . time() . '.csv'; |
| 87 | |
| 88 | if (!\tao_helpers_File::securityCheck($fileName, true)) { |
| 89 | throw new \Exception('Unauthorized file name'); |
| 90 | } |
| 91 | |
| 92 | /** @var ItemExporter $exporterService */ |
| 93 | $exporterService = $this->getServiceManager()->get(SimpleExporter::SERVICE_ID); |
| 94 | $exporterService->setOption(ItemExporter::OPTION_FILE_LOCATION, $fileName); |
| 95 | |
| 96 | $filePath = $exporterService->export($this->getInstances($classToExport)); |
| 97 | |
| 98 | $report->setData($filePath); |
| 99 | $report->setMessage(__('Item metadata successfully exported.')); |
| 100 | |
| 101 | $this->getEventManager()->trigger(new QtiItemMetadataExportEvent($classToExport)); |
| 102 | } catch (ExtractorException $e) { |
| 103 | $report = Report::createFailure('Selected object does not have any item to export.'); |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return $report; |
| 109 | } |
| 110 | |
| 111 | protected function getInstances($classToExport) |
| 112 | { |
| 113 | $instances = []; |
| 114 | $itemService = \taoItems_models_classes_ItemsService::singleton(); |
| 115 | foreach ($classToExport->getInstances(true) as $item) { |
| 116 | if ($itemService->hasItemModel($item, [ItemModel::MODEL_URI])) { |
| 117 | $instances[] = $item; |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | return $instances; |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Get class to export, from resource uri or class uri |
| 126 | * If parameter is null, check id http parameter |
| 127 | * |
| 128 | * @param null $uri |
| 129 | * @return \core_kernel_classes_Class|mixed |
| 130 | * @throws \common_exception_BadRequest |
| 131 | */ |
| 132 | protected function getClassToExport($uri) |
| 133 | { |
| 134 | $resource = $this->getResource($uri); |
| 135 | |
| 136 | if ($resource->isClass()) { |
| 137 | $classToExport = $this->getClass($uri); |
| 138 | } else { |
| 139 | $classToExport = reset($resource->getTypes()); |
| 140 | } |
| 141 | |
| 142 | return $classToExport; |
| 143 | } |
| 144 | |
| 145 | /** |
| 146 | * @return ServiceManager |
| 147 | */ |
| 148 | protected function getServiceManager() |
| 149 | { |
| 150 | return ServiceManager::getServiceManager(); |
| 151 | } |
| 152 | } |