ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilNumberInputGUI.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2008 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 $value;
34  protected $maxlength = 200;
35  protected $size = 40;
36  protected $suffix;
37  protected $minvalue = false;
38  protected $minvalueShouldBeGreater = false;
39  protected $maxvalue = false;
40  protected $maxvalueShouldBeLess = false;
41 
48  function __construct($a_title = "", $a_postvar = "")
49  {
50  parent::__construct($a_title, $a_postvar);
51  }
52 
58  function setSuffix($a_value)
59  {
60  $this->suffix = $a_value;
61  }
62 
68  function getSuffix()
69  {
70  return $this->suffix;
71  }
72 
78  function setValue($a_value)
79  {
80  $this->value = str_replace(',', '.', $a_value);
81  }
82 
88  function getValue()
89  {
90  return $this->value;
91  }
92 
98  function setMaxLength($a_maxlength)
99  {
100  $this->maxlength = $a_maxlength;
101  }
102 
108  function getMaxLength()
109  {
110  return $this->maxlength;
111  }
112 
118  function setMinvalueShouldBeGreater($a_bool)
119  {
120  $this->minvalueShouldBeGreater = $a_bool;
121  }
122 
129  {
131  }
132 
138  function setMaxvalueShouldBeLess($a_bool)
139  {
140  $this->maxvalueShouldBeLess = $a_bool;
141  }
142 
149  {
151  }
152 
158  function setSize($a_size)
159  {
160  $this->size = $a_size;
161  }
162 
168  function setValueByArray($a_values)
169  {
170  $this->setValue($a_values[$this->getPostVar()]);
171  }
172 
178  function getSize()
179  {
180  return $this->size;
181  }
182 
188  function setMinValue($a_minvalue)
189  {
190  $this->minvalue = $a_minvalue;
191  }
192 
198  function getMinValue()
199  {
200  return $this->minvalue;
201  }
202 
208  function setMaxValue($a_maxvalue)
209  {
210  $this->maxvalue = $a_maxvalue;
211  }
212 
218  function getMaxValue()
219  {
220  return $this->maxvalue;
221  }
222 
228  function setDecimals($a_decimals)
229  {
230  $this->decimals = $a_decimals;
231  }
232 
238  function getDecimals()
239  {
240  return $this->decimals;
241  }
242 
248  function checkInput()
249  {
250  global $lng;
251 
252  $_POST[$this->getPostVar()] = ilUtil::stripSlashes($_POST[$this->getPostVar()]);
253  if ($this->getRequired() && trim($_POST[$this->getPostVar()]) == "")
254  {
255  $this->setAlert($lng->txt("msg_input_is_required"));
256 
257  return false;
258  }
259 
260  if (trim($_POST[$this->getPostVar()]) != "" &&
261  ! is_numeric(str_replace(',', '.', $_POST[$this->getPostVar()])))
262  {
263  $this->setAlert($lng->txt("form_msg_numeric_value_required"));
264 
265  return false;
266  }
267 
268  if ($this->minvalueShouldBeGreater())
269  {
270  if (trim($_POST[$this->getPostVar()]) != "" &&
271  $this->getMinValue() !== false &&
272  $_POST[$this->getPostVar()] <= $this->getMinValue())
273  {
274  $this->setAlert($lng->txt("form_msg_value_too_low"));
275 
276  return false;
277  }
278  }
279  else
280  {
281  if (trim($_POST[$this->getPostVar()]) != "" &&
282  $this->getMinValue() !== false &&
283  $_POST[$this->getPostVar()] < $this->getMinValue())
284  {
285  $this->setAlert($lng->txt("form_msg_value_too_low"));
286 
287  return false;
288  }
289  }
290 
291  if ($this->maxvalueShouldBeLess())
292  {
293  if (trim($_POST[$this->getPostVar()]) != "" &&
294  $this->getMaxValue() !== false &&
295  $_POST[$this->getPostVar()] >= $this->getMaxValue())
296  {
297  $this->setAlert($lng->txt("form_msg_value_too_high"));
298 
299  return false;
300  }
301  }
302  else
303  {
304  if (trim($_POST[$this->getPostVar()]) != "" &&
305  $this->getMaxValue() !== false &&
306  $_POST[$this->getPostVar()] > $this->getMaxValue())
307  {
308  $this->setAlert($lng->txt("form_msg_value_too_high"));
309 
310  return false;
311  }
312  }
313 
314  return $this->checkSubItemsInput();
315  }
316 
322  function insert(&$a_tpl)
323  {
324  $html = $this->render();
325 
326  $a_tpl->setCurrentBlock("prop_generic");
327  $a_tpl->setVariable("PROP_GENERIC", $html);
328  $a_tpl->parseCurrentBlock();
329  }
330 
334  function render()
335  {
336  global $lng;
337 
338  $tpl = new ilTemplate("tpl.prop_number.html", true, true, "Services/Form");
339 
340  if (strlen($this->getValue()))
341  {
342  $tpl->setCurrentBlock("prop_number_propval");
343  $tpl->setVariable("PROPERTY_VALUE", ilUtil::prepareFormOutput($this->getValue()));
344  $tpl->parseCurrentBlock();
345  }
346  $tpl->setCurrentBlock("prop_number");
347 
348  $tpl->setVariable("POST_VAR", $this->getPostVar());
349  $tpl->setVariable("ID", $this->getFieldId());
350  $tpl->setVariable("SIZE", $this->getSize());
351  $tpl->setVariable("MAXLENGTH", $this->getMaxLength());
352  if (strlen($this->getSuffix())) $tpl->setVariable("INPUT_SUFFIX", $this->getSuffix());
353  if ($this->getDisabled())
354  {
355  $tpl->setVariable("DISABLED",
356  " disabled=\"disabled\"");
357  }
358 
359  // constraints
360  if ($this->getDecimals() > 0)
361  {
362  $constraints = $lng->txt("form_format").": ###.".str_repeat("#", $this->getDecimals());
363  $delim = ", ";
364  }
365  if ($this->getMinValue() !== false)
366  {
367  $constraints.= $delim.$lng->txt("form_min_value").": ".(($this->minvalueShouldBeGreater()) ? "&gt; " : "").$this->getMinValue();
368  $delim = ", ";
369  }
370  if ($this->getMaxValue() !== false)
371  {
372  $constraints.= $delim.$lng->txt("form_max_value").": ".(($this->maxvalueShouldBeLess()) ? "&lt; " : "").$this->getMaxValue();
373  $delim = ", ";
374  }
375  if ($constraints != "")
376  {
377  $tpl->setVariable("TXT_NUMBER_CONSTRAINTS", $constraints);
378  }
379 
380  $tpl->parseCurrentBlock();
381 
382  return $tpl->get();
383  }
384 
391  {
393  if($value != "")
394  {
395  return (int)$value;
396  }
397  }
398 }
399 ?>