ILIAS  trunk Revision v12.0_alpha-1227-g7ff6d300864
class.ilMatchingWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
22use ILIAS\UI\Component\Button\Factory as ButtonFactory;
24
36{
37 private string $pending;
38 protected $text_name = '';
39 protected $image_name = '';
40 protected $values = [];
41 protected $qstObject = null;
42 protected $suffixes = [];
43 protected $hideImages = false;
44
46 protected GlyphFactory $glyph_factory;
47 protected ButtonFactory $button_factory;
50
51 public function __construct($a_title = "", $a_postvar = "")
52 {
53 parent::__construct($a_title, $a_postvar);
54
55 global $DIC;
56
57 $this->forms_helper = new ilTestLegacyFormsHelper();
58 $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
59 $this->button_factory = $DIC->ui()->factory()->button();
60 $this->renderer = $DIC->ui()->renderer();
61 $this->upload_limit = $DIC['ui.upload_limit_resolver'];
62
63 $this->setSuffixes(["jpg", "jpeg", "png", "gif"]);
64 $this->setSize('40');
65 $this->setMaxLength(800);
66
67 $lng = $DIC['lng'];
68 $this->text_name = $lng->txt('answer_text');
69 $this->image_name = $lng->txt('answer_image');
70 }
71
77 public function setSuffixes($a_suffixes): void
78 {
79 $this->suffixes = $a_suffixes;
80 }
81
87 public function getSuffixes(): array
88 {
89 return $this->suffixes;
90 }
91
97 public function setHideImages($a_hide): void
98 {
99 $this->hideImages = $a_hide;
100 }
101
107 public function setValues($a_values): void
108 {
109 $this->values = $a_values;
110 }
111
117 public function getValues(): array
118 {
119 return $this->values;
120 }
121
122 public function setTextName($a_value): void
123 {
124 $this->text_name = $a_value;
125 }
126
127 public function setImageName($a_value): void
128 {
129 $this->image_name = $a_value;
130 }
131
137 public function setQuestionObject($a_value): void
138 {
139 $this->qstObject = &$a_value;
140 }
141
147 public function getQuestionObject(): ?object
148 {
149 return $this->qstObject;
150 }
151
152 public function setValue($a_value): void
153 {
154 $this->values = [];
155
156 $answers = $this->forms_helper->transformArray($a_value, 'answer', $this->refinery->kindlyTo()->string());
157 $imagename = $this->forms_helper->transformArray($a_value, 'imagename', $this->refinery->kindlyTo()->string());
158 $identifier = $this->forms_helper->transformArray($a_value, 'identifier', $this->refinery->kindlyTo()->int());
159
160 foreach ($answers as $index => $value) {
161 $this->values[] = new assAnswerMatchingTerm(
162 $value,
163 $imagename[$index] ?? '',
164 $identifier[$index] ?? 0
165 );
166 }
167 }
168
173 public function checkInput(): bool
174 {
175 $data = $this->raw($this->getPostVar());
176
177 if (!is_array($data)) {
178 $this->setAlert($this->lng->txt('msg_input_is_required'));
179 return false;
180 }
181
182 // check answers
183 $answers = $this->forms_helper->transformArray($data, 'answer', $this->refinery->kindlyTo()->string());
184 $images = $this->forms_helper->transformArray($data, 'imagename', $this->refinery->kindlyTo()->string());
185 foreach ($answers as $index => $value) {
186 if (
187 $value === ''
188 && !$this->forms_helper->inArray($images, $index)
189 && !isset($_FILES[$this->getPostVar()]['tmp_name']['image'][$index])
190 ) {
191 $this->setAlert($this->lng->txt('msg_input_is_required'));
192 return false;
193 }
194 }
195
196 if (!$this->hideImages) {
197 if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
198 foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
199 // error handling
200 if ($error > 0) {
201 switch ($error) {
202 case UPLOAD_ERR_FORM_SIZE:
203 case UPLOAD_ERR_INI_SIZE:
204 $this->setAlert($this->lng->txt('form_msg_file_size_exceeds'));
205 return false;
206 break;
207
208 case UPLOAD_ERR_PARTIAL:
209 $this->setAlert($this->lng->txt('form_msg_file_partially_uploaded'));
210 return false;
211 break;
212
213 case UPLOAD_ERR_NO_FILE:
214 if (
215 !$this->forms_helper->inArray($images, $index)
216 && !$this->forms_helper->inArray($answers, $index)
217 && $this->getRequired()
218 ) {
219 $this->setAlert($this->lng->txt('form_msg_file_no_upload'));
220 return false;
221 }
222 break;
223
224 case UPLOAD_ERR_NO_TMP_DIR:
225 $this->setAlert($this->lng->txt('form_msg_file_missing_tmp_dir'));
226 return false;
227 break;
228
229 case UPLOAD_ERR_CANT_WRITE:
230 $this->setAlert($this->lng->txt('form_msg_file_cannot_write_to_disk'));
231 return false;
232 break;
233
234 case UPLOAD_ERR_EXTENSION:
235 $this->setAlert($this->lng->txt('form_msg_file_upload_stopped_ext'));
236 return false;
237 break;
238 }
239 }
240 }
241 }
242
243 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
244 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
245 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
246 $filename_arr = pathinfo($filename);
247 $suffix = $filename_arr['extension'] ?? '';
248
249 // check suffixes
250 if ($tmpname !== '' && is_array($this->getSuffixes())) {
252 if ($vir[0] == false) {
253 $this->setAlert($this->lng->txt('form_msg_file_virus_found') . '<br />' . $vir[1]);
254 return false;
255 }
256
257 if (!in_array(strtolower($suffix), $this->getSuffixes(), true)) {
258 $this->setAlert($this->lng->txt('form_msg_file_wrong_file_type'));
259 return false;
260 }
261 }
262 }
263 }
264 }
265
266 return $this->checkSubItemsInput();
267 }
268
273 public function insert(ilTemplate $a_tpl): void
274 {
275 global $DIC;
276 $lng = $DIC['lng'];
277 $global_tpl = $DIC['tpl'];
278 $global_tpl->addJavascript('assets/js/matchinginput.js');
279 $global_tpl->addOnLoadCode('il.test.matchingquestion.init();');
280
281 $tpl = new ilTemplate("tpl.prop_matchingwizardinput.html", true, true, "components/ILIAS/TestQuestionPool");
282 $i = 0;
283 foreach ($this->values as $value) {
284 if (!$this->hideImages) {
285 if ($value->getPicture() &&
286 file_exists($this->qstObject->getImagePath() . $value->getPicture())
287 ) {
288 $imagename = $this->qstObject->getImagePathWeb() . $value->getPicture();
289 if ($this->qstObject->getThumbSize()) {
290 if (file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getPicture())) {
291 $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getPicture();
292 }
293 }
294
295 $tpl->setCurrentBlock('image');
296 $tpl->setVariable('SRC_IMAGE', $imagename);
297 $tpl->setVariable('IMAGE_NAME', $value->getPicture());
298 $tpl->setVariable('ALT_IMAGE', ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
299 $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
300 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
301 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
302 $tpl->parseCurrentBlock();
303 }
304 $tpl->setCurrentBlock('addimage');
305 $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
306 $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
307 $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
308 $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
309 $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
310 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
311 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
312 $tpl->parseCurrentBlock();
313 }
314
315 if (is_object($value)) {
316 $tpl->setCurrentBlock("prop_text_propval");
317 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
318 $tpl->parseCurrentBlock();
319 }
320 // this block does not exist in the template
321 // $tpl->setCurrentBlock('singleline');
322 $tpl->setVariable("SIZE", $this->getSize());
323 $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
324 $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
325 $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
326 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
327 if ($this->getDisabled()) {
328 $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
329 }
330 $tpl->parseCurrentBlock();
331 $tpl->setCurrentBlock("row");
332 $tpl->setVariable("POST_VAR", $this->getPostVar());
333 $tpl->setVariable("ROW_NUMBER", $i + 1);
334 $tpl->setVariable("ROW_IDENTIFIER", $value->getIdentifier());
335 $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
336 $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
337 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->add())
338 ));
339 $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
340 $this->button_factory->shy('', '')->withSymbol($this->glyph_factory->remove())
341 ));
342 $tpl->parseCurrentBlock();
343 $i++;
344 }
345
346 if (!$this->hideImages) {
347 if (is_array($this->getSuffixes())) {
348 $suff_str = $delim = "";
349 foreach ($this->getSuffixes() as $suffix) {
350 $suff_str .= $delim . "." . $suffix;
351 $delim = ", ";
352 }
353 $tpl->setCurrentBlock('allowed_image_suffixes');
354 $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
355 $tpl->parseCurrentBlock();
356 }
357 $tpl->setCurrentBlock("image_heading");
358 $tpl->setVariable("ANSWER_IMAGE", $this->image_name);
359 $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
360 $tpl->parseCurrentBlock();
361 }
362
363 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
364 $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
365 $tpl->setVariable("TEXT_NO", $lng->txt('no'));
366 $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
367 $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
368 $tpl->setVariable("ANSWER_TEXT", $this->text_name);
369 $tpl->setVariable("NUMBER_TEXT", $lng->txt('row'));
370 $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
371
372 $a_tpl->setCurrentBlock("prop_generic");
373 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
374 $a_tpl->parseCurrentBlock();
375 }
376
377 public function setPending(string $a_val): void
378 {
379 $this->pending = $a_val;
380 }
381
382 public function getPending(): string
383 {
384 return $this->pending;
385 }
386}
renderer()
$filename
Definition: buildRTE.php:78
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Builds a Color from either hex- or rgb values.
Definition: Factory.php:31
Class for matching question terms.
static getFileSizeInfo()
ilGlobalTemplateInterface $global_tpl
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
static prepareFormOutput($a_str, bool $a_strip=false)
This class represents a single choice wizard property in a property form.
setQuestionObject($a_value)
Set question object.
insert(ilTemplate $a_tpl)
Insert property html.
setHideImages($a_hide)
Set hide images.
checkInput()
Check input, strip slashes etc.
setSuffixes($a_suffixes)
Set Accepted Suffixes.
__construct($a_title="", $a_postvar="")
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This class represents a text property in a property form.
setMaxLength(?int $a_maxlength)
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
This is how a factory for glyphs looks like.
Definition: Factory.php:27
An entity that renders components to a string output.
Definition: Renderer.php:31
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26