ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSingleChoiceWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
23
32{
33 protected $values = [];
34 protected $allowMove = false;
35 protected $singleline = true;
36 protected $qstObject = null;
37 protected $suffixes = [];
38 protected $showPoints = true;
39 protected $hideImages = false;
40
42 protected GlyphFactory $glyph_factory;
45
52 public function __construct($a_title = '', $a_postvar = '')
53 {
54 parent::__construct($a_title, $a_postvar);
55 $this->setSuffixes(['jpg', 'jpeg', 'png', 'gif']);
56 $this->setSize('25');
57 $this->setMaxLength(1000);
58 $this->validationRegexp = '';
59
60 global $DIC;
61 $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
62 $this->renderer = $DIC->ui()->renderer();
63 $this->upload_limit = $DIC['ui.upload_limit_resolver'];
64 $this->forms_helper = new ilTestLegacyFormsHelper();
65 }
66
67 public function setValue($a_value): void
68 {
69 $this->values = [];
70
71 $answers = $this->forms_helper->transformArray($a_value, 'answer', $this->refinery->kindlyTo()->string());
72 $points = $this->forms_helper->transformPoints($a_value);
73
74 foreach ($answers as $index => $value) {
75 $answer = new ASS_AnswerBinaryStateImage(
76 $value,
77 $points[$index] ?? 0.0,
78 (int) $index,
79 true,
80 null,
81 (int) $a_value['answer_id'][$index] ?? 0
82 );
83 if (isset($a_value['imagename'][$index])) {
84 $answer->setImage($a_value['imagename'][$index]);
85 }
86 $this->values[] = $answer;
87 }
88 }
89
90 public function setValueByArray(array $a_values): void
91 {
92 if (isset($a_values[$this->getPostVar()])) {
93 $this->setValue($a_values[$this->getPostVar()] ?? []);
94 }
95 }
96
102 public function setSuffixes($a_suffixes): void
103 {
104 $this->suffixes = $a_suffixes;
105 }
106
112 public function setHideImages($a_hide): void
113 {
114 $this->hideImages = $a_hide;
115 }
116
122 public function getSuffixes(): array
123 {
124 return $this->suffixes;
125 }
126
127 public function setShowPoints($a_value): void
128 {
129 $this->showPoints = $a_value;
130 }
131
132 public function getShowPoints(): bool
133 {
134 return $this->showPoints;
135 }
136
142 public function setValues($a_values): void
143 {
144 $this->values = $a_values;
145 }
146
152 public function getValues(): array
153 {
154 return $this->values;
155 }
156
162 public function setSingleline($a_value): void
163 {
164 $this->singleline = $a_value;
165 }
166
172 public function getSingleline(): bool
173 {
174 return $this->singleline;
175 }
176
182 public function setQuestionObject($a_value): void
183 {
184 $this->qstObject = &$a_value;
185 }
186
192 public function getQuestionObject(): ?object
193 {
194 return $this->qstObject;
195 }
196
202 public function setAllowMove($a_allow_move): void
203 {
204 $this->allowMove = $a_allow_move;
205 }
206
212 public function getAllowMove(): bool
213 {
214 return $this->allowMove;
215 }
216
217
218 // Set pending filename value
219 public function setPending(string $val): void
220 {
225 }
226
233 protected function checkAnswersInput(array $data): array|string
234 {
235 $to_string = $this->refinery->kindlyTo()->string();
236 $answers = $this->forms_helper->transformArray($data, 'answer', $to_string);
237 $image_names = $this->forms_helper->transformArray($data, 'imagename', $to_string);
238 foreach ($answers as $index => $value) {
239 if ($value === '' && !$this->forms_helper->inArray($image_names, $index)) {
240 return 'msg_input_is_required';
241 }
242
243 if (mb_strlen($value) > $this->getMaxLength()) {
244 return 'msg_input_char_limit_max';
245 }
246 }
247
248 return $answers;
249 }
250
251 public function checkInput(): bool
252 {
253 $data = $this->raw($this->getPostVar());
254
255 if (!is_array($data)) {
256 $this->setAlert($this->lng->txt('msg_input_is_required'));
257 return false;
258 }
259
260 // check points
261 $points = $this->forms_helper->checkPointsInputEnoughPositive($data, true);
262 if (!is_array($points)) {
263 $this->setAlert($this->lng->txt($points));
264 return false;
265 }
266
267 // check answers
268 $answers = $this->checkAnswersInput($data);
269 if (!is_array($answers)) {
270 $this->setAlert($this->lng->txt($answers));
271 return false;
272 }
273 $image_names = $this->forms_helper->transformArray($data, 'imagename', $this->refinery->kindlyTo()->string());
274
275 if (is_array($_FILES) && count($_FILES) && $this->getSingleline() && (!$this->hideImages)) {
276 if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
277 foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
278 // error handling
279 if ($error > 0) {
280 switch ($error) {
281 case UPLOAD_ERR_FORM_SIZE:
282 case UPLOAD_ERR_INI_SIZE:
283 $this->setAlert($this->lng->txt('form_msg_file_size_exceeds'));
284 return false;
285 break;
286
287 case UPLOAD_ERR_PARTIAL:
288 $this->setAlert($this->lng->txt('form_msg_file_partially_uploaded'));
289 return false;
290 break;
291
292 case UPLOAD_ERR_NO_FILE:
293 if (
294 !$this->forms_helper->inArray($image_names, $index)
295 && !$this->forms_helper->inArray($answers, $index)
296 && $this->getRequired()
297 ) {
298 $this->setAlert($this->lng->txt('form_msg_file_no_upload'));
299 return false;
300 }
301 break;
302
303 case UPLOAD_ERR_NO_TMP_DIR:
304 $this->setAlert($this->lng->txt('form_msg_file_missing_tmp_dir'));
305 return false;
306 break;
307
308 case UPLOAD_ERR_CANT_WRITE:
309 $this->setAlert($this->lng->txt('form_msg_file_cannot_write_to_disk'));
310 return false;
311 break;
312
313 case UPLOAD_ERR_EXTENSION:
314 $this->setAlert($this->lng->txt('form_msg_file_upload_stopped_ext'));
315 return false;
316 break;
317 }
318 }
319 }
320 } elseif ($this->getRequired()) {
321 $this->setAlert($this->lng->txt('form_msg_file_no_upload'));
322 return false;
323 }
324
325 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
326 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
327 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
328 if ($filename !== '') {
329 $filename_arr = pathinfo($filename);
330 $suffix = $filename_arr['extension'];
331 $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
332 $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
333 // check suffixes
334 if (
335 $tmpname !== ''
336 && is_array($this->getSuffixes())
337 && !in_array(strtolower($suffix), $this->getSuffixes(), true)
338 ) {
339 $this->setAlert($this->lng->txt('form_msg_file_wrong_file_type'));
340 return false;
341 }
342 }
343 }
344 }
345
346 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
347 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
348 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
349 if ($filename !== '') {
350 $filename_arr = pathinfo($filename);
351 $suffix = $filename_arr['extension'];
352 $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
353 $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
354 // virus handling
355 if ($tmpname !== '') {
357 if ($vir[0] == false) {
358 $this->setAlert($this->lng->txt('form_msg_file_virus_found') . '<br />' . $vir[1]);
359 return false;
360 }
361 }
362 }
363 }
364 }
365 }
366
367 return $this->checkSubItemsInput();
368 }
369
370 public function insert(ilTemplate $a_tpl): void
371 {
372 $tpl = new ilTemplate('tpl.prop_singlechoicewizardinput.html', true, true, 'components/ILIAS/TestQuestionPool');
373 $i = 0;
374 foreach ($this->values as $value) {
375 if ($this->getSingleline()) {
376 if (!$this->hideImages) {
377 if ($value->getImage()) {
378 $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
379 if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
380 if (file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage())) {
381 $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
382 }
383 }
384 $tpl->setCurrentBlock('image');
385 $tpl->setVariable('SRC_IMAGE', $imagename);
386 $tpl->setVariable('IMAGE_NAME', $value->getImage());
387 $tpl->setVariable(
388 'ALT_IMAGE',
390 );
391 $tpl->setVariable('TXT_DELETE_EXISTING', $this->lng->txt('delete_existing_file'));
392 $tpl->setVariable('IMAGE_ROW_NUMBER', $i);
393 $tpl->setVariable('IMAGE_POST_VAR', $this->getPostVar());
394 $tpl->parseCurrentBlock();
395 }
396 $tpl->setCurrentBlock('addimage');
397 $tpl->setVariable('IMAGE_BROWSE', $this->lng->txt('select_file'));
398 $tpl->setVariable('IMAGE_ID', $this->getPostVar() . "[image][$i]");
399 $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
400 $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
401 $tpl->setVariable('IMAGE_SUBMIT', $this->lng->txt('upload'));
402 $tpl->setVariable('IMAGE_ROW_NUMBER', $i);
403 $tpl->setVariable('IMAGE_POST_VAR', $this->getPostVar());
404 $tpl->parseCurrentBlock();
405 }
406
407 if (is_object($value)) {
408 $tpl->setCurrentBlock('prop_text_propval');
409 $tpl->setVariable(
410 'PROPERTY_VALUE',
412 );
413 $tpl->parseCurrentBlock();
414 if ($this->getShowPoints()) {
415 $tpl->setCurrentBlock('prop_points_propval');
416 $tpl->setVariable(
417 'PROPERTY_VALUE',
419 );
420 $tpl->parseCurrentBlock();
421 }
422 $tpl->setCurrentBlock('prop_answer_id_propval');
423 $tpl->setVariable('PROPERTY_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
424 $tpl->parseCurrentBlock();
425 }
426 $tpl->setCurrentBlock('singleline');
427 $tpl->setVariable('SIZE', $this->getSize());
428 $tpl->setVariable('SINGLELINE_ID', $this->getPostVar() . "[answer][$i]");
429 $tpl->setVariable('SINGLELINE_ROW_NUMBER', $i);
430 $tpl->setVariable('SINGLELINE_POST_VAR', $this->getPostVar());
431 $tpl->setVariable('MAXLENGTH', $this->getMaxLength());
432 if ($this->getDisabled()) {
433 $tpl->setVariable('DISABLED_SINGLELINE', ' disabled="disabled"');
434 }
435 $tpl->parseCurrentBlock();
436 } elseif (!$this->getSingleline()) {
437 if (is_object($value)) {
438 if ($this->getShowPoints()) {
439 $tpl->setCurrentBlock('prop_points_propval');
440 $tpl->setVariable(
441 'PROPERTY_VALUE',
443 );
444 $tpl->parseCurrentBlock();
445 }
446 $tpl->setCurrentBlock('prop_answer_id_propval');
447 $tpl->setVariable('PROPERTY_VALUE', ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
448 $tpl->parseCurrentBlock();
449 }
450 $tpl->setCurrentBlock('multiline');
451 $tpl->setVariable(
452 'PROPERTY_VALUE',
454 );
455 $tpl->setVariable('MULTILINE_ID', $this->getPostVar() . "[answer][$i]");
456 $tpl->setVariable('MULTILINE_ROW_NUMBER', $i);
457 $tpl->setVariable('MULTILINE_POST_VAR', $this->getPostVar());
458 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
459 if ($this->getDisabled()) {
460 $tpl->setVariable('DISABLED_MULTILINE', ' disabled="disabled"');
461 }
462 $tpl->parseCurrentBlock();
463 }
464 if ($this->getAllowMove()) {
465 $tpl->setCurrentBlock('move');
466 $tpl->setVariable('ID', $this->getPostVar() . "[$i]");
467 $tpl->setVariable('UP_BUTTON', $this->renderer->render(
468 $this->glyph_factory->up()->withAction('#')
469 ));
470 $tpl->setVariable('DOWN_BUTTON', $this->renderer->render(
471 $this->glyph_factory->down()->withAction('#')
472 ));
473 $tpl->parseCurrentBlock();
474 }
475 if ($this->getShowPoints()) {
476 $tpl->setCurrentBlock('points');
477 $tpl->setVariable('POINTS_ID', $this->getPostVar() . "[points][$i]");
478 $tpl->setVariable('POINTS_POST_VAR', $this->getPostVar());
479 $tpl->setVariable('POINTS_ROW_NUMBER', $i);
480 $tpl->parseCurrentBlock();
481 }
482 $tpl->setCurrentBlock('row');
483 $tpl->setVariable('POST_VAR', $this->getPostVar());
484 $tpl->setVariable('ROW_NUMBER', $i);
485 $tpl->setVariable('ID', $this->getPostVar() . "[answer][$i]");
486 if ($this->getDisabled()) {
487 $tpl->setVariable('DISABLED_POINTS', ' disabled="disabled"');
488 }
489 $tpl->setVariable('ADD_BUTTON', $this->renderer->render(
490 $this->glyph_factory->add()->withAction('#')
491 ));
492 $tpl->setVariable('REMOVE_BUTTON', $this->renderer->render(
493 $this->glyph_factory->remove()->withAction('#')
494 ));
495 $tpl->parseCurrentBlock();
496 $i++;
497 }
498
499 if ($this->getSingleline()) {
500 if (!$this->hideImages) {
501 if (is_array($this->getSuffixes())) {
502 $suff_str = $delim = '';
503 foreach ($this->getSuffixes() as $suffix) {
504 $suff_str .= $delim . '.' . $suffix;
505 $delim = ', ';
506 }
507 $tpl->setCurrentBlock('allowed_image_suffixes');
508 $tpl->setVariable('TXT_ALLOWED_SUFFIXES', $this->lng->txt('file_allowed_suffixes') . ' ' . $suff_str);
509 $tpl->parseCurrentBlock();
510 }
511 $tpl->setCurrentBlock('image_heading');
512 $tpl->setVariable('ANSWER_IMAGE', $this->lng->txt('answer_image'));
513 $tpl->setVariable('TXT_MAX_SIZE', ilFileUtils::getFileSizeInfo());
514 $tpl->parseCurrentBlock();
515 }
516 }
517
518 if ($this->getShowPoints()) {
519 $tpl->setCurrentBlock('points_heading');
520 $tpl->setVariable('POINTS_TEXT', $this->lng->txt('points'));
521 $tpl->parseCurrentBlock();
522 }
523
524 $tpl->setVariable('ELEMENT_ID', $this->getPostVar());
525 $tpl->setVariable('TEXT_YES', $this->lng->txt('yes'));
526 $tpl->setVariable('TEXT_NO', $this->lng->txt('no'));
527 $tpl->setVariable('DELETE_IMAGE_HEADER', $this->lng->txt('delete_image_header'));
528 $tpl->setVariable('DELETE_IMAGE_QUESTION', $this->lng->txt('delete_image_question'));
529 $tpl->setVariable('ANSWER_TEXT', $this->lng->txt('answer_text'));
530 $tpl->setVariable('COMMANDS_TEXT', $this->lng->txt('actions'));
531
532 $a_tpl->setCurrentBlock('prop_generic');
533 $a_tpl->setVariable('PROP_GENERIC', $tpl->get());
534 $a_tpl->parseCurrentBlock();
535
536 global $DIC;
537 $tpl = $DIC['tpl'];
538 $tpl->addJavascript('assets/js/answerwizardinput.js');
539 $tpl->addJavascript('assets/js/singlechoicewizard.js');
540 }
541}
renderer()
$filename
Definition: buildRTE.php:78
Class for answers with a binary state indicator.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
static getFileSizeInfo()
static prepareFormOutput($a_str, bool $a_strip=false)
This class represents a single choice wizard property in a property form.
checkInput()
Check input, strip slashes etc.
__construct($a_title='', $a_postvar='')
Constructor.
checkAnswersInput(array $data)
Checks the input of the answers and returns the answers as an array if the input is valid or a string...
setQuestionObject($a_value)
Set question object.
setSuffixes($a_suffixes)
Set Accepted Suffixes.
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)
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