ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilImageWizardInputGUI.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 $qstObject = null;
36  protected $suffixes = array();
37 
44  function __construct($a_title = "", $a_postvar = "")
45  {
46  parent::__construct($a_title, $a_postvar);
47  $this->setSuffixes(array("jpg", "jpeg", "png", "gif"));
48  $this->setSize('25');
49  $this->validationRegexp = "";
50  }
51 
57  function setValue($a_value)
58  {
59  $this->values = array();
60  if (is_array($a_value))
61  {
62  if (is_array($a_value['count']))
63  {
64  foreach ($a_value['count'] as $index => $value)
65  {
66  array_push($this->values, $a_value['imagename'][$index]);
67  }
68  }
69  }
70  }
71 
77  function setSuffixes($a_suffixes)
78  {
79  $this->suffixes = $a_suffixes;
80  }
81 
87  function getSuffixes()
88  {
89  return $this->suffixes;
90  }
91 
97  function setValues($a_values)
98  {
99  $this->values = $a_values;
100  }
101 
107  function getValues()
108  {
109  return $this->values;
110  }
111 
117  function setQuestionObject($a_value)
118  {
119  $this->qstObject =& $a_value;
120  }
121 
127  function getQuestionObject()
128  {
129  return $this->qstObject;
130  }
131 
137  function setAllowMove($a_allow_move)
138  {
139  $this->allowMove = $a_allow_move;
140  }
141 
147  function getAllowMove()
148  {
149  return $this->allowMove;
150  }
151 
157  function checkInput()
158  {
159  global $lng;
160 
161  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()]);
162  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
163  {
164  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
165  {
166  // error handling
167  if ($error > 0)
168  {
169  switch ($error)
170  {
171  case UPLOAD_ERR_INI_SIZE:
172  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
173  return false;
174  break;
175 
176  case UPLOAD_ERR_FORM_SIZE:
177  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
178  return false;
179  break;
180 
181  case UPLOAD_ERR_PARTIAL:
182  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
183  return false;
184  break;
185 
186  case UPLOAD_ERR_NO_FILE:
187  if ($this->getRequired())
188  {
189  if (!strlen($_POST[$this->getPostVar()]['imagename'][$index]))
190  {
191  $this->setAlert($lng->txt("form_msg_file_no_upload"));
192  return false;
193  }
194  }
195  break;
196 
197  case UPLOAD_ERR_NO_TMP_DIR:
198  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
199  return false;
200  break;
201 
202  case UPLOAD_ERR_CANT_WRITE:
203  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
204  return false;
205  break;
206 
207  case UPLOAD_ERR_EXTENSION:
208  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
209  return false;
210  break;
211  }
212  }
213  }
214  }
215  else
216  {
217  if ($this->getRequired())
218  {
219  $this->setAlert($lng->txt("form_msg_file_no_upload"));
220  return false;
221  }
222  }
223 
224  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
225  {
226  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
227  {
228  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
229  $filename_arr = pathinfo($filename);
230  $suffix = $filename_arr["extension"];
231  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
232  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
233  // check suffixes
234  if (strlen($tmpname) && is_array($this->getSuffixes()))
235  {
236  if (!in_array(strtolower($suffix), $this->getSuffixes()))
237  {
238  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
239  return false;
240  }
241  }
242  }
243  }
244 
245  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
246  {
247  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
248  {
249  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
250  $filename_arr = pathinfo($filename);
251  $suffix = $filename_arr["extension"];
252  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
253  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
254  // virus handling
255  if (strlen($tmpname))
256  {
257  $vir = ilUtil::virusHandling($tmpname, $filename);
258  if ($vir[0] == false)
259  {
260  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
261  return false;
262  }
263  }
264  }
265  }
266 
267  return $this->checkSubItemsInput();
268  }
269 
275  function insert(&$a_tpl)
276  {
277  global $lng;
278 
279  $tpl = new ilTemplate("tpl.prop_imagewizardinput.html", true, true, "Modules/TestQuestionPool");
280  $i = 0;
281  foreach ($this->values as $value)
282  {
283  if (strlen($value))
284  {
285  $imagename = $this->qstObject->getImagePathWeb() . $value;
286  if ($this->qstObject->getThumbSize())
287  {
288  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value))
289  {
290  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value;
291  }
292  }
293  $tpl->setCurrentBlock('image');
294  $tpl->setVariable('SRC_IMAGE', $imagename);
295  $tpl->setVariable('IMAGE_NAME', $value);
296  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value));
297  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
298  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
299  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
300  $tpl->parseCurrentBlock();
301  }
302  $tpl->setCurrentBlock('addimage');
303  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
304  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
305  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
306  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
307  $tpl->parseCurrentBlock();
308 
309  if ($this->getAllowMove())
310  {
311  $tpl->setCurrentBlock("move");
312  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
313  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
314  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
315  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.gif'));
316  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.gif'));
317  $tpl->parseCurrentBlock();
318  }
319  $tpl->setCurrentBlock("row");
320  $class = ($i % 2 == 0) ? "even" : "odd";
321  if ($i == 0) $class .= " first";
322  if ($i == count($this->values)-1) $class .= " last";
323  $tpl->setVariable("ROW_CLASS", $class);
324  $tpl->setVariable("POST_VAR", $this->getPostVar());
325  $tpl->setVariable("ROW_NUMBER", $i);
326  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
327  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
328  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
329  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
330  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
331  $tpl->parseCurrentBlock();
332  $i++;
333  }
334 
335  if (is_array($this->getSuffixes()))
336  {
337  $suff_str = $delim = "";
338  foreach($this->getSuffixes() as $suffix)
339  {
340  $suff_str.= $delim.".".$suffix;
341  $delim = ", ";
342  }
343  $tpl->setCurrentBlock('allowed_image_suffixes');
344  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
345  $tpl->parseCurrentBlock();
346  }
347  $tpl->setCurrentBlock("image_heading");
348  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
349  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
350  $tpl->parseCurrentBlock();
351 
352  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
353  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
354  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
355  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
356  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
357  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
358  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
359 
360  $a_tpl->setCurrentBlock("prop_generic");
361  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
362  $a_tpl->parseCurrentBlock();
363 
364  global $tpl;
365  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
367  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/imagewizard.js");
368  }
369 }