ILIAS  release_4-3 Revision
 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  if( $this->getShowPoints() ) {
411  $tpl->setCurrentBlock("prop_points_propval");
412  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
413  $tpl->parseCurrentBlock();
414  }
415  }
416  $tpl->setCurrentBlock('singleline');
417  $tpl->setVariable("SIZE", $this->getSize());
418  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
419  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
420  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
421  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
422  if ($this->getDisabled())
423  {
424  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
425  }
426  $tpl->parseCurrentBlock();
427  }
428  else if (!$this->getSingleline())
429  {
430  if (is_object($value))
431  {
432  if ($this->getShowPoints())
433  {
434  $tpl->setCurrentBlock("prop_points_propval");
435  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
436  $tpl->parseCurrentBlock();
437  }
438  }
439  $tpl->setCurrentBlock('multiline');
440  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
441  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
442  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
443  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
444  if ($this->getDisabled())
445  {
446  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
447  }
448  $tpl->parseCurrentBlock();
449  }
450  if ($this->getAllowMove())
451  {
452  $tpl->setCurrentBlock("move");
453  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
454  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
455  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
456  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.png'));
457  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.png'));
458  $tpl->parseCurrentBlock();
459  }
460  if ($this->getShowPoints())
461  {
462  $tpl->setCurrentBlock("points");
463  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
464  $tpl->setVariable("POINTS_POST_VAR", $this->getPostVar());
465  $tpl->setVariable("POINTS_ROW_NUMBER", $i);
466  $tpl->parseCurrentBlock();
467  }
468  $tpl->setCurrentBlock("row");
469  $class = ($i % 2 == 0) ? "even" : "odd";
470  if ($i == 0) $class .= " first";
471  if ($i == count($this->values)-1) $class .= " last";
472  $tpl->setVariable("ROW_CLASS", $class);
473  $tpl->setVariable("POST_VAR", $this->getPostVar());
474  $tpl->setVariable("ROW_NUMBER", $i);
475  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
476  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
477  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
478  if ($this->getDisabled())
479  {
480  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
481  }
482  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
483  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
484  $tpl->parseCurrentBlock();
485  $i++;
486  }
487 
488  if ($this->getSingleline())
489  {
490  if (!$this->hideImages)
491  {
492  if (is_array($this->getSuffixes()))
493  {
494  $suff_str = $delim = "";
495  foreach($this->getSuffixes() as $suffix)
496  {
497  $suff_str.= $delim.".".$suffix;
498  $delim = ", ";
499  }
500  $tpl->setCurrentBlock('allowed_image_suffixes');
501  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
502  $tpl->parseCurrentBlock();
503  }
504  $tpl->setCurrentBlock("image_heading");
505  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
506  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
507  $tpl->parseCurrentBlock();
508  }
509  }
510 
511  if ($this->getShowPoints())
512  {
513  $tpl->setCurrentBlock("points_heading");
514  $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
515  $tpl->parseCurrentBlock();
516  }
517 
518  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
519  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
520  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
521  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
522  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
523  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
524  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
525 
526  $a_tpl->setCurrentBlock("prop_generic");
527  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
528  $a_tpl->parseCurrentBlock();
529 
530  global $tpl;
531  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
533  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/singlechoicewizard.js");
534  }
535 }