ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSingleChoiceWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
12 {
13  protected $values = array();
14  protected $allowMove = false;
15  protected $singleline = true;
16  protected $qstObject = null;
17  protected $suffixes = array();
18  protected $showPoints = true;
19  protected $hideImages = false;
20 
21  protected $disable_text = false;
22  protected $disable_upload = false;
23  protected $disable_actions = false;
24 
31  function __construct($a_title = "", $a_postvar = "")
32  {
33  parent::__construct($a_title, $a_postvar);
34  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
35  $this->setSize('25');
36  $this->validationRegexp = "";
37  }
38 
43  {
44  $this->disable_upload = $disable_upload;
45  }
46 
51  {
52  $this->disable_actions = $disable_actions;
53  }
54 
58  public function setDisableText($disable_text)
59  {
60  $this->disable_text = $disable_text;
61  }
62 
68  function setValue($a_value)
69  {
70  $this->values = array();
71  if (is_array($a_value))
72  {
73  if (is_array($a_value['answer']))
74  {
75  foreach ($a_value['answer'] as $index => $value)
76  {
77  include_once "./Modules/TestQuestionPool/classes/class.assAnswerBinaryStateImage.php";
78  $answer = new ASS_AnswerBinaryStateImage($value, $a_value['points'][$index], $index, 1, $a_value['imagename'][$index]);
79  array_push($this->values, $answer);
80  }
81  }
82  }
83  }
84 
90  function setSuffixes($a_suffixes)
91  {
92  $this->suffixes = $a_suffixes;
93  }
94 
100  function setHideImages($a_hide)
101  {
102  $this->hideImages = $a_hide;
103  }
104 
110  function getSuffixes()
111  {
112  return $this->suffixes;
113  }
114 
115  public function setShowPoints($a_value)
116  {
117  $this->showPoints = $a_value;
118  }
119 
120  public function getShowPoints()
121  {
122  return $this->showPoints;
123  }
124 
130  function setValues($a_values)
131  {
132  $this->values = $a_values;
133  }
134 
140  function getValues()
141  {
142  return $this->values;
143  }
144 
150  function setSingleline($a_value)
151  {
152  $this->singleline = $a_value;
153  }
154 
160  function getSingleline()
161  {
162  return $this->singleline;
163  }
164 
170  function setQuestionObject($a_value)
171  {
172  $this->qstObject =& $a_value;
173  }
174 
180  function getQuestionObject()
181  {
182  return $this->qstObject;
183  }
184 
190  function setAllowMove($a_allow_move)
191  {
192  $this->allowMove = $a_allow_move;
193  }
194 
200  function getAllowMove()
201  {
202  return $this->allowMove;
203  }
204 
210  function checkInput()
211  {
212  global $lng;
213 
214  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
215 
216  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()], true, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
217  $foundvalues = $_POST[$this->getPostVar()];
218  if (is_array($foundvalues))
219  {
220  // check answers
221  if (is_array($foundvalues['answer']))
222  {
223  foreach ($foundvalues['answer'] as $aidx => $answervalue)
224  {
225  if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0))
226  {
227  $this->setAlert($lng->txt("msg_input_is_required"));
228  return FALSE;
229  }
230  }
231  }
232  // check points
233  $max = 0;
234  if (is_array($foundvalues['points']))
235  {
236  foreach ($foundvalues['points'] as $points)
237  {
238  if ($points > $max) $max = $points;
239  if (((strlen($points)) == 0) || (!is_numeric($points)))
240  {
241  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
242  return FALSE;
243  }
244  }
245  }
246  if ($max == 0)
247  {
248  $this->setAlert($lng->txt("enter_enough_positive_points"));
249  return false;
250  }
251 
252  if (is_array($_FILES) && count($_FILES) && $this->getSingleline() && (!$this->hideImages))
253  {
254  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
255  {
256  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
257  {
258  // error handling
259  if ($error > 0)
260  {
261  switch ($error)
262  {
263  case UPLOAD_ERR_INI_SIZE:
264  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
265  return false;
266  break;
267 
268  case UPLOAD_ERR_FORM_SIZE:
269  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
270  return false;
271  break;
272 
273  case UPLOAD_ERR_PARTIAL:
274  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
275  return false;
276  break;
277 
278  case UPLOAD_ERR_NO_FILE:
279  if ($this->getRequired())
280  {
281  if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index])))
282  {
283  $this->setAlert($lng->txt("form_msg_file_no_upload"));
284  return false;
285  }
286  }
287  break;
288 
289  case UPLOAD_ERR_NO_TMP_DIR:
290  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
291  return false;
292  break;
293 
294  case UPLOAD_ERR_CANT_WRITE:
295  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
296  return false;
297  break;
298 
299  case UPLOAD_ERR_EXTENSION:
300  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
301  return false;
302  break;
303  }
304  }
305  }
306  }
307  else
308  {
309  if ($this->getRequired())
310  {
311  $this->setAlert($lng->txt("form_msg_file_no_upload"));
312  return false;
313  }
314  }
315 
316  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
317  {
318  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
319  {
320  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
321  $filename_arr = pathinfo($filename);
322  $suffix = $filename_arr["extension"];
323  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
324  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
325  // check suffixes
326  if (strlen($tmpname) && is_array($this->getSuffixes()))
327  {
328  if (!in_array(strtolower($suffix), $this->getSuffixes()))
329  {
330  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
331  return false;
332  }
333  }
334  }
335  }
336 
337  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
338  {
339  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
340  {
341  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
342  $filename_arr = pathinfo($filename);
343  $suffix = $filename_arr["extension"];
344  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
345  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
346  // virus handling
347  if (strlen($tmpname))
348  {
349  $vir = ilUtil::virusHandling($tmpname, $filename);
350  if ($vir[0] == false)
351  {
352  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
353  return false;
354  }
355  }
356  }
357  }
358  }
359  }
360  else
361  {
362  $this->setAlert($lng->txt("msg_input_is_required"));
363  return FALSE;
364  }
365 
366  return $this->checkSubItemsInput();
367  }
368 
374  function insert(&$a_tpl)
375  {
376  global $lng;
377 
378  $tpl = new ilTemplate("tpl.prop_singlechoicewizardinput.html", true, true, "Modules/TestQuestionPool");
379  $i = 0;
380  foreach ($this->values as $value)
381  {
382  if ($this->getSingleline())
383  {
384  if (!$this->hideImages)
385  {
386  if (strlen($value->getImage()))
387  {
388  $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
389  if (($this->getSingleline()) && ($this->qstObject->getThumbSize()))
390  {
391  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage()))
392  {
393  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
394  }
395  }
396  $tpl->setCurrentBlock('image');
397  $tpl->setVariable('SRC_IMAGE', $imagename);
398  $tpl->setVariable('IMAGE_NAME', $value->getImage());
399  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->getAnswertext()));
400  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
401  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
402  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
403  if($this->disable_upload)
404  {
405  $tpl->setVariable('DISABLED_UPLOAD', 'type="hidden" disabled="disabled"');
406  }
407  $tpl->parseCurrentBlock();
408  }
409  $tpl->setCurrentBlock('addimage');
410  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
411  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
412  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
413  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
414  if($this->disable_upload)
415  {
416  $tpl->setVariable('DISABLED_UPLOAD', 'type="hidden" disabled="disabled"');
417  }
418 
419  $tpl->parseCurrentBlock();
420  }
421 
422  if (is_object($value))
423  {
424  $tpl->setCurrentBlock("prop_text_propval");
425  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
426  $tpl->parseCurrentBlock();
427  if( $this->getShowPoints() ) {
428  $tpl->setCurrentBlock("prop_points_propval");
429  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
430  $tpl->parseCurrentBlock();
431  }
432  }
433  $tpl->setCurrentBlock('singleline');
434  if($this->disable_text)
435  {
436  $tpl->setVariable("DISABLED_SINGLELINE", 'readonly="readonly"');
437  $tpl->setVariable("DISABLED_SINGLELINE_BTN", 'readonly="readonly"');
438  }
439  $tpl->setVariable("SIZE", $this->getSize());
440  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
441  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
442  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
443  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
444  if ($this->getDisabled())
445  {
446  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
447  }
448  $tpl->parseCurrentBlock();
449  }
450  else if (!$this->getSingleline())
451  {
452  if (is_object($value))
453  {
454  if ($this->getShowPoints())
455  {
456  $tpl->setCurrentBlock("prop_points_propval");
457  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
458  $tpl->parseCurrentBlock();
459  }
460  }
461  $tpl->setCurrentBlock('multiline');
462  if($this->disable_text)
463  {
464  $tpl->setVariable("DISABLED_MULTILINE", 'readonly="readonly"');
465  }
466 
467  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
468  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
469  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
470  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
471  if ($this->getDisabled())
472  {
473  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
474  }
475  $tpl->parseCurrentBlock();
476  }
477  if ($this->getAllowMove())
478  {
479  $tpl->setCurrentBlock("move");
480  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
481  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
482  $tpl->setVariable("MOVE_ID", $this->getPostVar() . "[$i]");
483  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.png'));
484  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.png'));
485  $tpl->parseCurrentBlock();
486  }
487  if ($this->getShowPoints())
488  {
489  $tpl->setCurrentBlock("points");
490  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
491  $tpl->setVariable("POINTS_POST_VAR", $this->getPostVar());
492  $tpl->setVariable("POINTS_ROW_NUMBER", $i);
493  $tpl->parseCurrentBlock();
494  }
495  $tpl->setCurrentBlock("row");
496  $class = ($i % 2 == 0) ? "even" : "odd";
497  if ($i == 0) $class .= " first";
498  if ($i == count($this->values)-1) $class .= " last";
499  $tpl->setVariable("ROW_CLASS", $class);
500  $tpl->setVariable("POST_VAR", $this->getPostVar());
501  $tpl->setVariable("ROW_NUMBER", $i);
502  if(!$this->disable_actions)
503  {
504  //$tpl->setCurrentBlock('addremove');
505  $tpl->setVariable( "ID", $this->getPostVar() . "[answer][$i]" );
506  $tpl->setVariable( "CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]" );
507  $tpl->setVariable( "CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]" );
508  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
509  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
510  }
511  if ($this->getDisabled())
512  {
513  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
514  }
515  $tpl->parseCurrentBlock();
516  $i++;
517  }
518 
519  if ($this->getSingleline())
520  {
521  if (!$this->hideImages)
522  {
523  if (is_array($this->getSuffixes()))
524  {
525  $suff_str = $delim = "";
526  foreach($this->getSuffixes() as $suffix)
527  {
528  $suff_str.= $delim.".".$suffix;
529  $delim = ", ";
530  }
531  $tpl->setCurrentBlock('allowed_image_suffixes');
532  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
533  $tpl->parseCurrentBlock();
534  }
535  $tpl->setCurrentBlock("image_heading");
536  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
537  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
538  $tpl->parseCurrentBlock();
539  }
540  }
541 
542  if ($this->getShowPoints())
543  {
544  $tpl->setCurrentBlock("points_heading");
545  $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
546  $tpl->parseCurrentBlock();
547  }
548  if(!$this->disable_actions)
549  {
550  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
551  }
552  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
553  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
554  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
555  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
556  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
557  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
558  //$tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
559 
560  $a_tpl->setCurrentBlock("prop_generic");
561  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
562  $a_tpl->parseCurrentBlock();
563 
564  global $tpl;
565  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
567  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/singlechoicewizard.js");
568  }
569 }