Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 78 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| ExportForm | |
0.00% |
0 / 78 |
|
0.00% |
0 / 6 |
380 | |
0.00% |
0 / 1 |
| initForm | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
2 | |||
| initElements | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
90 | |||
| getItemsService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFileName | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getOptions | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| isInstanceValid | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
| getFormGroupName | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 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) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | */ |
| 23 | |
| 24 | namespace oat\taoQtiItem\model\Export; |
| 25 | |
| 26 | use common_Exception; |
| 27 | use common_exception_UserReadableException; |
| 28 | use core_kernel_classes_Class; |
| 29 | use core_kernel_classes_Resource; |
| 30 | use oat\oatbox\filesystem\FilesystemException; |
| 31 | use oat\tao\model\export\ExportElementException; |
| 32 | use oat\taoQtiItem\model\ItemModel; |
| 33 | use oat\taoQtiItem\model\qti\Service; |
| 34 | use tao_helpers_Display; |
| 35 | use tao_helpers_form_FormContainer; |
| 36 | use tao_helpers_form_FormFactory; |
| 37 | use tao_helpers_form_xhtml_Form; |
| 38 | use tao_helpers_form_xhtml_TagWrapper; |
| 39 | use tao_helpers_Uri; |
| 40 | use taoItems_models_classes_ItemsService; |
| 41 | |
| 42 | /** |
| 43 | * Export form for QTI packages |
| 44 | * |
| 45 | * @access public |
| 46 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 47 | * @package taoItems |
| 48 | */ |
| 49 | abstract class ExportForm extends tao_helpers_form_FormContainer |
| 50 | { |
| 51 | // --- ASSOCIATIONS --- |
| 52 | |
| 53 | |
| 54 | // --- ATTRIBUTES --- |
| 55 | |
| 56 | // --- OPERATIONS --- |
| 57 | /** |
| 58 | * Short description of method initForm |
| 59 | * |
| 60 | * @access public |
| 61 | * @return mixed |
| 62 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 63 | */ |
| 64 | public function initForm() |
| 65 | { |
| 66 | $this->form = new tao_helpers_form_xhtml_Form('export'); |
| 67 | |
| 68 | $this->form->setDecorators( |
| 69 | [ |
| 70 | 'element' => new tao_helpers_form_xhtml_TagWrapper(['tag' => 'div']), |
| 71 | 'group' => new tao_helpers_form_xhtml_TagWrapper(['tag' => 'div', 'cssClass' => 'form-group']), |
| 72 | 'error' => new tao_helpers_form_xhtml_TagWrapper( |
| 73 | ['tag' => 'div', 'cssClass' => 'form-error ui-state-error ui-corner-all'] |
| 74 | ), |
| 75 | 'actions-bottom' => new tao_helpers_form_xhtml_TagWrapper( |
| 76 | ['tag' => 'div', 'cssClass' => 'form-toolbar'] |
| 77 | ), |
| 78 | 'actions-top' => new tao_helpers_form_xhtml_TagWrapper( |
| 79 | ['tag' => 'div', 'cssClass' => 'form-toolbar'] |
| 80 | ) |
| 81 | ] |
| 82 | ); |
| 83 | |
| 84 | $exportElt = tao_helpers_form_FormFactory::getElement('export', 'Free'); |
| 85 | $exportElt->setValue( |
| 86 | '<a href="#" class="form-submitter btn-success small"><span class="icon-export"></span> ' |
| 87 | . __('Export') . '</a>' |
| 88 | ); |
| 89 | |
| 90 | $this->form->setActions([$exportElt], 'bottom'); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * overriden |
| 95 | * |
| 96 | * @access public |
| 97 | * @return mixed |
| 98 | * @throws common_Exception |
| 99 | * @author Joel Bout, <joel.bout@tudor.lu> |
| 100 | */ |
| 101 | public function initElements() |
| 102 | { |
| 103 | $itemService = $this->getItemsService(); |
| 104 | |
| 105 | $fileName = ''; |
| 106 | $options = ['options' => []]; |
| 107 | if (isset($this->data['items'])) { |
| 108 | $fileName = $this->getFileName($this->data['file_name']); |
| 109 | $options = $this->getOptions(...array_values($this->data['items'])); |
| 110 | } elseif (isset($this->data['instance'])) { |
| 111 | $item = $this->data['instance']; |
| 112 | if ( |
| 113 | $item instanceof core_kernel_classes_Resource |
| 114 | && $itemService->hasItemModel($item, [ItemModel::MODEL_URI]) |
| 115 | ) { |
| 116 | $fileName = $this->getFileName($item->getLabel()); |
| 117 | $options = $this->getOptions($item); |
| 118 | } |
| 119 | } else { |
| 120 | $class = $this->data['class'] ?? $itemService->getRootClass(); |
| 121 | |
| 122 | if ($class instanceof core_kernel_classes_Class) { |
| 123 | $fileName = $this->getFileName($class->getLabel()); |
| 124 | $options = $this->getOptions(...$class->getInstances(true)); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | $nameElt = tao_helpers_form_FormFactory::getElement('filename', 'Textbox'); |
| 129 | $nameElt->setDescription(__('File name')); |
| 130 | $nameElt->setValue($fileName); |
| 131 | $nameElt->setUnit('.zip'); |
| 132 | $nameElt->addValidator(tao_helpers_form_FormFactory::getValidator('NotEmpty')); |
| 133 | $this->form->addElement($nameElt); |
| 134 | |
| 135 | $instanceElt = tao_helpers_form_FormFactory::getElement('instances', 'Checkbox'); |
| 136 | $instanceElt->setDescription(__('Items')); |
| 137 | |
| 138 | if (count($options['options']) > 1) { |
| 139 | $instanceElt->setAttribute('checkAll', true); |
| 140 | } |
| 141 | |
| 142 | $instanceElt->setOptions(tao_helpers_Uri::encodeArray($options['options'], tao_helpers_Uri::ENCODE_ARRAY_KEYS)); |
| 143 | $instanceElt->setReadOnly( |
| 144 | tao_helpers_Uri::encodeArray($options['disabledOptions'], tao_helpers_Uri::ENCODE_ARRAY_KEYS) |
| 145 | ); |
| 146 | foreach (array_keys($options['options']) as $value) { |
| 147 | if (!isset($options['disabledOptions'][$value])) { |
| 148 | $instanceElt->setValue($value); |
| 149 | } |
| 150 | } |
| 151 | $this->form->addElement($instanceElt); |
| 152 | |
| 153 | $this->form->createGroup('options', '<h3>' . $this->getFormGroupName() . '</h3>', ['filename', 'instances']); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return taoItems_models_classes_ItemsService |
| 158 | */ |
| 159 | protected function getItemsService() |
| 160 | { |
| 161 | return taoItems_models_classes_ItemsService::singleton(); |
| 162 | } |
| 163 | |
| 164 | protected function getFileName(string $name): string |
| 165 | { |
| 166 | return strtolower(tao_helpers_Display::textCleaner($name, '*')); |
| 167 | } |
| 168 | |
| 169 | protected function getOptions(core_kernel_classes_Resource ...$resource): array |
| 170 | { |
| 171 | $itemService = taoItems_models_classes_ItemsService::singleton(); |
| 172 | $options = []; |
| 173 | $disabledOptions = []; |
| 174 | foreach ($resource as $instance) { |
| 175 | if (!$itemService->hasItemModel($instance, [ItemModel::MODEL_URI])) { |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | try { |
| 180 | $this->isInstanceValid($instance); |
| 181 | } catch (common_exception_UserReadableException $e) { |
| 182 | $disabledOptions[$instance->getUri()] = $e->getUserMessage(); |
| 183 | } |
| 184 | |
| 185 | $options[$instance->getUri()] = $instance->getLabel(); |
| 186 | } |
| 187 | |
| 188 | return [ |
| 189 | 'options' => $options, |
| 190 | 'disabledOptions' => $disabledOptions |
| 191 | ]; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * @param $item |
| 196 | * |
| 197 | * @return bool |
| 198 | * @throws ExportElementException |
| 199 | * @throws common_Exception |
| 200 | */ |
| 201 | protected function isInstanceValid($item) |
| 202 | { |
| 203 | try { |
| 204 | $xml = Service::singleton()->getXmlByRdfItem($item); |
| 205 | } catch (FilesystemException $e) { |
| 206 | } |
| 207 | |
| 208 | if (empty($xml)) { |
| 209 | throw new ExportElementException($item, __('no item content')); |
| 210 | } |
| 211 | |
| 212 | return true; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Get the form group name to be display |
| 217 | * @return string |
| 218 | */ |
| 219 | abstract protected function getFormGroupName(); |
| 220 | } |