ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMatchingWizardInputGUI.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 
24 include_once "./Modules/TestQuestionPool/classes/class.ilSingleChoiceWizardInputGUI.php";
25 
34 {
35  protected $text_name = '';
36  protected $image_name = '';
37  protected $values = array();
38  protected $allowMove = false;
39  protected $qstObject = null;
40  protected $suffixes = array();
41  protected $hideImages = false;
42 
49  function __construct($a_title = "", $a_postvar = "")
50  {
51  global $lng;
52 
53  parent::__construct($a_title, $a_postvar);
54  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
55  $this->setSize('50');
56  $this->text_name = $lng->txt('answer_text');
57  $this->image_name = $lng->txt('answer_image');
58  }
59 
65  function setSuffixes($a_suffixes)
66  {
67  $this->suffixes = $a_suffixes;
68  }
69 
75  function getSuffixes()
76  {
77  return $this->suffixes;
78  }
79 
85  function setHideImages($a_hide)
86  {
87  $this->hideImages = $a_hide;
88  }
89 
95  function setValues($a_values)
96  {
97  $this->values = $a_values;
98  }
99 
105  function getValues()
106  {
107  return $this->values;
108  }
109 
110  function setTextName($a_value)
111  {
112  $this->text_name = $a_value;
113  }
114 
115  function setImageName($a_value)
116  {
117  $this->image_name = $a_value;
118  }
119 
125  function setQuestionObject($a_value)
126  {
127  $this->qstObject =& $a_value;
128  }
129 
135  function getQuestionObject()
136  {
137  return $this->qstObject;
138  }
139 
145  function setAllowMove($a_allow_move)
146  {
147  $this->allowMove = $a_allow_move;
148  }
149 
155  function getAllowMove()
156  {
157  return $this->allowMove;
158  }
159 
165  function setValue($a_value)
166  {
167  $this->values = array();
168  if (is_array($a_value))
169  {
170  if (is_array($a_value['answer']))
171  {
172  foreach ($a_value['answer'] as $index => $value)
173  {
174  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMatchingTerm.php";
175  $answer = new assAnswerMatchingTerm($value, $a_value['imagename'][$index], $a_value['identifier'][$index]);
176  array_push($this->values, $answer);
177  }
178  }
179  }
180  }
181 
187  function checkInput()
188  {
189  global $lng;
190  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
191  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()], true, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
192  $foundvalues = $_POST[$this->getPostVar()];
193  if (is_array($foundvalues))
194  {
195  // check answers
196  if (is_array($foundvalues['answer']))
197  {
198  foreach ($foundvalues['answer'] as $aidx => $answervalue)
199  {
200  if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0))
201  {
202  $this->setAlert($lng->txt("msg_input_is_required"));
203  return FALSE;
204  }
205  }
206  }
207 
208  if (is_array($_FILES) && (!$this->hideImages))
209  {
210  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
211  {
212  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
213  {
214  // error handling
215  if ($error > 0)
216  {
217  switch ($error)
218  {
219  case UPLOAD_ERR_INI_SIZE:
220  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
221  return false;
222  break;
223 
224  case UPLOAD_ERR_FORM_SIZE:
225  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
226  return false;
227  break;
228 
229  case UPLOAD_ERR_PARTIAL:
230  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
231  return false;
232  break;
233 
234  case UPLOAD_ERR_NO_FILE:
235  if ($this->getRequired())
236  {
237  if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index])))
238  {
239  $this->setAlert($lng->txt("form_msg_file_no_upload"));
240  return false;
241  }
242  }
243  break;
244 
245  case UPLOAD_ERR_NO_TMP_DIR:
246  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
247  return false;
248  break;
249 
250  case UPLOAD_ERR_CANT_WRITE:
251  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
252  return false;
253  break;
254 
255  case UPLOAD_ERR_EXTENSION:
256  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
257  return false;
258  break;
259  }
260  }
261  }
262  }
263  else
264  {
265  if ($this->getRequired())
266  {
267  $this->setAlert($lng->txt("form_msg_file_no_upload"));
268  return false;
269  }
270  }
271 
272  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
273  {
274  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
275  {
276  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
277  $filename_arr = pathinfo($filename);
278  $suffix = $filename_arr["extension"];
279  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
280  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
281  // check suffixes
282  if (strlen($tmpname) && is_array($this->getSuffixes()))
283  {
284  if (!in_array(strtolower($suffix), $this->getSuffixes()))
285  {
286  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
287  return false;
288  }
289  }
290  }
291  }
292 
293  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
294  {
295  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
296  {
297  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
298  $filename_arr = pathinfo($filename);
299  $suffix = $filename_arr["extension"];
300  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
301  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
302  // virus handling
303  if (strlen($tmpname))
304  {
305  $vir = ilUtil::virusHandling($tmpname, $filename);
306  if ($vir[0] == false)
307  {
308  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
309  return false;
310  }
311  }
312  }
313  }
314  }
315  }
316  else
317  {
318  $this->setAlert($lng->txt("msg_input_is_required"));
319  return FALSE;
320  }
321 
322  return $this->checkSubItemsInput();
323  }
324 
330  function insert(&$a_tpl)
331  {
332  global $lng;
333 
334  $tpl = new ilTemplate("tpl.prop_matchingwizardinput.html", true, true, "Modules/TestQuestionPool");
335  $i = 0;
336  foreach ($this->values as $value)
337  {
338  if (!$this->hideImages)
339  {
340  if (strlen($value->picture))
341  {
342  $imagename = $this->qstObject->getImagePathWeb() . $value->picture;
343  if ($this->qstObject->getThumbSize())
344  {
345  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->picture))
346  {
347  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->picture;
348  }
349  }
350  $tpl->setCurrentBlock('image');
351  $tpl->setVariable('SRC_IMAGE', $imagename);
352  $tpl->setVariable('IMAGE_NAME', $value->picture);
353  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->text));
354  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
355  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
356  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
357  $tpl->parseCurrentBlock();
358  }
359  $tpl->setCurrentBlock('addimage');
360  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
361  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
362  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
363  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
364  $tpl->parseCurrentBlock();
365  }
366 
367  if (is_object($value))
368  {
369  $tpl->setCurrentBlock("prop_text_propval");
370  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->text));
371  $tpl->parseCurrentBlock();
372  }
373  $tpl->setCurrentBlock('singleline');
374  $tpl->setVariable("SIZE", $this->getSize());
375  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
376  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
377  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
378  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
379  if ($this->getDisabled())
380  {
381  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
382  }
383  $tpl->parseCurrentBlock();
384  if ($this->getAllowMove())
385  {
386  $tpl->setCurrentBlock("move");
387  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
388  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
389  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
390  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.gif'));
391  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.gif'));
392  $tpl->parseCurrentBlock();
393  }
394  $tpl->setCurrentBlock("row");
395  $class = ($i % 2 == 0) ? "even" : "odd";
396  if ($i == 0) $class .= " first";
397  if ($i == count($this->values)-1) $class .= " last";
398  $tpl->setVariable("ROW_CLASS", $class);
399  $tpl->setVariable("POST_VAR", $this->getPostVar());
400  $tpl->setVariable("ROW_NUMBER", $i+1);
401  $tpl->setVariable("ROW_IDENTIFIER", $value->identifier);
402  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
403  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
404  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
405  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
406  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
407  $tpl->parseCurrentBlock();
408  $i++;
409  }
410 
411  if (!$this->hideImages)
412  {
413  if (is_array($this->getSuffixes()))
414  {
415  $suff_str = $delim = "";
416  foreach($this->getSuffixes() as $suffix)
417  {
418  $suff_str.= $delim.".".$suffix;
419  $delim = ", ";
420  }
421  $tpl->setCurrentBlock('allowed_image_suffixes');
422  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
423  $tpl->parseCurrentBlock();
424  }
425  $tpl->setCurrentBlock("image_heading");
426  $tpl->setVariable("ANSWER_IMAGE", $this->image_name);
427  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
428  $tpl->parseCurrentBlock();
429  }
430 
431  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
432  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
433  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
434  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
435  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
436  $tpl->setVariable("ANSWER_TEXT", $this->text_name);
437  $tpl->setVariable("NUMBER_TEXT", $lng->txt('row'));
438  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
439 
440  $a_tpl->setCurrentBlock("prop_generic");
441  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
442  $a_tpl->parseCurrentBlock();
443  /*
444  global $tpl;
445  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
446  ilYuiUtil::initDomEvent();
447  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/matchingwizard.js");*/
448  }
449 }