ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
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 
152  public function insert(&$a_tpl)
153  {
154  $tpl = new ilTemplate("tpl.prop_kprimchoicewizardinput.html", true, true, "Modules/TestQuestionPool");
155 
156  foreach ($this->values as $value)
157  {
162  if ($this->getSingleline())
163  {
164  if (!$this->hideImages)
165  {
166  if (strlen($value->getImageFile()))
167  {
168  $imagename = $value->getImageWebPath();
169 
170  if (($this->getSingleline()) && ($this->qstObject->getThumbSize()))
171  {
172  if (@file_exists($value->getThumbFsPath()))
173  {
174  $imagename = $value->getThumbWebPath();
175  }
176  }
177 
178  $tpl->setCurrentBlock('image');
179  $tpl->setVariable('SRC_IMAGE', $imagename);
180  $tpl->setVariable('IMAGE_NAME', $value->getImageFile());
181  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->getAnswertext()));
182  $tpl->setVariable("TXT_DELETE_EXISTING", $this->lng->txt("delete_existing_file"));
183  $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
184  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
185  $tpl->parseCurrentBlock();
186  }
187  $tpl->setCurrentBlock('addimage');
188  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][{$value->getPosition()}]");
189  $tpl->setVariable("IMAGE_SUBMIT", $this->lng->txt("upload"));
190  $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
191  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
192  $tpl->parseCurrentBlock();
193  }
194 
195  $tpl->setCurrentBlock("prop_text_propval");
196  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
197  $tpl->parseCurrentBlock();
198 
199  $tpl->setCurrentBlock('singleline');
200  $tpl->setVariable("SIZE", $this->getSize());
201  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
202  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $value->getPosition());
203  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
204  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
205  if ($this->getDisabled())
206  {
207  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
208  }
209  $tpl->parseCurrentBlock();
210  }
211  else if (!$this->getSingleline())
212  {
213  $tpl->setCurrentBlock('multiline');
214  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
215  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
216  $tpl->setVariable("MULTILINE_ROW_NUMBER", $value->getPosition());
217  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
218  if ($this->getDisabled())
219  {
220  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
221  }
222  $tpl->parseCurrentBlock();
223  }
224  if ($this->getAllowMove())
225  {
226  $tpl->setCurrentBlock("move");
227  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][{$value->getPosition()}]");
228  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][{$value->getPosition()}]");
229  $tpl->setVariable("UP_ID", "up_{$this->getPostVar()}[{$value->getPosition()}]");
230  $tpl->setVariable("DOWN_ID", "down_{$this->getPostVar()}[{$value->getPosition()}]");
231  $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
232  $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
233  $tpl->parseCurrentBlock();
234  }
235 
236  $tpl->setCurrentBlock("row");
237 
238  $tpl->setVariable("POST_VAR", $this->getPostVar());
239  $tpl->setVariable("ROW_NUMBER", $value->getPosition());
240  $tpl->setVariable("ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
241 
242  $tpl->setVariable(
243  "CORRECTNESS_TRUE_ID", $this->getPostVar() . "[correctness][{$value->getPosition()}][true]"
244  );
245  $tpl->setVariable(
246  "CORRECTNESS_FALSE_ID", $this->getPostVar() . "[correctness][{$value->getPosition()}][false]"
247  );
248  $tpl->setVariable("CORRECTNESS_TRUE_VALUE", 1);
249  $tpl->setVariable("CORRECTNESS_FALSE_VALUE", 0);
250 
251  if( $value->getCorrectness() !== null )
252  {
253  if( $value->getCorrectness() )
254  {
255  $tpl->setVariable('CORRECTNESS_TRUE_SELECTED', ' checked="checked"');
256  }
257  else
258  {
259  $tpl->setVariable('CORRECTNESS_FALSE_SELECTED', ' checked="checked"');
260  }
261  }
262 
263  if ($this->getDisabled())
264  {
265  $tpl->setVariable("DISABLED_CORRECTNESS", " disabled=\"disabled\"");
266  }
267 
268  $tpl->parseCurrentBlock();
269  }
270 
271  if ($this->getSingleline())
272  {
273  if (!$this->hideImages)
274  {
275  if (is_array($this->getSuffixes()))
276  {
277  $suff_str = $delim = "";
278  foreach($this->getSuffixes() as $suffix)
279  {
280  $suff_str.= $delim.".".$suffix;
281  $delim = ", ";
282  }
283  $tpl->setCurrentBlock('allowed_image_suffixes');
284  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $this->lng->txt("file_allowed_suffixes")." ".$suff_str);
285  $tpl->parseCurrentBlock();
286  }
287 
288  $tpl->setCurrentBlock("image_heading");
289  $tpl->setVariable("ANSWER_IMAGE", $this->lng->txt('answer_image'));
290  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
291  $tpl->parseCurrentBlock();
292  }
293  }
294 
295  foreach($this->qstObject->getValidOptionLabels() as $optionLabel)
296  {
297  if( $this->qstObject->isCustomOptionLabel($optionLabel) )
298  {
299  continue;
300  }
301 
302  $tpl->setCurrentBlock('option_label_translations');
303  $tpl->setVariable('OPTION_LABEL', $optionLabel);
304  $tpl->setVariable('TRANSLATION_TRUE', $this->qstObject->getTrueOptionLabelTranslation($this->lng, $optionLabel));
305  $tpl->setVariable('TRANSLATION_FALSE', $this->qstObject->getFalseOptionLabelTranslation($this->lng, $optionLabel));
306  $tpl->parseCurrentBlock();
307  }
308 
309  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
310  $tpl->setVariable("DELETE_IMAGE_HEADER", $this->lng->txt('delete_image_header'));
311  $tpl->setVariable("DELETE_IMAGE_QUESTION", $this->lng->txt('delete_image_question'));
312  $tpl->setVariable("ANSWER_TEXT", $this->lng->txt('answer_text'));
313 
314  $tpl->setVariable("OPTIONS_TEXT", $this->lng->txt('options'));
315 
316  // winzards input column label values will be updated on document ready js
317  //$tpl->setVariable("TRUE_TEXT", $this->qstObject->getTrueOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
318  //$tpl->setVariable("FALSE_TEXT", $this->qstObject->getFalseOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
319 
320  $a_tpl->setCurrentBlock("prop_generic");
321  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
322  $a_tpl->parseCurrentBlock();
323 
324  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
325  $this->tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
326  $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/kprimchoicewizard.js");
327  $this->tpl->addJavascript('Modules/TestQuestionPool/js/ilAssKprimChoice.js');
328  }
329 
330  public function checkUploads($foundvalues)
331  {
332  if (is_array($_FILES) && count($_FILES) && $this->getSingleline())
333  {
334  if (!$this->hideImages)
335  {
336  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
337  {
338  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
339  {
340  // error handling
341  if ($error > 0)
342  {
343  switch ($error)
344  {
345  case UPLOAD_ERR_INI_SIZE:
346  $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
347  return false;
348  break;
349 
350  case UPLOAD_ERR_FORM_SIZE:
351  $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
352  return false;
353  break;
354 
355  case UPLOAD_ERR_PARTIAL:
356  $this->setAlert($this->lng->txt("form_msg_file_partially_uploaded"));
357  return false;
358  break;
359 
360  case UPLOAD_ERR_NO_FILE:
361  if($this->getRequired() && !$this->isIgnoreMissingUploadsEnabled())
362  {
363  if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index])))
364  {
365  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
366  return false;
367  }
368  }
369  break;
370 
371  case UPLOAD_ERR_NO_TMP_DIR:
372  $this->setAlert($this->lng->txt("form_msg_file_missing_tmp_dir"));
373  return false;
374  break;
375 
376  case UPLOAD_ERR_CANT_WRITE:
377  $this->setAlert($this->lng->txt("form_msg_file_cannot_write_to_disk"));
378  return false;
379  break;
380 
381  case UPLOAD_ERR_EXTENSION:
382  $this->setAlert($this->lng->txt("form_msg_file_upload_stopped_ext"));
383  return false;
384  break;
385  }
386  }
387  }
388  }
389  else
390  {
391  if ($this->getRequired())
392  {
393  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
394  return false;
395  }
396  }
397 
398  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
399  {
400  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
401  {
402  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
403  $filename_arr = pathinfo($filename);
404  $suffix = $filename_arr["extension"];
405  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
406  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
407  // check suffixes
408  if (strlen($tmpname) && is_array($this->getSuffixes()))
409  {
410  if (!in_array(strtolower($suffix), $this->getSuffixes()))
411  {
412  $this->setAlert($this->lng->txt("form_msg_file_wrong_file_type"));
413  return false;
414  }
415  }
416  }
417  }
418 
419  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
420  {
421  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
422  {
423  if( $_FILES[$this->getPostVar()]['error']['image'][$index] > 0 )
424  {
425  continue;
426  }
427 
428  $mimetype = ilObjMediaObject::getMimeType($tmpname);
429 
430  if( !preg_match("/^image/", $mimetype) )
431  {
432  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
433  $this->setAlert($this->lng->txt("form_msg_file_wrong_mime_type"));
434  return false;
435  }
436  }
437  }
438 
439 
440  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
441  {
442  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
443  {
444  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
445  $filename_arr = pathinfo($filename);
446  $suffix = $filename_arr["extension"];
447  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
448  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
449  // virus handling
450  if (strlen($tmpname))
451  {
452  $vir = ilUtil::virusHandling($tmpname, $filename);
453  if ($vir[0] == false)
454  {
455  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
456  $this->setAlert($this->lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
457  return false;
458  }
459  }
460  }
461  }
462  }
463  }
464 
465  return true;
466  }
467 
468  public function collectValidFiles()
469  {
470  foreach($_FILES[$this->getPostVar()]['error']['image'] as $index => $err)
471  {
472  if( $err > 0 )
473  {
474  continue;
475  }
476 
477  $this->files[$index] = array(
478  'position' => $index,
479  'tmp_name' => $_FILES[$this->getPostVar()]['tmp_name']['image'][$index],
480  'name' => $_FILES[$this->getPostVar()]['name']['image'][$index],
481  'type' => $_FILES[$this->getPostVar()]['type']['image'][$index],
482  'size' => $_FILES[$this->getPostVar()]['size']['image'][$index]
483  );
484  }
485  }
486 }