ILIAS  release_7 Revision v7.30-3-g800a261c036
class.ilKprimChoiceWizardInputGUI.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4require_once 'Modules/TestQuestionPool/classes/class.ilSingleChoiceWizardInputGUI.php';
5require_once 'Modules/TestQuestionPool/classes/class.ilAssKprimChoiceAnswer.php';
6require_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
7
15{
20 protected $lng;
21
25 protected $tpl;
26
30 protected $qstObject;
31
32 private $files;
33
35
36 public function __construct($a_title = "", $a_postvar = "")
37 {
38 parent::__construct($a_title, $a_postvar);
39
40 global $DIC;
41 $lng = $DIC['lng'];
42 $tpl = $DIC['tpl'];
43
44 $this->lng = $lng;
45 $this->tpl = $tpl;
46
47 $this->files = [];
48
49 $this->ignoreMissingUploadsEnabled = false;
50 }
51
52 public function setFiles($files)
53 {
54 $this->files = $files;
55 }
56
57 public function getFiles()
58 {
59 return $this->files;
60 }
61
63 {
64 $this->ignoreMissingUploadsEnabled = $ignoreMissingUploadsEnabled;
65 }
66
68 {
70 }
71
72 public function setValue($a_value)
73 {
74 $this->values = array();
75
76 if (is_array($a_value) && is_array($a_value['answer'])) {
77 foreach ($a_value['answer'] as $index => $value) {
78 $answer = new ilAssKprimChoiceAnswer();
79
80 $answer->setPosition($index);
81 $answer->setAnswertext($value);
82 $answer->setImageFile($a_value['imagename'][$index]);
83
84 if (strlen($a_value['correctness'][$index])) {
85 $answer->setCorrectness((bool) $a_value['correctness'][$index]);
86 }
87
88 $answer->setThumbPrefix($this->qstObject->getThumbPrefix());
89 $answer->setImageFsDir($this->qstObject->getImagePath());
90 $answer->setImageWebDir($this->qstObject->getImagePathWeb());
91
92 $this->values[] = $answer;
93 }
94 }
95
96 #vd($this->values);
97 }
98
99 public function checkInput()
100 {
101 global $DIC;
102 $lng = $DIC['lng'];
103
104 include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
105
106 if (is_array($_POST[$this->getPostVar()]) && $_POST["answer_type"] == "multiLine") {
109 } elseif (is_array($_POST[$this->getPostVar()]) && $_POST["answer_type"] == "singleLine") {
112 }
113
114 $foundvalues = $_POST[$this->getPostVar()];
115
116 #vd($foundvalues);
117
118 if (is_array($foundvalues)) {
119 // check answers
120 if (is_array($foundvalues['answer'])) {
121 foreach ($foundvalues['answer'] as $aidx => $answervalue) {
122 if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0)) {
123 $this->setAlert($lng->txt("msg_input_is_required"));
124 return false;
125 }
126 }
127 }
128
129 // check correctness
130 if (!isset($foundvalues['correctness']) || count($foundvalues['correctness']) < count($foundvalues['answer'])) {
131 $this->setAlert($lng->txt("msg_input_is_required"));
132 return false;
133 }
134
135 if (!$this->checkUploads($foundvalues)) {
136 return false;
137 }
138 } else {
139 $this->setAlert($lng->txt("msg_input_is_required"));
140 return false;
141 }
142
143 return $this->checkSubItemsInput();
144 }
145
149 public function insert($a_tpl)
150 {
151 $tpl = new ilTemplate("tpl.prop_kprimchoicewizardinput.html", true, true, "Modules/TestQuestionPool");
152
153 foreach ($this->values as $value) {
158 if ($this->getSingleline()) {
159 if (!$this->hideImages) {
160 if (strlen($value->getImageFile())) {
161 $imagename = $value->getImageWebPath();
162
163 if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
164 if (@file_exists($value->getThumbFsPath())) {
165 $imagename = $value->getThumbWebPath();
166 }
167 }
168
169 $tpl->setCurrentBlock('image');
170 $tpl->setVariable('SRC_IMAGE', $imagename);
171 $tpl->setVariable('IMAGE_NAME', $value->getImageFile());
172 $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->getAnswertext()));
173 $tpl->setVariable("TXT_DELETE_EXISTING", $this->lng->txt("delete_existing_file"));
174 $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
175 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
176 $tpl->parseCurrentBlock();
177 }
178 $tpl->setCurrentBlock('addimage');
179 $tpl->setVariable("IMAGE_BROWSE", $this->lng->txt('select_file'));
180 $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][{$value->getPosition()}]");
181 $tpl->setVariable("IMAGE_SUBMIT", $this->lng->txt("upload"));
182 $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
183 $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
184 $tpl->parseCurrentBlock();
185 }
186
187 $tpl->setCurrentBlock("prop_text_propval");
188 $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput(htmlspecialchars_decode($value->getAnswertext())));
189 $tpl->parseCurrentBlock();
190
191 $tpl->setCurrentBlock('singleline');
192 $tpl->setVariable("SIZE", $this->getSize());
193 $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
194 $tpl->setVariable("SINGLELINE_ROW_NUMBER", $value->getPosition());
195 $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
196 $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
197 if ($this->getDisabled()) {
198 $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
199 }
200 $tpl->parseCurrentBlock();
201 } elseif (!$this->getSingleline()) {
202 $tpl->setCurrentBlock('multiline');
203 $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
204 $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
205 $tpl->setVariable("MULTILINE_ROW_NUMBER", $value->getPosition());
206 $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
207 if ($this->getDisabled()) {
208 $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
209 }
210 $tpl->parseCurrentBlock();
211 }
212 if ($this->getAllowMove()) {
213 $tpl->setCurrentBlock("move");
214 $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][{$value->getPosition()}]");
215 $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][{$value->getPosition()}]");
216 $tpl->setVariable("UP_ID", "up_{$this->getPostVar()}[{$value->getPosition()}]");
217 $tpl->setVariable("DOWN_ID", "down_{$this->getPostVar()}[{$value->getPosition()}]");
218 $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
219 $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
220 $tpl->parseCurrentBlock();
221 }
222
223 $tpl->setCurrentBlock("row");
224
225 $tpl->setVariable("POST_VAR", $this->getPostVar());
226 $tpl->setVariable("ROW_NUMBER", $value->getPosition());
227 $tpl->setVariable("ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
228
229 $tpl->setVariable(
230 "CORRECTNESS_TRUE_ID",
231 $this->getPostVar() . "[correctness][{$value->getPosition()}][true]"
232 );
233 $tpl->setVariable(
234 "CORRECTNESS_FALSE_ID",
235 $this->getPostVar() . "[correctness][{$value->getPosition()}][false]"
236 );
237 $tpl->setVariable("CORRECTNESS_TRUE_VALUE", 1);
238 $tpl->setVariable("CORRECTNESS_FALSE_VALUE", 0);
239
240 if ($value->getCorrectness() !== null) {
241 if ($value->getCorrectness()) {
242 $tpl->setVariable('CORRECTNESS_TRUE_SELECTED', ' checked="checked"');
243 } else {
244 $tpl->setVariable('CORRECTNESS_FALSE_SELECTED', ' checked="checked"');
245 }
246 }
247
248 if ($this->getDisabled()) {
249 $tpl->setVariable("DISABLED_CORRECTNESS", " disabled=\"disabled\"");
250 }
251
252 $tpl->parseCurrentBlock();
253 }
254
255 if ($this->getSingleline()) {
256 if (!$this->hideImages) {
257 if (is_array($this->getSuffixes())) {
258 $suff_str = $delim = "";
259 foreach ($this->getSuffixes() as $suffix) {
260 $suff_str .= $delim . "." . $suffix;
261 $delim = ", ";
262 }
263 $tpl->setCurrentBlock('allowed_image_suffixes');
264 $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $this->lng->txt("file_allowed_suffixes") . " " . $suff_str);
265 $tpl->parseCurrentBlock();
266 }
267
268 $tpl->setCurrentBlock("image_heading");
269 $tpl->setVariable("ANSWER_IMAGE", $this->lng->txt('answer_image'));
270 $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
271 $tpl->parseCurrentBlock();
272 }
273 }
274
275 foreach ($this->qstObject->getValidOptionLabels() as $optionLabel) {
276 if ($this->qstObject->isCustomOptionLabel($optionLabel)) {
277 continue;
278 }
279
280 $tpl->setCurrentBlock('option_label_translations');
281 $tpl->setVariable('OPTION_LABEL', $optionLabel);
282 $tpl->setVariable('TRANSLATION_TRUE', $this->qstObject->getTrueOptionLabelTranslation($this->lng, $optionLabel));
283 $tpl->setVariable('TRANSLATION_FALSE', $this->qstObject->getFalseOptionLabelTranslation($this->lng, $optionLabel));
284 $tpl->parseCurrentBlock();
285 }
286
287 $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
288 $tpl->setVariable("DELETE_IMAGE_HEADER", $this->lng->txt('delete_image_header'));
289 $tpl->setVariable("DELETE_IMAGE_QUESTION", $this->lng->txt('delete_image_question'));
290 $tpl->setVariable("ANSWER_TEXT", $this->lng->txt('answer_text'));
291
292 $tpl->setVariable("OPTIONS_TEXT", $this->lng->txt('options'));
293
294 // winzards input column label values will be updated on document ready js
295 //$tpl->setVariable("TRUE_TEXT", $this->qstObject->getTrueOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
296 //$tpl->setVariable("FALSE_TEXT", $this->qstObject->getFalseOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
297
298 $a_tpl->setCurrentBlock("prop_generic");
299 $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
300 $a_tpl->parseCurrentBlock();
301
302 include_once "./Services/YUI/classes/class.ilYuiUtil.php";
303 $this->tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
304 $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/kprimchoicewizard.js");
305 $this->tpl->addJavascript('Modules/TestQuestionPool/js/ilAssKprimChoice.js');
306 }
307
308 public function checkUploads($foundvalues)
309 {
310 if (is_array($_FILES) && count($_FILES) && $this->getSingleline()) {
311 if (!$this->hideImages) {
312 if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
313 foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
314 // error handling
315 if ($error > 0) {
316 switch ($error) {
317 case UPLOAD_ERR_INI_SIZE:
318 $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
319 return false;
320 break;
321
322 case UPLOAD_ERR_FORM_SIZE:
323 $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
324 return false;
325 break;
326
327 case UPLOAD_ERR_PARTIAL:
328 $this->setAlert($this->lng->txt("form_msg_file_partially_uploaded"));
329 return false;
330 break;
331
332 case UPLOAD_ERR_NO_FILE:
333 if ($this->getRequired() && !$this->isIgnoreMissingUploadsEnabled()) {
334 if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index]))) {
335 $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
336 return false;
337 }
338 }
339 break;
340
341 case UPLOAD_ERR_NO_TMP_DIR:
342 $this->setAlert($this->lng->txt("form_msg_file_missing_tmp_dir"));
343 return false;
344 break;
345
346 case UPLOAD_ERR_CANT_WRITE:
347 $this->setAlert($this->lng->txt("form_msg_file_cannot_write_to_disk"));
348 return false;
349 break;
350
351 case UPLOAD_ERR_EXTENSION:
352 $this->setAlert($this->lng->txt("form_msg_file_upload_stopped_ext"));
353 return false;
354 break;
355 }
356 }
357 }
358 } else {
359 if ($this->getRequired()) {
360 $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
361 return false;
362 }
363 }
364
365 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
366 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
367 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
368 $filename_arr = pathinfo($filename);
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 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
383 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
384 if ($_FILES[$this->getPostVar()]['error']['image'][$index] > 0) {
385 continue;
386 }
387
388 $mimetype = ilObjMediaObject::getMimeType($tmpname);
389
390 if (!preg_match("/^image/", $mimetype)) {
391 $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
392 $this->setAlert($this->lng->txt("form_msg_file_wrong_mime_type"));
393 return false;
394 }
395 }
396 }
397
398
399 if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
400 foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
401 $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
402 $filename_arr = pathinfo($filename);
403 $suffix = $filename_arr["extension"];
404 $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
405 $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
406 // virus handling
407 if (strlen($tmpname)) {
408 $vir = ilUtil::virusHandling($tmpname, $filename);
409 if ($vir[0] == false) {
410 $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
411 $this->setAlert($this->lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
412 return false;
413 }
414 }
415 }
416 }
417 }
418 }
419
420 return true;
421 }
422
423 public function collectValidFiles()
424 {
425 foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $err) {
426 if ($err > 0) {
427 continue;
428 }
429
430 $this->files[$index] = [
431 'position' => $index,
432 'tmp_name' => $_FILES[$this->getPostVar()]['tmp_name']['image'][$index],
433 'name' => $_FILES[$this->getPostVar()]['name']['image'][$index],
434 'type' => $_FILES[$this->getPostVar()]['type']['image'][$index],
435 'size' => $_FILES[$this->getPostVar()]['size']['image'][$index]
436 ];
437 }
438 }
439}
$filename
Definition: buildRTE.php:89
$_POST["username"]
An exception for terminatinating execution or to throw for unit testing.
getPostVar()
Get Post Variable.
setAlert($a_alert)
Set Alert Text.
getFieldId()
Get Post Variable.
static get($a_glyph, $a_text="")
Get glyph html.
__construct($a_title="", $a_postvar="")
Constructor.
checkInput()
Check input, strip slashes etc.
setIgnoreMissingUploadsEnabled($ignoreMissingUploadsEnabled)
static _getUsedHTMLTagsAsString($a_module="")
Returns a string of all allowed HTML tags for text editing.
static getMimeType($a_file, $a_external=null)
get mime type for file
This class represents a single choice wizard property in a property form.
special template class to simplify handling of ITX/PEAR
getMaxLength()
Get Max Length.
static stripSlashesRecursive($a_data, $a_strip_html=true, $a_allow="")
Strip slashes from array and sub-arrays.
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
static getFileSizeInfo()
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms @access public
global $DIC
Definition: goto.php:24
$index
Definition: metadata.php:128
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc