ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilSingleChoiceWizardInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2007 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
32 {
33  protected $values = array();
34  protected $allowMove = false;
35  protected $singleline = true;
36  protected $qstObject = null;
37  protected $suffixes = array();
38  protected $showPoints = true;
39  protected $hideImages = false;
40 
47  function __construct($a_title = "", $a_postvar = "")
48  {
49  parent::__construct($a_title, $a_postvar);
50  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
51  $this->setSize('25');
52  $this->validationRegexp = "";
53  }
54 
60  function setValue($a_value)
61  {
62  $this->values = array();
63  if (is_array($a_value))
64  {
65  if (is_array($a_value['answer']))
66  {
67  foreach ($a_value['answer'] as $index => $value)
68  {
69  include_once "./Modules/TestQuestionPool/classes/class.assAnswerBinaryStateImage.php";
70  $answer = new ASS_AnswerBinaryStateImage($value, $a_value['points'][$index], $index, 1, $a_value['imagename'][$index]);
71  array_push($this->values, $answer);
72  }
73  }
74  }
75  }
76 
82  function setSuffixes($a_suffixes)
83  {
84  $this->suffixes = $a_suffixes;
85  }
86 
92  function setHideImages($a_hide)
93  {
94  $this->hideImages = $a_hide;
95  }
96 
102  function getSuffixes()
103  {
104  return $this->suffixes;
105  }
106 
107  public function setShowPoints($a_value)
108  {
109  $this->showPoints = $a_value;
110  }
111 
112  public function getShowPoints()
113  {
114  return $this->showPoints;
115  }
116 
122  function setValues($a_values)
123  {
124  $this->values = $a_values;
125  }
126 
132  function getValues()
133  {
134  return $this->values;
135  }
136 
142  function setSingleline($a_value)
143  {
144  $this->singleline = $a_value;
145  }
146 
152  function getSingleline()
153  {
154  return $this->singleline;
155  }
156 
162  function setQuestionObject($a_value)
163  {
164  $this->qstObject =& $a_value;
165  }
166 
172  function getQuestionObject()
173  {
174  return $this->qstObject;
175  }
176 
182  function setAllowMove($a_allow_move)
183  {
184  $this->allowMove = $a_allow_move;
185  }
186 
192  function getAllowMove()
193  {
194  return $this->allowMove;
195  }
196 
202  function checkInput()
203  {
204  global $lng;
205 
206  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
207 
208  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()], true, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
209  $foundvalues = $_POST[$this->getPostVar()];
210  if (is_array($foundvalues))
211  {
212  // check answers
213  if (is_array($foundvalues['answer']))
214  {
215  foreach ($foundvalues['answer'] as $aidx => $answervalue)
216  {
217  if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0))
218  {
219  $this->setAlert($lng->txt("msg_input_is_required"));
220  return FALSE;
221  }
222  }
223  }
224  // check points
225  $max = 0;
226  if (is_array($foundvalues['points']))
227  {
228  foreach ($foundvalues['points'] as $points)
229  {
230  if ($points > $max) $max = $points;
231  if (((strlen($points)) == 0) || (!is_numeric($points)))
232  {
233  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
234  return FALSE;
235  }
236  }
237  }
238  if ($max == 0)
239  {
240  $this->setAlert($lng->txt("enter_enough_positive_points"));
241  return false;
242  }
243 
244  if (is_array($_FILES) && count($_FILES) && $this->getSingleline() && (!$this->hideImages))
245  {
246  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
247  {
248  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
249  {
250  // error handling
251  if ($error > 0)
252  {
253  switch ($error)
254  {
255  case UPLOAD_ERR_INI_SIZE:
256  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
257  return false;
258  break;
259 
260  case UPLOAD_ERR_FORM_SIZE:
261  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
262  return false;
263  break;
264 
265  case UPLOAD_ERR_PARTIAL:
266  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
267  return false;
268  break;
269 
270  case UPLOAD_ERR_NO_FILE:
271  if ($this->getRequired())
272  {
273  if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index])))
274  {
275  $this->setAlert($lng->txt("form_msg_file_no_upload"));
276  return false;
277  }
278  }
279  break;
280 
281  case UPLOAD_ERR_NO_TMP_DIR:
282  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
283  return false;
284  break;
285 
286  case UPLOAD_ERR_CANT_WRITE:
287  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
288  return false;
289  break;
290 
291  case UPLOAD_ERR_EXTENSION:
292  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
293  return false;
294  break;
295  }
296  }
297  }
298  }
299  else
300  {
301  if ($this->getRequired())
302  {
303  $this->setAlert($lng->txt("form_msg_file_no_upload"));
304  return false;
305  }
306  }
307 
308  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
309  {
310  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
311  {
312  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
313  $filename_arr = pathinfo($filename);
314  $suffix = $filename_arr["extension"];
315  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
316  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
317  // check suffixes
318  if (strlen($tmpname) && is_array($this->getSuffixes()))
319  {
320  if (!in_array(strtolower($suffix), $this->getSuffixes()))
321  {
322  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
323  return false;
324  }
325  }
326  }
327  }
328 
329  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
330  {
331  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
332  {
333  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
334  $filename_arr = pathinfo($filename);
335  $suffix = $filename_arr["extension"];
336  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
337  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
338  // virus handling
339  if (strlen($tmpname))
340  {
341  $vir = ilUtil::virusHandling($tmpname, $filename);
342  if ($vir[0] == false)
343  {
344  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
345  return false;
346  }
347  }
348  }
349  }
350  }
351  }
352  else
353  {
354  $this->setAlert($lng->txt("msg_input_is_required"));
355  return FALSE;
356  }
357 
358  return $this->checkSubItemsInput();
359  }
360 
366  function insert(&$a_tpl)
367  {
368  global $lng;
369 
370  $tpl = new ilTemplate("tpl.prop_singlechoicewizardinput.html", true, true, "Modules/TestQuestionPool");
371  $i = 0;
372  foreach ($this->values as $value)
373  {
374  if ($this->getSingleline())
375  {
376  if (!$this->hideImages)
377  {
378  if (strlen($value->getImage()))
379  {
380  $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
381  if (($this->getSingleline()) && ($this->qstObject->getThumbSize()))
382  {
383  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage()))
384  {
385  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
386  }
387  }
388  $tpl->setCurrentBlock('image');
389  $tpl->setVariable('SRC_IMAGE', $imagename);
390  $tpl->setVariable('IMAGE_NAME', $value->getImage());
391  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->getAnswertext()));
392  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
393  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
394  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
395  $tpl->parseCurrentBlock();
396  }
397  $tpl->setCurrentBlock('addimage');
398  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
399  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
400  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
401  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
402  $tpl->parseCurrentBlock();
403  }
404 
405  if (is_object($value))
406  {
407  $tpl->setCurrentBlock("prop_text_propval");
408  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
409  $tpl->parseCurrentBlock();
410  $tpl->setCurrentBlock("prop_points_propval");
411  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
412  $tpl->parseCurrentBlock();
413  }
414  $tpl->setCurrentBlock('singleline');
415  $tpl->setVariable("SIZE", $this->getSize());
416  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
417  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
418  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
419  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
420  if ($this->getDisabled())
421  {
422  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
423  }
424  $tpl->parseCurrentBlock();
425  }
426  else if (!$this->getSingleline())
427  {
428  if (is_object($value))
429  {
430  if ($this->getShowPoints())
431  {
432  $tpl->setCurrentBlock("prop_points_propval");
433  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
434  $tpl->parseCurrentBlock();
435  }
436  }
437  $tpl->setCurrentBlock('multiline');
438  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
439  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
440  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
441  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
442  if ($this->getDisabled())
443  {
444  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
445  }
446  $tpl->parseCurrentBlock();
447  }
448  if ($this->getAllowMove())
449  {
450  $tpl->setCurrentBlock("move");
451  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
452  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
453  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
454  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.gif'));
455  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.gif'));
456  $tpl->parseCurrentBlock();
457  }
458  if ($this->getShowPoints())
459  {
460  $tpl->setCurrentBlock("points");
461  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
462  $tpl->setVariable("POINTS_POST_VAR", $this->getPostVar());
463  $tpl->setVariable("POINTS_ROW_NUMBER", $i);
464  $tpl->parseCurrentBlock();
465  }
466  $tpl->setCurrentBlock("row");
467  $class = ($i % 2 == 0) ? "even" : "odd";
468  if ($i == 0) $class .= " first";
469  if ($i == count($this->values)-1) $class .= " last";
470  $tpl->setVariable("ROW_CLASS", $class);
471  $tpl->setVariable("POST_VAR", $this->getPostVar());
472  $tpl->setVariable("ROW_NUMBER", $i);
473  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
474  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
475  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
476  if ($this->getDisabled())
477  {
478  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
479  }
480  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
481  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
482  $tpl->parseCurrentBlock();
483  $i++;
484  }
485 
486  if ($this->getSingleline())
487  {
488  if (!$this->hideImages)
489  {
490  if (is_array($this->getSuffixes()))
491  {
492  $suff_str = $delim = "";
493  foreach($this->getSuffixes() as $suffix)
494  {
495  $suff_str.= $delim.".".$suffix;
496  $delim = ", ";
497  }
498  $tpl->setCurrentBlock('allowed_image_suffixes');
499  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
500  $tpl->parseCurrentBlock();
501  }
502  $tpl->setCurrentBlock("image_heading");
503  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
504  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
505  $tpl->parseCurrentBlock();
506  }
507  }
508 
509  if ($this->getShowPoints())
510  {
511  $tpl->setCurrentBlock("points_heading");
512  $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
513  $tpl->parseCurrentBlock();
514  }
515 
516  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
517  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
518  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
519  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
520  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
521  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
522  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
523 
524  $a_tpl->setCurrentBlock("prop_generic");
525  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
526  $a_tpl->parseCurrentBlock();
527 
528  global $tpl;
529  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
531  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/singlechoicewizard.js");
532  }
533 }