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