67 public function __construct(
$result,
$range_min,
$range_max,
$tolerance,
$unit,
$formula,
$points,
$precision,
$rating_simple =
true,
$rating_sign = 33,
$rating_value = 34,
$rating_unit = 33,
$result_type = 0)
70 $this->main_tpl = $DIC->ui()->mainTemplate();
72 # $this->setRangeMin((is_numeric($range_min)) ? $range_min : NULL); 73 # $this->setRangeMax((is_numeric($range_max)) ? $range_max : NULL); 99 if (preg_match_all(
"/(\\\$r\\d+)/ims",
$formula, $matches)) {
100 foreach ($matches[1] as
$result) {
101 if (strcmp($result, $this->
getResult()) == 0) {
102 $this->main_tpl->setOnScreenMessage(
'failure',
$lng->txt(
"errRecursionInResult"));
109 $this->main_tpl->setOnScreenMessage(
'failure',
$lng->txt(
"errFormulaQuestion"));
120 $resultunits = array();
121 if ($question_id > 0) {
125 include_once
"./Services/Math/classes/class.ilMath.php";
126 include_once
"./Services/Math/classes/class.EvalMath.php";
128 if (preg_match_all(
"/(\\\$v\\d+)/ims",
$formula, $matches)) {
129 foreach ($matches[1] as $variable) {
130 $varObj = $variables[$variable];
131 if (!is_object($varObj)) {
134 $value = $varObj->getBaseValue();
135 $formula = preg_replace(
"/\\\$" . substr($variable, 1) .
"(?![0-9]+)/",
"(" . $value .
")" .
"\\1",
$formula);
139 $math->suppress_errors =
true;
143 if (is_object($this->
getUnit())) {
149 if ($use_precision ==
true) {
162 include_once
"./Services/Math/classes/class.EvalMath.php";
165 while (
$i < 1000 && !$inRange) {
167 if (preg_match_all(
"/(\\\$v\\d+)/ims",
$formula, $matches)) {
168 foreach ($matches[1] as $variable) {
169 $varObj = $variables[$variable];
170 if (!is_object($varObj)) {
173 $varObj->setRandomValue();
174 $formula = preg_replace(
"/\\\$" . substr($variable, 1) .
"(?![0-9]+)/",
"(" . $varObj->getBaseValue() .
")" .
"\\1",
$formula);
178 $math->suppress_errors =
true;
180 $inRange = (is_numeric(
$result)) ?
true :
false;
201 include_once
"./Services/Math/classes/class.EvalMath.php";
204 for (
$i = 0;
$i < 1000;
$i++) {
206 if (preg_match_all(
"/(\\\$v\\d+)/ims",
$formula, $matches)) {
207 foreach ($matches[1] as $variable) {
208 $varObj = $variables[$variable];
209 if (!is_object($varObj)) {
212 $varObj->setRandomValue();
213 $formula = preg_replace(
"/\\\$" . substr($variable, 1) .
"(?![0-9]+)/",
"(" . $varObj->getBaseValue() .
")" .
"\\1",
$formula);
217 $math->suppress_errors =
true;
226 include_once
"./Services/Math/classes/class.ilMath.php";
227 if (is_object($this->
getUnit())) {
245 if ($value === null || 0 == strlen($value)) {
248 $value = str_replace(
' ',
'', $value);
250 include_once
"./Services/Math/classes/class.EvalMath.php";
251 include_once
"./Services/Math/classes/class.ilMath.php";
254 $check_valid_chars =
true;
256 if (preg_match_all(
"/(\\\$v\\d+)/ims",
$formula, $matches)) {
257 foreach ($matches[1] as $variable) {
258 $varObj = $variables[$variable];
259 if (!is_object($varObj)) {
263 if ($varObj->getUnit() != null) {
265 if ($varObj->getUnit()->getBaseUnit() != -1) { #$this->
getUnit() != NULL)
266 $tmp_value = $varObj->getValue() * $varObj->getUnit()->getFactor();
268 $tmp_value = $varObj->getValue();
271 $tmp_value = $varObj->getValue();
274 $formula = preg_replace(
"/\\\$" . substr($variable, 1) .
"(?![0-9]+)/",
"(" . $tmp_value .
")" .
"\\1",
$formula);
279 $math->suppress_errors =
true;
281 $resultWithRespectedUnit =
$result;
283 if (is_object($this->
getUnit())) {
287 if ($this->
getUnit()->getBaseUnit() != -1) {
294 if (
$unit->getFactor() != 1 && strlen(trim(
$unit->getFactor())) != 1) {
300 $result = substr($result, 0, strlen($resultWithRespectedUnit));
303 $has_valid_chars = preg_match(
"/^-?([0-9]*)(,|\\.|\\/){0,1}([0-9]*)([eE][\\+|-]([0-9])+)?$/", $value, $matches);
304 if (!$has_valid_chars) {
305 $check_valid_chars =
false;
307 (isset($matches[2]) && $matches[2] ==
'/') &&
308 (isset($matches[4]) && strtolower($matches[4]) ==
"e") &&
309 (!isset($matches[1]) || !strlen($matches[1]) || !isset($matches[3]) || !strlen($matches[3]) || $matches[3] == 0)) {
310 $check_valid_chars =
false;
316 if (substr_count($value,
'.') == 1 || substr_count($value,
',') == 1) {
318 $frac_value = str_replace(
',',
'.', $exp_val);
320 $frac_value = $value;
323 if (substr_count($value,
'/') >= 1) {
324 $check_fraction =
false;
326 $check_fraction =
true;
332 $exp_val = explode(
'/', $value);
333 if (count($exp_val) == 1) {
336 if (ilMath::_equals($frac_value, $resultWithRespectedUnit, $this->
getPrecision())) {
337 $check_fraction =
true;
339 $check_fraction =
false;
351 $frac_value = str_replace(
',',
'.', $frac_value);
353 if (ilMath::_equals($frac_value, $resultWithRespectedUnit, $this->
getPrecision())) {
354 $check_fraction =
true;
358 if (!self::isCoprimeFraction($exp_val[0], $exp_val[1])) {
359 $check_fraction =
false;
364 if (substr_count($value,
'.') >= 1 || substr_count($value,
',') >= 1) {
365 $check_fraction =
false;
371 if (substr_count($value,
'.') == 1 || substr_count($value,
',') == 1) {
372 $frac_value = str_replace(
',',
'.', $value);
373 } elseif (substr_count($value,
'/') == 1) {
374 $exp_val = explode(
'/', $value);
385 $frac_value = $value;
388 $check_fraction =
true;
392 if (is_object(
$unit)) {
393 if (isset($frac_value)) {
398 $frac_value = ilMath::_round($frac_value, $this->
getPrecision());
399 $resultWithRespectedUnit = ilMath::_round($resultWithRespectedUnit, $this->
getPrecision());
402 if (isset($frac_value)) {
413 if (is_object($this->
getUnit())) {
414 if (is_object(
$unit)) {
420 return $checkvalue && $checkunit && $check_fraction && $check_valid_chars;
423 protected function isInTolerance($user_answer, $expected, $tolerated_percentage): bool
427 $lower_boundary =
ilMath::_sub($expected, $tolerance_abs);
428 $upper_boundary =
ilMath::_add($expected, $tolerance_abs);
430 return $lower_boundary <= $user_answer
431 && $user_answer <= $upper_boundary;
436 if ((($v1 >= 0) && ($v2 >= 0)) || (($v1 <= 0) && ($v2 <= 0))) {
446 $ilLog = $DIC[
'ilLog'];
455 include_once
"./Services/Math/classes/class.EvalMath.php";
456 include_once
"./Services/Math/classes/class.ilMath.php";
459 if (preg_match_all(
"/(\\\$v\\d+)/ims",
$formula, $matches)) {
460 foreach ($matches[1] as $variable) {
461 $varObj = $variables[$variable];
462 if (!is_object($varObj)) {
465 if ($varObj->getUnit() != null) {
467 if ($varObj->getUnit()->getBaseUnit() != -1) {
468 $tmp_value = $varObj->getValue() * $varObj->getUnit()->getFactor();
470 $tmp_value = $varObj->getValue();
473 $tmp_value = $varObj->getValue();
475 $formula = preg_replace(
"/\\\$" . substr($variable, 1) .
"(?![0-9]+)/",
"(" . $tmp_value .
")" .
"\\1",
$formula);
480 $math->suppress_errors =
true;
486 if ((substr_count($value,
'.') == 1) || (substr_count($value,
',') == 1)) {
488 $frac_value = str_replace(
',',
'.', $exp_val);
490 $frac_value = $value;
492 $check_fraction =
true;
495 $exp_val = explode(
'/', $value);
496 if (count($exp_val) == 1) {
499 $check_fraction =
true;
501 $check_fraction =
false;
506 $check_fraction =
true;
511 $exp_val = explode(
'/', $value);
512 if (count($exp_val) == 1) {
513 $check_fraction =
false;
516 if (self::isCoprimeFraction($exp_val[0], $exp_val[1])) {
517 $check_fraction =
true;
523 $check_fraction =
true;
528 if (is_object($this->
getUnit())) {
530 if ($this->
getUnit()->getBaseUnit() != -1) {
538 if (is_object(
$unit)) {
539 if (isset($frac_value)) {
551 if (is_object($this->
getUnit())) {
552 $base1 = $units[
$unit] ?? null;
553 if (is_object($base1)) {
554 $base1 = $units[$base1->getBaseUnit()];
556 $base2 = $units[$this->
getUnit()->getBaseUnit()];
557 if (is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId()) {
569 return array(
"points" => $this->
getPoints());
571 return array(
"points" => 0);
574 include_once
"./Services/Math/classes/class.EvalMath.php";
575 include_once
"./Services/Math/classes/class.ilMath.php";
578 if (preg_match_all(
"/(\\\$v\\d+)/ims",
$formula, $matches)) {
579 foreach ($matches[1] as $variable) {
580 $varObj = $variables[$variable];
581 $formula = preg_replace(
"/\\\$" . substr($variable, 1) .
"(?![0-9]+)/",
"(" . $varObj->getBaseValue() .
")" .
"\\1",
$formula);
585 $math->suppress_errors =
true;
587 if (is_object($this->
getUnit())) {
590 if (is_object(
$unit)) {
605 if (is_object($this->
getUnit())) {
606 $base1 = $units[
$unit];
607 if (is_object($base1)) {
608 $base1 = $units[$base1->getBaseUnit()];
610 $base2 = $units[$this->
getUnit()->getBaseUnit()];
611 if (is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId()) {
645 include_once
"./Services/Math/classes/class.EvalMath.php";
647 $math->suppress_errors =
true;
660 if (is_object($this->
getUnit())) {
661 include_once
"./Services/Math/classes/class.ilMath.php";
677 include_once
"./Services/Math/classes/class.EvalMath.php";
679 $math->suppress_errors =
true;
692 if (is_object($this->
getUnit())) {
693 include_once
"./Services/Math/classes/class.ilMath.php";
732 $this->points = (float) str_replace(
",",
".",
$points);
792 $this->result_type = $a_result_type;
823 $ilDB = $DIC[
'ilDB'];
828 FROM il_qpl_qst_fq_res 829 WHERE question_fi = %s 831 array(
'integer',
'text'),
832 array($a_qst_id, $a_result)
837 return $row[
'result_type'];
842 $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
844 return $gcd == 1 ? true :
false;
849 if (empty($decimal_value)) {
853 $to_string = (string) $decimal_value;
854 $is_negative = strpos($to_string,
'-') === 0;
856 $decimal_value = substr($decimal_value, 1);
862 $b = 1 / $decimal_value;
867 $h1 =
$a * $h1 + $h2;
870 $k1 =
$a * $k1 + $k2;
873 }
while ((abs($decimal_value - $h1 / $k1) > $decimal_value *
$tolerance) || ($k1 < 0 ||
$b < 0));
879 $checkResult = ($h1 / $k1);
883 $checkResult = ($h1 / $k1) * -1;
885 if ($to_string == $checkResult .
'' || $checkResult .
'' ==
$result) {
888 return array($to_string,
$result);
895 return self::getGreatestCommonDivisor(
$b,
$a %
$b);
905 $ilDB = $DIC[
'ilDB'];
909 SELECT * FROM il_qpl_qst_fq_res_unit 910 WHERE question_fi = %s 918 $this->available_units[$row[
'result']][] = $row[
'unit_fi'] ;
static _add($left_operand, $right_operand, int $scale=50)
static _div($left_operand, $right_operand, int $scale=50)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _sub($left_operand, $right_operand, int $scale=50)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
static _mul($left_operand, $right_operand, int $scale=50)