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