ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilMatchingWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
23
35{
36 private string $pending;
37 protected $text_name = '';
38 protected $image_name = '';
39 protected $values = [];
40 protected $qstObject = null;
41 protected $suffixes = [];
42 protected $hideImages = false;
43
45 protected GlyphFactory $glyph_factory;
48
49 public function __construct($a_title = "", $a_postvar = "")
50 {
51 parent::__construct($a_title, $a_postvar);
52
53 global $DIC;
54
55 $this->forms_helper = new ilTestLegacyFormsHelper();
56 $this->glyph_factory = $DIC->ui()->factory()->symbol()->glyph();
57 $this->renderer = $DIC->ui()->renderer();
58 $this->upload_limit = $DIC['ui.upload_limit_resolver'];
59
60 $this->setSuffixes(["jpg", "jpeg", "png", "gif"]);
61 $this->setSize('40');
62 $this->setMaxLength(800);
63
64 $lng = $DIC['lng'];
65 $this->text_name = $lng->txt('answer_text');
66 $this->image_name = $lng->txt('answer_image');
67 }
68
74 public function setSuffixes($a_suffixes): void
75 {
76 $this->suffixes = $a_suffixes;
77 }
78
84 public function getSuffixes(): array
85 {
86 return $this->suffixes;
87 }
88
94 public function setHideImages($a_hide): void
95 {
96 $this->hideImages = $a_hide;
97 }
98
104 public function setValues($a_values): void
105 {
106 $this->values = $a_values;
107 }
108
114 public function getValues(): array
115 {
116 return $this->values;
117 }
118
119 public function setTextName($a_value): void
120 {
121 $this->text_name = $a_value;
122 }
123
124 public function setImageName($a_value): void
125 {
126 $this->image_name = $a_value;
127 }
128
134 public function setQuestionObject($a_value): void
135 {
136 $this->qstObject = &$a_value;
137 }
138
144 public function getQuestionObject(): ?object
145 {
146 return $this->qstObject;
147 }
148
149 public function setValue($a_value): void
150 {
151 $this->values = [];
152
153 $answers = $this->forms_helper->transformArray($a_value, 'answer', $this->refinery->kindlyTo()->string());
154 $imagename = $this->forms_helper->transformArray($a_value, 'imagename', $this->refinery->kindlyTo()->string());
155 $identifier = $this->forms_helper->transformArray($a_value, 'identifier', $this->refinery->kindlyTo()->int());
156
157 foreach ($answers as $index => $value) {
158 $this->values[] = new assAnswerMatchingTerm(
159 $value,
160 $imagename[$index] ?? '',
161 $identifier[$index] ?? 0
162 );
163 }
164 }
165
170 public function checkInput(): bool
171 {
172 $data = $this->raw($this->getPostVar());
173
174 if (!is_array($data)) {
175 $this->setAlert($this->lng->txt('msg_input_is_required'));
176 return false;
177 }
178
179 // check answers
180 $answers = $this->forms_helper->transformArray($data, 'answer', $this->refinery->kindlyTo()->string());
181 $images = $this->forms_helper->transformArray($data, 'imagename', $this->refinery->kindlyTo()->string());
182 foreach ($answers as $index => $value) {
183 if (
184 $value === ''
185 && !$this->forms_helper->inArray($images, $index)
186 && !isset($_FILES[$this->getPostVar()]['tmp_name']['image'][$index])
187 ) {
188 $this->setAlert($this->lng->txt('msg_input_is_required'));
189 return false;
190 }
191 }
192
193 if (!$this->hideImages) {
194 if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
195 foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
196 // error handling
197 if ($error > 0) {
198 switch ($error) {
199 case UPLOAD_ERR_FORM_SIZE:
200 case UPLOAD_ERR_INI_SIZE:
201 $this->setAlert($this->lng->txt('form_msg_file_size_exceeds'));
202 return false;
203 break;
204
205 case UPLOAD_ERR_PARTIAL:
206 $this->setAlert($this->lng->txt('form_msg_file_partially_uploaded'));
207 return false;
208 break;
209
210 case UPLOAD_ERR_NO_FILE:
211 if (
212 !$this->forms_helper->inArray($images, $index)
213 && !$this->forms_helper->inArray($answers, $index)
214 && $this->getRequired()
215 ) {
216 $this->setAlert($this->lng->txt('form_msg_file_no_upload'));
217 return false;
218 }
219 break;
220
221 case UPLOAD_ERR_NO_TMP_DIR:
222 $this->setAlert($this->lng->txt('form_msg_file_missing_tmp_dir'));
223 return false;
224 break;
225
226 case UPLOAD_ERR_CANT_WRITE:
227 $this->setAlert($this->lng->txt('form_msg_file_cannot_write_to_disk'));
228 return false;
229 break;
230
231 case UPLOAD_ERR_EXTENSION:
232 $this->setAlert($this->lng->txt('form_msg_file_upload_stopped_ext'));
233 return false;
234 break;
235 }
236 }
237 }
238 }
239
240 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
241 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
242 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
243 $filename_arr = pathinfo($filename);
244 $suffix = $filename_arr['extension'] ?? '';
245
246 // check suffixes
247 if ($tmpname !== '' && is_array($this->getSuffixes())) {
249 if ($vir[0] == false) {
250 $this->setAlert($this->lng->txt('form_msg_file_virus_found') . '<br />' . $vir[1]);
251 return false;
252 }
253
254 if (!in_array(strtolower($suffix), $this->getSuffixes(), true)) {
255 $this->setAlert($this->lng->txt('form_msg_file_wrong_file_type'));
256 return false;
257 }
258 }
259 }
260 }
261 }
262
263 return $this->checkSubItemsInput();
264 }
265
270 public function insert(ilTemplate $a_tpl): void
271 {
272 global $DIC;
273 $lng = $DIC['lng'];
274 $global_tpl = $DIC['tpl'];
275 $global_tpl->addJavascript('assets/js/matchinginput.js');
276 $global_tpl->addOnLoadCode('il.test.matchingquestion.init();');
277
278 $tpl = new ilTemplate("tpl.prop_matchingwizardinput.html", true, true, "components/ILIAS/TestQuestionPool");
279 $i = 0;
280 foreach ($this->values as $value) {
281 if (!$this->hideImages) {
282 if ($value->getPicture() &&
283 file_exists($this->qstObject->getImagePath() . $value->getPicture())
284 ) {
285 $imagename = $this->qstObject->getImagePathWeb() . $value->getPicture();
286 if ($this->qstObject->getThumbSize()) {
287 if (file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getPicture())) {
288 $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getPicture();
289 }
290 }
291
292 $tpl->setCurrentBlock('image');
293 $tpl->setVariable('SRC_IMAGE', $imagename);
294 $tpl->setVariable('IMAGE_NAME', $value->getPicture());
295 $tpl->setVariable('ALT_IMAGE', ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
296 $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
297 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
298 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
299 $tpl->parseCurrentBlock();
300 }
301 $tpl->setCurrentBlock('addimage');
302 $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
303 $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
304 $tpl->setVariable('MAX_SIZE_WARNING', $this->lng->txt('form_msg_file_size_exceeds'));
305 $tpl->setVariable('MAX_SIZE', $this->upload_limit->getPhpUploadLimitInBytes());
306 $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
307 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
308 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
309 $tpl->parseCurrentBlock();
310 }
311
312 if (is_object($value)) {
313 $tpl->setCurrentBlock("prop_text_propval");
314 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
315 $tpl->parseCurrentBlock();
316 }
317 // this block does not exist in the template
318 // $tpl->setCurrentBlock('singleline');
319 $tpl->setVariable("SIZE", $this->getSize());
320 $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
321 $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
322 $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
323 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
324 if ($this->getDisabled()) {
325 $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
326 }
327 $tpl->parseCurrentBlock();
328 $tpl->setCurrentBlock("row");
329 $tpl->setVariable("POST_VAR", $this->getPostVar());
330 $tpl->setVariable("ROW_NUMBER", $i + 1);
331 $tpl->setVariable("ROW_IDENTIFIER", $value->getIdentifier());
332 $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
333 $tpl->setVariable("ADD_BUTTON", $this->renderer->render(
334 $this->glyph_factory->add()->withAction('#')
335 ));
336 $tpl->setVariable("REMOVE_BUTTON", $this->renderer->render(
337 $this->glyph_factory->remove()->withAction('#')
338 ));
339 $tpl->parseCurrentBlock();
340 $i++;
341 }
342
343 if (!$this->hideImages) {
344 if (is_array($this->getSuffixes())) {
345 $suff_str = $delim = "";
346 foreach ($this->getSuffixes() as $suffix) {
347 $suff_str .= $delim . "." . $suffix;
348 $delim = ", ";
349 }
350 $tpl->setCurrentBlock('allowed_image_suffixes');
351 $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
352 $tpl->parseCurrentBlock();
353 }
354 $tpl->setCurrentBlock("image_heading");
355 $tpl->setVariable("ANSWER_IMAGE", $this->image_name);
356 $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
357 $tpl->parseCurrentBlock();
358 }
359
360 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
361 $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
362 $tpl->setVariable("TEXT_NO", $lng->txt('no'));
363 $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
364 $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
365 $tpl->setVariable("ANSWER_TEXT", $this->text_name);
366 $tpl->setVariable("NUMBER_TEXT", $lng->txt('row'));
367 $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
368
369 $a_tpl->setCurrentBlock("prop_generic");
370 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
371 $a_tpl->parseCurrentBlock();
372 }
373
374 public function setPending(string $a_val): void
375 {
376 $this->pending = $a_val;
377 }
378
379 public function getPending(): string
380 {
381 return $this->pending;
382 }
383}
renderer()
$filename
Definition: buildRTE.php:78
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:544
Class for matching question terms.
static getFileSizeInfo()
ilGlobalTemplateInterface $global_tpl
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 prepareFormOutput($a_str, bool $a_strip=false)
This class represents a single choice wizard property in a property form.
setQuestionObject($a_value)
Set question object.
insert(ilTemplate $a_tpl)
Insert property html.
setHideImages($a_hide)
Set hide images.
checkInput()
Check input, strip slashes etc.
setSuffixes($a_suffixes)
Set Accepted Suffixes.
__construct($a_title="", $a_postvar="")
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
This class represents a text property in a property form.
setMaxLength(?int $a_maxlength)
static virusHandling(string $a_file, string $a_orig_name='', bool $a_clean=true)
addOnLoadCode(string $a_code, int $a_batch=2)
Add on load code.
This is how a factory for glyphs looks like.
Definition: Factory.php:27
An entity that renders components to a string output.
Definition: Renderer.php:31
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
global $DIC
Definition: shib_login.php:26