ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMultipleChoiceWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 
27 {
28  private string $pending;
29 
30  public function setValue($a_value): void
31  {
32  $this->values = [];
33  if (is_array($a_value) && isset($a_value['answer']) && is_array($a_value['answer'])) {
34  foreach ($a_value['answer'] as $index => $value) {
35  $answer = new ASS_AnswerMultipleResponseImage($value, (float) ($a_value['points'][$index] ?? 0), $index);
36  $answer->setPointsChecked((float) ($a_value['points'][$index] ?? 0));
37  $answer->setPointsUnchecked((float) ($a_value['points_unchecked'][$index] ?? 0));
38  if (isset($a_value['imagename'][$index])) {
39  $answer->setImage($a_value['imagename'][$index]);
40  }
41  $this->values[] = $answer;
42  }
43  }
44  }
45 
50  public function checkInput(): bool
51  {
52  global $DIC;
53  $lng = $DIC['lng'];
54 
55  $post_var = $this->getPostVar();
56  if (!$this->post_wrapper->has($post_var)) {
57  return false;
58  }
59 
60  $values = $this->post_wrapper->retrieve($post_var, $this->refinery->custom()->transformation(fn($v) => $v));
61 
62  $values = ilArrayUtil::stripSlashesRecursive( //TODO: move into transform
63  $values,
64  false,
66  );
67 
68  $foundvalues = $values;
69 
70  if (is_array($foundvalues)) {
71  // check answers
72  if (is_array($foundvalues['answer'])) {
73  foreach ($foundvalues['answer'] as $aidx => $answervalue) {
74  if (((strlen($answervalue)) == 0) && (!isset($foundvalues['imagename']) || !isset($foundvalues['imagename'][$aidx]) || strlen($foundvalues['imagename'][$aidx]) == 0)) {
75  $this->setAlert($lng->txt("msg_input_is_required"));
76  return false;
77  }
78 
79  if (mb_strlen($answervalue) > $this->getMaxLength()) {
80  $this->setAlert($lng->txt("msg_input_char_limit_max"));
81  return false;
82  }
83  }
84  }
85  // check points
86  $max = 0;
87  if (is_array($foundvalues['points'])) {
88  foreach ($foundvalues['points'] as $points) {
89  $points = str_replace(',', '.', $points);
90  if ($points > $max) {
91  $max = $points;
92  }
93  if (((strlen($points)) == 0) || (!is_numeric($points))) {
94  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
95  return false;
96  }
97  }
98  foreach ($foundvalues['points_unchecked'] as $points) {
99  $points = str_replace(',', '.', $points);
100  if (((strlen($points)) == 0) || (!is_numeric($points))) {
101  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
102  return false;
103  }
104  }
105  }
106  if ($max == 0) {
107  $this->setAlert($lng->txt("enter_enough_positive_points_checked"));
108  return false;
109  }
110 
111  if (is_array($_FILES) && count($_FILES) && $this->getSingleline()) {
112  if (!$this->hideImages) {
113  if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
114  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
115  // error handling
116  if ($error > 0) {
117  switch ($error) {
118  case UPLOAD_ERR_FORM_SIZE:
119  case UPLOAD_ERR_INI_SIZE:
120  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
121  return false;
122  break;
123 
124  case UPLOAD_ERR_PARTIAL:
125  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
126  return false;
127  break;
128 
129  case UPLOAD_ERR_NO_FILE:
130  if ($this->getRequired()) {
131  if (isset($a_value['imagename']) && (!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index]))) {
132  $this->setAlert($lng->txt("form_msg_file_no_upload"));
133  return false;
134  }
135  }
136  break;
137 
138  case UPLOAD_ERR_NO_TMP_DIR:
139  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
140  return false;
141  break;
142 
143  case UPLOAD_ERR_CANT_WRITE:
144  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
145  return false;
146  break;
147 
148  case UPLOAD_ERR_EXTENSION:
149  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
150  return false;
151  break;
152  }
153  }
154  }
155  } else {
156  if ($this->getRequired()) {
157  $this->setAlert($lng->txt("form_msg_file_no_upload"));
158  return false;
159  }
160  }
161 
162  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
163  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
164  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
165  $filename_arr = pathinfo($filename);
166  if (isset($filename_arr["extension"])) {
167  $suffix = $filename_arr["extension"];
168  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
169  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
170  // check suffixes
171  if (strlen($tmpname) && is_array($this->getSuffixes())) {
172  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
173  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
174  return false;
175  }
176  }
177  }
178  }
179  }
180 
181  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
182  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
183  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
184  $filename_arr = pathinfo($filename);
185  if (isset($filename_arr["extension"])) {
186  $suffix = $filename_arr["extension"];
187  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
188  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
189  // virus handling
190  if (strlen($tmpname)) {
191  $vir = ilVirusScanner::virusHandling($tmpname, $filename);
192  if ($vir[0] == false) {
193  $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
194  return false;
195  }
196  }
197  }
198  }
199  }
200  }
201  }
202  } else {
203  $this->setAlert($lng->txt("msg_input_is_required"));
204  return false;
205  }
206 
207  return $this->checkSubItemsInput();
208  }
209 
214  public function insert(ilTemplate $a_tpl): void
215  {
216  global $DIC;
217  $lng = $DIC['lng'];
218 
219  $tpl = new ilTemplate("tpl.prop_multiplechoicewizardinput.html", true, true, "Modules/TestQuestionPool");
220  $i = 0;
221  foreach ($this->values as $value) {
222  if ($this->getSingleline()) {
223  if (!$this->hideImages) {
224  if ($value->hasImage()) {
225  $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
226  if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
227  if (file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage())) {
228  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
229  }
230  }
231  $tpl->setCurrentBlock('image');
232  $tpl->setVariable('SRC_IMAGE', $imagename);
233  $tpl->setVariable('IMAGE_NAME', $value->getImage());
234  $tpl->setVariable(
235  'ALT_IMAGE',
236  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
237  );
238  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
239  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
240  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
241  $tpl->parseCurrentBlock();
242  }
243  $tpl->setCurrentBlock('addimage');
244  $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
245  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
246  $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
247  $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
248  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
249  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
250  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
251  $tpl->parseCurrentBlock();
252  }
253 
254  if (is_object($value)) {
255  $tpl->setCurrentBlock("prop_text_propval");
256  $tpl->setVariable(
257  "PROPERTY_VALUE",
258  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
259  );
260  $tpl->parseCurrentBlock();
261  $tpl->setCurrentBlock("prop_points_propval");
262  $tpl->setVariable(
263  "PROPERTY_VALUE",
264  ilLegacyFormElementsUtil::prepareFormOutput($value->getPointsChecked())
265  );
266  $tpl->parseCurrentBlock();
267  $tpl->setCurrentBlock("prop_points_unchecked_propval");
268  $tpl->setVariable(
269  "PROPERTY_VALUE",
270  ilLegacyFormElementsUtil::prepareFormOutput($value->getPointsUnchecked())
271  );
272  $tpl->parseCurrentBlock();
273  $tpl->setCurrentBlock("prop_answer_id_propval");
274  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
275  $tpl->parseCurrentBlock();
276  }
277  $tpl->setCurrentBlock('singleline');
278  $tpl->setVariable("SIZE", $this->getSize());
279  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
280  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
281  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
282  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
283  if ($this->getDisabled()) {
284  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
285  }
286  $tpl->parseCurrentBlock();
287  } elseif (!$this->getSingleline()) {
288  if (is_object($value)) {
289  $tpl->setCurrentBlock("prop_points_propval");
290  $tpl->setVariable(
291  "PROPERTY_VALUE",
293  );
294  $tpl->parseCurrentBlock();
295  $tpl->setCurrentBlock("prop_points_unchecked_propval");
296  $tpl->setVariable(
297  "PROPERTY_VALUE",
298  ilLegacyFormElementsUtil::prepareFormOutput($value->getPointsUnchecked())
299  );
300  $tpl->parseCurrentBlock();
301  $tpl->setCurrentBlock("prop_answer_id_propval");
302  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
303  $tpl->parseCurrentBlock();
304  }
305  $tpl->setCurrentBlock('multiline');
306  $tpl->setVariable(
307  "PROPERTY_VALUE",
308  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
309  );
310  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
311  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
312  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
313  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
314  if ($this->getDisabled()) {
315  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
316  }
317  $tpl->parseCurrentBlock();
318  }
319  if ($this->getAllowMove()) {
320  $tpl->setCurrentBlock("move");
321  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
322  $tpl->setVariable("UP_BUTTON", $this->renderer->render(
323  $this->glyph_factory->up()->withAction('#')
324  ));
325  $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
326  $this->glyph_factory->down()->withAction('#')
327  ));
328  $tpl->parseCurrentBlock();
329  }
330  $tpl->setCurrentBlock("row");
331  $tpl->setVariable("POST_VAR", $this->getPostVar());
332  $tpl->setVariable("ROW_NUMBER", $i);
333  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
334  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
335  $tpl->setVariable("POINTS_UNCHECKED_ID", $this->getPostVar() . "[points_unchecked][$i]");
336  if ($this->getDisabled()) {
337  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
338  }
339  $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
340  $this->glyph_factory->add()->withAction('#')
341  ));
342  $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
343  $this->glyph_factory->remove()->withAction('#')
344  ));
345  $tpl->parseCurrentBlock();
346  $i++;
347  }
348 
349  if ($this->getSingleline()) {
350  if (!$this->hideImages) {
351  if (is_array($this->getSuffixes())) {
352  $suff_str = $delim = "";
353  foreach ($this->getSuffixes() as $suffix) {
354  $suff_str .= $delim . "." . $suffix;
355  $delim = ", ";
356  }
357  $tpl->setCurrentBlock('allowed_image_suffixes');
358  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
359  $tpl->parseCurrentBlock();
360  }
361 
362  $tpl->setCurrentBlock("image_heading");
363  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
364  $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
365  $tpl->parseCurrentBlock();
366  }
367  }
368 
369  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
370  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
371  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
372  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
373  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
374  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
375  $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
376  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
377  $tpl->setVariable("POINTS_CHECKED_TEXT", $lng->txt('checkbox_checked'));
378  $tpl->setVariable("POINTS_UNCHECKED_TEXT", $lng->txt('checkbox_unchecked'));
379 
380  $a_tpl->setCurrentBlock("prop_generic");
381  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
382  $a_tpl->parseCurrentBlock();
383 
384  global $DIC;
385  $tpl = $DIC['tpl'];
386  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/answerwizardinput.js");
387  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/multiplechoicewizard.js");
388  }
389 
390  public function setPending(string $a_val): void
391  {
392  $this->pending = $a_val;
393  }
394 
395  public function getPending(): string
396  {
397  return $this->pending;
398  }
399 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
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...
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
ASS_AnswerBinaryStateImage is a class for answers with a binary state indicator (checked/unchecked, set/unset) and an image file.
static prepareFormOutput($a_str, bool $a_strip=false)
checkInput()
Check input, strip slashes etc.
global $DIC
Definition: feed.php:28
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()
insert(ilTemplate $a_tpl)
Insert property html.
$filename
Definition: buildRTE.php:78
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This class represents a single choice wizard property in a property form.