Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 67 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CsvImportForm | |
0.00% |
0 / 67 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
initForm | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
initElements | |
0.00% |
0 / 54 |
|
0.00% |
0 / 1 |
6 | |||
getServiceLocator | |
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) 2021 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | */ |
20 | |
21 | declare(strict_types=1); |
22 | |
23 | namespace oat\taoQtiItem\model\import; |
24 | |
25 | use oat\generis\Helper\SystemHelper; |
26 | use tao_helpers_form_FormFactory; |
27 | use tao_helpers_form_xhtml_Form; |
28 | use tao_models_classes_import_CsvUploadForm; |
29 | use oat\oatbox\service\ServiceManager; |
30 | use Zend\ServiceManager\ServiceLocatorInterface; |
31 | use oat\tao\helpers\UrlHelper; |
32 | |
33 | class CsvImportForm extends tao_models_classes_import_CsvUploadForm |
34 | { |
35 | /** @var string */ |
36 | private $classUri; |
37 | |
38 | public function __construct(array $data = [], array $options = []) |
39 | { |
40 | $this->classUri = $options['classUri'] ?? ''; |
41 | $options[tao_models_classes_import_CsvUploadForm::IS_OPTION_FIRST_COLUMN_ENABLE] = false; |
42 | parent::__construct($data, $options); |
43 | } |
44 | |
45 | /** |
46 | * @inheritdoc |
47 | */ |
48 | public function initForm() |
49 | { |
50 | $this->form = new tao_helpers_form_xhtml_Form('export'); |
51 | $submitElt = tao_helpers_form_FormFactory::getElement('import', 'Free'); |
52 | $submitElt->setValue( |
53 | '<a href="#" class="form-submitter btn-success small"><span class="icon-import"></span> ' |
54 | . __('Import') . '</a>' |
55 | ); |
56 | |
57 | $this->form->setActions([$submitElt], 'bottom'); |
58 | $this->form->setActions([], 'top'); |
59 | } |
60 | |
61 | /** |
62 | * @inheritdoc |
63 | */ |
64 | public function initElements() |
65 | { |
66 | $formElement = tao_helpers_form_FormFactory::getElement('source', 'AsyncFile'); |
67 | $formElement->setDescription(__("Add a CSV file")); |
68 | |
69 | if (isset($_POST['import_sent_csv'])) { |
70 | $formElement->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
71 | } else { |
72 | $formElement->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty', ['message' => ''])); |
73 | } |
74 | |
75 | $formElement->addValidators( |
76 | [ |
77 | tao_helpers_form_FormFactory::getValidator( |
78 | 'FileMimeType', |
79 | [ |
80 | 'mimetype' => [ |
81 | 'text/plain', |
82 | 'text/csv', |
83 | 'text/comma-separated-values', |
84 | 'text/anytext', |
85 | 'application/csv', |
86 | 'application/txt', |
87 | 'application/csv-tab-delimited-table', |
88 | 'application/vnd.ms-excel', |
89 | 'application/vnd.msexcel', |
90 | ], |
91 | 'extension' => ['csv', 'txt'] |
92 | ] |
93 | ), |
94 | tao_helpers_form_FormFactory::getValidator( |
95 | 'FileSize', |
96 | [ |
97 | 'max' => SystemHelper::getFileUploadLimit() |
98 | ] |
99 | ) |
100 | ] |
101 | ); |
102 | |
103 | $this->form->addElement($formElement); |
104 | $downloadLink = $this->getServiceLocator()->get(UrlHelper::class)->buildUrl( |
105 | 'downloadTemplate', |
106 | 'ItemImportSampleDownload', |
107 | 'taoQtiItem', |
108 | ['uri' => $this->classUri] |
109 | ); |
110 | $this->form->createGroup( |
111 | 'file', |
112 | sprintf( |
113 | '%s <a href="%s">[%s]</a>', |
114 | __('Import item content and metadata from CSV file. Only choice interactions are supported.'), |
115 | $downloadLink, |
116 | __('Download sample csv file') |
117 | ), |
118 | [ |
119 | 'source' |
120 | ] |
121 | ); |
122 | |
123 | $csvSentElt = tao_helpers_form_FormFactory::getElement('import_sent_csv', 'Hidden'); |
124 | $csvSentElt->setValue(1); |
125 | |
126 | $this->form->addElement($csvSentElt); |
127 | } |
128 | |
129 | private function getServiceLocator(): ServiceLocatorInterface |
130 | { |
131 | return ServiceManager::getServiceManager(); |
132 | } |
133 | } |