ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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  foreach ($a_value['answer'] as $index => $value) {
76  $answer = new ilAssKprimChoiceAnswer();
77 
78  $answer->setPosition($index);
79  $answer->setAnswertext($value);
80  $answer->setImageFile($a_value['imagename'][$index]);
81 
82  if (strlen($a_value['correctness'][$index])) {
83  $answer->setCorrectness((bool) $a_value['correctness'][$index]);
84  }
85 
86  $answer->setThumbPrefix($this->qstObject->getThumbPrefix());
87  $answer->setImageFsDir($this->qstObject->getImagePath());
88  $answer->setImageWebDir($this->qstObject->getImagePathWeb());
89 
90  $this->values[] = $answer;
91  }
92  }
93 
94  #vd($this->values);
95  }
96 
97  public function checkInput()
98  {
99  global $lng;
100 
101  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
102  if (is_array($_POST[$this->getPostVar()])) {
104  $_POST[$this->getPostVar()],
105  false,
107  );
108  }
109 
110  $foundvalues = $_POST[$this->getPostVar()];
111 
112  #vd($foundvalues);
113 
114  if (is_array($foundvalues)) {
115  // check answers
116  if (is_array($foundvalues['answer'])) {
117  foreach ($foundvalues['answer'] as $aidx => $answervalue) {
118  if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0)) {
119  $this->setAlert($lng->txt("msg_input_is_required"));
120  return false;
121  }
122  }
123  }
124 
125  // check correctness
126  if (!isset($foundvalues['correctness']) || count($foundvalues['correctness']) < count($foundvalues['answer'])) {
127  $this->setAlert($lng->txt("msg_input_is_required"));
128  return false;
129  }
130 
131  if (!$this->checkUploads($foundvalues)) {
132  return false;
133  }
134  } else {
135  $this->setAlert($lng->txt("msg_input_is_required"));
136  return false;
137  }
138 
139  return $this->checkSubItemsInput();
140  }
141 
145  public function insert($a_tpl)
146  {
147  $tpl = new ilTemplate("tpl.prop_kprimchoicewizardinput.html", true, true, "Modules/TestQuestionPool");
148 
149  foreach ($this->values as $value) {
154  if ($this->getSingleline()) {
155  if (!$this->hideImages) {
156  if (strlen($value->getImageFile())) {
157  $imagename = $value->getImageWebPath();
158 
159  if (($this->getSingleline()) && ($this->qstObject->getThumbSize())) {
160  if (@file_exists($value->getThumbFsPath())) {
161  $imagename = $value->getThumbWebPath();
162  }
163  }
164 
165  $tpl->setCurrentBlock('image');
166  $tpl->setVariable('SRC_IMAGE', $imagename);
167  $tpl->setVariable('IMAGE_NAME', $value->getImageFile());
168  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->getAnswertext()));
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("IMAGE_SUBMIT", $this->lng->txt("upload"));
178  $tpl->setVariable("IMAGE_ROW_NUMBER", $value->getPosition());
179  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
180  $tpl->parseCurrentBlock();
181  }
182 
183  $tpl->setCurrentBlock("prop_text_propval");
184  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
185  $tpl->parseCurrentBlock();
186 
187  $tpl->setCurrentBlock('singleline');
188  $tpl->setVariable("SIZE", $this->getSize());
189  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
190  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $value->getPosition());
191  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
192  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
193  if ($this->getDisabled()) {
194  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
195  }
196  $tpl->parseCurrentBlock();
197  } elseif (!$this->getSingleline()) {
198  $tpl->setCurrentBlock('multiline');
199  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
200  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
201  $tpl->setVariable("MULTILINE_ROW_NUMBER", $value->getPosition());
202  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
203  if ($this->getDisabled()) {
204  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
205  }
206  $tpl->parseCurrentBlock();
207  }
208  if ($this->getAllowMove()) {
209  $tpl->setCurrentBlock("move");
210  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][{$value->getPosition()}]");
211  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][{$value->getPosition()}]");
212  $tpl->setVariable("UP_ID", "up_{$this->getPostVar()}[{$value->getPosition()}]");
213  $tpl->setVariable("DOWN_ID", "down_{$this->getPostVar()}[{$value->getPosition()}]");
214  $tpl->setVariable("UP_BUTTON", ilGlyphGUI::get(ilGlyphGUI::UP));
215  $tpl->setVariable("DOWN_BUTTON", ilGlyphGUI::get(ilGlyphGUI::DOWN));
216  $tpl->parseCurrentBlock();
217  }
218 
219  $tpl->setCurrentBlock("row");
220 
221  $tpl->setVariable("POST_VAR", $this->getPostVar());
222  $tpl->setVariable("ROW_NUMBER", $value->getPosition());
223  $tpl->setVariable("ID", $this->getPostVar() . "[answer][{$value->getPosition()}]");
224 
225  $tpl->setVariable(
226  "CORRECTNESS_TRUE_ID",
227  $this->getPostVar() . "[correctness][{$value->getPosition()}][true]"
228  );
229  $tpl->setVariable(
230  "CORRECTNESS_FALSE_ID",
231  $this->getPostVar() . "[correctness][{$value->getPosition()}][false]"
232  );
233  $tpl->setVariable("CORRECTNESS_TRUE_VALUE", 1);
234  $tpl->setVariable("CORRECTNESS_FALSE_VALUE", 0);
235 
236  if ($value->getCorrectness() !== null) {
237  if ($value->getCorrectness()) {
238  $tpl->setVariable('CORRECTNESS_TRUE_SELECTED', ' checked="checked"');
239  } else {
240  $tpl->setVariable('CORRECTNESS_FALSE_SELECTED', ' checked="checked"');
241  }
242  }
243 
244  if ($this->getDisabled()) {
245  $tpl->setVariable("DISABLED_CORRECTNESS", " disabled=\"disabled\"");
246  }
247 
248  $tpl->parseCurrentBlock();
249  }
250 
251  if ($this->getSingleline()) {
252  if (!$this->hideImages) {
253  if (is_array($this->getSuffixes())) {
254  $suff_str = $delim = "";
255  foreach ($this->getSuffixes() as $suffix) {
256  $suff_str.= $delim . "." . $suffix;
257  $delim = ", ";
258  }
259  $tpl->setCurrentBlock('allowed_image_suffixes');
260  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $this->lng->txt("file_allowed_suffixes") . " " . $suff_str);
261  $tpl->parseCurrentBlock();
262  }
263 
264  $tpl->setCurrentBlock("image_heading");
265  $tpl->setVariable("ANSWER_IMAGE", $this->lng->txt('answer_image'));
266  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
267  $tpl->parseCurrentBlock();
268  }
269  }
270 
271  foreach ($this->qstObject->getValidOptionLabels() as $optionLabel) {
272  if ($this->qstObject->isCustomOptionLabel($optionLabel)) {
273  continue;
274  }
275 
276  $tpl->setCurrentBlock('option_label_translations');
277  $tpl->setVariable('OPTION_LABEL', $optionLabel);
278  $tpl->setVariable('TRANSLATION_TRUE', $this->qstObject->getTrueOptionLabelTranslation($this->lng, $optionLabel));
279  $tpl->setVariable('TRANSLATION_FALSE', $this->qstObject->getFalseOptionLabelTranslation($this->lng, $optionLabel));
280  $tpl->parseCurrentBlock();
281  }
282 
283  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
284  $tpl->setVariable("DELETE_IMAGE_HEADER", $this->lng->txt('delete_image_header'));
285  $tpl->setVariable("DELETE_IMAGE_QUESTION", $this->lng->txt('delete_image_question'));
286  $tpl->setVariable("ANSWER_TEXT", $this->lng->txt('answer_text'));
287 
288  $tpl->setVariable("OPTIONS_TEXT", $this->lng->txt('options'));
289 
290  // winzards input column label values will be updated on document ready js
291  //$tpl->setVariable("TRUE_TEXT", $this->qstObject->getTrueOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
292  //$tpl->setVariable("FALSE_TEXT", $this->qstObject->getFalseOptionLabelTranslation($this->lng, $this->qstObject->getOptionLabel()));
293 
294  $a_tpl->setCurrentBlock("prop_generic");
295  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
296  $a_tpl->parseCurrentBlock();
297 
298  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
299  $this->tpl->addJavascript("./Services/Form/js/ServiceFormWizardInput.js");
300  $this->tpl->addJavascript("./Modules/TestQuestionPool/templates/default/kprimchoicewizard.js");
301  $this->tpl->addJavascript('Modules/TestQuestionPool/js/ilAssKprimChoice.js');
302  }
303 
304  public function checkUploads($foundvalues)
305  {
306  if (is_array($_FILES) && count($_FILES) && $this->getSingleline()) {
307  if (!$this->hideImages) {
308  if (is_array($_FILES[$this->getPostVar()]['error']['image'])) {
309  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error) {
310  // error handling
311  if ($error > 0) {
312  switch ($error) {
313  case UPLOAD_ERR_INI_SIZE:
314  $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
315  return false;
316  break;
317 
318  case UPLOAD_ERR_FORM_SIZE:
319  $this->setAlert($this->lng->txt("form_msg_file_size_exceeds"));
320  return false;
321  break;
322 
323  case UPLOAD_ERR_PARTIAL:
324  $this->setAlert($this->lng->txt("form_msg_file_partially_uploaded"));
325  return false;
326  break;
327 
328  case UPLOAD_ERR_NO_FILE:
329  if ($this->getRequired() && !$this->isIgnoreMissingUploadsEnabled()) {
330  if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index]))) {
331  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
332  return false;
333  }
334  }
335  break;
336 
337  case UPLOAD_ERR_NO_TMP_DIR:
338  $this->setAlert($this->lng->txt("form_msg_file_missing_tmp_dir"));
339  return false;
340  break;
341 
342  case UPLOAD_ERR_CANT_WRITE:
343  $this->setAlert($this->lng->txt("form_msg_file_cannot_write_to_disk"));
344  return false;
345  break;
346 
347  case UPLOAD_ERR_EXTENSION:
348  $this->setAlert($this->lng->txt("form_msg_file_upload_stopped_ext"));
349  return false;
350  break;
351  }
352  }
353  }
354  } else {
355  if ($this->getRequired()) {
356  $this->setAlert($this->lng->txt("form_msg_file_no_upload"));
357  return false;
358  }
359  }
360 
361  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
362  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
363  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
364  $filename_arr = pathinfo($filename);
365  $suffix = $filename_arr["extension"];
366  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
367  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
368  // check suffixes
369  if (strlen($tmpname) && is_array($this->getSuffixes())) {
370  if (!in_array(strtolower($suffix), $this->getSuffixes())) {
371  $this->setAlert($this->lng->txt("form_msg_file_wrong_file_type"));
372  return false;
373  }
374  }
375  }
376  }
377 
378  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
379  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
380  if ($_FILES[$this->getPostVar()]['error']['image'][$index] > 0) {
381  continue;
382  }
383 
384  $mimetype = ilObjMediaObject::getMimeType($tmpname);
385 
386  if (!preg_match("/^image/", $mimetype)) {
387  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
388  $this->setAlert($this->lng->txt("form_msg_file_wrong_mime_type"));
389  return false;
390  }
391  }
392  }
393 
394 
395  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image'])) {
396  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname) {
397  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
398  $filename_arr = pathinfo($filename);
399  $suffix = $filename_arr["extension"];
400  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
401  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
402  // virus handling
403  if (strlen($tmpname)) {
404  $vir = ilUtil::virusHandling($tmpname, $filename);
405  if ($vir[0] == false) {
406  $_FILES[$this->getPostVar()]['error']['image'][$index] = self::CUSTOM_UPLOAD_ERR;
407  $this->setAlert($this->lng->txt("form_msg_file_virus_found") . "<br />" . $vir[1]);
408  return false;
409  }
410  }
411  }
412  }
413  }
414  }
415 
416  return true;
417  }
418 
419  public function collectValidFiles()
420  {
421  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $err) {
422  if ($err > 0) {
423  continue;
424  }
425 
426  $this->files[$index] = array(
427  'position' => $index,
428  'tmp_name' => $_FILES[$this->getPostVar()]['tmp_name']['image'][$index],
429  'name' => $_FILES[$this->getPostVar()]['name']['image'][$index],
430  'type' => $_FILES[$this->getPostVar()]['type']['image'][$index],
431  'size' => $_FILES[$this->getPostVar()]['size']['image'][$index]
432  );
433  }
434  }
435 }
static prepareFormOutput($a_str, $a_strip=false)
prepares string output for html forms public
getMaxLength()
Get Max Length.
static getMimeType($a_file, $a_external=null)
get mime type for file
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()
$index
Definition: metadata.php:60
setAlert($a_alert)
Set Alert Text.
$error
Definition: Error.php:17
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.
setIgnoreMissingUploadsEnabled($ignoreMissingUploadsEnabled)
Done writing files
$_POST["username"]