Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExportForm
0.00% covered (danger)
0.00%
0 / 65
0.00% covered (danger)
0.00%
0 / 8
272
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 initForm
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
2
 initElements
0.00% covered (danger)
0.00%
0 / 34
0.00% covered (danger)
0.00%
0 / 1
56
 getInstanceOptions
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
12
 getFileName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTestService
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setTestModel
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getTestModel
0.00% covered (danger)
0.00%
0 / 1
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)
19 * 2008-2010 (orig. work) Deutsche Institut für Internationale Pädagogische Forschung (under the project TAO-TRANSFER);
20 * 2009-2012 (update and modification) Public Research Centre Henri Tudor (under the project TAO-SUSTAIN & TAO-DEV);
21 * 2012-2022 (further updates) Open Assessment Technologies SA;
22 */
23
24declare(strict_types=1);
25
26namespace oat\taoQtiTest\models\Export;
27
28use common_Exception;
29use core_kernel_classes_Class;
30use core_kernel_classes_Resource as Resource;
31use Exception;
32use Laminas\ServiceManager\ServiceLocatorAwareTrait;
33use oat\taoTests\models\MissingTestmodelException;
34use tao_helpers_Display as Display;
35use tao_helpers_form_FormContainer as FormContainer;
36use tao_helpers_form_FormFactory as FormFactory;
37use tao_helpers_form_xhtml_Form as Form;
38use tao_helpers_form_xhtml_TagWrapper as TagWrapper;
39use tao_helpers_Uri as Uri;
40use taoQtiTest_models_classes_QtiTestService as QtiTestService;
41use taoTests_models_classes_TestsService as TestsService;
42
43class ExportForm extends FormContainer
44{
45    use ServiceLocatorAwareTrait;
46
47    private static string $title;
48
49    private Resource $testModel;
50
51    public function __construct(array $data = [], array $options = [], string $title = '')
52    {
53        static::$title = $title;
54
55        parent::__construct($data, $options);
56    }
57
58    /**
59     * @access public
60     * @return void
61     * @throws common_Exception
62     * @throws Exception
63     * @author Joel Bout, <joel.bout@tudor.lu>
64     */
65    public function initForm()
66    {
67        $this->form = new Form('export');
68
69        $this->form->setDecorators([
70            'element' => new TagWrapper(['tag' => 'div']),
71            'group' => new TagWrapper(['tag' => 'div', 'cssClass' => 'form-group']),
72            'error' => new TagWrapper(['tag' => 'div', 'cssClass' => 'form-error ui-state-error ui-corner-all']),
73            'actions-bottom' => new TagWrapper(['tag' => 'div', 'cssClass' => 'form-toolbar']),
74            'actions-top' => new TagWrapper(['tag' => 'div', 'cssClass' => 'form-toolbar'])
75        ]);
76
77        $exportElement = FormFactory::getElement('export', 'Free');
78        $exportElement->setValue(
79            sprintf(
80                '<a href="#" class="form-submitter btn-success small"><span class="icon-export"></span>%s</a>',
81                __('Export')
82            )
83        );
84
85        $this->form->setActions([$exportElement]);
86    }
87
88    /**
89     * @throws common_Exception
90     * @throws MissingTestmodelException
91     *
92     * @author Joel Bout, <joel.bout@tudor.lu>
93     */
94    public function initElements(): void
95    {
96        $testService = $this->getTestService();
97        $testModel = $this->getTestModel();
98
99        $fileName = '';
100        $options = [];
101        if (isset($this->data['items'])) {
102            $fileName = $this->getFileName($this->data['file_name']);
103            $options = $this->getInstanceOptions(...array_values($this->data['items']));
104        } elseif (isset($this->data['instance'])) {
105            $test = $this->data['instance'];
106            if (
107                $test instanceof Resource
108                && $testModel->equals($testService->getTestModel($test))
109            ) {
110                $fileName = $this->getFileName($test->getLabel());
111                $options = $this->getInstanceOptions($test);
112            }
113        } else {
114            $class = $this->data['class'] ?? $testService->getRootClass();
115
116            if ($class instanceof core_kernel_classes_Class) {
117                $fileName = $this->getFileName($class->getLabel());
118                $options = $this->getInstanceOptions(...$class->getInstances());
119            }
120        }
121
122        $fileNameElement = FormFactory::getElement('filename', 'Textbox');
123        $fileNameElement->setDescription(__('File name'));
124        $fileNameElement->setValue($fileName);
125        $fileNameElement->setUnit('.zip');
126        $fileNameElement->addValidator(FormFactory::getValidator('NotEmpty'));
127
128        $this->form->addElement($fileNameElement);
129
130        $instancesElement = FormFactory::getElement('instances', 'Checkbox');
131        $instancesElement->setDescription(__('Test'));
132        $instancesElement->setOptions(Uri::encodeArray($options, Uri::ENCODE_ARRAY_KEYS));
133        foreach (array_keys($options) as $value) {
134            $instancesElement->setValue($value);
135        }
136
137        $this->form->addElement($instancesElement);
138
139        $this->form->createGroup(
140            'options',
141            sprintf('<h3>%s</h3>', static::$title),
142            ['filename', 'instances']
143        );
144    }
145
146    /** @throws MissingTestmodelException */
147    protected function getInstanceOptions(Resource ...$resources): array
148    {
149        $testService = $this->getTestService();
150        $testModel = $this->getTestModel();
151
152        $options = [];
153
154        foreach ($resources as $resource) {
155            if ($testModel->equals($testService->getTestModel($resource))) {
156                $options[$resource->getUri()] = $resource->getLabel();
157            }
158        }
159
160        return $options;
161    }
162
163    protected function getFileName(string $name): string
164    {
165        return strtolower(Display::textCleaner($name, '*'));
166    }
167
168    /** @noinspection PhpIncompatibleReturnTypeInspection */
169    private function getTestService(): TestsService
170    {
171        return TestsService::singleton();
172    }
173
174    private function setTestModel(Resource $model): Resource
175    {
176        $this->testModel = $model;
177
178        return $this->testModel;
179    }
180
181    private function getTestModel(): Resource
182    {
183        return $this->testModel ?? $this->setTestModel(new Resource(QtiTestService::INSTANCE_TEST_MODEL_QTI));
184    }
185}