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