Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
QtiPackageImport
0.00% covered (danger)
0.00%
0 / 50
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 getLabel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getForm
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 import
0.00% covered (danger)
0.00%
0 / 38
0.00% covered (danger)
0.00%
0 / 1
56
 getTaskParameters
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
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) 2013 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT);
19 *
20 *
21 */
22
23namespace oat\taoQtiItem\model\import;
24
25use oat\oatbox\event\EventManagerAwareTrait;
26use oat\oatbox\PhpSerializable;
27use oat\oatbox\PhpSerializeStateless;
28use oat\tao\model\featureFlag\FeatureFlagChecker;
29use oat\tao\model\import\ImportHandlerHelperTrait;
30use oat\tao\model\import\TaskParameterProviderInterface;
31use oat\taoQtiItem\model\event\QtiItemImportEvent;
32use oat\taoQtiItem\model\qti\ImportService;
33use oat\taoQtiItem\model\qti\exception\ExtractException;
34use oat\taoQtiItem\model\qti\exception\ParsingException;
35use oat\taoQtiTest\models\classes\metadata\MetadataLomService;
36use tao_models_classes_import_ImportHandler;
37use helpers_TimeOutHelper;
38use common_report_Report;
39use Exception;
40use Zend\ServiceManager\ServiceLocatorAwareInterface;
41
42/**
43 * Import handler for QTI packages
44 *
45 * @access  public
46 * @author  Joel Bout, <joel@taotesting.com>
47 * @package taoQTIItem
48 */
49class QtiPackageImport implements
50    tao_models_classes_import_ImportHandler,
51    PhpSerializable,
52    ServiceLocatorAwareInterface,
53    TaskParameterProviderInterface
54{
55    use PhpSerializeStateless;
56    use EventManagerAwareTrait;
57    use ImportHandlerHelperTrait {
58        getTaskParameters as getDefaultTaskParameters;
59    }
60
61    public const METADATA_IMPORT_ELEMENT_NAME = 'metadataImport';
62    public const DISABLED_ELEMENTS = 'disabledFields';
63
64    /**
65     * @see tao_models_classes_import_ImportHandler::getLabel()
66     */
67    public function getLabel()
68    {
69        return __('QTI/APIP Content Package');
70    }
71
72    /**
73     * @see tao_models_classes_import_ImportHandler::getForm()
74     */
75    public function getForm()
76    {
77        $form = new QtiPackageImportForm();
78        return $form->getForm();
79    }
80
81    /**
82     * @see tao_models_classes_import_ImportHandler::import()
83     * @param \core_kernel_classes_Class $class
84     * @param \tao_helpers_form_Form|array $form
85     * @param string|null $userId owner of the resource
86     * @return common_report_Report
87     * @throws \oat\oatbox\service\ServiceNotFoundException
88     */
89    public function import($class, $form, $userId = null)
90    {
91        try {
92            // for backward compatibility
93            $rollbackInfo = $form instanceof \tao_helpers_form_Form
94                ? (array) $form->getValue('rollback')
95                : (array) $form['rollback'];
96
97            $uploadedFile = $this->fetchUploadedFile($form);
98
99            //the zip extraction is a long process that can exced the 30s timeout
100            helpers_TimeOutHelper::setTimeOutLimit(helpers_TimeOutHelper::LONG);
101
102            $isImportMetadataEnabled = false;
103            if (isset($form[QtiPackageImportForm::METADATA_FORM_ELEMENT_NAME])) {
104                $isImportMetadataEnabled = (bool) $form[QtiPackageImportForm::METADATA_FORM_ELEMENT_NAME] === true;
105            }
106
107            $report = ImportService::singleton()->importQTIPACKFile(
108                $uploadedFile,
109                $class,
110                true,
111                in_array('error', $rollbackInfo),
112                in_array('warning', $rollbackInfo),
113                null,
114                null,
115                null,
116                null,
117                $isImportMetadataEnabled
118            );
119
120            helpers_TimeOutHelper::reset();
121
122            $this->getUploadService()->remove($uploadedFile);
123
124            if (common_report_Report::TYPE_SUCCESS == $report->getType()) {
125                $this->getEventManager()->trigger(new QtiItemImportEvent($report));
126            }
127        } catch (ExtractException $e) {
128            $report = common_report_Report::createFailure(
129                __('The ZIP archive containing the IMS QTI Item cannot be extracted.')
130            );
131        } catch (ParsingException $e) {
132            $report = common_report_Report::createFailure(
133                __('The ZIP archive does not contain an imsmanifest.xml file or is an invalid ZIP archive.')
134            );
135        } catch (Exception $e) {
136            $report = common_report_Report::createFailure(
137                // phpcs:disable Generic.Files.LineLength
138                __('An unexpected error occurred during the import of the IMS QTI Item Package. The system returned the following error: "%s"', $e->getMessage())
139                // phpcs:enable Generic.Files.LineLength
140            );
141        }
142
143        return $report;
144    }
145
146    /**
147     * Defines the task parameters to be stored for later use.
148     *
149     * @param \tao_helpers_form_Form $form
150     * @return array
151     */
152    public function getTaskParameters(\tao_helpers_form_Form $form)
153    {
154        return array_merge(
155            [
156                'rollback' => $form->getValue('rollback'),
157                QtiPackageImportForm::METADATA_FORM_ELEMENT_NAME => $form->getValue(
158                    QtiPackageImportForm::METADATA_FORM_ELEMENT_NAME
159                ) ?? null,
160            ],
161            $this->getDefaultTaskParameters($form)
162        );
163    }
164}