ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
class.assFormulaQuestionVariable.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
12  private $variable;
13  private $range_min;
14  private $range_max;
15  private $unit;
16  private $value;
17  private $precision;
18  private $intprecision;
19  private $range_min_txt;
20  private $range_max_txt;
21 
22 
34  {
35  $this->variable = $variable;
36  $this->setRangeMin($range_min);
37  $this->setRangeMax($range_max);
38  $this->unit = $unit;
39  $this->value = null;
40  $this->precision = $precision;
41  $this->intprecision = $intprecision;
42  $this->setRangeMinTxt($range_min);
43  $this->setRangeMaxTxt($range_max);
44  }
45 
46  public function getRandomValue()
47  {
48 
49 // @todo check this
50  if ($this->getPrecision() == 0) {
51  if (!$this->isIntPrecisionValid(
52  $this->getIntprecision(),
53  $this->getRangeMin(),
54  $this->getRangeMax()
55  )) {
56  global $DIC;
57  $lng = $DIC['lng'];
58  ilUtil::sendFailure($lng->txt('err_divider_too_big'));
59  }
60  }
61 
62  include_once "./Services/Math/classes/class.ilMath.php";
63  $mul = ilMath::_pow(10, $this->getPrecision());
64  $r1 = round(ilMath::_mul($this->getRangeMin(), $mul));
65  $r2 = round(ilMath::_mul($this->getRangeMax(), $mul));
66  $calcval = $this->getRangeMin() - 1;
67  //test
68 
69  $roundedRangeMIN = round($this->getRangeMin(), $this->getPrecision());
70  $roundedRangeMAX = round($this->getRangeMax(), $this->getPrecision());
71  while ($calcval < $roundedRangeMIN || $calcval > $roundedRangeMAX) {
72 
73 
74 // while($calcval < $this->getRangeMin() || $calcval > $this->getRangeMax())
75  $rnd = mt_rand($r1, $r2);
76  $calcval = ilMath::_div($rnd, $mul, $this->getPrecision());
77  if (($this->getPrecision() == 0) && ($this->getIntprecision() != 0)) {
78  if ($this->getIntprecision() > 0) {
79  $modulo = $calcval % $this->getIntprecision();
80  if ($modulo != 0) {
81  if ($modulo < ilMath::_div($this->getIntprecision(), 2)) {
82  $calcval = ilMath::_sub($calcval, $modulo, $this->getPrecision());
83  } else {
84  $calcval = ilMath::_add($calcval, ilMath::_sub($this->getIntprecision(), $modulo, $this->getPrecision()), $this->getPrecision());
85  }
86  }
87  }
88  }
89  }
90  return $calcval;
91  }
92 
93  public function setRandomValue()
94  {
95  $this->setValue($this->getRandomValue());
96  }
97 
98  public function isIntPrecisionValid($int_precision, $min_range, $max_range)
99  {
100  $min_abs = abs($min_range);
101  $max_abs = abs($max_range);
102  $bigger_abs = $max_abs > $min_abs ? $max_abs : $min_abs;
103  if ($int_precision > $bigger_abs) {
104  return false;
105  }
106  return true;
107  }
108 
109  /************************************
110  * Getter and Setter
111  ************************************/
112 
113  public function setValue($value)
114  {
115  $this->value = $value;
116  }
117 
118  public function getValue()
119  {
120  return $this->value;
121  }
122 
123  public function getBaseValue()
124  {
125  if (!is_object($this->getUnit())) {
126  return $this->value;
127  } else {
128  include_once "./Services/Math/classes/class.ilMath.php";
129  return ilMath::_mul($this->value, $this->getUnit()->getFactor());
130  }
131  }
132 
133  public function setPrecision($precision)
134  {
135  $this->precision = $precision;
136  }
137 
138  public function getPrecision()
139  {
140  //@todo TEST
141 
142  return (int) $this->precision;
143  }
144 
145  public function setVariable($variable)
146  {
147  $this->variable = $variable;
148  }
149 
150  public function getVariable()
151  {
152  return $this->variable;
153  }
154 
155  public function setRangeMin($range_min)
156  {
157  include_once "./Services/Math/classes/class.EvalMath.php";
158  $math = new EvalMath();
159  $math->suppress_errors = true;
160  $result = $math->evaluate($range_min);
161 
162  $this->range_min = $result;
163  }
164 
165  public function getRangeMin()
166  {
167  return (double) $this->range_min;
168  }
169 
170  public function setRangeMax($range_max)
171  {
172  include_once "./Services/Math/classes/class.EvalMath.php";
173  $math = new EvalMath();
174  $math->suppress_errors = true;
175  $result = $math->evaluate($range_max);
176  $this->range_max = $result;
177  }
178 
179  public function getRangeMax()
180  {
181  return (double) $this->range_max;
182  }
183 
184  public function setUnit($unit)
185  {
186  $this->unit = $unit;
187  }
188 
189  public function getUnit()
190  {
191  return $this->unit;
192  }
193 
195  {
196  $this->intprecision = $intprecision;
197  }
198 
199  public function getIntprecision()
200  {
201  return $this->intprecision;
202  }
203 
205  {
206  $this->range_max_txt = $range_max_txt;
207  }
208 
209  public function getRangeMaxTxt()
210  {
211  return $this->range_max_txt;
212  }
213 
215  {
216  $this->range_min_txt = $range_min_txt;
217  }
218 
219  public function getRangeMinTxt()
220  {
221  return $this->range_min_txt;
222  }
223 }
$result
static _div($left_operand, $right_operand, $scale=50)
static _pow($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)
$lng
global $DIC
Definition: goto.php:24
isIntPrecisionValid($int_precision, $min_range, $max_range)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
__construct($variable, $range_min, $range_max, $unit=null, $precision=0, $intprecision=1)
assFormulaQuestionVariable constructor
static _mul($left_operand, $right_operand, $scale=50)
static _sub($left_operand, $right_operand, $scale=50)