ILIAS  Release_4_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilMultipleChoiceWizardInputGUI.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 {
40  function setValue($a_value)
41  {
42  $this->values = array();
43  if (is_array($a_value))
44  {
45  if (is_array($a_value['answer']))
46  {
47  foreach ($a_value['answer'] as $index => $value)
48  {
49  include_once "./Modules/TestQuestionPool/classes/class.assAnswerMultipleResponseImage.php";
50  $answer = new ASS_AnswerMultipleResponseImage($value, $a_value['points'][$index], $index, $a_value['points_unchecked'][$index], $a_value['imagename'][$index]);
51  array_push($this->values, $answer);
52  }
53  }
54  }
55  }
56 
62  function checkInput()
63  {
64  global $lng;
65 
66  include_once "./Services/AdvancedEditing/classes/class.ilObjAdvancedEditing.php";
67  if (is_array($_POST[$this->getPostVar()])) $_POST[$this->getPostVar()] = ilUtil::stripSlashesRecursive($_POST[$this->getPostVar()], false, ilObjAdvancedEditing::_getUsedHTMLTagsAsString("assessment"));
68  $foundvalues = $_POST[$this->getPostVar()];
69  if (is_array($foundvalues))
70  {
71  // check answers
72  if (is_array($foundvalues['answer']))
73  {
74  foreach ($foundvalues['answer'] as $aidx => $answervalue)
75  {
76  if (((strlen($answervalue)) == 0) && (strlen($foundvalues['imagename'][$aidx]) == 0))
77  {
78  $this->setAlert($lng->txt("msg_input_is_required"));
79  return FALSE;
80  }
81  }
82  }
83  // check points
84  $max = 0;
85  if (is_array($foundvalues['points']))
86  {
87  foreach ($foundvalues['points'] as $points)
88  {
89  if ($points > $max) $max = $points;
90  if (((strlen($points)) == 0) || (!is_numeric($points)))
91  {
92  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
93  return FALSE;
94  }
95  }
96  foreach ($foundvalues['points_unchecked'] as $points)
97  {
98  if ($points > $max) $max = $points;
99  if (((strlen($points)) == 0) || (!is_numeric($points)))
100  {
101  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
102  return FALSE;
103  }
104  }
105  }
106  if ($max == 0)
107  {
108  $this->setAlert($lng->txt("enter_enough_positive_points"));
109  return false;
110  }
111 
112  if (is_array($_FILES) && count($_FILES) && $this->getSingleline())
113  {
114  if (!$this->hideImages)
115  {
116  if (is_array($_FILES[$this->getPostVar()]['error']['image']))
117  {
118  foreach ($_FILES[$this->getPostVar()]['error']['image'] as $index => $error)
119  {
120  // error handling
121  if ($error > 0)
122  {
123  switch ($error)
124  {
125  case UPLOAD_ERR_INI_SIZE:
126  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
127  return false;
128  break;
129 
130  case UPLOAD_ERR_FORM_SIZE:
131  $this->setAlert($lng->txt("form_msg_file_size_exceeds"));
132  return false;
133  break;
134 
135  case UPLOAD_ERR_PARTIAL:
136  $this->setAlert($lng->txt("form_msg_file_partially_uploaded"));
137  return false;
138  break;
139 
140  case UPLOAD_ERR_NO_FILE:
141  if ($this->getRequired())
142  {
143  if ((!strlen($foundvalues['imagename'][$index])) && (!strlen($foundvalues['answer'][$index])))
144  {
145  $this->setAlert($lng->txt("form_msg_file_no_upload"));
146  return false;
147  }
148  }
149  break;
150 
151  case UPLOAD_ERR_NO_TMP_DIR:
152  $this->setAlert($lng->txt("form_msg_file_missing_tmp_dir"));
153  return false;
154  break;
155 
156  case UPLOAD_ERR_CANT_WRITE:
157  $this->setAlert($lng->txt("form_msg_file_cannot_write_to_disk"));
158  return false;
159  break;
160 
161  case UPLOAD_ERR_EXTENSION:
162  $this->setAlert($lng->txt("form_msg_file_upload_stopped_ext"));
163  return false;
164  break;
165  }
166  }
167  }
168  }
169  else
170  {
171  if ($this->getRequired())
172  {
173  $this->setAlert($lng->txt("form_msg_file_no_upload"));
174  return false;
175  }
176  }
177 
178  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
179  {
180  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
181  {
182  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
183  $filename_arr = pathinfo($filename);
184  $suffix = $filename_arr["extension"];
185  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
186  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
187  // check suffixes
188  if (strlen($tmpname) && is_array($this->getSuffixes()))
189  {
190  if (!in_array(strtolower($suffix), $this->getSuffixes()))
191  {
192  $this->setAlert($lng->txt("form_msg_file_wrong_file_type"));
193  return false;
194  }
195  }
196  }
197  }
198 
199  if (is_array($_FILES[$this->getPostVar()]['tmp_name']['image']))
200  {
201  foreach ($_FILES[$this->getPostVar()]['tmp_name']['image'] as $index => $tmpname)
202  {
203  $filename = $_FILES[$this->getPostVar()]['name']['image'][$index];
204  $filename_arr = pathinfo($filename);
205  $suffix = $filename_arr["extension"];
206  $mimetype = $_FILES[$this->getPostVar()]['type']['image'][$index];
207  $size_bytes = $_FILES[$this->getPostVar()]['size']['image'][$index];
208  // virus handling
209  if (strlen($tmpname))
210  {
211  $vir = ilUtil::virusHandling($tmpname, $filename);
212  if ($vir[0] == false)
213  {
214  $this->setAlert($lng->txt("form_msg_file_virus_found")."<br />".$vir[1]);
215  return false;
216  }
217  }
218  }
219  }
220  }
221  }
222  }
223  else
224  {
225  $this->setAlert($lng->txt("msg_input_is_required"));
226  return FALSE;
227  }
228 
229  return $this->checkSubItemsInput();
230  }
231 
237  function insert(&$a_tpl)
238  {
239  global $lng;
240 
241  $tpl = new ilTemplate("tpl.prop_multiplechoicewizardinput.html", true, true, "Modules/TestQuestionPool");
242  $i = 0;
243  foreach ($this->values as $value)
244  {
245  if ($this->getSingleline())
246  {
247  if (!$this->hideImages)
248  {
249  if (strlen($value->getImage()))
250  {
251  $imagename = $this->qstObject->getImagePathWeb() . $value->getImage();
252  if (($this->getSingleline()) && ($this->qstObject->getThumbSize()))
253  {
254  if (@file_exists($this->qstObject->getImagePath() . $this->qstObject->getThumbPrefix() . $value->getImage()))
255  {
256  $imagename = $this->qstObject->getImagePathWeb() . $this->qstObject->getThumbPrefix() . $value->getImage();
257  }
258  }
259  $tpl->setCurrentBlock('image');
260  $tpl->setVariable('SRC_IMAGE', $imagename);
261  $tpl->setVariable('IMAGE_NAME', $value->getImage());
262  $tpl->setVariable('ALT_IMAGE', ilUtil::prepareFormOutput($value->getAnswertext()));
263  $tpl->setVariable("TXT_DELETE_EXISTING", $lng->txt("delete_existing_file"));
264  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
265  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
266  $tpl->parseCurrentBlock();
267  }
268  $tpl->setCurrentBlock('addimage');
269  $tpl->setVariable("IMAGE_ID", $this->getPostVar() . "[image][$i]");
270  $tpl->setVariable("IMAGE_SUBMIT", $lng->txt("upload"));
271  $tpl->setVariable("IMAGE_ROW_NUMBER", $i);
272  $tpl->setVariable("IMAGE_POST_VAR", $this->getPostVar());
273  $tpl->parseCurrentBlock();
274  }
275 
276  if (is_object($value))
277  {
278  $tpl->setCurrentBlock("prop_text_propval");
279  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
280  $tpl->parseCurrentBlock();
281  $tpl->setCurrentBlock("prop_points_propval");
282  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPointsChecked()));
283  $tpl->parseCurrentBlock();
284  $tpl->setCurrentBlock("prop_points_unchecked_propval");
285  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPointsUnchecked()));
286  $tpl->parseCurrentBlock();
287  }
288  $tpl->setCurrentBlock('singleline');
289  $tpl->setVariable("SIZE", $this->getSize());
290  $tpl->setVariable("SINGLELINE_ID", $this->getPostVar() . "[answer][$i]");
291  $tpl->setVariable("SINGLELINE_ROW_NUMBER", $i);
292  $tpl->setVariable("SINGLELINE_POST_VAR", $this->getPostVar());
293  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
294  if ($this->getDisabled())
295  {
296  $tpl->setVariable("DISABLED_SINGLELINE", " disabled=\"disabled\"");
297  }
298  $tpl->parseCurrentBlock();
299  }
300  else if (!$this->getSingleline())
301  {
302  if (is_object($value))
303  {
304  $tpl->setCurrentBlock("prop_points_propval");
305  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPoints()));
306  $tpl->parseCurrentBlock();
307  $tpl->setCurrentBlock("prop_points_unchecked_propval");
308  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getPointsUnchecked()));
309  $tpl->parseCurrentBlock();
310  }
311  $tpl->setCurrentBlock('multiline');
312  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($value->getAnswertext()));
313  $tpl->setVariable("MULTILINE_ID", $this->getPostVar() . "[answer][$i]");
314  $tpl->setVariable("MULTILINE_ROW_NUMBER", $i);
315  $tpl->setVariable("MULTILINE_POST_VAR", $this->getPostVar());
316  if ($this->getDisabled())
317  {
318  $tpl->setVariable("DISABLED_MULTILINE", " disabled=\"disabled\"");
319  }
320  $tpl->parseCurrentBlock();
321  }
322  if ($this->getAllowMove())
323  {
324  $tpl->setCurrentBlock("move");
325  $tpl->setVariable("CMD_UP", "cmd[up" . $this->getFieldId() . "][$i]");
326  $tpl->setVariable("CMD_DOWN", "cmd[down" . $this->getFieldId() . "][$i]");
327  $tpl->setVariable("ID", $this->getPostVar() . "[$i]");
328  $tpl->setVariable("UP_BUTTON", ilUtil::getImagePath('a_up.gif'));
329  $tpl->setVariable("DOWN_BUTTON", ilUtil::getImagePath('a_down.gif'));
330  $tpl->parseCurrentBlock();
331  }
332  $tpl->setCurrentBlock("row");
333  $class = ($i % 2 == 0) ? "even" : "odd";
334  if ($i == 0) $class .= " first";
335  if ($i == count($this->values)-1) $class .= " last";
336  $tpl->setVariable("ROW_CLASS", $class);
337  $tpl->setVariable("POST_VAR", $this->getPostVar());
338  $tpl->setVariable("ROW_NUMBER", $i);
339  $tpl->setVariable("ID", $this->getPostVar() . "[answer][$i]");
340  $tpl->setVariable("POINTS_ID", $this->getPostVar() . "[points][$i]");
341  $tpl->setVariable("POINTS_UNCHECKED_ID", $this->getPostVar() . "[points_unchecked][$i]");
342  $tpl->setVariable("CMD_ADD", "cmd[add" . $this->getFieldId() . "][$i]");
343  $tpl->setVariable("CMD_REMOVE", "cmd[remove" . $this->getFieldId() . "][$i]");
344  if ($this->getDisabled())
345  {
346  $tpl->setVariable("DISABLED_POINTS", " disabled=\"disabled\"");
347  }
348  $tpl->setVariable("ADD_BUTTON", ilUtil::getImagePath('edit_add.png'));
349  $tpl->setVariable("REMOVE_BUTTON", ilUtil::getImagePath('edit_remove.png'));
350  $tpl->parseCurrentBlock();
351  $i++;
352  }
353 
354  if ($this->getSingleline())
355  {
356  if (!$this->hideImages)
357  {
358  if (is_array($this->getSuffixes()))
359  {
360  $suff_str = $delim = "";
361  foreach($this->getSuffixes() as $suffix)
362  {
363  $suff_str.= $delim.".".$suffix;
364  $delim = ", ";
365  }
366  $tpl->setCurrentBlock('allowed_image_suffixes');
367  $tpl->setVariable("TXT_ALLOWED_SUFFIXES", $lng->txt("file_allowed_suffixes")." ".$suff_str);
368  $tpl->parseCurrentBlock();
369  }
370 
371  $tpl->setCurrentBlock("image_heading");
372  $tpl->setVariable("ANSWER_IMAGE", $lng->txt('answer_image'));
373  $tpl->setVariable("TXT_MAX_SIZE", ilUtil::getFileSizeInfo());
374  $tpl->parseCurrentBlock();
375  }
376  }
377 
378  $tpl->setVariable("ELEMENT_ID", $this->getPostVar());
379  $tpl->setVariable("TEXT_YES", $lng->txt('yes'));
380  $tpl->setVariable("TEXT_NO", $lng->txt('no'));
381  $tpl->setVariable("DELETE_IMAGE_HEADER", $lng->txt('delete_image_header'));
382  $tpl->setVariable("DELETE_IMAGE_QUESTION", $lng->txt('delete_image_question'));
383  $tpl->setVariable("ANSWER_TEXT", $lng->txt('answer_text'));
384  $tpl->setVariable("POINTS_TEXT", $lng->txt('points'));
385  $tpl->setVariable("COMMANDS_TEXT", $lng->txt('actions'));
386  $tpl->setVariable("POINTS_CHECKED_TEXT", $lng->txt('checkbox_checked'));
387  $tpl->setVariable("POINTS_UNCHECKED_TEXT", $lng->txt('checkbox_unchecked'));
388 
389  $a_tpl->setCurrentBlock("prop_generic");
390  $a_tpl->setVariable("PROP_GENERIC", $tpl->get());
391  $a_tpl->parseCurrentBlock();
392 
393  global $tpl;
394  include_once "./Services/YUI/classes/class.ilYuiUtil.php";
396  $tpl->addJavascript("./Modules/TestQuestionPool/templates/default/multiplechoicewizard.js");
397  }
398 }