ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 
4 require_once 'Modules/TestQuestionPool/classes/class.ilSingleChoiceWizardInputGUI.php';
5 require_once 'Modules/TestQuestionPool/classes/class.ilAssKprimChoiceAnswer.php';
6 require_once 'Services/MediaObjects/classes/class.ilObjMediaObject.php';
7 
15 {
16  const CUSTOM_UPLOAD_ERR = 99;
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 $lng, $tpl;
41 
42  $this->lng = $lng;
43  $this->tpl = $tpl;
44 
45  $this->files = array();
46 
47  $this->ignoreMissingUploadsEnabled = false;
48  }
49 
50  public function setFiles($files)
51  {
52  $this->files = $files;
53  }
54 
55  public function getFiles()
56  {
57  return $this->files;
58  }
59 
61  {
62  $this->ignoreMissingUploadsEnabled = $ignoreMissingUploadsEnabled;
63  }
64 
66  {
68  }
69 
70  public function setValue($a_value)
71  {
72  $this->values = array();
73 
74  if( is_array($a_value) && is_array($a_value['answer']) )
75  {
76  foreach ($a_value['answer'] as $index => $value)
77  {
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  {
86  $answer->setCorrectness((bool)$a_value['correctness'][$index]);
87  }
88 
89  $answer->setThumbPrefix($this->qstObject->getThumbPrefix());
90  $answer->setImageFsDir($this->qstObject->getImagePath());
91  $answer->setImageWebDir($this->qstObject->getImagePathWeb());
92 
93  $this->values[] = $answer;
94  }
95  }
96 
97  #vd($this->values);
98  }
99 
100  function checkInput()
101  {
102  global $lng;
103 
104  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
105  if( is_array($_POST[$this->getPostVar()]) )
106  {
109  );
110  }
111 
112  $foundvalues = $_POST[$this->getPostVar()];
113 
114  #vd($foundvalues);
115 
116  if( is_array($foundvalues) )
117  {
118  // check answers
119  if (is_array($foundvalues['answer']))
120  {
121  foreach ($foundvalues['answer'] as $aidx => $answervalue)
122  {
123  if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0))
124  {
125  $this->setAlert($lng->txt("msg_input_is_required"));
126  return FALSE;
127  }
128  }
129  }
130 
131  // check correctness
132  if( !isset($foundvalues['correctness']) || count($foundvalues['correctness']) < count($foundvalues['answer']) )
133  {
134  $this->setAlert($lng->txt("msg_input_is_required"));
135  return false;
136  }
137 
138  if( !$this->checkUploads($foundvalues) )
139  {
140  return false;
141  }
142  }
143  else
144  {
145  $this->setAlert($lng->txt("msg_input_is_required"));
146  return FALSE;
147  }
148 
149  return $this->checkSubItemsInput();
150  }
151 
155  public function insert($a_tpl)
156  {
157  $tpl = new ilTemplate("tpl.prop_kprimchoicewizardinput.html", true, true, "Modules/TestQuestionPool");
158 
159  foreach ($this->values as $value)
160  {
165  if ($this->getSingleline())
166  {
167  if (!$this->hideImages)
168  {
169  if (strlen($value->getImageFile()))
170  {
171  $imagename = $value->getImageWebPath();
172 
173  if (($this->getSingleline()) && ($this->qstObject->getThumbSize()))
174  {
175  if (@file_exists($value->getThumbFsPath()))
176  {
177  $imagename = $value->getThumbWebPath();
178  }
179  }
180 
181  $tpl->setCurrentBlock('image');
182  $tpl->setVariable('SRC_IMAGE', $imagename);
183  $tpl->setVariable('IMAGE_NAME', $value->getImageFile());
184  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->getAnswertext()));
185  $tpl->setVariable("TXT_DELETE_EXISTING", $this->lng->txt("delete_existing_file"));
186  $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
187  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
188  $tpl->parseCurrentBlock();
189  }
190  $tpl->setCurrentBlock('addimage');
191  $tpl->setVariable("IMAGE_BROWSE", $this->lng->txt('select_file'));
192  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][{$value->getPosition()}]");
193  $tpl->setVariable("IMAGE_SUBMIT", $this->lng->txt("upload"));
194  $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
195  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
196  $tpl->parseCurrentBlock();
197  }
198 
199  $tpl->setCurrentBlock("prop_text_propval");
200  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
201  $tpl->parseCurrentBlock();
202 
203  $tpl->setCurrentBlock('singleline');
204  $tpl->setVariable("SIZE", $this->getSize());
205  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
206  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $value->getPosition());
207  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
208  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
209  if ($this->getDisabled())
210  {
211  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
212  }
213  $tpl->parseCurrentBlock();
214  }
215  else if (!$this->getSingleline())
216  {
217  $tpl->setCurrentBlock('multiline');
218  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
219  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
220  $tpl->setVariable("MULTILINE_ROW_NUMBER", $value->getPosition());
221  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
222  if ($this->getDisabled())
223  {
224  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
225  }
226  $tpl->parseCurrentBlock();
227  }
228  if ($this->getAllowMove())
229  {
230  $tpl->setCurrentBlock("move");
231  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][{$value->getPosition()}]");
232  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][{$value->getPosition()}]");
233  $tpl->setVariable("UP_ID", "up_{$this->getPostVar()}[{$value->getPosition()}]");
234  $tpl->setVariable("DOWN_ID", "down_{$this->getPostVar()}[{$value->getPosition()}]");
235  $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
236  $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
237  $tpl->parseCurrentBlock();
238  }
239 
240  $tpl->setCurrentBlock("row");
241 
242  $tpl->setVariable("POST_VAR", $this->getPostVar());
243  $tpl->setVariable("ROW_NUMBER", $value->getPosition());
244  $tpl->setVariable("ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
245 
246  $tpl->setVariable(
247  "CORRECTNESS_TRUE_ID", $this->getPostVar() . "[correctness][{$value->getPosition()}][true]"
248  );
249  $tpl->setVariable(
250  "CORRECTNESS_FALSE_ID", $this->getPostVar() . "[correctness][{$value->getPosition()}][false]"
251  );
252  $tpl->setVariable("CORRECTNESS_TRUE_VALUE", 1);
253  $tpl->setVariable("CORRECTNESS_FALSE_VALUE", 0);
254 
255  if( $value->getCorrectness() !== null )
256  {
257  if( $value->getCorrectness() )
258  {
259  $tpl->setVariable('CORRECTNESS_TRUE_SELECTED', ' checked="checked"');
260  }
261  else
262  {
263  $tpl->setVariable('CORRECTNESS_FALSE_SELECTED', ' checked="checked"');
264  }
265  }
266 
267  if ($this->getDisabled())
268  {
269  $tpl->setVariable("DISABLED_CORRECTNESS", " disabled=\"disabled\"");
270  }
271 
272  $tpl->parseCurrentBlock();
273  }
274 
275  if ($this->getSingleline())
276  {
277  if (!$this->hideImages)
278  {
279  if (is_array($this->getSuffixes()))
280  {
281  $suff_str = $delim = "";
282  foreach($this->getSuffixes() as $suffix)
283  {
284  $suff_str.= $delim.".".$suffix;
285  $delim = ", ";
286  }
287  $tpl->setCurrentBlock('allowed_image_suffixes');
288  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $this->lng->txt("file_allowed_suffixes")." ".$suff_str);
289  $tpl->parseCurrentBlock();
290  }
291 
292  $tpl->setCurrentBlock("image_heading");
293  $tpl->setVariable("ANSWER_IMAGE", $this->lng->txt('answer_image'));
294  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
295  $tpl->parseCurrentBlock();
296  }
297  }
298 
299  foreach($this->qstObject->getValidOptionLabels() as $optionLabel)
300  {
301  if( $this->qstObject->isCustomOptionLabel($optionLabel) )
302  {
303  continue;
304  }
305 
306  $tpl->setCurrentBlock('option_label_translations');
307  $tpl->setVariable('OPTION_LABEL', $optionLabel);
308  $tpl->setVariable('TRANSLATION_TRUE', $this->qstObject->getTrueOptionLabelTranslation($this->lng, $optionLabel));
309  $tpl->setVariable('TRANSLATION_FALSE', $this->qstObject->getFalseOptionLabelTranslation($this->lng, $optionLabel));
310  $tpl->parseCurrentBlock();
311  }
312 
313  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
314  $tpl->setVariable("DELETE_IMAGE_HEADER", $this->lng->txt('delete_image_header'));
315  $tpl->setVariable("DELETE_IMAGE_QUESTION", $this->lng->txt('delete_image_question'));
316  $tpl->setVariable("ANSWER_TEXT", $this->lng->txt('answer_text'));
317 
318  $tpl->setVariable("OPTIONS_TEXT", $this->lng->txt('options'));
319 
320  // winzards input column label values will be updated on document ready js
321  //$tpl->setVariable("TRUE_TEXT", $this->qstObject->getTrueOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
322  //$tpl->setVariable("FALSE_TEXT", $this->qstObject->getFalseOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
323 
324  $a_tpl->setCurrentBlock("prop_generic");
325  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
326  $a_tpl->parseCurrentBlock();
327 
328  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
329  $this->tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
330  $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/kprimchoicewizard.js");
331  $this->tpl->addJavascript('Modules/TestQuestionPool/js/ilAssKprimChoice.js');
332  }
333 
334  public function checkUploads($foundvalues)
335  {
336  if (is_array($_FILES) && count($_FILES) && $this->getSingleline())
337  {
338  if (!$this->hideImages)
339  {
340  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
341  {
342  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
343  {
344  // error handling
345  if ($error > 0)
346  {
347  switch ($error)
348  {
349  case UPLOAD_ERR_INI_SIZE:
350  $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
351  return false;
352  break;
353 
354  case UPLOAD_ERR_FORM_SIZE:
355  $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
356  return false;
357  break;
358 
359  case UPLOAD_ERR_PARTIAL:
360  $this->setAlert($this->lng->txt("form_msg_file_partially_uploaded"));
361  return false;
362  break;
363 
364  case UPLOAD_ERR_NO_FILE:
365  if($this->getRequired() && !$this->isIgnoreMissingUploadsEnabled())
366  {
367  if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index])))
368  {
369  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
370  return false;
371  }
372  }
373  break;
374 
375  case UPLOAD_ERR_NO_TMP_DIR:
376  $this->setAlert($this->lng->txt("form_msg_file_missing_tmp_dir"));
377  return false;
378  break;
379 
380  case UPLOAD_ERR_CANT_WRITE:
381  $this->setAlert($this->lng->txt("form_msg_file_cannot_write_to_disk"));
382  return false;
383  break;
384 
385  case UPLOAD_ERR_EXTENSION:
386  $this->setAlert($this->lng->txt("form_msg_file_upload_stopped_ext"));
387  return false;
388  break;
389  }
390  }
391  }
392  }
393  else
394  {
395  if ($this->getRequired())
396  {
397  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
398  return false;
399  }
400  }
401 
402  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
403  {
404  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
405  {
406  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
407  $filename_arr = pathinfo($filename);
408  $suffix = $filename_arr["extension"];
409  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
410  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
411  // check suffixes
412  if (strlen($tmpname) && is_array($this->getSuffixes()))
413  {
414  if (!in_array(strtolower($suffix), $this->getSuffixes()))
415  {
416  $this->setAlert($this->lng->txt("form_msg_file_wrong_file_type"));
417  return false;
418  }
419  }
420  }
421  }
422 
423  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
424  {
425  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
426  {
427  if( $_FILES[$this->getPostVar()]['error']['image'][$index] > 0 )
428  {
429  continue;
430  }
431 
432  $mimetype = ilObjMediaObject::getMimeType($tmpname);
433 
434  if( !preg_match("/^image/", $mimetype) )
435  {
436  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
437  $this->setAlert($this->lng->txt("form_msg_file_wrong_mime_type"));
438  return false;
439  }
440  }
441  }
442 
443 
444  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
445  {
446  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
447  {
448  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
449  $filename_arr = pathinfo($filename);
450  $suffix = $filename_arr["extension"];
451  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
452  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
453  // virus handling
454  if (strlen($tmpname))
455  {
456  $vir = ilUtil::virusHandling($tmpname, $filename);
457  if ($vir[0] == false)
458  {
459  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
460  $this->setAlert($this->lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
461  return false;
462  }
463  }
464  }
465  }
466  }
467  }
468 
469  return true;
470  }
471 
472  public function collectValidFiles()
473  {
474  foreach($_FILES[$this->getPostVar()]['error']['image'] as $index => $err)
475  {
476  if( $err > 0 )
477  {
478  continue;
479  }
480 
481  $this->files[$index] = array(
482  'position' => $index,
483  'tmp_name' => $_FILES[$this->getPostVar()]['tmp_name']['image'][$index],
484  'name' => $_FILES[$this->getPostVar()]['name']['image'][$index],
485  'type' => $_FILES[$this->getPostVar()]['type']['image'][$index],
486  'size' => $_FILES[$this->getPostVar()]['size']['image'][$index]
487  );
488  }
489  }
490 }
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
getMaxLength()
Get Max Length.
$error
Definition: Error.php:17
static virusHandling($a_file, $a_orig_name="", $a_clean=true)
scan file for viruses and clean files if possible
static _getUsedHTMLTagsAsString($a_module="")
Returns a string of all allowed HTML tags for text editing.
getPostVar()
Get Post Variable.
static get($a_glyph, $a_text="")
Get glyph html.
static getFileSizeInfo()
setAlert($a_alert)
Set Alert Text.
getFieldId()
Get Post Variable.
special template class to simplify handling of ITX/PEAR
static stripSlashesRecursive($a_data, $a_strip_html=true, $a_allow="")
Strip slashes from array and sub-arrays.
Create styles array
The data for the language used.
This class represents a single choice wizard property in a property form.
static getMimeType($a_file, $a_external=false)
get mime type for file
setIgnoreMissingUploadsEnabled($ignoreMissingUploadsEnabled)
$_POST["username"]