Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
27 / 27 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| QtiPackageExporter | |
100.00% |
27 / 27 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| exportDeliveryQtiPackage | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
4 | |||
| exportQtiTestPackageToFile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| moveFileToSharedFileSystem | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| 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) 2020-2022 (original work) Open Assessment Technologies SA; |
| 19 | */ |
| 20 | |
| 21 | declare(strict_types=1); |
| 22 | |
| 23 | namespace oat\taoQtiTest\helpers; |
| 24 | |
| 25 | use common_Exception; |
| 26 | use common_exception_Error; |
| 27 | use oat\oatbox\filesystem\File; |
| 28 | use oat\oatbox\filesystem\FileSystemService; |
| 29 | use oat\oatbox\reporting\Report; |
| 30 | use oat\oatbox\reporting\ReportInterface; |
| 31 | use oat\tao\helpers\FileHelperService; |
| 32 | use oat\tao\model\service\InjectionAwareService; |
| 33 | use oat\taoQtiTest\models\export\Formats\Package2p2\TestPackageExport; |
| 34 | |
| 35 | class QtiPackageExporter extends InjectionAwareService |
| 36 | { |
| 37 | public const SERVICE_ID = 'taoQtiTest/QtiPackageExporter'; |
| 38 | public const QTI_PACKAGE_FILENAME = 'qti_test_export'; |
| 39 | |
| 40 | private TestPackageExport $testExporter; |
| 41 | |
| 42 | private FileSystemService $fileSystemService; |
| 43 | |
| 44 | private FileHelperService $fileHelperService; |
| 45 | |
| 46 | public function __construct( |
| 47 | TestPackageExport $testExporter, |
| 48 | FileSystemService $fileSystemService, |
| 49 | FileHelperService $fileHelperService |
| 50 | ) { |
| 51 | $this->testExporter = $testExporter; |
| 52 | $this->fileSystemService = $fileSystemService; |
| 53 | $this->fileHelperService = $fileHelperService; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * @throws common_Exception |
| 58 | * @throws common_exception_Error |
| 59 | */ |
| 60 | public function exportDeliveryQtiPackage(string $testUri): Report |
| 61 | { |
| 62 | $exportReport = $this->testExporter->export( |
| 63 | [ |
| 64 | 'filename' => self::QTI_PACKAGE_FILENAME, |
| 65 | 'instances' => $testUri, |
| 66 | 'uri' => $testUri |
| 67 | ], |
| 68 | $this->fileHelperService->createTempDir() |
| 69 | ); |
| 70 | |
| 71 | if ($exportReport->getType() === ReportInterface::TYPE_ERROR) { |
| 72 | throw new common_Exception('QTI Test package export failed.'); |
| 73 | } |
| 74 | |
| 75 | $reportData = $exportReport->getData(); |
| 76 | |
| 77 | if (!isset($reportData['path']) || !is_string($reportData['path'])) { |
| 78 | throw new common_Exception( |
| 79 | 'Export report does not contain path to exported QTI package: ' . json_encode($reportData) |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | return $exportReport; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @throws common_Exception |
| 88 | */ |
| 89 | public function exportQtiTestPackageToFile(string $testUri, string $fileSystemId, string $filePath): File |
| 90 | { |
| 91 | $result = $this->exportDeliveryQtiPackage($testUri)->getData(); |
| 92 | |
| 93 | return $this->moveFileToSharedFileSystem($result['path'], $fileSystemId, $filePath); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * @throws common_Exception |
| 98 | */ |
| 99 | private function moveFileToSharedFileSystem( |
| 100 | string $sourceFilePath, |
| 101 | string $fileSystemId, |
| 102 | string $destinationFilePath |
| 103 | ): File { |
| 104 | $source = $this->fileHelperService->readFile($sourceFilePath); |
| 105 | |
| 106 | $file = $this->fileSystemService->getDirectory($fileSystemId)->getFile($destinationFilePath); |
| 107 | $file->put($source); |
| 108 | |
| 109 | $this->fileHelperService->closeFile($source); |
| 110 | $this->fileHelperService->removeFile($sourceFilePath); |
| 111 | |
| 112 | return $file; |
| 113 | } |
| 114 | } |