ILIAS  release_8 Revision v8.24
class.assFormulaQuestionVariable.php
Go to the documentation of this file.
1<?php
25{
26 private $variable;
27 private $range_min;
28 private $range_max;
29 private $unit;
30 private $value;
31 private $precision;
35
36
48 {
49 $this->variable = $variable;
50 $this->setRangeMin($range_min);
51 $this->setRangeMax($range_max);
52 $this->unit = $unit;
53 $this->value = null;
54 $this->precision = $precision;
55 $this->intprecision = $intprecision;
58 }
59
60 public function getRandomValue()
61 {
62 if ($this->getPrecision() == 0) {
63 if (!$this->isIntPrecisionValid(
64 $this->getIntprecision(),
65 $this->getRangeMin(),
66 $this->getRangeMax()
67 )) {
68 global $DIC;
69 $lng = $DIC['lng'];
70 $DIC->ui()->mainTemplate()->setOnScreenMessage(
71 "failure",
72 $lng->txt('err_divider_too_big')
73 );
74 }
75 }
76
77 include_once "./Services/Math/classes/class.ilMath.php";
78 $mul = ilMath::_pow(10, $this->getPrecision());
79 $r1 = round(ilMath::_mul($this->getRangeMin(), $mul));
80 $r2 = round(ilMath::_mul($this->getRangeMax(), $mul));
81 $calcval = $this->getRangeMin() - 1;
82 //test
83
84 $roundedRangeMIN = round($this->getRangeMin(), $this->getPrecision());
85 $roundedRangeMAX = round($this->getRangeMax(), $this->getPrecision());
86 while ($calcval < $roundedRangeMIN || $calcval > $roundedRangeMAX) {
87
88
89// while($calcval < $this->getRangeMin() || $calcval > $this->getRangeMax())
90 $rnd = mt_rand($r1, $r2);
91 $calcval = ilMath::_div($rnd, $mul, $this->getPrecision());
92 if (($this->getPrecision() == 0) && ($this->getIntprecision() != 0)) {
93 if ($this->getIntprecision() > 0) {
94 $modulo = $calcval % $this->getIntprecision();
95 if ($modulo != 0) {
96 if ($modulo < ilMath::_div($this->getIntprecision(), 2)) {
97 $calcval = ilMath::_sub($calcval, $modulo, $this->getPrecision());
98 } else {
99 $calcval = ilMath::_add($calcval, ilMath::_sub($this->getIntprecision(), $modulo, $this->getPrecision()), $this->getPrecision());
100 }
101 }
102 }
103 }
104 }
105 return $calcval;
106 }
107
108 public function setRandomValue(): void
109 {
110 $this->setValue($this->getRandomValue());
111 }
112
113 public function isIntPrecisionValid($int_precision, $min_range, $max_range)
114 {
115 $min_abs = abs($min_range);
116 $max_abs = abs($max_range);
117 $bigger_abs = $max_abs > $min_abs ? $max_abs : $min_abs;
118 if ($int_precision > $bigger_abs) {
119 return false;
120 }
121 return true;
122 }
123
124 /************************************
125 * Getter and Setter
126 ************************************/
127
128 public function setValue($value): void
129 {
130 $this->value = $value;
131 }
132
133 public function getValue()
134 {
135 return $this->value;
136 }
137
138 public function getBaseValue()
139 {
140 if (!is_object($this->getUnit())) {
141 return $this->value;
142 } else {
143 include_once "./Services/Math/classes/class.ilMath.php";
144 return ilMath::_mul($this->value, $this->getUnit()->getFactor());
145 }
146 }
147
148 public function setPrecision($precision): void
149 {
150 $this->precision = $precision;
151 }
152
153 public function getPrecision(): int
154 {
155 //@todo TEST
156
157 return $this->precision;
158 }
159
160 public function setVariable($variable): void
161 {
162 $this->variable = $variable;
163 }
164
165 public function getVariable(): string
166 {
167 return $this->variable;
168 }
169
170 public function setRangeMin($range_min): void
171 {
172 include_once "./Services/Math/classes/class.EvalMath.php";
173 $math = new EvalMath();
174 $math->suppress_errors = true;
175 $result = $math->evaluate($range_min);
176
177 $this->range_min = $result;
178 }
179
180 public function getRangeMin(): float
181 {
182 return (float) $this->range_min;
183 }
184
185 public function setRangeMax($range_max): void
186 {
187 include_once "./Services/Math/classes/class.EvalMath.php";
188 $math = new EvalMath();
189 $math->suppress_errors = true;
190 $result = $math->evaluate($range_max);
191 $this->range_max = $result;
192 }
193
194 public function getRangeMax(): float
195 {
196 return (float) $this->range_max;
197 }
198
199 public function setUnit($unit): void
200 {
201 $this->unit = $unit;
202 }
203
204 public function getUnit(): ?object
205 {
206 return $this->unit;
207 }
208
209 public function setIntprecision($intprecision): void
210 {
211 $this->intprecision = $intprecision;
212 }
213
214 public function getIntprecision(): int
215 {
216 return $this->intprecision;
217 }
218
219 public function setRangeMaxTxt($range_max_txt): void
220 {
221 $this->range_max_txt = $range_max_txt;
222 }
223
224 public function getRangeMaxTxt()
225 {
227 }
228
229 public function setRangeMinTxt($range_min_txt): void
230 {
231 $this->range_min_txt = $range_min_txt;
232 }
233
234 public function getRangeMinTxt()
235 {
237 }
238}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct($variable, $range_min, $range_max, $unit=null, $precision=0, $intprecision=1)
assFormulaQuestionVariable constructor
isIntPrecisionValid($int_precision, $min_range, $max_range)
static _sub($left_operand, $right_operand, int $scale=50)
static _pow($left_operand, $right_operand, int $scale=50)
static _add($left_operand, $right_operand, int $scale=50)
static _div($left_operand, $right_operand, int $scale=50)
static _mul($left_operand, $right_operand, int $scale=50)
global $DIC
Definition: feed.php:28
$lng