ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilKprimChoiceWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 
26 {
27  public const CUSTOM_UPLOAD_ERR = 99;
28 
32  protected $tpl;
33 
37  protected $qstObject;
38 
39  private $files;
40 
42 
43  public function __construct($a_title = "", $a_postvar = "")
44  {
45  parent::__construct($a_title, $a_postvar);
46 
47  global $DIC;
48  $lng = $DIC['lng'];
49  $tpl = $DIC['tpl'];
50 
51  $this->lng = $lng;
52  $this->tpl = $tpl;
53 
54  $this->files = [];
55 
56  $this->ignoreMissingUploadsEnabled = false;
57  }
58 
59  public function setFiles($files): void
60  {
61  $this->files = $files;
62  }
63 
64  public function getFiles(): array
65  {
66  return $this->files;
67  }
68 
70  {
71  $this->ignoreMissingUploadsEnabled = $ignoreMissingUploadsEnabled;
72  }
73 
74  public function isIgnoreMissingUploadsEnabled(): bool
75  {
77  }
78 
79  public function setValue($value): void
80  {
81  $this->values = [];
82 
83  $is_rte = isset($_POST["answer_type"]) && $_POST["answer_type"] == "multiLine";
84  $a_value = $this->cleanupAnswerText($value, $is_rte);
85 
86  if (is_array($a_value) && is_array($a_value['answer'])) {
87  foreach ($a_value['answer'] as $index => $value) {
88  $answer = new ilAssKprimChoiceAnswer();
89 
90  $answer->setPosition($index);
91  $answer->setAnswertext($value);
92  if (isset($a_value['imagename'])) {
93  $answer->setImageFile($a_value['imagename'][$index] ?? '');
94  }
95 
96  if (isset($a_value['correctness']) && isset($a_value['correctness'][$index]) && strlen($a_value['correctness'][$index])) {
97  $answer->setCorrectness((bool) $a_value['correctness'][$index]);
98  }
99 
100  $answer->setThumbPrefix($this->qstObject->getThumbPrefix());
101  $answer->setImageFsDir($this->qstObject->getImagePath());
102  $answer->setImageWebDir($this->qstObject->getImagePathWeb());
103 
104  $this->values[] = $answer;
105  }
106  }
107 
108  #vd($this->values);
109  }
110 
111  public function checkInput(): bool
112  {
113  global $DIC;
114  $lng = $DIC['lng'];
115 
116  if (is_array($_POST[$this->getPostVar()])) {
117  $foundvalues = ilArrayUtil::stripSlashesRecursive(
118  $_POST[$this->getPostVar()],
119  false,
121  );
122  } else {
123  $foundvalues = $_POST[$this->getPostVar()];
124  }
125 
126  if (is_array($foundvalues)) {
127  // check answers
128  if (is_array($foundvalues['answer'])) {
129  foreach ($foundvalues['answer'] as $aidx => $answervalue) {
130  $hasImage = isset($foundvalues['imagename']) ? true : false;
131  if (((strlen($answervalue)) == 0) && !$hasImage) {
132  $this->setAlert($lng->txt("msg_input_is_required"));
133  return false;
134  }
135 
136  if (mb_strlen($answervalue) > $this->getMaxLength()) {
137  $this->setAlert($lng->txt("msg_input_char_limit_max"));
138  return false;
139  }
140  }
141  }
142 
143  // check correctness
144  if (!isset($foundvalues['correctness']) || count($foundvalues['correctness']) < count($foundvalues['answer'])) {
145  $this->setAlert($lng->txt("msg_input_is_required"));
146  return false;
147  }
148 
149  if (!$this->checkUploads($foundvalues)) {
150  return false;
151  }
152  } else {
153  $this->setAlert($lng->txt("msg_input_is_required"));
154  return false;
155  }
156 
157  return $this->checkSubItemsInput();
158  }
159 
163  public function insert(ilTemplate $a_tpl): void
164  {
165  $tpl = new ilTemplate("tpl.prop_kprimchoicewizardinput.html", true, true, "Modules/TestQuestionPool");
166 
167  foreach ($this->values as $value) {
172  if ($this->getSingleline()) {
173  if (!$this->hideImages) {
174  if ($value->getImageFile() !== null
175  && $value->getImageFile() !== '') {
176  $imagename = $value->getImageWebPath();
177 
178  if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
179  if (@file_exists($value->getThumbFsPath())) {
180  $imagename = $value->getThumbWebPath();
181  }
182  }
183 
184  $tpl->setCurrentBlock('image');
185  $tpl->setVariable('SRC_IMAGE', $imagename);
186  $tpl->setVariable('IMAGE_NAME', $value->getImageFile());
187  $tpl->setVariable(
188  'ALT_IMAGE',
189  ilLegacyFormElementsUtil::prepareFormOutput($value->getAnswertext())
190  );
191  $tpl->setVariable("TXT_DELETE_EXISTING", $this->lng->txt("delete_existing_file"));
192  $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
193  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
194  $tpl->parseCurrentBlock();
195  }
196  $tpl->setCurrentBlock('addimage');
197  $tpl->setVariable("IMAGE_BROWSE", $this->lng->txt('select_file'));
198  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][{$value->getPosition()}]");
199  $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
200  $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
201  $tpl->setVariable("IMAGE_SUBMIT", $this->lng->txt("upload"));
202  $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
203  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
204  $tpl->parseCurrentBlock();
205  }
206 
207  $tpl->setCurrentBlock("prop_text_propval");
208  $tpl->setVariable(
209  "PROPERTY_VALUE",
210  ilLegacyFormElementsUtil::prepareFormOutput(htmlspecialchars_decode((string) $value->getAnswertext()))
211  );
212  $tpl->parseCurrentBlock();
213 
214  $tpl->setCurrentBlock('singleline');
215  $tpl->setVariable("SIZE", $this->getSize());
216  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
217  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $value->getPosition());
218  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
219  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
220  if ($this->getDisabled()) {
221  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
222  }
223  $tpl->parseCurrentBlock();
224  } elseif (!$this->getSingleline()) {
225  $tpl->setCurrentBlock('multiline');
226  $tpl->setVariable(
227  "PROPERTY_VALUE",
228  ilLegacyFormElementsUtil::prepareFormOutput((string) $value->getAnswertext())
229  );
230  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
231  $tpl->setVariable("MULTILINE_ROW_NUMBER", $value->getPosition());
232  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
233  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
234  if ($this->getDisabled()) {
235  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
236  }
237  $tpl->parseCurrentBlock();
238  }
239  if ($this->getAllowMove()) {
240  $tpl->setCurrentBlock("move");
241  $tpl->setVariable("UP_ID", "up_{$this->getPostVar()}[{$value->getPosition()}]");
242  $tpl->setVariable("DOWN_ID", "down_{$this->getPostVar()}[{$value->getPosition()}]");
243  $tpl->setVariable("UP_BUTTON", $this->renderer->render(
244  $this->glyph_factory->up()->withAction('#')
245  ));
246  $tpl->setVariable("DOWN_BUTTON", $this->renderer->render(
247  $this->glyph_factory->down()->withAction('#')
248  ));
249  $tpl->parseCurrentBlock();
250  }
251 
252  $tpl->setCurrentBlock("row");
253 
254  $tpl->setVariable("POST_VAR", $this->getPostVar());
255  $tpl->setVariable("ROW_NUMBER", $value->getPosition());
256  $tpl->setVariable("ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
257 
258  $tpl->setVariable(
259  "CORRECTNESS_TRUE_ID",
260  $this->getPostVar() . "[correctness][{$value->getPosition()}][true]"
261  );
262  $tpl->setVariable(
263  "CORRECTNESS_FALSE_ID",
264  $this->getPostVar() . "[correctness][{$value->getPosition()}][false]"
265  );
266  $tpl->setVariable("CORRECTNESS_TRUE_VALUE", 1);
267  $tpl->setVariable("CORRECTNESS_FALSE_VALUE", 0);
268 
269  if ($value->getCorrectness() !== null) {
270  if ($value->getCorrectness()) {
271  $tpl->setVariable('CORRECTNESS_TRUE_SELECTED', ' checked="checked"');
272  } else {
273  $tpl->setVariable('CORRECTNESS_FALSE_SELECTED', ' checked="checked"');
274  }
275  }
276 
277  if ($this->getDisabled()) {
278  $tpl->setVariable("DISABLED_CORRECTNESS", " disabled=\"disabled\"");
279  }
280 
281  $tpl->parseCurrentBlock();
282  }
283 
284  if ($this->getSingleline()) {
285  if (!$this->hideImages) {
286  if (is_array($this->getSuffixes())) {
287  $suff_str = $delim = "";
288  foreach ($this->getSuffixes() as $suffix) {
289  $suff_str .= $delim . "." . $suffix;
290  $delim = ", ";
291  }
292  $tpl->setCurrentBlock('allowed_image_suffixes');
293  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $this->lng->txt("file_allowed_suffixes") . " " . $suff_str);
294  $tpl->parseCurrentBlock();
295  }
296 
297  $tpl->setCurrentBlock("image_heading");
298  $tpl->setVariable("ANSWER_IMAGE", $this->lng->txt('answer_image'));
299  $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
300  $tpl->parseCurrentBlock();
301  }
302  }
303 
304  foreach ($this->qstObject->getValidOptionLabels() as $optionLabel) {
305  if ($this->qstObject->isCustomOptionLabel($optionLabel)) {
306  continue;
307  }
308 
309  $tpl->setCurrentBlock('option_label_translations');
310  $tpl->setVariable('OPTION_LABEL', $optionLabel);
311  $tpl->setVariable('TRANSLATION_TRUE', $this->qstObject->getTrueOptionLabelTranslation($this->lng, $optionLabel));
312  $tpl->setVariable('TRANSLATION_FALSE', $this->qstObject->getFalseOptionLabelTranslation($this->lng, $optionLabel));
313  $tpl->parseCurrentBlock();
314  }
315 
316  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
317  $tpl->setVariable("DELETE_IMAGE_HEADER", $this->lng->txt('delete_image_header'));
318  $tpl->setVariable("DELETE_IMAGE_QUESTION", $this->lng->txt('delete_image_question'));
319  $tpl->setVariable("ANSWER_TEXT", $this->lng->txt('answer_text'));
320 
321  $tpl->setVariable("OPTIONS_TEXT", $this->lng->txt('options'));
322 
323  $a_tpl->setCurrentBlock("prop_generic");
324  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
325  $a_tpl->parseCurrentBlock();
326 
327  $this->tpl->addJavascript("Modules/TestQuestionPool/templates/default/answerwizardinput.js");
328  $this->tpl->addJavascript("Modules/TestQuestionPool/templates/default/kprimchoicewizard.js");
329  $this->tpl->addJavascript('Modules/TestQuestionPool/js/ilAssKprimChoice.js');
330  }
331 
332  public function checkUploads($foundvalues): bool
333  {
334  if (is_array($_FILES) && count($_FILES) && $this->getSingleline()) {
335  if (!$this->hideImages) {
336  if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
337  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
338  // error handling
339  if ($error > 0) {
340  switch ($error) {
341  case UPLOAD_ERR_FORM_SIZE:
342  case UPLOAD_ERR_INI_SIZE:
343  $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
344  return false;
345  break;
346 
347  case UPLOAD_ERR_PARTIAL:
348  $this->setAlert($this->lng->txt("form_msg_file_partially_uploaded"));
349  return false;
350  break;
351 
352  case UPLOAD_ERR_NO_FILE:
353  if ($this->getRequired() && !$this->isIgnoreMissingUploadsEnabled()) {
354  $has_image = isset($foundvalues['imagename'][$index]) ? true : false;
355  if (!$has_image && (!strlen($foundvalues['answer'][$index]))) {
356  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
357  return false;
358  }
359  }
360  break;
361 
362  case UPLOAD_ERR_NO_TMP_DIR:
363  $this->setAlert($this->lng->txt("form_msg_file_missing_tmp_dir"));
364  return false;
365  break;
366 
367  case UPLOAD_ERR_CANT_WRITE:
368  $this->setAlert($this->lng->txt("form_msg_file_cannot_write_to_disk"));
369  return false;
370  break;
371 
372  case UPLOAD_ERR_EXTENSION:
373  $this->setAlert($this->lng->txt("form_msg_file_upload_stopped_ext"));
374  return false;
375  break;
376  }
377  }
378  }
379  } else {
380  if ($this->getRequired()) {
381  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
382  return false;
383  }
384  }
385 
386  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
387  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
388  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
389  $filename_arr = pathinfo($filename);
390  if (isset($filename_arr["extension"])) {
391  $suffix = $filename_arr["extension"];
392  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
393  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
394  // check suffixes
395  if (strlen($tmpname) && is_array($this->getSuffixes())) {
396  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
397  $this->setAlert($this->lng->txt("form_msg_file_wrong_file_type"));
398  return false;
399  }
400  }
401  }
402  }
403  }
404 
405  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
406  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
407  if ($_FILES[$this->getPostVar()]['error']['image'][$index] > 0) {
408  continue;
409  }
410 
411  $mimetype = ilObjMediaObject::getMimeType($tmpname);
412 
413  if (!preg_match("/^image/", $mimetype)) {
414  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
415  $this->setAlert($this->lng->txt("form_msg_file_wrong_mime_type"));
416  return false;
417  }
418  }
419  }
420 
421 
422  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
423  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
424  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
425  $filename_arr = pathinfo($filename);
426  if (isset($filename_arr["extension"])) {
427  $suffix = $filename_arr["extension"];
428  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
429  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
430  // virus handling
431  if (strlen($tmpname)) {
432  $vir = ilVirusScanner::virusHandling($tmpname, $filename);
433  if ($vir[0] == false) {
434  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
435  $this->setAlert($this->lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
436  return false;
437  }
438  }
439  }
440  }
441  }
442  }
443  }
444 
445  return true;
446  }
447 
448  public function collectValidFiles(): void
449  {
450  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $err) {
451  if ($err > 0) {
452  continue;
453  }
454 
455  $this->files[$index] = array(
456  'position' => $index,
457  'tmp_name' => $_FILES[$this->getPostVar()]['tmp_name']['image'][$index],
458  'name' => $_FILES[$this->getPostVar()]['name']['image'][$index],
459  'type' => $_FILES[$this->getPostVar()]['type']['image'][$index],
460  'size' => $_FILES[$this->getPostVar()]['size']['image'][$index]
461  );
462  }
463  }
464 
469  private function cleanupAnswerText(array $answer_text, bool $is_rte): array
470  {
471  if (!is_array($answer_text)) {
472  return [];
473  }
474 
475  if ($is_rte) {
477  $answer_text,
478  false,
480  );
481  }
482 
484  $answer_text,
485  true,
487  );
488  }
489 }
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)
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 getMimeType(string $a_file, bool $a_external=false)
get mime type for file
cleanupAnswerText(array $answer_text, bool $is_rte)
sk - 12.05.2023: This is one more of those that we need, but don&#39;t want.
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)
const ALLOWED_PLAIN_TEXT_TAGS
sk - 12.05.2023: This const is also used in ilKprimChoiceWizardInputGUI.
This class represents a single choice wizard property in a property form.
setIgnoreMissingUploadsEnabled($ignoreMissingUploadsEnabled)