ILIAS  release_8 Revision v8.24
class.ilMatchingWizardInputGUI.php
Go to the documentation of this file.
1<?php
2
19require_once './Modules/TestQuestionPool/classes/class.ilSingleChoiceWizardInputGUI.php';
20require_once 'Services/UIComponent/Glyph/classes/class.ilGlyphGUI.php';
21
33{
34 private string $pending;
35 protected $text_name = '';
36 protected $image_name = '';
37 protected $values = array();
38 protected $allowMove = false;
39 protected $qstObject = null;
40 protected $suffixes = array();
41 protected $hideImages = false;
42
43 public function __construct($a_title = "", $a_postvar = "")
44 {
45 global $DIC;
46 $lng = $DIC['lng'];
47
48 parent::__construct($a_title, $a_postvar);
49 $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
50 $this->setSize('40');
51 $this->setMaxLength(800);
52 $this->text_name = $lng->txt('answer_text');
53 $this->image_name = $lng->txt('answer_image');
54 }
55
61 public function setSuffixes($a_suffixes): void
62 {
63 $this->suffixes = $a_suffixes;
64 }
65
71 public function getSuffixes(): array
72 {
73 return $this->suffixes;
74 }
75
81 public function setHideImages($a_hide): void
82 {
83 $this->hideImages = $a_hide;
84 }
85
91 public function setValues($a_values): void
92 {
93 $this->values = $a_values;
94 }
95
101 public function getValues(): array
102 {
103 return $this->values;
104 }
105
106 public function setTextName($a_value): void
107 {
108 $this->text_name = $a_value;
109 }
110
111 public function setImageName($a_value): void
112 {
113 $this->image_name = $a_value;
114 }
115
121 public function setQuestionObject($a_value): void
122 {
123 $this->qstObject = &$a_value;
124 }
125
131 public function getQuestionObject(): ?object
132 {
133 return $this->qstObject;
134 }
135
141 public function setAllowMove($a_allow_move): void
142 {
143 $this->allowMove = $a_allow_move;
144 }
145
151 public function getAllowMove(): bool
152 {
153 return $this->allowMove;
154 }
155
156 public function setValue($a_value): void
157 {
158 $this->values = array();
159 if (is_array($a_value)) {
160 if (is_array($a_value['answer'])) {
161 foreach ($a_value['answer'] as $index => $value) {
162 $answer = new assAnswerMatchingTerm(
163 $value,
164 $a_value['imagename'][$index] ?? '',
165 $a_value['identifier'][$index] ?? ''
166 );
167 array_push($this->values, $answer);
168 }
169 }
170 }
171 }
172
177 public function checkInput(): bool
178 {
179 global $DIC;
180 $lng = $DIC['lng'];
181 include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
182 if (is_array($_POST[$this->getPostVar()])) {
184 $_POST[$this->getPostVar()],
185 true,
187 );
188 } else {
189 $foundvalues = $_POST[$this->getPostVar()];
190 }
191 if (is_array($foundvalues)) {
192 // check answers
193 if (is_array($foundvalues['answer'])) {
194 foreach ($foundvalues['answer'] as $aidx => $answervalue) {
195 if ($answervalue === '' && (
196 (!isset($foundvalues['imagename'][$aidx]) || $foundvalues['imagename'][$aidx] === '') &&
197 !isset($_FILES[$this->getPostVar()]['tmp_name']['image'][$aidx])
198 )) {
199 // If there is to text answer, no already staged image, and no uploaded image ...
200 $this->setAlert($lng->txt("msg_input_is_required"));
201 return false;
202 }
203 }
204 }
205
206 if (!$this->hideImages) {
207 if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
208 foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
209 // error handling
210 if ($error > 0) {
211 switch ($error) {
212 case UPLOAD_ERR_FORM_SIZE:
213 case UPLOAD_ERR_INI_SIZE:
214 $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
215 return false;
216 break;
217
218 case UPLOAD_ERR_PARTIAL:
219 $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
220 return false;
221 break;
222
223 case UPLOAD_ERR_NO_FILE:
224 if ($this->getRequired()) {
225 if ((!isset($foundvalues['imagename'][$index]) || $foundvalues['imagename'][$index] === '') &&
226 (!isset($foundvalues['answer'][$index]) && $foundvalues['answer'][$index] === '')) {
227 $this->setAlert($lng->txt("form_msg_file_no_upload"));
228 return false;
229 }
230 }
231 break;
232
233 case UPLOAD_ERR_NO_TMP_DIR:
234 $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
235 return false;
236 break;
237
238 case UPLOAD_ERR_CANT_WRITE:
239 $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
240 return false;
241 break;
242
243 case UPLOAD_ERR_EXTENSION:
244 $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
245 return false;
246 break;
247 }
248 }
249 }
250 }
251
252 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
253 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
254 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
255 $filename_arr = pathinfo($filename);
256 $suffix = '';
257 if (isset($filename_arr['extension'])) {
258 $suffix = $filename_arr["extension"];
259 }
260
261 // check suffixes
262 if ($tmpname != '' && is_array($this->getSuffixes())) {
264 if ($vir[0] == false) {
265 $this->setAlert($lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
266 return false;
267 }
268
269 if (!in_array(strtolower($suffix), $this->getSuffixes())) {
270 $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
271 return false;
272 }
273 }
274 }
275 }
276 }
277 }
278 return $this->checkSubItemsInput();
279 }
280
285 public function insert(ilTemplate $a_tpl): void
286 {
287 global $DIC;
288 $lng = $DIC['lng'];
289
290 $tpl = new ilTemplate("tpl.prop_matchingwizardinput.html", true, true, "Modules/TestQuestionPool");
291 $i = 0;
292 foreach ($this->values as $value) {
293 if (!$this->hideImages) {
294 if (strlen($value->getPicture())) {
295 $imagename = $this->qstObject->getImagePathWeb() . $value->getPicture();
296 if ($this->qstObject->getThumbSize()) {
297 if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getPicture())) {
298 $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getPicture();
299 }
300 }
301 $tpl->setCurrentBlock('image');
302 $tpl->setVariable('SRC_IMAGE', $imagename);
303 $tpl->setVariable('IMAGE_NAME', $value->getPicture());
304 $tpl->setVariable('ALT_IMAGE', ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
305 $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
306 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
307 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
308 $tpl->parseCurrentBlock();
309 }
310 $tpl->setCurrentBlock('addimage');
311 $tpl->setVariable("IMAGE_BROWSE", $lng->txt('select_file'));
312 $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
313 $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
314 $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
315 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
316 $tpl->parseCurrentBlock();
317 }
318
319 if (is_object($value)) {
320 $tpl->setCurrentBlock("prop_text_propval");
321 $tpl->setVariable("PROPERTY_VALUE", ilLegacyFormElementsUtil::prepareFormOutput($value->getText()));
322 $tpl->parseCurrentBlock();
323 }
324 // this block does not exist in the template
325 // $tpl->setCurrentBlock('singleline');
326 $tpl->setVariable("SIZE", $this->getSize());
327 $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
328 $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
329 $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
330 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
331 if ($this->getDisabled()) {
332 $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
333 }
334 $tpl->parseCurrentBlock();
335 if ($this->getAllowMove()) {
336 $tpl->setCurrentBlock("move");
337 $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
338 $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
339 $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
340 $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
341 $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
342 $tpl->parseCurrentBlock();
343 }
344 $tpl->setCurrentBlock("row");
345 $tpl->setVariable("POST_VAR", $this->getPostVar());
346 $tpl->setVariable("ROW_NUMBER", $i + 1);
347 $tpl->setVariable("ROW_IDENTIFIER", $value->getIdentifier());
348 $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
349 $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
350 $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
351 $tpl->setVariable("ADD_BUTTON", ilGlyphGUI::get(ilGlyphGUI::ADD));
352 $tpl->setVariable("REMOVE_BUTTON", ilGlyphGUI::get(ilGlyphGUI::REMOVE));
353 $tpl->parseCurrentBlock();
354 $i++;
355 }
356
357 if (!$this->hideImages) {
358 if (is_array($this->getSuffixes())) {
359 $suff_str = $delim = "";
360 foreach ($this->getSuffixes() as $suffix) {
361 $suff_str .= $delim . "." . $suffix;
362 $delim = ", ";
363 }
364 $tpl->setCurrentBlock('allowed_image_suffixes');
365 $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes") . " " . $suff_str);
366 $tpl->parseCurrentBlock();
367 }
368 $tpl->setCurrentBlock("image_heading");
369 $tpl->setVariable("ANSWER_IMAGE", $this->image_name);
370 $tpl->setVariable("TXT_MAX_SIZE", ilFileUtils::getFileSizeInfo());
371 $tpl->parseCurrentBlock();
372 }
373
374 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
375 $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
376 $tpl->setVariable("TEXT_NO", $lng->txt('no'));
377 $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
378 $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
379 $tpl->setVariable("ANSWER_TEXT", $this->text_name);
380 $tpl->setVariable("NUMBER_TEXT", $lng->txt('row'));
381 $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
382
383 $a_tpl->setCurrentBlock("prop_generic");
384 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
385 $a_tpl->parseCurrentBlock();
386 }
387
388 public function setPending(string $a_val): void
389 {
390 $this->pending = $a_val;
391 }
392
393 public function getPending(): string
394 {
395 return $this->pending;
396 }
397}
$filename
Definition: buildRTE.php:78
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashesRecursive($a_data, bool $a_strip_html=true, string $a_allow="")
static getFileSizeInfo()
static get(string $a_glyph, string $a_text="")
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 file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
setAllowMove($a_allow_move)
Set allow move.
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="")
static _getUsedHTMLTagsAsString(string $a_module="")
Returns a string of all allowed HTML tags for text editing.
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)
global $DIC
Definition: feed.php:28
if($DIC->http() ->request() ->getMethod()=="GET" &&isset($DIC->http() ->request() ->getQueryParams()['tex'])) $tpl
Definition: latex.php:41
$index
Definition: metadata.php:145
$i
Definition: metadata.php:41
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc