ILIAS  release_8 Revision v8.24
class.ilSingleChoiceWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
19use ILIAS\HTTP\Wrapper\ArrayBasedRequestWrapper as ArrayBasedRequestWrapper;
20
29{
30 protected $values = [];
31 protected $allowMove = false;
32 protected $singleline = true;
33 protected $qstObject = null;
34 protected $suffixes = [];
35 protected $showPoints = true;
36 protected $hideImages = false;
37 protected ArrayBasedRequestWrapper $post_wrapper;
38
45 public function __construct($a_title = '', $a_postvar = '')
46 {
47 parent::__construct($a_title, $a_postvar);
48 $this->setSuffixes(['jpg', 'jpeg', 'png', 'gif']);
49 $this->setSize('25');
50 $this->setMaxLength(1000);
51 $this->validationRegexp = '';
52 global $DIC;
53 $this->post_wrapper = $DIC->http()->wrapper()->post();
54 }
55
56 public function setValue($a_value): void
57 {
58 $this->values = array();
59 if (is_array($a_value)) {
60 if (is_array($a_value['answer'])) {
61 foreach ($a_value['answer'] as $index => $value) {
62 $points = (float) str_replace(",", ".", $a_value['points'][$index]);
63 $answer = new ASS_AnswerBinaryStateImage(
64 (string) $value,
65 $points,
66 (int) $index,
67 1,
68 "",
69 (int) $a_value['answer_id'][$index]
70 );
71 if (isset($a_value['imagename'][$index])) {
72 $answer->setImage($a_value['imagename'][$index]);
73 }
74 $this->values[] = $answer;
75 }
76 }
77 }
78 }
79
80
81 public function setValueByArray(array $a_values): void
82 {
83 if (isset($a_values[$this->getPostVar()])) {
84 $this->setValue($a_values[$this->getPostVar()] ?? []);
85 }
86 }
87
93 public function setSuffixes($a_suffixes): void
94 {
95 $this->suffixes = $a_suffixes;
96 }
97
103 public function setHideImages($a_hide): void
104 {
105 $this->hideImages = $a_hide;
106 }
107
113 public function getSuffixes(): array
114 {
115 return $this->suffixes;
116 }
117
118 public function setShowPoints($a_value): void
119 {
120 $this->showPoints = $a_value;
121 }
122
123 public function getShowPoints(): bool
124 {
125 return $this->showPoints;
126 }
127
133 public function setValues($a_values): void
134 {
135 $this->values = $a_values;
136 }
137
143 public function getValues(): array
144 {
145 return $this->values;
146 }
147
153 public function setSingleline($a_value): void
154 {
155 $this->singleline = $a_value;
156 }
157
163 public function getSingleline(): bool
164 {
165 return $this->singleline;
166 }
167
173 public function setQuestionObject($a_value): void
174 {
175 $this->qstObject = &$a_value;
176 }
177
183 public function getQuestionObject(): ?object
184 {
185 return $this->qstObject;
186 }
187
193 public function setAllowMove($a_allow_move): void
194 {
195 $this->allowMove = $a_allow_move;
196 }
197
203 public function getAllowMove(): bool
204 {
205 return $this->allowMove;
206 }
207
208
209 // Set pending filename value
210 public function setPending(string $val): void
211 {
216 }
217
222 public function checkInput(): bool
223 {
224 global $DIC;
225 $lng = $DIC['lng'];
226
227 include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
228
229 if (is_array($_POST[$this->getPostVar()])) {
231 $_POST[$this->getPostVar()],
232 true,
234 );
235 }
236 //$foundvalues = $_POST[$this->getPostVar()];
237 if (is_array($foundvalues)) {
238 // check answers
239 if (is_array($foundvalues['answer'])) {
240 foreach ($foundvalues['answer'] as $aidx => $answervalue) {
241 if (((strlen($answervalue)) == 0) && (!isset($foundvalues['imagename'][$aidx]) || strlen($foundvalues['imagename'][$aidx]) == 0)) {
242 $this->setAlert($lng->txt("msg_input_is_required"));
243 return false;
244 }
245
246 if (mb_strlen($answervalue) > $this->getMaxLength()) {
247 $this->setAlert($lng->txt("msg_input_char_limit_max"));
248 return false;
249 }
250 }
251 }
252 // check points
253 $max = 0;
254 if (is_array($foundvalues['points'])) {
255 foreach ($foundvalues['points'] as $points) {
256 $points = str_replace(',', '.', $points);
257 if ($points > $max) {
258 $max = $points;
259 }
260 if (((strlen($points)) == 0) || (!is_numeric($points))) {
261 $this->setAlert($lng->txt("form_msg_numeric_value_required"));
262 return false;
263 }
264 }
265 }
266 if ($max == 0) {
267 $this->setAlert($lng->txt("enter_enough_positive_points"));
268 return false;
269 }
270
271 if (is_array($_FILES) && count($_FILES) && $this->getSingleline() && (!$this->hideImages)) {
272 if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
273 foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
274 // error handling
275 if ($error > 0) {
276 switch ($error) {
277 case UPLOAD_ERR_FORM_SIZE:
278 case UPLOAD_ERR_INI_SIZE:
279 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
280 return false;
281 break;
282
283 case UPLOAD_ERR_PARTIAL:
284 $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
285 return false;
286 break;
287
288 case UPLOAD_ERR_NO_FILE:
289 if ($this->getRequired()) {
290 if ((!isset($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index]))) {
291 $this->setAlert($lng->txt("form_msg_file_no_upload"));
292 return false;
293 }
294 }
295 break;
296
297 case UPLOAD_ERR_NO_TMP_DIR:
298 $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
299 return false;
300 break;
301
302 case UPLOAD_ERR_CANT_WRITE:
303 $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
304 return false;
305 break;
306
307 case UPLOAD_ERR_EXTENSION:
308 $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
309 return false;
310 break;
311 }
312 }
313 }
314 } else {
315 if ($this->getRequired()) {
316 $this->setAlert($lng->txt("form_msg_file_no_upload"));
317 return false;
318 }
319 }
320
321 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
322 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
323 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
324 if ($filename != '') {
325 $filename_arr = pathinfo($filename);
326 $suffix = $filename_arr["extension"];
327 $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
328 $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
329 // check suffixes
330 if (strlen($tmpname) && is_array($this->getSuffixes())) {
331 if (!in_array(strtolower($suffix), $this->getSuffixes())) {
332 $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
333 return false;
334 }
335 }
336 }
337 }
338 }
339
340 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
341 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
342 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
343 if ($filename != '') {
344 $filename_arr = pathinfo($filename);
345 $suffix = $filename_arr["extension"];
346 $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
347 $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
348 // virus handling
349 if (strlen($tmpname)) {
351 if ($vir[0] == false) {
352 $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
353 return false;
354 }
355 }
356 }
357 }
358 }
359 }
360 } else {
361 $this->setAlert($lng->txt("msg_input_is_required"));
362 return false;
363 }
364
365 return $this->checkSubItemsInput();
366 }
367
371 public function insert(ilTemplate $a_tpl): void
372 {
373 global $DIC;
374 $lng = $DIC['lng'];
375
376 $tpl = new ilTemplate("tpl.prop_singlechoicewizardinput.html", true, true, "Modules/TestQuestionPool");
377 $i = 0;
378 foreach ($this->values as $value) {
379 if ($this->getSingleline()) {
380 if (!$this->hideImages) {
381 if (strlen($value->getImage())) {
382 $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
383 if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
384 if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage())) {
385 $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
386 }
387 }
388 $tpl->setCurrentBlock('image');
389 $tpl->setVariable('SRC_IMAGE', $imagename);
390 $tpl->setVariable('IMAGE_NAME', $value->getImage());
391 $tpl->setVariable(
392 'ALT_IMAGE',
394 );
395 $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
396 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
397 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
398 $tpl->parseCurrentBlock();
399 }
400 $tpl->setCurrentBlock('addimage');
401 $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
402 $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
403 $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
404 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
405 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
406 $tpl->parseCurrentBlock();
407 }
408
409 if (is_object($value)) {
410 $tpl->setCurrentBlock("prop_text_propval");
411 $tpl->setVariable(
412 "PROPERTY_VALUE",
414 );
415 $tpl->parseCurrentBlock();
416 if ($this->getShowPoints()) {
417 $tpl->setCurrentBlock("prop_points_propval");
418 $tpl->setVariable(
419 "PROPERTY_VALUE",
421 );
422 $tpl->parseCurrentBlock();
423 }
424 $tpl->setCurrentBlock("prop_answer_id_propval");
425 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
426 $tpl->parseCurrentBlock();
427 }
428 $tpl->setCurrentBlock('singleline');
429 $tpl->setVariable("SIZE", $this->getSize());
430 $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
431 $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
432 $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
433 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
434 if ($this->getDisabled()) {
435 $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
436 }
437 $tpl->parseCurrentBlock();
438 } elseif (!$this->getSingleline()) {
439 if (is_object($value)) {
440 if ($this->getShowPoints()) {
441 $tpl->setCurrentBlock("prop_points_propval");
442 $tpl->setVariable(
443 "PROPERTY_VALUE",
445 );
446 $tpl->parseCurrentBlock();
447 }
448 $tpl->setCurrentBlock("prop_answer_id_propval");
449 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
450 $tpl->parseCurrentBlock();
451 }
452 $tpl->setCurrentBlock('multiline');
453 $tpl->setVariable(
454 "PROPERTY_VALUE",
456 );
457 $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
458 $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
459 $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
460 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
461 if ($this->getDisabled()) {
462 $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
463 }
464 $tpl->parseCurrentBlock();
465 }
466 if ($this->getAllowMove()) {
467 $tpl->setCurrentBlock("move");
468 $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
469 $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
470 $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
471 $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
472 $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
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 $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
487 $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
488 if ($this->getDisabled()) {
489 $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
490 }
491 $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
492 $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
493 $tpl->parseCurrentBlock();
494 $i++;
495 }
496
497 if ($this->getSingleline()) {
498 if (!$this->hideImages) {
499 if (is_array($this->getSuffixes())) {
500 $suff_str = $delim = "";
501 foreach ($this->getSuffixes() as $suffix) {
502 $suff_str .= $delim . "." . $suffix;
503 $delim = ", ";
504 }
505 $tpl->setCurrentBlock('allowed_image_suffixes');
506 $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
507 $tpl->parseCurrentBlock();
508 }
509 $tpl->setCurrentBlock("image_heading");
510 $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
511 $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
512 $tpl->parseCurrentBlock();
513 }
514 }
515
516 if ($this->getShowPoints()) {
517 $tpl->setCurrentBlock("points_heading");
518 $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
519 $tpl->parseCurrentBlock();
520 }
521
522 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
523 $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
524 $tpl->setVariable("TEXT_NO", $lng->txt('no'));
525 $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
526 $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
527 $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
528 $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
529
530 $a_tpl->setCurrentBlock("prop_generic");
531 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
532 $a_tpl->parseCurrentBlock();
533
534 global $DIC;
535 $tpl = $DIC['tpl'];
536 $tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
537 $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/singlechoicewizard.js");
538 }
539}
$filename
Definition: buildRTE.php:78
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
static getFileSizeInfo()
static get(string $a_glyph, string $a_text="")
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)
static _getUsedHTMLTagsAsString(string $a_module="")
Returns a string of all allowed HTML tags for text editing.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
checkInput()
Check input, strip slashes etc.
__construct($a_title='', $a_postvar='')
Constructor.
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)
global $DIC
Definition: feed.php:28
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc