ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
class.ilMatchingWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 
20 use ILIAS\UI\Component\Symbol\Glyph\Factory as GlyphFactory;
22 
34 {
35  private string $pending;
36  protected $text_name = '';
37  protected $image_name = '';
38  protected $values = array();
39  protected $qstObject = null;
40  protected $suffixes = array();
41  protected $hideImages = false;
42 
43  protected GlyphFactory $glyph_factory;
44  protected Renderer $renderer;
46 
47  public function __construct($a_title = "", $a_postvar = "")
48  {
49  parent::__construct($a_title, $a_postvar);
50 
51  global $DIC;
52  $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
53  $this->renderer = $DIC->ui()->renderer();
54  $this->upload_limit = $DIC['ui.upload_limit_resolver'];
55 
56  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
57  $this->setSize('40');
58  $this->setMaxLength(800);
59 
60  $lng = $DIC['lng'];
61  $this->text_name = $lng->txt('answer_text');
62  $this->image_name = $lng->txt('answer_image');
63  }
64 
70  public function setSuffixes($a_suffixes): void
71  {
72  $this->suffixes = $a_suffixes;
73  }
74 
80  public function getSuffixes(): array
81  {
82  return $this->suffixes;
83  }
84 
90  public function setHideImages($a_hide): void
91  {
92  $this->hideImages = $a_hide;
93  }
94 
100  public function setValues($a_values): void
101  {
102  $this->values = $a_values;
103  }
104 
110  public function getValues(): array
111  {
112  return $this->values;
113  }
114 
115  public function setTextName($a_value): void
116  {
117  $this->text_name = $a_value;
118  }
119 
120  public function setImageName($a_value): void
121  {
122  $this->image_name = $a_value;
123  }
124 
130  public function setQuestionObject($a_value): void
131  {
132  $this->qstObject = &$a_value;
133  }
134 
140  public function getQuestionObject(): ?object
141  {
142  return $this->qstObject;
143  }
144 
145  public function setValue($a_value): void
146  {
147  $this->values = array();
148  if (is_array($a_value)) {
149  if (is_array($a_value['answer'])) {
150  foreach ($a_value['answer'] as $index => $value) {
151  $answer = new assAnswerMatchingTerm(
152  $value,
153  $a_value['imagename'][$index] ?? 0,
154  $a_value['identifier'][$index] ?? 0
155  );
156  array_push($this->values, $answer);
157  }
158  }
159  }
160  }
161 
166  public function checkInput(): bool
167  {
168  global $DIC;
169  $lng = $DIC['lng'];
170 
171  if (is_array($_POST[$this->getPostVar()])) {
172  $foundvalues = ilArrayUtil::stripSlashesRecursive(
173  $_POST[$this->getPostVar()],
174  true,
176  );
177  } else {
178  $foundvalues = $_POST[$this->getPostVar()];
179  }
180  if (is_array($foundvalues)) {
181  // check answers
182  if (is_array($foundvalues['answer'])) {
183  foreach ($foundvalues['answer'] as $aidx => $answervalue) {
184  if ($answervalue === '' && (
185  (!isset($foundvalues['imagename'][$aidx]) || $foundvalues['imagename'][$aidx] === '') &&
186  !isset($_FILES[$this->getPostVar()]['tmp_name']['image'][$aidx])
187  )) {
188  // If there is to text answer, no already staged image, and no uploaded image ...
189  $this->setAlert($lng->txt("msg_input_is_required"));
190  return false;
191  }
192  }
193  }
194 
195  if (!$this->hideImages) {
196  if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
197  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
198  // error handling
199  if ($error > 0) {
200  switch ($error) {
201  case UPLOAD_ERR_FORM_SIZE:
202  case UPLOAD_ERR_INI_SIZE:
203  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
204  return false;
205  break;
206 
207  case UPLOAD_ERR_PARTIAL:
208  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
209  return false;
210  break;
211 
212  case UPLOAD_ERR_NO_FILE:
213  if ($this->getRequired()) {
214  if ((!isset($foundvalues['imagename'][$index]) || $foundvalues['imagename'][$index] === '') &&
215  (!isset($foundvalues['answer'][$index]) && $foundvalues['answer'][$index] === '')) {
216  $this->setAlert($lng->txt("form_msg_file_no_upload"));
217  return false;
218  }
219  }
220  break;
221 
222  case UPLOAD_ERR_NO_TMP_DIR:
223  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
224  return false;
225  break;
226 
227  case UPLOAD_ERR_CANT_WRITE:
228  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
229  return false;
230  break;
231 
232  case UPLOAD_ERR_EXTENSION:
233  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
234  return false;
235  break;
236  }
237  }
238  }
239  }
240 
241  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
242  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
243  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
244  $filename_arr = pathinfo($filename);
245  $suffix = '';
246  if (isset($filename_arr['extension'])) {
247  $suffix = $filename_arr["extension"];
248  }
249 
250  // check suffixes
251  if ($tmpname != '' && is_array($this->getSuffixes())) {
252  $vir = ilVirusScanner::virusHandling($tmpname, $filename);
253  if ($vir[0] == false) {
254  $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
255  return false;
256  }
257 
258  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
259  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
260  return false;
261  }
262  }
263  }
264  }
265  }
266  }
267  return $this->checkSubItemsInput();
268  }
269 
274  public function insert(ilTemplate $a_tpl): void
275  {
276  global $DIC;
277  $lng = $DIC['lng'];
278  $global_tpl = $DIC['tpl'];
279  $global_tpl->addJavascript('./Modules/TestQuestionPool/templates/default/matchinginput.js');
280  $global_tpl->addOnLoadCode('il.test.matchingquestion.init();');
281 
282  $tpl = new ilTemplate("tpl.prop_matchingwizardinput.html", true, true, "Modules/TestQuestionPool");
283  $i = 0;
284  foreach ($this->values as $value) {
285  if (!$this->hideImages) {
286  if ($value->getPicture() &&
287  file_exists($this->qstObject->getImagePath() . $value->getPicture())
288  ) {
289  $imagename = $this->qstObject->getImagePathWeb() . $value->getPicture();
290  if ($this->qstObject->getThumbSize()) {
291  if (file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getPicture())) {
292  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getPicture();
293  }
294  }
295 
296  $tpl->setCurrentBlock('image');
297  $tpl->setVariable('SRC_IMAGE', $imagename);
298  $tpl->setVariable('IMAGE_NAME', $value->getPicture());
299  $tpl->setVariable('ALT_IMAGE', ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
300  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
301  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
302  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
303  $tpl->parseCurrentBlock();
304  }
305  $tpl->setCurrentBlock('addimage');
306  $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
307  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
308  $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
309  $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
310  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
311  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
312  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
313  $tpl->parseCurrentBlock();
314  }
315 
316  if (is_object($value)) {
317  $tpl->setCurrentBlock("prop_text_propval");
318  $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
319  $tpl->parseCurrentBlock();
320  }
321  // this block does not exist in the template
322  // $tpl->setCurrentBlock('singleline');
323  $tpl->setVariable("SIZE", $this->getSize());
324  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
325  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
326  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
327  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
328  if ($this->getDisabled()) {
329  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
330  }
331  $tpl->parseCurrentBlock();
332  $tpl->setCurrentBlock("row");
333  $tpl->setVariable("POST_VAR", $this->getPostVar());
334  $tpl->setVariable("ROW_NUMBER", $i + 1);
335  $tpl->setVariable("ROW_IDENTIFIER", $value->getIdentifier());
336  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
337  $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
338  $this->glyph_factory->add()->withAction('#')
339  ));
340  $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
341  $this->glyph_factory->remove()->withAction('#')
342  ));
343  $tpl->parseCurrentBlock();
344  $i++;
345  }
346 
347  if (!$this->hideImages) {
348  if (is_array($this->getSuffixes())) {
349  $suff_str = $delim = "";
350  foreach ($this->getSuffixes() as $suffix) {
351  $suff_str .= $delim . "." . $suffix;
352  $delim = ", ";
353  }
354  $tpl->setCurrentBlock('allowed_image_suffixes');
355  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
356  $tpl->parseCurrentBlock();
357  }
358  $tpl->setCurrentBlock("image_heading");
359  $tpl->setVariable("ANSWER_IMAGE", $this->image_name);
360  $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
361  $tpl->parseCurrentBlock();
362  }
363 
364  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
365  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
366  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
367  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
368  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
369  $tpl->setVariable("ANSWER_TEXT", $this->text_name);
370  $tpl->setVariable("NUMBER_TEXT", $lng->txt('row'));
371  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
372 
373  $a_tpl->setCurrentBlock("prop_generic");
374  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
375  $a_tpl->parseCurrentBlock();
376  }
377 
378  public function setPending(string $a_val): void
379  {
380  $this->pending = $a_val;
381  }
382 
383  public function getPending(): string
384  {
385  return $this->pending;
386  }
387 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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="")
This class represents a single choice wizard property in a property form.
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...
setHideImages($a_hide)
Set hide images.
__construct($a_title="", $a_postvar="")
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
setSuffixes($a_suffixes)
Set Accepted Suffixes.
static prepareFormOutput($a_str, bool $a_strip=false)
global $DIC
Definition: feed.php:28
checkInput()
Check input, strip slashes etc.
__construct(VocabulariesInterface $vocabularies)
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:546
ilGlobalTemplateInterface $global_tpl
setQuestionObject($a_value)
Set question object.
static _getUsedHTMLTagsAsString(string $a_module="")
Returns a string of all allowed HTML tags for text editing.
static getFileSizeInfo()
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
insert(ilTemplate $a_tpl)
Insert property html.
$filename
Definition: buildRTE.php:78
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
setMaxLength(?int $a_maxlength)