ILIAS  trunk Revision v11.0_alpha-2638-g80c1d007f79
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 
34  $answers = $this->forms_helper->transformArray($a_value, 'answer', $this->refinery->kindlyTo()->string());
35  $points = $this->forms_helper->transformPoints($a_value);
36  $points_unchecked = $this->forms_helper->transformPoints($a_value, 'points_unchecked');
37  $images = $this->forms_helper->transformArray($a_value, 'imagename', $this->refinery->kindlyTo()->string());
38 
39  foreach ($answers as $index => $value) {
40  $answer = new ASS_AnswerMultipleResponseImage($value, $points[$index] ?? 0.0, $index);
41  $answer->setPointsChecked($points[$index] ?? 0.0);
42  $answer->setPointsUnchecked($points_unchecked[$index] ?? 0.0);
43 
44  if ($this->forms_helper->inArray($images, $index)) {
45  $answer->setImage($images[$index]);
46  }
47 
48  $this->values[] = $answer;
49  }
50  }
51 
56  public function checkInput(): bool
57  {
58  $data = $this->raw($this->getPostVar());
59 
60  if (!is_array($data)) {
61  $this->setAlert($this->lng->txt('msg_input_is_required'));
62  return false;
63  }
64 
65  // check points
66  $points = $this->forms_helper->checkPointsInputEnoughPositive($data, true);
67  if (!is_array($points)) {
68  $this->setAlert($this->lng->txt($points));
69  return false;
70  }
71  $points_unchecked = $this->forms_helper->checkPointsInput($data, true, 'points_unchecked');
72  if (!is_array($points_unchecked)) {
73  $this->setAlert($this->lng->txt($points_unchecked));
74  return false;
75  }
76 
77  // check answers
78  $answers = $this->checkAnswersInput($data);
79  if (!is_array($answers)) {
80  $this->setAlert($this->lng->txt($answers));
81  return false;
82  }
83  $image_names = $this->forms_helper->transformArray($data, 'imagename', $this->refinery->kindlyTo()->string());
84 
85  // check upload
86  if (is_array($_FILES) && count($_FILES) && $this->getSingleline()) {
87  if (!$this->hideImages) {
88  if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
89  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
90  // error handling
91  if ($error > 0) {
92  switch ($error) {
93  case UPLOAD_ERR_FORM_SIZE:
94  case UPLOAD_ERR_INI_SIZE:
95  $this->setAlert($this->lng->txt('form_msg_file_size_exceeds'));
96  return false;
97  break;
98 
99  case UPLOAD_ERR_PARTIAL:
100  $this->setAlert($this->lng->txt('form_msg_file_partially_uploaded'));
101  return false;
102  break;
103 
104  case UPLOAD_ERR_NO_FILE:
105  if (
106  !$this->forms_helper->inArray($image_names, $index)
107  && !$this->forms_helper->inArray($answers, $index)
108  && $this->getRequired()
109  ) {
110  $this->setAlert($this->lng->txt('form_msg_file_no_upload'));
111  return false;
112  }
113  break;
114 
115  case UPLOAD_ERR_NO_TMP_DIR:
116  $this->setAlert($this->lng->txt('form_msg_file_missing_tmp_dir'));
117  return false;
118  break;
119 
120  case UPLOAD_ERR_CANT_WRITE:
121  $this->setAlert($this->lng->txt('form_msg_file_cannot_write_to_disk'));
122  return false;
123  break;
124 
125  case UPLOAD_ERR_EXTENSION:
126  $this->setAlert($this->lng->txt('form_msg_file_upload_stopped_ext'));
127  return false;
128  break;
129  }
130  }
131  }
132  } else {
133  if ($this->getRequired()) {
134  $this->setAlert($this->lng->txt('form_msg_file_no_upload'));
135  return false;
136  }
137  }
138 
139  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
140  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
141  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
142  $filename_arr = pathinfo($filename);
143  if (isset($filename_arr['extension'])) {
144  $suffix = $filename_arr['extension'];
145  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
146  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
147  // check suffixes
148  if (strlen($tmpname) && is_array($this->getSuffixes())) {
149  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
150  $this->setAlert($this->lng->txt('form_msg_file_wrong_file_type'));
151  return false;
152  }
153  }
154  }
155  }
156  }
157 
158  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
159  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
160  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
161  $filename_arr = pathinfo($filename);
162  if (isset($filename_arr['extension'])) {
163  $suffix = $filename_arr['extension'];
164  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
165  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
166  // virus handling
167  if (strlen($tmpname)) {
168  $vir = ilVirusScanner::virusHandling($tmpname, $filename);
169  if ($vir[0] == false) {
170  $this->setAlert($this->lng->txt('form_msg_file_virus_found') . '<br />' . $vir[1]);
171  return false;
172  }
173  }
174  }
175  }
176  }
177  }
178  }
179 
180  return $this->checkSubItemsInput();
181  }
182 
187  public function insert(ilTemplate $a_tpl): void
188  {
189  global $DIC;
190  $lng = $DIC['lng'];
191 
192  $tpl = new ilTemplate("tpl.prop_multiplechoicewizardinput.html", true, true, "components/ILIAS/TestQuestionPool");
193  $i = 0;
194  foreach ($this->values as $value) {
195  if ($this->getSingleline()) {
196  if (!$this->hideImages) {
197  if ($value->hasImage()) {
198  $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
199  if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
200  if (file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage())) {
201  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
202  }
203  }
204  $tpl->setCurrentBlock('image');
205  $tpl->setVariable('SRC_IMAGE', $imagename);
206  $tpl->setVariable('IMAGE_NAME', $value->getImage());
207  $tpl->setVariable(
208  'ALT_IMAGE',
209  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
210  );
211  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
212  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
213  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
214  $tpl->parseCurrentBlock();
215  }
216  $tpl->setCurrentBlock('addimage');
217  $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
218  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
219  $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
220  $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
221  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
222  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
223  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
224  $tpl->parseCurrentBlock();
225  }
226 
227  if (is_object($value)) {
228  $tpl->setCurrentBlock("prop_text_propval");
229  $tpl->setVariable(
230  "PROPERTY_VALUE",
231  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
232  );
233  $tpl->parseCurrentBlock();
234  $tpl->setCurrentBlock("prop_points_propval");
235  $tpl->setVariable(
236  "PROPERTY_VALUE",
237  ilLegacyFormElementsUtil::prepareFormOutput($value->getPointsChecked())
238  );
239  $tpl->parseCurrentBlock();
240  $tpl->setCurrentBlock("prop_points_unchecked_propval");
241  $tpl->setVariable(
242  "PROPERTY_VALUE",
243  ilLegacyFormElementsUtil::prepareFormOutput($value->getPointsUnchecked())
244  );
245  $tpl->parseCurrentBlock();
246  $tpl->setCurrentBlock("prop_answer_id_propval");
247  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
248  $tpl->parseCurrentBlock();
249  }
250  $tpl->setCurrentBlock('singleline');
251  $tpl->setVariable("SIZE", $this->getSize());
252  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
253  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
254  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
255  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
256  if ($this->getDisabled()) {
257  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
258  }
259  $tpl->parseCurrentBlock();
260  } elseif (!$this->getSingleline()) {
261  if (is_object($value)) {
262  $tpl->setCurrentBlock("prop_points_propval");
263  $tpl->setVariable(
264  "PROPERTY_VALUE",
266  );
267  $tpl->parseCurrentBlock();
268  $tpl->setCurrentBlock("prop_points_unchecked_propval");
269  $tpl->setVariable(
270  "PROPERTY_VALUE",
271  ilLegacyFormElementsUtil::prepareFormOutput($value->getPointsUnchecked())
272  );
273  $tpl->parseCurrentBlock();
274  $tpl->setCurrentBlock("prop_answer_id_propval");
275  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getId()));
276  $tpl->parseCurrentBlock();
277  }
278  $tpl->setCurrentBlock('multiline');
279  $tpl->setVariable(
280  "PROPERTY_VALUE",
281  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
282  );
283  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
284  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
285  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
286  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
287  if ($this->getDisabled()) {
288  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
289  }
290  $tpl->parseCurrentBlock();
291  }
292  if ($this->getAllowMove()) {
293  $tpl->setCurrentBlock("move");
294  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
295  $tpl->setVariable("UP_BUTTON", $this->renderer->render(
296  $this->glyph_factory->up()->withAction('#')
297  ));
298  $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
299  $this->glyph_factory->down()->withAction('#')
300  ));
301  $tpl->parseCurrentBlock();
302  }
303  $tpl->setCurrentBlock("row");
304  $tpl->setVariable("POST_VAR", $this->getPostVar());
305  $tpl->setVariable("ROW_NUMBER", $i);
306  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
307  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
308  $tpl->setVariable("POINTS_UNCHECKED_ID", $this->getPostVar() . "[points_unchecked][$i]");
309  if ($this->getDisabled()) {
310  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
311  }
312  $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
313  $this->glyph_factory->add()->withAction('#')
314  ));
315  $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
316  $this->glyph_factory->remove()->withAction('#')
317  ));
318  $tpl->parseCurrentBlock();
319  $i++;
320  }
321 
322  if ($this->getSingleline()) {
323  if (!$this->hideImages) {
324  if (is_array($this->getSuffixes())) {
325  $suff_str = $delim = "";
326  foreach ($this->getSuffixes() as $suffix) {
327  $suff_str .= $delim . "." . $suffix;
328  $delim = ", ";
329  }
330  $tpl->setCurrentBlock('allowed_image_suffixes');
331  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
332  $tpl->parseCurrentBlock();
333  }
334 
335  $tpl->setCurrentBlock("image_heading");
336  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
337  $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
338  $tpl->parseCurrentBlock();
339  }
340  }
341 
342  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
343  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
344  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
345  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
346  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
347  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
348  $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
349  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
350  $tpl->setVariable("POINTS_CHECKED_TEXT", $lng->txt('checkbox_checked'));
351  $tpl->setVariable("POINTS_UNCHECKED_TEXT", $lng->txt('checkbox_unchecked'));
352 
353  $a_tpl->setCurrentBlock("prop_generic");
354  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
355  $a_tpl->parseCurrentBlock();
356 
357  global $DIC;
358  $tpl = $DIC['tpl'];
359  $tpl->addJavascript("assets/js/answerwizardinput.js");
360  $tpl->addJavascript("assets/js/multiplechoicewizard.js");
361  }
362 
363  public function setPending(string $a_val): void
364  {
365  $this->pending = $a_val;
366  }
367 
368  public function getPending(): string
369  {
370  return $this->pending;
371  }
372 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
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...
checkAnswersInput(array $data)
Checks the input of the answers and returns the answers as an array if the input is valid or a string...
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.
renderer()
static prepareFormOutput($a_str, bool $a_strip=false)
checkInput()
Check input, strip slashes etc.
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
global $DIC
Definition: shib_login.php:26
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.