ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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;
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;
44 }
45
46 public function getRandomValue()
47 {
48
49// @todo check this
50
51 include_once "./Services/Math/classes/class.ilMath.php";
52 $mul = ilMath::_pow(10, $this->getPrecision());
53 $r1 = round(ilMath::_mul($this->getRangeMin(), $mul));
54 $r2 = round(ilMath::_mul($this->getRangeMax(), $mul));
55 $calcval = $this->getRangeMin() - 1;
56 //test
57
58 $roundedRangeMIN = round($this->getRangeMin(), $this->getPrecision());
59 $roundedRangeMAX = round($this->getRangeMax(), $this->getPrecision());
60 while ($calcval < $roundedRangeMIN || $calcval > $roundedRangeMAX) {
61
62
63// while($calcval < $this->getRangeMin() || $calcval > $this->getRangeMax())
64 $rnd = mt_rand($r1, $r2);
65 $calcval = ilMath::_div($rnd, $mul, $this->getPrecision());
66 if (($this->getPrecision() == 0) && ($this->getIntprecision() != 0)) {
67 if ($this->getIntprecision() > 0) {
68 $modulo = $calcval % $this->getIntprecision();
69 if ($modulo != 0) {
70 if ($modulo < ilMath::_div($this->getIntprecision(), 2)) {
71 $calcval = ilMath::_sub($calcval, $modulo, $this->getPrecision());
72 } else {
73 $calcval = ilMath::_add($calcval, ilMath::_sub($this->getIntprecision(), $modulo, $this->getPrecision()), $this->getPrecision());
74 }
75 }
76 }
77 }
78 }
79 return $calcval;
80 }
81
82 public function setRandomValue()
83 {
84 $this->setValue($this->getRandomValue());
85 }
86
87 /************************************
88 * Getter and Setter
89 ************************************/
90
91 public function setValue($value)
92 {
93 $this->value = $value;
94 }
95
96 public function getValue()
97 {
98 return $this->value;
99 }
100
101 public function getBaseValue()
102 {
103 if (!is_object($this->getUnit())) {
104 return $this->value;
105 } else {
106 include_once "./Services/Math/classes/class.ilMath.php";
107 return ilMath::_mul($this->value, $this->getUnit()->getFactor());
108 }
109 }
110
111 public function setPrecision($precision)
112 {
113 $this->precision = $precision;
114 }
115
116 public function getPrecision()
117 {
118 //@todo TEST
119
120 return (int) $this->precision;
121 }
122
123 public function setVariable($variable)
124 {
125 $this->variable = $variable;
126 }
127
128 public function getVariable()
129 {
130 return $this->variable;
131 }
132
133 public function setRangeMin($range_min)
134 {
135 include_once "./Services/Math/classes/class.EvalMath.php";
136 $math = new EvalMath();
137 $math->suppress_errors = true;
138 $result = $math->evaluate($range_min);
139
140 $this->range_min = $result;
141 }
142
143 public function getRangeMin()
144 {
145 return (double) $this->range_min;
146 }
147
148 public function setRangeMax($range_max)
149 {
150 include_once "./Services/Math/classes/class.EvalMath.php";
151 $math = new EvalMath();
152 $math->suppress_errors = true;
153 $result = $math->evaluate($range_max);
154 $this->range_max = $result;
155 }
156
157 public function getRangeMax()
158 {
159 return (double) $this->range_max;
160 }
161
162 public function setUnit($unit)
163 {
164 $this->unit = $unit;
165 }
166
167 public function getUnit()
168 {
169 return $this->unit;
170 }
171
173 {
174 $this->intprecision = $intprecision;
175 }
176
177 public function getIntprecision()
178 {
179 return $this->intprecision;
180 }
181
183 {
184 $this->range_max_txt = $range_max_txt;
185 }
186
187 public function getRangeMaxTxt()
188 {
190 }
191
193 {
194 $this->range_min_txt = $range_min_txt;
195 }
196
197 public function getRangeMinTxt()
198 {
200 }
201}
$result
An exception for terminatinating execution or to throw for unit testing.
__construct($variable, $range_min, $range_max, $unit=null, $precision=0, $intprecision=1)
assFormulaQuestionVariable constructor
static _mul($left_operand, $right_operand, $scale=50)
static _div($left_operand, $right_operand, $scale=50)
static _pow($left_operand, $right_operand, $scale=50)
static _sub($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)