ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 99 of file class.assFormulaQuestionResult.php.

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

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

◆ checkSign()

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

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

Referenced by getReachedPoints(), and getResultInfo().

396  {
397  if ((($v1 >= 0) && ($v2 >= 0)) || (($v1 <= 0) && ($v2 <= 0))) {
398  return true;
399  } else {
400  return false;
401  }
402  }
+ Here is the caller graph for this function:

◆ convertDecimalToCoprimeFraction()

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

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

References $result, $tolerance, array, and string.

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

806  {
807  $to_string = (string) $decimal_value;
808  $is_negative = strpos($to_string, '-') === 0;
809  if ($is_negative) {
810  $decimal_value = substr($decimal_value, 1);
811  }
812  $h1=1;
813  $h2=0;
814  $k1=0;
815  $k2=1;
816  $b = 1 / $decimal_value;
817  do {
818  $b = 1 / $b;
819  $a = floor($b);
820  $aux = $h1;
821  $h1 = $a * $h1 + $h2;
822  $h2 = $aux;
823  $aux = $k1;
824  $k1 = $a * $k1 + $k2;
825  $k2 = $aux;
826  $b = $b - $a;
827  } while ((abs($decimal_value - $h1 / $k1) > $decimal_value * $tolerance) || ($k1 < 0 || $b < 0));
828  if ($k1 == 1) {
829  $result = $h1;
830  $checkResult = $h1;
831  } else {
832  $result = "$h1/$k1";
833  $checkResult = ($h1/$k1);
834  }
835  if ($is_negative) {
836  $result = '-' . $result;
837  $checkResult = ($h1/$k1)*-1;
838  }
839  if ($to_string == $checkResult . '' || $checkResult . '' == $result) {
840  return $result;
841  } else {
842  return array($to_string,$result);
843  }
844  }
Add rich text string
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ findValidRandomVariables()

assFormulaQuestionResult::findValidRandomVariables (   $variables,
  $results 
)

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

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

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

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

Referenced by calculateFormula().

857  {
858  global $ilDB;
859 
860  $res = $ilDB->queryF(
861  '
862  SELECT * FROM il_qpl_qst_fq_res_unit
863  WHERE question_fi = %s
864  ORDER BY result',
865  array('integer'),
866  array($question_id)
867  );
868 
869 
870  while ($row = $ilDB->fetchAssoc($res)) {
871  $this->available_units[$row['result']][] = $row['unit_fi'] ;
872  }
873 
874  return $this->available_units;
875  }
foreach($_POST as $key=> $value) $res
Create styles array
The data for the language used.
global $ilDB
+ Here is the caller graph for this function:

◆ getFormula()

assFormulaQuestionResult::getFormula ( )

Definition at line 684 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 846 of file class.assFormulaQuestionResult.php.

847  {
848  if ($b > 0) {
849  return self::getGreatestCommonDivisor($b, $a % $b);
850  } else {
851  return $a;
852  }
853  }

◆ getPoints()

assFormulaQuestionResult::getPoints ( )

Definition at line 694 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 744 of file class.assFormulaQuestionResult.php.

References $precision.

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

745  {
746  return (int) $this->precision;
747  }
+ Here is the caller graph for this function:

◆ getRangeMax()

assFormulaQuestionResult::getRangeMax ( )

Definition at line 643 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 648 of file class.assFormulaQuestionResult.php.

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

Referenced by findValidRandomVariables().

649  {
650  if (is_numeric($this->getRangeMax())) {
651  if (is_object($this->getUnit())) {
652  include_once "./Services/Math/classes/class.ilMath.php";
653  return ilMath::_mul($this->getRangeMax(), $this->getUnit()->getFactor(), 100);
654  }
655  }
656  return $this->getRangeMax();
657  }
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 764 of file class.assFormulaQuestionResult.php.

References $range_max_txt.

◆ getRangeMin()

assFormulaQuestionResult::getRangeMin ( )

Definition at line 611 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 616 of file class.assFormulaQuestionResult.php.

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

Referenced by findValidRandomVariables().

617  {
618  if (is_numeric($this->getRangeMin())) {
619  if (is_object($this->getUnit())) {
620  include_once "./Services/Math/classes/class.ilMath.php";
621  return ilMath::_mul($this->getRangeMin(), $this->getUnit()->getFactor(), 100);
622  }
623  }
624  return $this->getRangeMin();
625  }
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 774 of file class.assFormulaQuestionResult.php.

References $range_min_txt.

◆ getRatingSign()

assFormulaQuestionResult::getRatingSign ( )

Definition at line 714 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 704 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 734 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 724 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 404 of file class.assFormulaQuestionResult.php.

References $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().

405  {
406  global $ilLog;
407  if ($this->getRatingSimple()) {
408  if ($this->isCorrect($variables, $results, $value, $units[$unit])) {
409  return $this->getPoints();
410  } else {
411  return 0;
412  }
413  } else {
414  $points = 0;
415  include_once "./Services/Math/classes/class.EvalMath.php";
416  include_once "./Services/Math/classes/class.ilMath.php";
417  $formula = $this->substituteFormula($variables, $results);
418 
419  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
420  foreach ($matches[1] as $variable) {
421  $varObj = $variables[$variable];
422  if (!is_object($varObj)) {
423  continue;
424  }
425  if ($varObj->getUnit() != null) {
426  //convert unit and value to baseunit
427  if ($varObj->getUnit()->getBaseUnit() != -1) {
428  $tmp_value = $varObj->getValue() * $varObj->getUnit()->getFactor();
429  } else {
430  $tmp_value = $varObj->getValue();
431  }
432  } else {
433  $tmp_value = $varObj->getValue();
434  }
435  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $tmp_value . ")" . "\\1", $formula);
436  }
437  }
438 
439  $math = new EvalMath();
440  $math->suppress_errors = true;
441  $result = $math->evaluate($formula);
442 
443  // result_type extension
444  switch ($this->getResultType()) {
446  if ((substr_count($value, '.') == 1) || (substr_count($value, ',') == 1)) {
447  $exp_val = $value;
448  $frac_value = str_replace(',', '.', $exp_val);
449  } else {
450  $frac_value = $value;
451  }
452  $check_fraction = true;
453  break;
455  $exp_val = explode('/', $value);
456  if (count($exp_val) == 1) {
457  $frac_value = ilMath::_div($exp_val[0], 1, $this->getPrecision());
458  if (ilMath::_equals(abs($frac_value), abs($result), $this->getPrecision())) {
459  $check_fraction = true;
460  } else {
461  $check_fraction = false;
462  }
463  } else {
464  $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
465  if (ilMath::_equals(abs($frac_value), abs($result), $this->getPrecision())) {
466  $check_fraction = true;
467  }
468  }
469  break;
471  $exp_val = explode('/', $value);
472  if (count($exp_val) == 1) {
473  $check_fraction = false;
474  } else {
475  $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
476  if (self::isCoprimeFraction($exp_val[0], $exp_val[1])) {
477  $check_fraction = true;
478  }
479  }
480  break;
482  default:
483  $check_fraction = true;
484  break;
485  }
486 
487  // result unit!!
488  if (is_object($this->getUnit())) {
489  // if expected resultunit != baseunit convert to resultunit
490  if ($this->getUnit()->getBaseUnit() != -1) {
491  $result = ilMath::_div($result, $this->getUnit()->getFactor(), $this->getPrecision());
492  } else {
493  //if resultunit == baseunit calculate to get correct precision
494  $result = ilMath::_mul($result, $this->getUnit()->getFactor(), $this->getPrecision());
495  }
496  }
497 
498  if (is_object($unit)) {
499  if (isset($frac_value)) {
500  $value = ilMath::_mul($frac_value, $unit->getFactor(), 100);
501  }
502  }
503 
504  if ($this->checkSign($result, $value)) {
505  $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingSign(), 100));
506  }
507  if ($this->isInTolerance(abs($value), abs($result), $this->getTolerance())) {
508  $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingValue(), 100));
509  }
510  if (is_object($this->getUnit())) {
511  $base1 = $units[$unit];
512  if (is_object($base1)) {
513  $base1 = $units[$base1->getBaseUnit()];
514  }
515  $base2 = $units[$this->getUnit()->getBaseUnit()];
516  if (is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId()) {
517  $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingUnit(), 100));
518  }
519  }
520  return $points;
521  }
522  }
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 590 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 524 of file class.assFormulaQuestionResult.php.

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

525  {
526  if ($this->getRatingSimple()) {
527  if ($this->isCorrect($variables, $results, $value, $units[$unit])) {
528  return array("points" => $this->getPoints());
529  } else {
530  return array("points" => 0);
531  }
532  } else {
533  include_once "./Services/Math/classes/class.EvalMath.php";
534  include_once "./Services/Math/classes/class.ilMath.php";
535  $totalpoints = 0;
536  $formula = $this->substituteFormula($variables, $results);
537  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
538  foreach ($matches[1] as $variable) {
539  $varObj = $variables[$variable];
540  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $varObj->getBaseValue() . ")" . "\\1", $formula);
541  }
542  }
543  $math = new EvalMath();
544  $math->suppress_errors = true;
545  $result = $math->evaluate($formula);
546  if (is_object($this->getUnit())) {
547  $result = ilMath::_mul($result, $this->getUnit()->getFactor(), 100);
548  }
549  if (is_object($unit)) {
550  $value = ilMath::_mul($value, $unit->getFactor(), 100);
551  } else {
552  }
553  $details = array();
554  if ($this->checkSign($result, $value)) {
555  $points = ilMath::_mul($this->getPoints(), $this->getRatingSign()/100);
556  $totalpoints += $points;
557  $details['sign'] = $points;
558  }
559  if ($this->isInTolerance(abs($value), abs($result), $this->getTolerance())) {
560  $points = ilMath::_mul($this->getPoints(), $this->getRatingValue()/100);
561  $totalpoints += $points;
562  $details['value'] = $points;
563  }
564  if (is_object($this->getUnit())) {
565  $base1 = $units[$unit];
566  if (is_object($base1)) {
567  $base1 = $units[$base1->getBaseUnit()];
568  }
569  $base2 = $units[$this->getUnit()->getBaseUnit()];
570  if (is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId()) {
571  $points = ilMath::_mul($this->getPoints(), $this->getRatingUnit()/100);
572  $totalpoints += $points;
573  $details['unit'] = $points;
574  }
575  }
576  $details['points'] = $totalpoints;
577  return $details;
578  }
579  }
isCorrect($variables, $results, $value, $unit=null)
Create styles array
The data for the language used.
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 754 of file class.assFormulaQuestionResult.php.

References $result_type.

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

755  {
756  return (int) $this->result_type;
757  }
+ Here is the caller graph for this function:

◆ getResultTypeByQstId()

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

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

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

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

780  {
781  global $ilDB;
782 
783  $res = $ilDB->queryF(
784  '
785  SELECT result_type
786  FROM il_qpl_qst_fq_res
787  WHERE question_fi = %s
788  AND result = %s',
789  array('integer', 'text'),
790  array($a_qst_id, $a_result)
791  );
792 
793  $row = $ilDB->fetchAssoc($res);
794 
795  return $row['result_type'];
796  }
foreach($_POST as $key=> $value) $res
Create styles array
The data for the language used.
global $ilDB
+ Here is the caller graph for this function:

◆ getTolerance()

assFormulaQuestionResult::getTolerance ( )

Definition at line 664 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 674 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 798 of file class.assFormulaQuestionResult.php.

799  {
800  $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
801 
802  return $gcd == 1 ? true : false;
803  }

◆ 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 223 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().

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

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

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

383  {
384  include_once "./Services/Math/classes/class.ilMath.php";
385  $v1 = ilMath::_mul($v1, 1, $this->getPrecision());
386  $b1 = ilMath::_sub($v2, abs(ilMath::_div(ilMath::_mul($p, $v2, 100), 100)), $this->getPrecision());
387  $b2 = ilMath::_add($v2, abs(ilMath::_div(ilMath::_mul($p, $v2, 100), 100)), $this->getPrecision());
388  if (($b1 <= $v1) && ($b2 >= $v1)) {
389  return true;
390  } else {
391  return false;
392  }
393  }
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 679 of file class.assFormulaQuestionResult.php.

References $formula.

680  {
681  $this->formula = $formula;
682  }

◆ setPoints()

assFormulaQuestionResult::setPoints (   $points)

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

References $points.

690  {
691  $this->points = $points;
692  }

◆ setPrecision()

assFormulaQuestionResult::setPrecision (   $precision)

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

References $precision.

740  {
741  $this->precision = $precision;
742  }

◆ setRangeMax()

assFormulaQuestionResult::setRangeMax (   $range_max)

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

References $range_max, and $result.

Referenced by __construct(), and suggestRange().

628  {
629  // include_once "./Services/Math/classes/class.EvalMath.php";
630  // $math = new EvalMath();
631  // $math->suppress_errors = TRUE;
632  // $result = $math->evaluate($range_max);
633  // $val = (strlen($result) > 8) ? strtoupper(sprintf("%e", $result)) : $result;
634  // $this->range_max = $val;
635 
636  include_once "./Services/Math/classes/class.EvalMath.php";
637  $math = new EvalMath();
638  $math->suppress_errors = true;
639  $result = $math->evaluate($range_max);
640  $this->range_max = $result;
641  }
+ Here is the caller graph for this function:

◆ setRangeMaxTxt()

assFormulaQuestionResult::setRangeMaxTxt (   $range_max_txt)

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

References $range_max_txt.

Referenced by __construct().

760  {
761  $this->range_max_txt = $range_max_txt;
762  }
+ Here is the caller graph for this function:

◆ setRangeMin()

assFormulaQuestionResult::setRangeMin (   $range_min)

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

References $range_min, and $result.

Referenced by __construct(), and suggestRange().

596  {
597  // include_once "./Services/Math/classes/class.EvalMath.php";
598  // $math = new EvalMath();
599  // $math->suppress_errors = TRUE;
600  // $result = $math->evaluate($range_min);
601  // $val = (strlen($result) > 8) ? strtoupper(sprintf("%e", $result)) : $result;
602  // $this->range_min = $val;
603 
604  include_once "./Services/Math/classes/class.EvalMath.php";
605  $math = new EvalMath();
606  $math->suppress_errors = true;
607  $result = $math->evaluate($range_min);
608  $this->range_min = $result;
609  }
+ Here is the caller graph for this function:

◆ setRangeMinTxt()

assFormulaQuestionResult::setRangeMinTxt (   $range_min_txt)

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

References $range_min_txt.

Referenced by __construct().

770  {
771  $this->range_min_txt = $range_min_txt;
772  }
+ Here is the caller graph for this function:

◆ setRatingSign()

assFormulaQuestionResult::setRatingSign (   $rating_sign)

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

References $rating_sign.

710  {
711  $this->rating_sign = $rating_sign;
712  }

◆ setRatingSimple()

assFormulaQuestionResult::setRatingSimple (   $rating_simple)

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

References $rating_simple.

700  {
701  $this->rating_simple = $rating_simple;
702  }

◆ setRatingUnit()

assFormulaQuestionResult::setRatingUnit (   $rating_unit)

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

References $rating_unit.

730  {
731  $this->rating_unit = $rating_unit;
732  }

◆ setRatingValue()

assFormulaQuestionResult::setRatingValue (   $rating_value)

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

References $rating_value.

720  {
721  $this->rating_value = $rating_value;
722  }

◆ setResult()

assFormulaQuestionResult::setResult (   $result)

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

References $result.

586  {
587  $this->result = $result;
588  }

◆ setResultType()

assFormulaQuestionResult::setResultType (   $a_result_type)

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

750  {
751  $this->result_type = $a_result_type;
752  }

◆ setTolerance()

assFormulaQuestionResult::setTolerance (   $tolerance)

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

References $tolerance.

660  {
661  $this->tolerance = $tolerance;
662  }

◆ setUnit()

assFormulaQuestionResult::setUnit (   $unit)

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

References $unit.

670  {
671  $this->unit = $unit;
672  }

◆ substituteFormula()

assFormulaQuestionResult::substituteFormula (   $variables,
  $results 
)

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

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

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

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

◆ suggestRange()

assFormulaQuestionResult::suggestRange (   $variables,
  $results 
)

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

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

179  {
180 
181 // @todo Check this
182  include_once "./Services/Math/classes/class.EvalMath.php";
183  $range_min = null;
184  $range_max = null;
185  for ($i = 0; $i < 1000; $i++) {
186  $formula = $this->substituteFormula($variables, $results);
187  if (preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches)) {
188  foreach ($matches[1] as $variable) {
189  $varObj = $variables[$variable];
190  if (!is_object($varObj)) {
191  continue;
192  }
193  $varObj->setRandomValue();
194  $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(" . $varObj->getBaseValue() . ")" . "\\1", $formula);
195  }
196  }
197  $math = new EvalMath();
198  $math->suppress_errors = true;
199  $result = $math->evaluate($formula);
200  if (($range_min == null) || ($result < $range_min)) {
202  }
203  if (($range_max == null) || ($result > $range_max)) {
205  }
206  }
207  include_once "./Services/Math/classes/class.ilMath.php";
208  if (is_object($this->getUnit())) {
209  $range_min = ilMath::_div($range_min, $this->getUnit()->getFactor());
210  $range_max = ilMath::_div($range_max, $this->getUnit()->getFactor());
211  }
212  $this->setRangeMin(ilMath::_mul($range_min, 1, $this->getPrecision()));
213  $this->setRangeMax(ilMath::_mul($range_max, 1, $this->getPrecision()));
214  }
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: