ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.assFormulaQuestionVariable.php
Go to the documentation of this file.
1<?php
18declare(strict_types=1);
19
27{
28 private $value = null;
29 private float $range_min;
30 private float $range_max;
31
32 public function __construct(
33 private string $variable,
34 private string $range_min_txt,
35 private string $range_max_txt,
36 private ?assFormulaQuestionUnit $unit = null,
37 private int $precision = 0,
38 private int $intprecision = 1
39 ) {
40 $this->setRangeMin($range_min_txt);
41 $this->setRangeMax($range_max_txt);
42 }
43
44 public function getRandomValue()
45 {
46 if ($this->getPrecision() === 0
47 && !$this->isIntPrecisionValid(
48 $this->getIntprecision(),
49 $this->getRangeMin(),
50 $this->getRangeMax()
51 )
52 ) {
53 global $DIC;
54 $lng = $DIC['lng'];
55 $DIC->ui()->mainTemplate()->setOnScreenMessage(
56 "failure",
57 $lng->txt('err_divider_too_big')
58 );
59 }
60
61 $mul = ilMath::_pow(10, $this->getPrecision());
62 $r1 = round((float) ilMath::_mul($this->getRangeMin(), $mul));
63 $r2 = round((float) ilMath::_mul($this->getRangeMax(), $mul));
64 $calcval = $this->getRangeMin() - 1;
65 //test
66
67 $roundedRangeMIN = round($this->getRangeMin(), $this->getPrecision());
68 $roundedRangeMAX = round($this->getRangeMax(), $this->getPrecision());
69 while ($calcval < $roundedRangeMIN || $calcval > $roundedRangeMAX) {
70 // while($calcval < $this->getRangeMin() || $calcval > $this->getRangeMax())
71 $rnd = mt_rand((int) $r1, (int) $r2);
72 $calcval = ilMath::_div($rnd, $mul, $this->getPrecision());
73 if (($this->getPrecision() == 0) && ($this->getIntprecision() != 0)) {
74 if ($this->getIntprecision() > 0) {
75 $modulo = $calcval % $this->getIntprecision();
76 if ($modulo != 0) {
77 if ($modulo < ilMath::_div($this->getIntprecision(), 2)) {
78 $calcval = ilMath::_sub($calcval, $modulo, $this->getPrecision());
79 } else {
80 $calcval = ilMath::_add($calcval, ilMath::_sub($this->getIntprecision(), $modulo, $this->getPrecision()), $this->getPrecision());
81 }
82 }
83 }
84 }
85 }
86 return $calcval;
87 }
88
89 public function setRandomValue(): void
90 {
91 $this->setValue($this->getRandomValue());
92 }
93
94 public function isIntPrecisionValid(?int $int_precision, float $min_range, float $max_range)
95 {
96 if ($int_precision === null) {
97 return false;
98 }
99 $min_abs = abs($min_range);
100 $max_abs = abs($max_range);
101 $bigger_abs = $max_abs > $min_abs ? $max_abs : $min_abs;
102 if ($int_precision > $bigger_abs) {
103 return false;
104 }
105 return true;
106 }
107
108 /************************************
109 * Getter and Setter
110 ************************************/
111
112 public function setValue($value): void
113 {
114 $this->value = $value;
115 }
116
117 public function getValue()
118 {
119 return $this->value;
120 }
121
122 public function getBaseValue()
123 {
124 if (!is_object($this->getUnit())) {
125 return $this->value;
126 } else {
127 return ilMath::_mul($this->value, $this->getUnit()->getFactor());
128 }
129 }
130
131 public function setPrecision(int $precision): void
132 {
133 $this->precision = $precision;
134 }
135
136 public function getPrecision(): int
137 {
138 return $this->precision;
139 }
140
141 public function setVariable($variable): void
142 {
143 $this->variable = $variable;
144 }
145
146 public function getVariable(): string
147 {
148 return $this->variable;
149 }
150
151 public function setRangeMin(string $range_min): void
152 {
153 $math = new EvalMath();
154 $math->suppress_errors = true;
155 $this->range_min = (float) $math->evaluate($range_min);
156 }
157
158 public function getRangeMin(): float
159 {
160 return $this->range_min;
161 }
162
163 public function setRangeMax(string $range_max): void
164 {
165 $math = new EvalMath();
166 $math->suppress_errors = true;
167 $this->range_max = (float) $math->evaluate($range_max);
168 }
169
170 public function getRangeMax(): float
171 {
172 return $this->range_max;
173 }
174
175 public function setUnit(?assFormulaQuestionUnit $unit): void
176 {
177 $this->unit = $unit;
178 }
179
180 public function getUnit(): ?assFormulaQuestionUnit
181 {
182 return $this->unit;
183 }
184
185 public function setIntprecision($intprecision): void
186 {
187 $this->intprecision = $intprecision;
188 }
189
190 public function getIntprecision(): int
191 {
192 return $this->intprecision;
193 }
194
195 public function setRangeMaxTxt(string $range_max_txt): void
196 {
197 $this->range_max_txt = $range_max_txt;
198 }
199
200 public function getRangeMaxTxt(): string
201 {
202 return $this->range_max_txt;
203 }
204
205 public function setRangeMinTxt(string $range_min_txt): void
206 {
207 $this->range_min_txt = $range_min_txt;
208 }
209
210 public function getRangeMinTxt(): string
211 {
212 return $this->range_min_txt;
213 }
214}
setUnit(?assFormulaQuestionUnit $unit)
isIntPrecisionValid(?int $int_precision, float $min_range, float $max_range)
__construct(private string $variable, private string $range_min_txt, private string $range_max_txt, private ?assFormulaQuestionUnit $unit=null, private int $precision=0, private int $intprecision=1)
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 $lng
Definition: privfeed.php:31
global $DIC
Definition: shib_login.php:26