ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilSingleChoiceWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 
21 use ILIAS\UI\Component\Symbol\Glyph\Factory as GlyphFactory;
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;
43  protected Renderer $renderer;
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->post_wrapper = $DIC->http()->wrapper()->post();
62  $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
63  $this->renderer = $DIC->ui()->renderer();
64  $this->upload_limit = $DIC['ui.upload_limit_resolver'];
65  }
66 
67  public function setValue($a_value): void
68  {
69  $this->values = array();
70  if (is_array($a_value)) {
71  if (is_array($a_value['answer'])) {
72  foreach ($a_value['answer'] as $index => $value) {
73  $points = (float) str_replace(",", ".", $a_value['points'][$index]);
74  $answer = new ASS_AnswerBinaryStateImage(
75  (string) $value,
76  $points,
77  (int) $index,
78  true,
79  null,
80  (int) $a_value['answer_id'][$index]
81  );
82  if (isset($a_value['imagename'][$index])) {
83  $answer->setImage($a_value['imagename'][$index]);
84  }
85  $this->values[] = $answer;
86  }
87  }
88  }
89  }
90 
91 
92  public function setValueByArray(array $a_values): void
93  {
94  if (isset($a_values[$this->getPostVar()])) {
95  $this->setValue($a_values[$this->getPostVar()] ?? []);
96  }
97  }
98 
104  public function setSuffixes($a_suffixes): void
105  {
106  $this->suffixes = $a_suffixes;
107  }
108 
114  public function setHideImages($a_hide): void
115  {
116  $this->hideImages = $a_hide;
117  }
118 
124  public function getSuffixes(): array
125  {
126  return $this->suffixes;
127  }
128 
129  public function setShowPoints($a_value): void
130  {
131  $this->showPoints = $a_value;
132  }
133 
134  public function getShowPoints(): bool
135  {
136  return $this->showPoints;
137  }
138 
144  public function setValues($a_values): void
145  {
146  $this->values = $a_values;
147  }
148 
154  public function getValues(): array
155  {
156  return $this->values;
157  }
158 
164  public function setSingleline($a_value): void
165  {
166  $this->singleline = $a_value;
167  }
168 
174  public function getSingleline(): bool
175  {
176  return $this->singleline;
177  }
178 
184  public function setQuestionObject($a_value): void
185  {
186  $this->qstObject = &$a_value;
187  }
188 
194  public function getQuestionObject(): ?object
195  {
196  return $this->qstObject;
197  }
198 
204  public function setAllowMove($a_allow_move): void
205  {
206  $this->allowMove = $a_allow_move;
207  }
208 
214  public function getAllowMove(): bool
215  {
216  return $this->allowMove;
217  }
218 
219 
220  // Set pending filename value
221  public function setPending(string $val): void
222  {
227  }
228 
233  public function checkInput(): bool
234  {
235  global $DIC;
236  $lng = $DIC['lng'];
237 
238  if (is_array($_POST[$this->getPostVar()])) {
239  $foundvalues = ilArrayUtil::stripSlashesRecursive(
240  $_POST[$this->getPostVar()],
241  true,
243  );
244  }
245  //$foundvalues = $_POST[$this->getPostVar()];
246  if (is_array($foundvalues)) {
247  // check answers
248  if (is_array($foundvalues['answer'])) {
249  foreach ($foundvalues['answer'] as $aidx => $answervalue) {
250  if (((strlen($answervalue)) == 0) && (!isset($foundvalues['imagename'][$aidx]) || strlen($foundvalues['imagename'][$aidx]) == 0)) {
251  $this->setAlert($lng->txt("msg_input_is_required"));
252  return false;
253  }
254 
255  if (mb_strlen($answervalue) > $this->getMaxLength()) {
256  $this->setAlert($lng->txt("msg_input_char_limit_max"));
257  return false;
258  }
259  }
260  }
261  // check points
262  $max = 0;
263  if (is_array($foundvalues['points'])) {
264  foreach ($foundvalues['points'] as $points) {
265  $points = str_replace(',', '.', $points);
266  if ($points > $max) {
267  $max = $points;
268  }
269  if (((strlen($points)) == 0) || (!is_numeric($points))) {
270  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
271  return false;
272  }
273  }
274  }
275  if ($max == 0) {
276  $this->setAlert($lng->txt("enter_enough_positive_points"));
277  return false;
278  }
279 
280  if (is_array($_FILES) && count($_FILES) && $this->getSingleline() && (!$this->hideImages)) {
281  if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
282  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
283  // error handling
284  if ($error > 0) {
285  switch ($error) {
286  case UPLOAD_ERR_FORM_SIZE:
287  case UPLOAD_ERR_INI_SIZE:
288  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
289  return false;
290  break;
291 
292  case UPLOAD_ERR_PARTIAL:
293  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
294  return false;
295  break;
296 
297  case UPLOAD_ERR_NO_FILE:
298  if ($this->getRequired()) {
299  if ((!isset($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index]))) {
300  $this->setAlert($lng->txt("form_msg_file_no_upload"));
301  return false;
302  }
303  }
304  break;
305 
306  case UPLOAD_ERR_NO_TMP_DIR:
307  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
308  return false;
309  break;
310 
311  case UPLOAD_ERR_CANT_WRITE:
312  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
313  return false;
314  break;
315 
316  case UPLOAD_ERR_EXTENSION:
317  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
318  return false;
319  break;
320  }
321  }
322  }
323  } else {
324  if ($this->getRequired()) {
325  $this->setAlert($lng->txt("form_msg_file_no_upload"));
326  return false;
327  }
328  }
329 
330  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
331  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
332  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
333  if ($filename != '') {
334  $filename_arr = pathinfo($filename);
335  $suffix = $filename_arr["extension"];
336  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
337  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
338  // check suffixes
339  if (strlen($tmpname) && is_array($this->getSuffixes())) {
340  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
341  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
342  return false;
343  }
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 (strlen($tmpname)) {
359  $vir = ilVirusScanner::virusHandling($tmpname, $filename);
360  if ($vir[0] == false) {
361  $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
362  return false;
363  }
364  }
365  }
366  }
367  }
368  }
369  } else {
370  $this->setAlert($lng->txt("msg_input_is_required"));
371  return false;
372  }
373 
374  return $this->checkSubItemsInput();
375  }
376 
380  public function insert(ilTemplate $a_tpl): void
381  {
382  global $DIC;
383  $lng = $DIC['lng'];
384 
385  $tpl = new ilTemplate("tpl.prop_singlechoicewizardinput.html", true, true, "Modules/TestQuestionPool");
386  $i = 0;
387  foreach ($this->values as $value) {
388  if ($this->getSingleline()) {
389  if (!$this->hideImages) {
390  if ($value->getImage()) {
391  $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
392  if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
393  if (file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage())) {
394  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
395  }
396  }
397  $tpl->setCurrentBlock('image');
398  $tpl->setVariable('SRC_IMAGE', $imagename);
399  $tpl->setVariable('IMAGE_NAME', $value->getImage());
400  $tpl->setVariable(
401  'ALT_IMAGE',
402  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
403  );
404  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
405  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
406  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
407  $tpl->parseCurrentBlock();
408  }
409  $tpl->setCurrentBlock('addimage');
410  $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
411  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
412  $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
413  $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
414  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
415  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
416  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
417  $tpl->parseCurrentBlock();
418  }
419 
420  if (is_object($value)) {
421  $tpl->setCurrentBlock("prop_text_propval");
422  $tpl->setVariable(
423  "PROPERTY_VALUE",
424  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
425  );
426  $tpl->parseCurrentBlock();
427  if ($this->getShowPoints()) {
428  $tpl->setCurrentBlock("prop_points_propval");
429  $tpl->setVariable(
430  "PROPERTY_VALUE",
432  );
433  $tpl->parseCurrentBlock();
434  }
435  $tpl->setCurrentBlock("prop_answer_id_propval");
436  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
437  $tpl->parseCurrentBlock();
438  }
439  $tpl->setCurrentBlock('singleline');
440  $tpl->setVariable("SIZE", $this->getSize());
441  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
442  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
443  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
444  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
445  if ($this->getDisabled()) {
446  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
447  }
448  $tpl->parseCurrentBlock();
449  } elseif (!$this->getSingleline()) {
450  if (is_object($value)) {
451  if ($this->getShowPoints()) {
452  $tpl->setCurrentBlock("prop_points_propval");
453  $tpl->setVariable(
454  "PROPERTY_VALUE",
456  );
457  $tpl->parseCurrentBlock();
458  }
459  $tpl->setCurrentBlock("prop_answer_id_propval");
460  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
461  $tpl->parseCurrentBlock();
462  }
463  $tpl->setCurrentBlock('multiline');
464  $tpl->setVariable(
465  "PROPERTY_VALUE",
466  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
467  );
468  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
469  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
470  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
471  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
472  if ($this->getDisabled()) {
473  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
474  }
475  $tpl->parseCurrentBlock();
476  }
477  if ($this->getAllowMove()) {
478  $tpl->setCurrentBlock("move");
479  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
480  $tpl->setVariable("UP_BUTTON", $this->renderer->render(
481  $this->glyph_factory->up()->withAction('#')
482  ));
483  $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
484  $this->glyph_factory->down()->withAction('#')
485  ));
486  $tpl->parseCurrentBlock();
487  }
488  if ($this->getShowPoints()) {
489  $tpl->setCurrentBlock("points");
490  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
491  $tpl->setVariable("POINTS_POST_VAR", $this->getPostVar());
492  $tpl->setVariable("POINTS_ROW_NUMBER", $i);
493  $tpl->parseCurrentBlock();
494  }
495  $tpl->setCurrentBlock("row");
496  $tpl->setVariable("POST_VAR", $this->getPostVar());
497  $tpl->setVariable("ROW_NUMBER", $i);
498  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
499  if ($this->getDisabled()) {
500  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
501  }
502  $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
503  $this->glyph_factory->add()->withAction('#')
504  ));
505  $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
506  $this->glyph_factory->remove()->withAction('#')
507  ));
508  $tpl->parseCurrentBlock();
509  $i++;
510  }
511 
512  if ($this->getSingleline()) {
513  if (!$this->hideImages) {
514  if (is_array($this->getSuffixes())) {
515  $suff_str = $delim = "";
516  foreach ($this->getSuffixes() as $suffix) {
517  $suff_str .= $delim . "." . $suffix;
518  $delim = ", ";
519  }
520  $tpl->setCurrentBlock('allowed_image_suffixes');
521  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
522  $tpl->parseCurrentBlock();
523  }
524  $tpl->setCurrentBlock("image_heading");
525  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
526  $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
527  $tpl->parseCurrentBlock();
528  }
529  }
530 
531  if ($this->getShowPoints()) {
532  $tpl->setCurrentBlock("points_heading");
533  $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
534  $tpl->parseCurrentBlock();
535  }
536 
537  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
538  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
539  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
540  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
541  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
542  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
543  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
544 
545  $a_tpl->setCurrentBlock("prop_generic");
546  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
547  $a_tpl->parseCurrentBlock();
548 
549  global $DIC;
550  $tpl = $DIC['tpl'];
551  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizardinput.js");
552  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/singlechoicewizard.js");
553  }
554 }
setQuestionObject($a_value)
Set question object.
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
An entity that renders components to a string output.
Definition: Renderer.php:30
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
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...
Class for answers with a binary state indicator.
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:26
__construct($a_title='', $a_postvar='')
Constructor.
static prepareFormOutput($a_str, bool $a_strip=false)
global $DIC
Definition: feed.php:28
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
static _getUsedHTMLTagsAsString(string $a_module="")
Returns a string of all allowed HTML tags for text editing.
static getFileSizeInfo()
$filename
Definition: buildRTE.php:78
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setSuffixes($a_suffixes)
Set Accepted Suffixes.
setMaxLength(?int $a_maxlength)
This class represents a single choice wizard property in a property form.
checkInput()
Check input, strip slashes etc.