ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
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 if ($this->getIntprecision() > $this->getRangeMax()){
51 global $DIC;
52 $lng = $DIC['lng'];
53 ilUtil::sendFailure($lng->txt('err_divider_too_big'));
54 }
55
56 include_once "./Services/Math/classes/class.ilMath.php";
57 $mul = ilMath::_pow(10, $this->getPrecision());
58 $r1 = round(ilMath::_mul($this->getRangeMin(), $mul));
59 $r2 = round(ilMath::_mul($this->getRangeMax(), $mul));
60 $calcval = $this->getRangeMin() - 1;
61 //test
62
63 $roundedRangeMIN = round($this->getRangeMin(), $this->getPrecision());
64 $roundedRangeMAX = round($this->getRangeMax(), $this->getPrecision());
65 while ($calcval < $roundedRangeMIN || $calcval > $roundedRangeMAX) {
66
67
68// while($calcval < $this->getRangeMin() || $calcval > $this->getRangeMax())
69 $rnd = mt_rand($r1, $r2);
70 $calcval = ilMath::_div($rnd, $mul, $this->getPrecision());
71 if (($this->getPrecision() == 0) && ($this->getIntprecision() != 0)) {
72 if ($this->getIntprecision() > 0) {
73 $modulo = $calcval % $this->getIntprecision();
74 if ($modulo != 0) {
75 if ($modulo < ilMath::_div($this->getIntprecision(), 2)) {
76 $calcval = ilMath::_sub($calcval, $modulo, $this->getPrecision());
77 } else {
78 $calcval = ilMath::_add($calcval, ilMath::_sub($this->getIntprecision(), $modulo, $this->getPrecision()), $this->getPrecision());
79 }
80 }
81 }
82 }
83 }
84 return $calcval;
85 }
86
87 public function setRandomValue()
88 {
89 $this->setValue($this->getRandomValue());
90 }
91
92 /************************************
93 * Getter and Setter
94 ************************************/
95
96 public function setValue($value)
97 {
98 $this->value = $value;
99 }
100
101 public function getValue()
102 {
103 return $this->value;
104 }
105
106 public function getBaseValue()
107 {
108 if (!is_object($this->getUnit())) {
109 return $this->value;
110 } else {
111 include_once "./Services/Math/classes/class.ilMath.php";
112 return ilMath::_mul($this->value, $this->getUnit()->getFactor());
113 }
114 }
115
116 public function setPrecision($precision)
117 {
118 $this->precision = $precision;
119 }
120
121 public function getPrecision()
122 {
123 //@todo TEST
124
125 return (int) $this->precision;
126 }
127
128 public function setVariable($variable)
129 {
130 $this->variable = $variable;
131 }
132
133 public function getVariable()
134 {
135 return $this->variable;
136 }
137
138 public function setRangeMin($range_min)
139 {
140 include_once "./Services/Math/classes/class.EvalMath.php";
141 $math = new EvalMath();
142 $math->suppress_errors = true;
143 $result = $math->evaluate($range_min);
144
145 $this->range_min = $result;
146 }
147
148 public function getRangeMin()
149 {
150 return (double) $this->range_min;
151 }
152
153 public function setRangeMax($range_max)
154 {
155 include_once "./Services/Math/classes/class.EvalMath.php";
156 $math = new EvalMath();
157 $math->suppress_errors = true;
158 $result = $math->evaluate($range_max);
159 $this->range_max = $result;
160 }
161
162 public function getRangeMax()
163 {
164 return (double) $this->range_max;
165 }
166
167 public function setUnit($unit)
168 {
169 $this->unit = $unit;
170 }
171
172 public function getUnit()
173 {
174 return $this->unit;
175 }
176
178 {
179 $this->intprecision = $intprecision;
180 }
181
182 public function getIntprecision()
183 {
184 return $this->intprecision;
185 }
186
188 {
189 $this->range_max_txt = $range_max_txt;
190 }
191
192 public function getRangeMaxTxt()
193 {
195 }
196
198 {
199 $this->range_min_txt = $range_min_txt;
200 }
201
202 public function getRangeMinTxt()
203 {
205 }
206}
$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)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$lng
$DIC
Definition: xapitoken.php:46