ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
assFormulaQuestionResult Class Reference

Formula Question Result. More...

+ Collaboration diagram for assFormulaQuestionResult:

Public Member Functions

 __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)
 assFormulaQuestionResult constructor More...
 
 substituteFormula ($variables, $results)
 
 calculateFormula ($variables, $results, $question_id=0, $use_precision=true)
 
 findValidRandomVariables ($variables, $results)
 
 suggestRange ($variables, $results)
 
 isCorrect ($variables, $results, $value, $unit=null)
 
 getReachedPoints ($variables, $results, $value, $unit, $units)
 
 getResultInfo ($variables, $results, $value, $unit, $units)
 
 setResult ($result)
 
 getResult ()
 
 setRangeMin ($range_min)
 
 getRangeMin ()
 
 getRangeMinBase ()
 
 setRangeMax ($range_max)
 
 getRangeMax ()
 
 getRangeMaxBase ()
 
 setTolerance ($tolerance)
 
 getTolerance ()
 
 setUnit ($unit)
 
 getUnit ()
 
 setFormula ($formula)
 
 getFormula ()
 
 setPoints ($points)
 
 getPoints ()
 
 setRatingSimple ($rating_simple)
 
 getRatingSimple ()
 
 setRatingSign ($rating_sign)
 
 getRatingSign ()
 
 setRatingValue ($rating_value)
 
 getRatingValue ()
 
 setRatingUnit ($rating_unit)
 
 getRatingUnit ()
 
 setPrecision ($precision)
 
 getPrecision ()
 
 setResultType ($a_result_type)
 
 getResultType ()
 
 setRangeMaxTxt ($range_max_txt)
 
 getRangeMaxTxt ()
 
 setRangeMinTxt ($range_min_txt)
 
 getRangeMinTxt ()
 
 getAvailableResultUnits ($question_id)
 

Static Public Member Functions

static getResultTypeByQstId ($a_qst_id, $a_result)
 
static isCoprimeFraction ($numerator, $denominator)
 
static convertDecimalToCoprimeFraction ($decimal_value, $tolerance=1.e-9)
 
static getGreatestCommonDivisor ($a, $b)
 

Data Fields

const RESULT_NO_SELECTION = 0
 
const RESULT_DEC = 1
 
const RESULT_FRAC = 2
 
const RESULT_CO_FRAC = 3
 

Protected Member Functions

 isInTolerance ($v1, $v2, $p)
 
 checkSign ($v1, $v2)
 

Private Attributes

 $result
 
 $range_min
 
 $range_max
 
 $tolerance
 
 $unit
 
 $formula
 
 $rating_simple
 
 $rating_sign
 
 $rating_value
 
 $rating_unit
 
 $points
 
 $precision
 
 $result_type
 
 $range_min_txt
 
 $range_max_txt
 
 $available_units = array()
 

Detailed Description

Formula Question Result.

Author
Helmut Schottmüller helmu.nosp@m.t.sc.nosp@m.hottm.nosp@m.uell.nosp@m.er@ma.nosp@m.c.co.nosp@m.m
Version
Id
class.assFormulaQuestionResult.php 944 2009-11-09 16:11:30Z hschottm

Definition at line 10 of file class.assFormulaQuestionResult.php.

Constructor & Destructor Documentation

◆ __construct()

assFormulaQuestionResult::__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 
)

assFormulaQuestionResult constructor

Parameters
string$resultResult name
double$range_minRange minimum
double$range_maxRange maximum
double$toleranceTolerance of the result in percent
object$unitUnit
string$formulaThe formula to calculate the result
double$pointsThe maximum available points for the result
integer$precisionNumber of decimal places of the value
boolean$rating_simpleUse simple rating (100% if right, 0 % if wrong)
double$rating_signPercentage of rating for the correct sign
double$rating_valuePercentage of rating for the correct value
double$rating_unitPercentage of rating for the correct unit public

Definition at line 51 of file class.assFormulaQuestionResult.php.

References $formula, $points, $precision, $range_max, $range_min, $rating_sign, $rating_simple, $rating_unit, $rating_value, $result, $result_type, $tolerance, $unit, setRangeMax(), setRangeMaxTxt(), setRangeMin(), and setRangeMinTxt().

52  {
53  $this->result = $result;
54  # $this->setRangeMin((is_numeric($range_min)) ? $range_min : NULL);
55  # $this->setRangeMax((is_numeric($range_max)) ? $range_max : NULL);
56  $this->setRangeMin($range_min);
57  $this->setRangeMax($range_max);
58 
59 
60  $this->tolerance = $tolerance;
61  $this->unit = $unit;
62  $this->formula = $formula;
63  $this->points = $points;
64  $this->precision = $precision;
65  $this->rating_simple = $rating_simple;
66  $this->rating_sign = $rating_sign;
67  $this->rating_value = $rating_value;
68  $this->rating_unit = $rating_unit;
69  $this->result_type = $result_type;
70  $this->setRangeMinTxt($range_min);
71  $this->setRangeMaxTxt($range_max);
72  }
+ Here is the call graph for this function:

Member Function Documentation

◆ calculateFormula()

assFormulaQuestionResult::calculateFormula (   $variables,
  $results,
  $question_id = 0,
  $use_precision = true 
)

Definition at line 100 of file class.assFormulaQuestionResult.php.

References $formula, $res, $result, $results, ilMath\_div(), getAvailableResultUnits(), getPrecision(), getResultType(), getUnit(), and substituteFormula().

101  {
102  $resultunits = array();
103  if ($question_id > 0) {
104  $resultunits = $this->getAvailableResultUnits($question_id);
105  }
106 
107  include_once "./Services/Math/classes/class.ilMath.php";
108  include_once "./Services/Math/classes/class.EvalMath.php";
109  $formula = $this->substituteFormula($variables, $results);
110  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
111  foreach ($matches[1] as $variable) {
112  $varObj = $variables[$variable];
113  if (!is_object($varObj)) {
114  continue;
115  }
116  $value = $varObj->getBaseValue();
117  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $value . ")" . "\\1", $formula);
118  }
119  }
120  $math = new EvalMath();
121  $math->suppress_errors = true;
122 
123  $formula = str_replace(",", ".", $formula);
124  $result = $math->evaluate($formula);
125  if (is_object($this->getUnit())) {
126  $result = ilMath::_div($result, $this->getUnit()->getFactor(), 100);
127  }
128 
129  // @todo DON'T USE ilMath::_mul() ... bcmul() returns wrong result !!!!
130 
131  if ($use_precision == true) {
132  $res = $result * 1;
133  if (is_numeric($this->getPrecision())) {
134  if ($this->getResultType() == self::RESULT_DEC || $this->getResultType() == self::RESULT_NO_SELECTION) {
135  $result = ilMath::_round($res, $this->getPrecision());
136  }
137  }
138  }
139  return $result;
140  }
static _div($left_operand, $right_operand, $scale=50)
foreach($_POST as $key=> $value) $res
$results
Definition: svg-scanner.php:47
+ Here is the call graph for this function:

◆ checkSign()

assFormulaQuestionResult::checkSign (   $v1,
  $v2 
)
protected

Definition at line 418 of file class.assFormulaQuestionResult.php.

Referenced by getReachedPoints(), and getResultInfo().

419  {
420  if ((($v1 >= 0) && ($v2 >= 0)) || (($v1 <= 0) && ($v2 <= 0))) {
421  return true;
422  } else {
423  return false;
424  }
425  }
+ Here is the caller graph for this function:

◆ convertDecimalToCoprimeFraction()

static assFormulaQuestionResult::convertDecimalToCoprimeFraction (   $decimal_value,
  $tolerance = 1.e-9 
)
static

Definition at line 830 of file class.assFormulaQuestionResult.php.

References $result, and $tolerance.

Referenced by assFormulaQuestion\getBestSolution(), and assFormulaQuestion\substituteVariables().

831  {
832  $to_string = (string) $decimal_value;
833  $is_negative = strpos($to_string, '-') === 0;
834  if ($is_negative) {
835  $decimal_value = substr($decimal_value, 1);
836  }
837  $h1 = 1;
838  $h2 = 0;
839  $k1 = 0;
840  $k2 = 1;
841  $b = 1 / $decimal_value;
842  do {
843  $b = 1 / $b;
844  $a = floor($b);
845  $aux = $h1;
846  $h1 = $a * $h1 + $h2;
847  $h2 = $aux;
848  $aux = $k1;
849  $k1 = $a * $k1 + $k2;
850  $k2 = $aux;
851  $b = $b - $a;
852  } while ((abs($decimal_value - $h1 / $k1) > $decimal_value * $tolerance) || ($k1 < 0 || $b < 0));
853  if ($k1 == 1) {
854  $result = $h1;
855  $checkResult = $h1;
856  } else {
857  $result = "$h1/$k1";
858  $checkResult = ($h1 / $k1);
859  }
860  if ($is_negative) {
861  $result = '-' . $result;
862  $checkResult = ($h1 / $k1) * -1;
863  }
864  if ($to_string == $checkResult . '' || $checkResult . '' == $result) {
865  return $result;
866  } else {
867  return array($to_string,$result);
868  }
869  }
+ Here is the caller graph for this function:

◆ findValidRandomVariables()

assFormulaQuestionResult::findValidRandomVariables (   $variables,
  $results 
)

Definition at line 142 of file class.assFormulaQuestionResult.php.

References $formula, $i, $result, $results, getRangeMax(), getRangeMaxBase(), getRangeMin(), getRangeMinBase(), and substituteFormula().

143  {
144  include_once "./Services/Math/classes/class.EvalMath.php";
145  $i = 0;
146  $inRange = false;
147  while ($i < 1000 && !$inRange) {
148  $formula = $this->substituteFormula($variables, $results);
149  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
150  foreach ($matches[1] as $variable) {
151  $varObj = $variables[$variable];
152  if (!is_object($varObj)) {
153  continue;
154  }
155  $varObj->setRandomValue();
156  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $varObj->getBaseValue() . ")" . "\\1", $formula);
157  }
158  }
159  $math = new EvalMath();
160  $math->suppress_errors = true;
161  $result = $math->evaluate($formula);
162  $inRange = (is_numeric($result)) ? true : false;
163  if ($inRange) {
164  if (is_numeric($this->getRangeMin())) {
165  if ($result < $this->getRangeMinBase()) {
166  $inRange = false;
167  }
168  }
169  if (is_numeric($this->getRangeMax())) {
170  if ($result > $this->getRangeMaxBase()) {
171  $inRange = false;
172  }
173  }
174  }
175  $i++;
176  }
177  }
$results
Definition: svg-scanner.php:47
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

◆ getAvailableResultUnits()

assFormulaQuestionResult::getAvailableResultUnits (   $question_id)

Definition at line 881 of file class.assFormulaQuestionResult.php.

References $available_units, $DIC, $ilDB, $res, and $row.

Referenced by calculateFormula().

882  {
883  global $DIC;
884  $ilDB = $DIC['ilDB'];
885 
886  $res = $ilDB->queryF(
887  '
888  SELECT * FROM il_qpl_qst_fq_res_unit
889  WHERE question_fi = %s
890  ORDER BY result',
891  array('integer'),
892  array($question_id)
893  );
894 
895 
896  while ($row = $ilDB->fetchAssoc($res)) {
897  $this->available_units[$row['result']][] = $row['unit_fi'] ;
898  }
899 
900  return $this->available_units;
901  }
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
$row
global $ilDB
+ Here is the caller graph for this function:

◆ getFormula()

assFormulaQuestionResult::getFormula ( )

Definition at line 708 of file class.assFormulaQuestionResult.php.

References $formula.

Referenced by substituteFormula().

+ Here is the caller graph for this function:

◆ getGreatestCommonDivisor()

static assFormulaQuestionResult::getGreatestCommonDivisor (   $a,
  $b 
)
static

Definition at line 871 of file class.assFormulaQuestionResult.php.

872  {
873  if ($b > 0) {
874  return self::getGreatestCommonDivisor($b, $a % $b);
875  } else {
876  return $a;
877  }
878  }

◆ getPoints()

assFormulaQuestionResult::getPoints ( )

Definition at line 718 of file class.assFormulaQuestionResult.php.

References $points.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getPrecision()

assFormulaQuestionResult::getPrecision ( )

Definition at line 768 of file class.assFormulaQuestionResult.php.

References $precision.

Referenced by calculateFormula(), getReachedPoints(), isCorrect(), isInTolerance(), and suggestRange().

769  {
770  return (int) $this->precision;
771  }
+ Here is the caller graph for this function:

◆ getRangeMax()

assFormulaQuestionResult::getRangeMax ( )

Definition at line 667 of file class.assFormulaQuestionResult.php.

References $range_max.

Referenced by findValidRandomVariables(), and getRangeMaxBase().

+ Here is the caller graph for this function:

◆ getRangeMaxBase()

assFormulaQuestionResult::getRangeMaxBase ( )

Definition at line 672 of file class.assFormulaQuestionResult.php.

References ilMath\_mul(), getRangeMax(), and getUnit().

Referenced by findValidRandomVariables().

673  {
674  if (is_numeric($this->getRangeMax())) {
675  if (is_object($this->getUnit())) {
676  include_once "./Services/Math/classes/class.ilMath.php";
677  return ilMath::_mul($this->getRangeMax(), $this->getUnit()->getFactor(), 100);
678  }
679  }
680  return $this->getRangeMax();
681  }
static _mul($left_operand, $right_operand, $scale=50)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getRangeMaxTxt()

assFormulaQuestionResult::getRangeMaxTxt ( )

Definition at line 788 of file class.assFormulaQuestionResult.php.

References $range_max_txt.

◆ getRangeMin()

assFormulaQuestionResult::getRangeMin ( )

Definition at line 635 of file class.assFormulaQuestionResult.php.

References $range_min.

Referenced by findValidRandomVariables(), and getRangeMinBase().

+ Here is the caller graph for this function:

◆ getRangeMinBase()

assFormulaQuestionResult::getRangeMinBase ( )

Definition at line 640 of file class.assFormulaQuestionResult.php.

References ilMath\_mul(), getRangeMin(), and getUnit().

Referenced by findValidRandomVariables().

641  {
642  if (is_numeric($this->getRangeMin())) {
643  if (is_object($this->getUnit())) {
644  include_once "./Services/Math/classes/class.ilMath.php";
645  return ilMath::_mul($this->getRangeMin(), $this->getUnit()->getFactor(), 100);
646  }
647  }
648  return $this->getRangeMin();
649  }
static _mul($left_operand, $right_operand, $scale=50)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getRangeMinTxt()

assFormulaQuestionResult::getRangeMinTxt ( )

Definition at line 798 of file class.assFormulaQuestionResult.php.

References $range_min_txt.

◆ getRatingSign()

assFormulaQuestionResult::getRatingSign ( )

Definition at line 738 of file class.assFormulaQuestionResult.php.

References $rating_sign.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getRatingSimple()

assFormulaQuestionResult::getRatingSimple ( )

Definition at line 728 of file class.assFormulaQuestionResult.php.

References $rating_simple.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getRatingUnit()

assFormulaQuestionResult::getRatingUnit ( )

Definition at line 758 of file class.assFormulaQuestionResult.php.

References $rating_unit.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getRatingValue()

assFormulaQuestionResult::getRatingValue ( )

Definition at line 748 of file class.assFormulaQuestionResult.php.

References $rating_value.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getReachedPoints()

assFormulaQuestionResult::getReachedPoints (   $variables,
  $results,
  $value,
  $unit,
  $units 
)

Definition at line 427 of file class.assFormulaQuestionResult.php.

References $DIC, $formula, $ilLog, $points, $result, $results, $unit, ilMath\_div(), ilMath\_mul(), checkSign(), getPoints(), getPrecision(), getRatingSign(), getRatingSimple(), getRatingUnit(), getRatingValue(), getResultType(), getTolerance(), getUnit(), isCorrect(), isInTolerance(), RESULT_CO_FRAC, RESULT_DEC, RESULT_FRAC, RESULT_NO_SELECTION, and substituteFormula().

428  {
429  global $DIC;
430  $ilLog = $DIC['ilLog'];
431  if ($this->getRatingSimple()) {
432  if ($this->isCorrect($variables, $results, $value, $units[$unit])) {
433  return $this->getPoints();
434  } else {
435  return 0;
436  }
437  } else {
438  $points = 0;
439  include_once "./Services/Math/classes/class.EvalMath.php";
440  include_once "./Services/Math/classes/class.ilMath.php";
441  $formula = $this->substituteFormula($variables, $results);
442 
443  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
444  foreach ($matches[1] as $variable) {
445  $varObj = $variables[$variable];
446  if (!is_object($varObj)) {
447  continue;
448  }
449  if ($varObj->getUnit() != null) {
450  //convert unit and value to baseunit
451  if ($varObj->getUnit()->getBaseUnit() != -1) {
452  $tmp_value = $varObj->getValue() * $varObj->getUnit()->getFactor();
453  } else {
454  $tmp_value = $varObj->getValue();
455  }
456  } else {
457  $tmp_value = $varObj->getValue();
458  }
459  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $tmp_value . ")" . "\\1", $formula);
460  }
461  }
462 
463  $math = new EvalMath();
464  $math->suppress_errors = true;
465  $result = $math->evaluate($formula);
466 
467  // result_type extension
468  switch ($this->getResultType()) {
470  if ((substr_count($value, '.') == 1) || (substr_count($value, ',') == 1)) {
471  $exp_val = $value;
472  $frac_value = str_replace(',', '.', $exp_val);
473  } else {
474  $frac_value = $value;
475  }
476  $check_fraction = true;
477  break;
479  $exp_val = explode('/', $value);
480  if (count($exp_val) == 1) {
481  $frac_value = ilMath::_div($exp_val[0], 1, $this->getPrecision());
482  if (ilMath::_equals(abs($frac_value), abs($result), $this->getPrecision())) {
483  $check_fraction = true;
484  } else {
485  $check_fraction = false;
486  }
487  } else {
488  $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
489  if (ilMath::_equals(abs($frac_value), abs($result), $this->getPrecision())) {
490  $check_fraction = true;
491  }
492  }
493  break;
495  $exp_val = explode('/', $value);
496  if (count($exp_val) == 1) {
497  $check_fraction = false;
498  } else {
499  $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
500  if (self::isCoprimeFraction($exp_val[0], $exp_val[1])) {
501  $check_fraction = true;
502  }
503  }
504  break;
506  default:
507  $check_fraction = true;
508  break;
509  }
510 
511  // result unit!!
512  if (is_object($this->getUnit())) {
513  // if expected resultunit != baseunit convert to resultunit
514  if ($this->getUnit()->getBaseUnit() != -1) {
515  $result = ilMath::_div($result, $this->getUnit()->getFactor(), $this->getPrecision());
516  } else {
517  //if resultunit == baseunit calculate to get correct precision
518  $result = ilMath::_mul($result, $this->getUnit()->getFactor(), $this->getPrecision());
519  }
520  }
521 
522  if (is_object($unit)) {
523  if (isset($frac_value)) {
524  $value = ilMath::_mul($frac_value, $unit->getFactor(), 100);
525  }
526  }
527 
528  if ($this->checkSign($result, $value)) {
529  $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingSign(), 100));
530  }
531  if ($this->isInTolerance(abs($value), abs($result), $this->getTolerance())) {
532  $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingValue(), 100));
533  }
534  if (is_object($this->getUnit())) {
535  $base1 = $units[$unit];
536  if (is_object($base1)) {
537  $base1 = $units[$base1->getBaseUnit()];
538  }
539  $base2 = $units[$this->getUnit()->getBaseUnit()];
540  if (is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId()) {
541  $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingUnit(), 100));
542  }
543  }
544  return $points;
545  }
546  }
global $DIC
Definition: saml.php:7
static _div($left_operand, $right_operand, $scale=50)
isCorrect($variables, $results, $value, $unit=null)
static _mul($left_operand, $right_operand, $scale=50)
$results
Definition: svg-scanner.php:47
+ Here is the call graph for this function:

◆ getResult()

assFormulaQuestionResult::getResult ( )

Definition at line 614 of file class.assFormulaQuestionResult.php.

References $result.

Referenced by assFormulaQuestionGUI\isSaveCommand(), and substituteFormula().

+ Here is the caller graph for this function:

◆ getResultInfo()

assFormulaQuestionResult::getResultInfo (   $variables,
  $results,
  $value,
  $unit,
  $units 
)

Definition at line 548 of file class.assFormulaQuestionResult.php.

References $formula, $points, $result, $results, $unit, ilMath\_mul(), checkSign(), getPoints(), getRatingSign(), getRatingSimple(), getRatingUnit(), getRatingValue(), getTolerance(), getUnit(), isCorrect(), isInTolerance(), and substituteFormula().

549  {
550  if ($this->getRatingSimple()) {
551  if ($this->isCorrect($variables, $results, $value, $units[$unit])) {
552  return array("points" => $this->getPoints());
553  } else {
554  return array("points" => 0);
555  }
556  } else {
557  include_once "./Services/Math/classes/class.EvalMath.php";
558  include_once "./Services/Math/classes/class.ilMath.php";
559  $totalpoints = 0;
560  $formula = $this->substituteFormula($variables, $results);
561  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
562  foreach ($matches[1] as $variable) {
563  $varObj = $variables[$variable];
564  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $varObj->getBaseValue() . ")" . "\\1", $formula);
565  }
566  }
567  $math = new EvalMath();
568  $math->suppress_errors = true;
569  $result = $math->evaluate($formula);
570  if (is_object($this->getUnit())) {
571  $result = ilMath::_mul($result, $this->getUnit()->getFactor(), 100);
572  }
573  if (is_object($unit)) {
574  $value = ilMath::_mul($value, $unit->getFactor(), 100);
575  } else {
576  }
577  $details = array();
578  if ($this->checkSign($result, $value)) {
579  $points = ilMath::_mul($this->getPoints(), $this->getRatingSign() / 100);
580  $totalpoints += $points;
581  $details['sign'] = $points;
582  }
583  if ($this->isInTolerance(abs($value), abs($result), $this->getTolerance())) {
584  $points = ilMath::_mul($this->getPoints(), $this->getRatingValue() / 100);
585  $totalpoints += $points;
586  $details['value'] = $points;
587  }
588  if (is_object($this->getUnit())) {
589  $base1 = $units[$unit];
590  if (is_object($base1)) {
591  $base1 = $units[$base1->getBaseUnit()];
592  }
593  $base2 = $units[$this->getUnit()->getBaseUnit()];
594  if (is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId()) {
595  $points = ilMath::_mul($this->getPoints(), $this->getRatingUnit() / 100);
596  $totalpoints += $points;
597  $details['unit'] = $points;
598  }
599  }
600  $details['points'] = $totalpoints;
601  return $details;
602  }
603  }
isCorrect($variables, $results, $value, $unit=null)
static _mul($left_operand, $right_operand, $scale=50)
$results
Definition: svg-scanner.php:47
+ Here is the call graph for this function:

◆ getResultType()

assFormulaQuestionResult::getResultType ( )

Definition at line 778 of file class.assFormulaQuestionResult.php.

References $result_type.

Referenced by calculateFormula(), getReachedPoints(), and isCorrect().

779  {
780  return (int) $this->result_type;
781  }
+ Here is the caller graph for this function:

◆ getResultTypeByQstId()

static assFormulaQuestionResult::getResultTypeByQstId (   $a_qst_id,
  $a_result 
)
static

Definition at line 803 of file class.assFormulaQuestionResult.php.

References $DIC, $ilDB, $res, and $row.

Referenced by assFormulaQuestionGUI\getPreview(), and assFormulaQuestionGUI\getTestOutput().

804  {
805  global $DIC;
806  $ilDB = $DIC['ilDB'];
807 
808  $res = $ilDB->queryF(
809  '
810  SELECT result_type
811  FROM il_qpl_qst_fq_res
812  WHERE question_fi = %s
813  AND result = %s',
814  array('integer', 'text'),
815  array($a_qst_id, $a_result)
816  );
817 
818  $row = $ilDB->fetchAssoc($res);
819 
820  return $row['result_type'];
821  }
global $DIC
Definition: saml.php:7
foreach($_POST as $key=> $value) $res
$row
global $ilDB
+ Here is the caller graph for this function:

◆ getTolerance()

assFormulaQuestionResult::getTolerance ( )

Definition at line 688 of file class.assFormulaQuestionResult.php.

References $tolerance.

Referenced by getReachedPoints(), getResultInfo(), and isCorrect().

+ Here is the caller graph for this function:

◆ getUnit()

assFormulaQuestionResult::getUnit ( )

Definition at line 698 of file class.assFormulaQuestionResult.php.

References $unit.

Referenced by calculateFormula(), getRangeMaxBase(), getRangeMinBase(), getReachedPoints(), getResultInfo(), isCorrect(), and suggestRange().

+ Here is the caller graph for this function:

◆ isCoprimeFraction()

static assFormulaQuestionResult::isCoprimeFraction (   $numerator,
  $denominator 
)
static

Definition at line 823 of file class.assFormulaQuestionResult.php.

824  {
825  $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
826 
827  return $gcd == 1 ? true : false;
828  }

◆ isCorrect()

assFormulaQuestionResult::isCorrect (   $variables,
  $results,
  $value,
  $unit = null 
)
Parameters
$variablesformula variables containing units
$resultsformula results containing units
$valueuser input value
null$unituser input unit
Returns
bool

Definition at line 224 of file class.assFormulaQuestionResult.php.

References $formula, $result, $results, $unit, ilMath\_div(), ilMath\_mul(), getPrecision(), getResultType(), getTolerance(), getUnit(), isInTolerance(), RESULT_CO_FRAC, RESULT_DEC, RESULT_FRAC, RESULT_NO_SELECTION, and substituteFormula().

Referenced by getReachedPoints(), getResultInfo(), and assFormulaQuestionTest\testSimpleRatedFormulaQuestionCalculations().

225  {
226  // The user did not answer the question ....
227  if ($value === null || 0 == strlen($value)) {
228  return false;
229  }
230  $value = str_replace(' ', '', $value);
231 
232  include_once "./Services/Math/classes/class.EvalMath.php";
233  include_once "./Services/Math/classes/class.ilMath.php";
234  $formula = $this->substituteFormula($variables, $results);
235 
236  $check_valid_chars = true;
237 
238  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
239  foreach ($matches[1] as $variable) {
240  $varObj = $variables[$variable];
241  if (!is_object($varObj)) {
242  continue;
243  }
244 
245  if ($varObj->getUnit() != null) {
246  //convert unit and value to baseunit.... because vars could have different units
247  if ($varObj->getUnit()->getBaseUnit() != -1) { #$this->getUnit() != NULL)
248  $tmp_value = $varObj->getValue() * $varObj->getUnit()->getFactor();
249  } else {
250  $tmp_value = $varObj->getValue();
251  }
252  } else {
253  $tmp_value = $varObj->getValue();
254  }
255 
256  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $tmp_value . ")" . "\\1", $formula);
257  }
258  }
259 
260  $math = new EvalMath();
261  $math->suppress_errors = true;
262  $result = $math->evaluate($formula); // baseunit-result!!
263 
264  $resultWithRespectedUnit = ilMath::_round($result, $this->getPrecision());
265  if (is_object($this->getUnit())) {
266  //there is a "fix" result_unit defined!
267 
268  // if expected resultunit != baseunit convert to "fix" result_unit
269  if ($this->getUnit()->getBaseUnit() != -1) {
270  $resultWithRespectedUnit = ilMath::_div($result, $this->getUnit()->getFactor(), $this->getPrecision());
271  } else {
272  //if resultunit == baseunit calculate to get correct precision
273  $resultWithRespectedUnit = ilMath::_mul($result, 1, $this->getPrecision());
274  }
275  } elseif ($this->getUnit() == null && $unit != null) {
276  // there is no "fix" result_unit defined, but the user has selected a unit ...
277  // so .... there are "available resultunits" in multi-selectbox selected
278  // -> check if selected user-unit is baseunit
279  if ($unit->getFactor() == 1 && strlen(trim($unit->getFactor())) == 1) {
280  // result is already calculated to baseunit.... -> get correct precision..
281  $resultWithRespectedUnit = ilMath::_mul($result, 1, $this->getPrecision());
282  } else {
283  $resultWithRespectedUnit = ilMath::_div($result, $unit->getFactor(), $this->getPrecision());
284  }
285  }
286 
287  // check for valid chars ("0-9",",|.|/","0-9","e|E","+|-","0-9")
288  $has_valid_chars = preg_match("/^-?([0-9]*)(,|\\.|\\/){0,1}([0-9]*)([eE][\\+|-]([0-9])+)?$/", $value, $matches);
289  if (!$has_valid_chars) {
290  $check_valid_chars = false;
291  } elseif (
292  (isset($matches[2]) && $matches[2] == '/') &&
293  (isset($matches[4]) && strtolower($matches[4]) == "e") &&
294  (!isset($matches[1]) || !strlen($matches[1]) || !isset($matches[3]) || !strlen($matches[3]) || $matches[3] == 0)) {
295  $check_valid_chars = false;
296  }
297  // result_type extension
298  switch ($this->getResultType()) {
300  if (substr_count($value, '.') == 1 || substr_count($value, ',') == 1) {
301  $exp_val = $value;
302  $frac_value = str_replace(',', '.', $exp_val);
303  } else {
304  $frac_value = $value;
305  }
306 
307  $frac_value = ilMath::_round($frac_value, $this->getPrecision());
308 
309  if (substr_count($value, '/') >= 1) {
310  $check_fraction = false;
311  } else {
312  $check_fraction = true;
313  }
314  break;
315 
318  $exp_val = explode('/', $value);
319  if (count($exp_val) == 1) {
320  $frac_value = ilMath::_div($exp_val[0], 1, $this->getPrecision());
321  if (ilMath::_equals($frac_value, $resultWithRespectedUnit, $this->getPrecision())) {
322  $check_fraction = true;
323  } else {
324  $check_fraction = false;
325  }
326  } else {
327  try {
328  $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
329  } catch (ilMathDivisionByZeroException $ex) {
330  if ($result) {
331  return false;
332  } else {
333  return true;
334  }
335  }
336  $frac_value = str_replace(',', '.', $frac_value);
337 
338  if (ilMath::_equals($frac_value, $resultWithRespectedUnit, $this->getPrecision())) {
339  $check_fraction = true;
340  }
341 
343  if (!self::isCoprimeFraction($exp_val[0], $exp_val[1])) {
344  $check_fraction = false;
345  }
346  }
347  }
348 
349  if (substr_count($value, '.') >= 1 || substr_count($value, ',') >= 1) {
350  $check_fraction = false;
351  }
352  break;
353 
355  default:
356  if (substr_count($value, '.') == 1 || substr_count($value, ',') == 1) {
357  $frac_value = str_replace(',', '.', $value);
358  } elseif (substr_count($value, '/') == 1) {
359  $exp_val = explode('/', $value);
360  try {
361  $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
362  } catch (ilMathDivisionByZeroException $ex) {
363  if ($result) {
364  return false;
365  } else {
366  return true;
367  }
368  }
369  } else {
370  $frac_value = $value;
371  }
372  $frac_value = ilMath::_round($frac_value, $this->getPrecision());
373  $check_fraction = true;
374  break;
375  }
376 
377  if (is_object($unit)) {
378  if (isset($frac_value)) {
379  $value = ilMath::_mul($frac_value, $unit->getFactor(), 100);
380  }
381  }
382 
383  $checkvalue = false;
384  if (isset($frac_value)) {
385  if ($this->isInTolerance($frac_value, $resultWithRespectedUnit, $this->getTolerance())) {
386  $checkvalue = true;
387  }
388  } else {
389  if ($this->isInTolerance($value, $resultWithRespectedUnit, $this->getTolerance())) {
390  $checkvalue = true;
391  }
392  }
393 
394  $checkunit = true;
395  if (is_object($this->getUnit())) {
396  if (is_object($unit)) {
397  if ($unit->getId() != $this->getUnit()->getId()) {
398  $checkunit = false;
399  }
400  }
401  }
402  return $checkvalue && $checkunit && $check_fraction && $check_valid_chars;
403  }
static _div($left_operand, $right_operand, $scale=50)
static _mul($left_operand, $right_operand, $scale=50)
$results
Definition: svg-scanner.php:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isInTolerance()

assFormulaQuestionResult::isInTolerance (   $v1,
  $v2,
  $p 
)
protected

Definition at line 405 of file class.assFormulaQuestionResult.php.

References ilMath\_add(), ilMath\_div(), ilMath\_mul(), ilMath\_sub(), and getPrecision().

Referenced by getReachedPoints(), getResultInfo(), and isCorrect().

406  {
407  include_once "./Services/Math/classes/class.ilMath.php";
408  $v1 = ilMath::_mul($v1, 1, $this->getPrecision());
409  $b1 = ilMath::_sub($v2, abs(ilMath::_div(ilMath::_mul($p, $v2, 100), 100)), $this->getPrecision());
410  $b2 = ilMath::_add($v2, abs(ilMath::_div(ilMath::_mul($p, $v2, 100), 100)), $this->getPrecision());
411  if (($b1 <= $v1) && ($b2 >= $v1)) {
412  return true;
413  } else {
414  return false;
415  }
416  }
static _div($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)
static _mul($left_operand, $right_operand, $scale=50)
static _sub($left_operand, $right_operand, $scale=50)
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setFormula()

assFormulaQuestionResult::setFormula (   $formula)

Definition at line 703 of file class.assFormulaQuestionResult.php.

References $formula.

704  {
705  $this->formula = $formula;
706  }

◆ setPoints()

assFormulaQuestionResult::setPoints (   $points)

Definition at line 713 of file class.assFormulaQuestionResult.php.

References $points.

714  {
715  $this->points = $points;
716  }

◆ setPrecision()

assFormulaQuestionResult::setPrecision (   $precision)

Definition at line 763 of file class.assFormulaQuestionResult.php.

References $precision.

764  {
765  $this->precision = $precision;
766  }

◆ setRangeMax()

assFormulaQuestionResult::setRangeMax (   $range_max)

Definition at line 651 of file class.assFormulaQuestionResult.php.

References $range_max, and $result.

Referenced by __construct(), and suggestRange().

652  {
653  // include_once "./Services/Math/classes/class.EvalMath.php";
654  // $math = new EvalMath();
655  // $math->suppress_errors = TRUE;
656  // $result = $math->evaluate($range_max);
657  // $val = (strlen($result) > 8) ? strtoupper(sprintf("%e", $result)) : $result;
658  // $this->range_max = $val;
659 
660  include_once "./Services/Math/classes/class.EvalMath.php";
661  $math = new EvalMath();
662  $math->suppress_errors = true;
663  $result = $math->evaluate($range_max);
664  $this->range_max = $result;
665  }
+ Here is the caller graph for this function:

◆ setRangeMaxTxt()

assFormulaQuestionResult::setRangeMaxTxt (   $range_max_txt)

Definition at line 783 of file class.assFormulaQuestionResult.php.

References $range_max_txt.

Referenced by __construct().

784  {
785  $this->range_max_txt = $range_max_txt;
786  }
+ Here is the caller graph for this function:

◆ setRangeMin()

assFormulaQuestionResult::setRangeMin (   $range_min)

Definition at line 619 of file class.assFormulaQuestionResult.php.

References $range_min, and $result.

Referenced by __construct(), and suggestRange().

620  {
621  // include_once "./Services/Math/classes/class.EvalMath.php";
622  // $math = new EvalMath();
623  // $math->suppress_errors = TRUE;
624  // $result = $math->evaluate($range_min);
625  // $val = (strlen($result) > 8) ? strtoupper(sprintf("%e", $result)) : $result;
626  // $this->range_min = $val;
627 
628  include_once "./Services/Math/classes/class.EvalMath.php";
629  $math = new EvalMath();
630  $math->suppress_errors = true;
631  $result = $math->evaluate($range_min);
632  $this->range_min = $result;
633  }
+ Here is the caller graph for this function:

◆ setRangeMinTxt()

assFormulaQuestionResult::setRangeMinTxt (   $range_min_txt)

Definition at line 793 of file class.assFormulaQuestionResult.php.

References $range_min_txt.

Referenced by __construct().

794  {
795  $this->range_min_txt = $range_min_txt;
796  }
+ Here is the caller graph for this function:

◆ setRatingSign()

assFormulaQuestionResult::setRatingSign (   $rating_sign)

Definition at line 733 of file class.assFormulaQuestionResult.php.

References $rating_sign.

734  {
735  $this->rating_sign = $rating_sign;
736  }

◆ setRatingSimple()

assFormulaQuestionResult::setRatingSimple (   $rating_simple)

Definition at line 723 of file class.assFormulaQuestionResult.php.

References $rating_simple.

724  {
725  $this->rating_simple = $rating_simple;
726  }

◆ setRatingUnit()

assFormulaQuestionResult::setRatingUnit (   $rating_unit)

Definition at line 753 of file class.assFormulaQuestionResult.php.

References $rating_unit.

754  {
755  $this->rating_unit = $rating_unit;
756  }

◆ setRatingValue()

assFormulaQuestionResult::setRatingValue (   $rating_value)

Definition at line 743 of file class.assFormulaQuestionResult.php.

References $rating_value.

744  {
745  $this->rating_value = $rating_value;
746  }

◆ setResult()

assFormulaQuestionResult::setResult (   $result)

Definition at line 609 of file class.assFormulaQuestionResult.php.

References $result.

610  {
611  $this->result = $result;
612  }

◆ setResultType()

assFormulaQuestionResult::setResultType (   $a_result_type)

Definition at line 773 of file class.assFormulaQuestionResult.php.

774  {
775  $this->result_type = $a_result_type;
776  }

◆ setTolerance()

assFormulaQuestionResult::setTolerance (   $tolerance)

Definition at line 683 of file class.assFormulaQuestionResult.php.

References $tolerance.

684  {
685  $this->tolerance = $tolerance;
686  }

◆ setUnit()

assFormulaQuestionResult::setUnit (   $unit)

Definition at line 693 of file class.assFormulaQuestionResult.php.

References $unit.

694  {
695  $this->unit = $unit;
696  }

◆ substituteFormula()

assFormulaQuestionResult::substituteFormula (   $variables,
  $results 
)

Definition at line 74 of file class.assFormulaQuestionResult.php.

References $DIC, $formula, $lng, $result, $results, getFormula(), getResult(), and ilUtil\sendFailure().

Referenced by calculateFormula(), findValidRandomVariables(), getReachedPoints(), getResultInfo(), isCorrect(), and suggestRange().

75  {
76  global $DIC;
77  $lng = $DIC['lng'];
78 
79  $formula = $this->getFormula();
80 
81  if (preg_match_all("/(\\\$r\\d+)/ims", $formula, $matches)) {
82  foreach ($matches[1] as $result) {
83  if (strcmp($result, $this->getResult()) == 0) {
84  ilUtil::sendFailure($lng->txt("errRecursionInResult"));
85  return false;
86  }
87 
88  if (is_object($results[$result])) {
89  $formula = str_replace($result, $results[$result]->substituteFormula($variables, $results), $formula);
90  } else {
91  ilUtil::sendFailure($lng->txt("errFormulaQuestion"));
92  return false;
93  }
94  }
95  }
96 
97  return "(" . $formula . ")";
98  }
global $DIC
Definition: saml.php:7
$lng
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$results
Definition: svg-scanner.php:47
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ suggestRange()

assFormulaQuestionResult::suggestRange (   $variables,
  $results 
)

Definition at line 179 of file class.assFormulaQuestionResult.php.

References $formula, $i, $range_max, $range_min, $result, $results, ilMath\_div(), ilMath\_mul(), getPrecision(), getUnit(), setRangeMax(), setRangeMin(), and substituteFormula().

180  {
181 
182 // @todo Check this
183  include_once "./Services/Math/classes/class.EvalMath.php";
184  $range_min = null;
185  $range_max = null;
186  for ($i = 0; $i < 1000; $i++) {
187  $formula = $this->substituteFormula($variables, $results);
188  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
189  foreach ($matches[1] as $variable) {
190  $varObj = $variables[$variable];
191  if (!is_object($varObj)) {
192  continue;
193  }
194  $varObj->setRandomValue();
195  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $varObj->getBaseValue() . ")" . "\\1", $formula);
196  }
197  }
198  $math = new EvalMath();
199  $math->suppress_errors = true;
200  $result = $math->evaluate($formula);
201  if (($range_min == null) || ($result < $range_min)) {
203  }
204  if (($range_max == null) || ($result > $range_max)) {
206  }
207  }
208  include_once "./Services/Math/classes/class.ilMath.php";
209  if (is_object($this->getUnit())) {
210  $range_min = ilMath::_div($range_min, $this->getUnit()->getFactor());
211  $range_max = ilMath::_div($range_max, $this->getUnit()->getFactor());
212  }
213  $this->setRangeMin(ilMath::_mul($range_min, 1, $this->getPrecision()));
214  $this->setRangeMax(ilMath::_mul($range_max, 1, $this->getPrecision()));
215  }
static _div($left_operand, $right_operand, $scale=50)
static _mul($left_operand, $right_operand, $scale=50)
$results
Definition: svg-scanner.php:47
$i
Definition: disco.tpl.php:19
+ Here is the call graph for this function:

Field Documentation

◆ $available_units

assFormulaQuestionResult::$available_units = array()
private

Definition at line 33 of file class.assFormulaQuestionResult.php.

Referenced by getAvailableResultUnits().

◆ $formula

◆ $points

assFormulaQuestionResult::$points
private

◆ $precision

assFormulaQuestionResult::$precision
private

Definition at line 28 of file class.assFormulaQuestionResult.php.

Referenced by __construct(), getPrecision(), and setPrecision().

◆ $range_max

assFormulaQuestionResult::$range_max
private

◆ $range_max_txt

assFormulaQuestionResult::$range_max_txt
private

Definition at line 31 of file class.assFormulaQuestionResult.php.

Referenced by getRangeMaxTxt(), and setRangeMaxTxt().

◆ $range_min

assFormulaQuestionResult::$range_min
private

◆ $range_min_txt

assFormulaQuestionResult::$range_min_txt
private

Definition at line 30 of file class.assFormulaQuestionResult.php.

Referenced by getRangeMinTxt(), and setRangeMinTxt().

◆ $rating_sign

assFormulaQuestionResult::$rating_sign
private

Definition at line 24 of file class.assFormulaQuestionResult.php.

Referenced by __construct(), getRatingSign(), and setRatingSign().

◆ $rating_simple

assFormulaQuestionResult::$rating_simple
private

◆ $rating_unit

assFormulaQuestionResult::$rating_unit
private

Definition at line 26 of file class.assFormulaQuestionResult.php.

Referenced by __construct(), getRatingUnit(), and setRatingUnit().

◆ $rating_value

assFormulaQuestionResult::$rating_value
private

Definition at line 25 of file class.assFormulaQuestionResult.php.

Referenced by __construct(), getRatingValue(), and setRatingValue().

◆ $result

◆ $result_type

assFormulaQuestionResult::$result_type
private

Definition at line 29 of file class.assFormulaQuestionResult.php.

Referenced by __construct(), and getResultType().

◆ $tolerance

assFormulaQuestionResult::$tolerance
private

◆ $unit

assFormulaQuestionResult::$unit
private

◆ RESULT_CO_FRAC

◆ RESULT_DEC

◆ RESULT_FRAC

◆ RESULT_NO_SELECTION

const assFormulaQuestionResult::RESULT_NO_SELECTION = 0

The documentation for this class was generated from the following file: