ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 @access public

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

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;
72 }

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

+ 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 105 of file class.assFormulaQuestionResult.php.

106 {
107
108 $resultunits = array();
109 if($question_id > 0)
110 {
111 $resultunits = $this->getAvailableResultUnits($question_id);
112 }
113
114 include_once "./Services/Math/classes/class.ilMath.php";
115 include_once "./Services/Math/classes/class.EvalMath.php";
116 $formula = $this->substituteFormula($variables, $results);
117 if(preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches))
118 {
119 foreach($matches[1] as $variable)
120 {
121 $varObj = $variables[$variable];
122 if(!is_object($varObj))
123 {
124 continue;
125 }
126 $value = $varObj->getBaseValue();
127 $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(".$value.")" . "\\1", $formula);
128 }
129 }
130 $math = new EvalMath();
131 $math->suppress_errors = TRUE;
132
133 $formula = str_replace(",", ".", $formula);
134 $result = $math->evaluate($formula);
135 if(is_object($this->getUnit()))
136 {
137 $result = ilMath::_div($result, $this->getUnit()->getFactor(), 100);
138 }
139
140 // @todo DON'T USE ilMath::_mul() ... bcmul() returns wrong result !!!!
141
142 if($use_precision == true)
143 {
144 $res = $result * 1;
145 if (is_numeric($this->getPrecision()))
146 {
147 if( $this->getResultType()==self::RESULT_DEC || $this->getResultType()==self::RESULT_NO_SELECTION )
148 {
150 }
151 }
152 }
153 return $result;
154 }
static _round($value, $precision=0)
static _div($left_operand, $right_operand, $scale=50)
$results

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

+ Here is the call graph for this function:

◆ checkSign()

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

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

478 {
479 if((($v1 >= 0) && ($v2 >= 0)) || (($v1 <= 0) && ($v2 <= 0)))
480 {
481 return TRUE;
482 }
483 else
484 {
485 return FALSE;
486 }
487 }

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ convertDecimalToCoprimeFraction()

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

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

949 {
950 $to_string = (string) $decimal_value;
951 $is_negative = strpos($to_string, '-') === 0;
952 if($is_negative)
953 {
954 $decimal_value = substr($decimal_value, 1);
955 }
956 $h1=1;
957 $h2=0;
958 $k1=0;
959 $k2=1;
960 $b = 1 / $decimal_value;
961 do {
962 $b = 1 / $b;
963 $a = floor($b);
964 $aux = $h1;
965 $h1 = $a * $h1 + $h2;
966 $h2 = $aux;
967 $aux = $k1;
968 $k1 = $a * $k1 + $k2;
969 $k2 = $aux;
970 $b = $b - $a;
971 }while ((abs($decimal_value - $h1 / $k1) > $decimal_value * $tolerance) || ( $k1 < 0 || $b < 0 ));
972 if($k1 == 1)
973 {
974 $result = $h1;
975 $checkResult = $h1;
976 }
977 else
978 {
979 $result = "$h1/$k1";
980 $checkResult = ($h1/$k1);
981 }
982 if($is_negative)
983 {
984 $result = '-'.$result;
985 $checkResult = ($h1/$k1)*-1;
986 }
987 if($to_string == $checkResult.'' || $checkResult.'' == $result)
988 {
989 return $result;
990 }
991 else
992 {
993 return array($to_string,$result);
994 }
995 }

References $result, and $tolerance.

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

+ Here is the caller graph for this function:

◆ findValidRandomVariables()

assFormulaQuestionResult::findValidRandomVariables (   $variables,
  $results 
)

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

157 {
158 include_once "./Services/Math/classes/class.EvalMath.php";
159 $i = 0;
160 $inRange = FALSE;
161 while($i < 1000 && !$inRange)
162 {
163 $formula = $this->substituteFormula($variables, $results);
164 if(preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches))
165 {
166 foreach($matches[1] as $variable)
167 {
168 $varObj = $variables[$variable];
169 if(!is_object($varObj))
170 {
171 continue;
172 }
173 $varObj->setRandomValue();
174 $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(".$varObj->getBaseValue().")" . "\\1", $formula);
175 }
176 }
177 $math = new EvalMath();
178 $math->suppress_errors = TRUE;
179 $result = $math->evaluate($formula);
180 $inRange = (is_numeric($result)) ? TRUE : FALSE;
181 if($inRange)
182 {
183 if(is_numeric($this->getRangeMin()))
184 {
185 if($result < $this->getRangeMinBase())
186 {
187 $inRange = FALSE;
188 }
189 }
190 if(is_numeric($this->getRangeMax()))
191 {
192 if($result > $this->getRangeMaxBase())
193 {
194 $inRange = FALSE;
195 }
196 }
197 }
198 $i++;
199 }
200 }

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

+ Here is the call graph for this function:

◆ getAvailableResultUnits()

assFormulaQuestionResult::getAvailableResultUnits (   $question_id)

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

1011 {
1012 global $ilDB;
1013
1014 $res = $ilDB->queryF('
1015 SELECT * FROM il_qpl_qst_fq_res_unit
1016 WHERE question_fi = %s
1017 ORDER BY result',
1018 array('integer'), array($question_id));
1019
1020
1021 while ($row = $ilDB->fetchAssoc($res))
1022 {
1023 $this->available_units[$row['result']][] = $row['unit_fi'] ;
1024 }
1025
1027 }
global $ilDB

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

Referenced by calculateFormula().

+ Here is the caller graph for this function:

◆ getFormula()

assFormulaQuestionResult::getFormula ( )

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

829 {
830 return $this->formula;
831 }

References $formula.

Referenced by substituteFormula().

+ Here is the caller graph for this function:

◆ getGreatestCommonDivisor()

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

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

998 {
999 if ($b > 0)
1000 {
1001 return self::getGreatestCommonDivisor($b, $a % $b);
1002 }
1003 else
1004 {
1005 return $a;
1006 }
1007 }

References getGreatestCommonDivisor().

Referenced by getGreatestCommonDivisor(), and isCoprimeFraction().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPoints()

assFormulaQuestionResult::getPoints ( )

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

839 {
840 return $this->points;
841 }

References $points.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getPrecision()

assFormulaQuestionResult::getPrecision ( )

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

889 {
890 return (int)$this->precision;
891 }

References $precision.

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

+ Here is the caller graph for this function:

◆ getRangeMax()

assFormulaQuestionResult::getRangeMax ( )

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

786 {
787 return $this->range_max;
788 }

References $range_max.

Referenced by findValidRandomVariables(), and getRangeMaxBase().

+ Here is the caller graph for this function:

◆ getRangeMaxBase()

assFormulaQuestionResult::getRangeMaxBase ( )

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

791 {
792 if(is_numeric($this->getRangeMax()))
793 {
794 if(is_object($this->getUnit()))
795 {
796 include_once "./Services/Math/classes/class.ilMath.php";
797 return ilMath::_mul($this->getRangeMax(), $this->getUnit()->getFactor(), 100);
798 }
799 }
800 return $this->getRangeMax();
801 }
static _mul($left_operand, $right_operand, $scale=50)

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

Referenced by findValidRandomVariables().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getRangeMaxTxt()

assFormulaQuestionResult::getRangeMaxTxt ( )

◆ getRangeMin()

assFormulaQuestionResult::getRangeMin ( )

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

751 {
752 return $this->range_min;
753 }

References $range_min.

Referenced by findValidRandomVariables(), and getRangeMinBase().

+ Here is the caller graph for this function:

◆ getRangeMinBase()

assFormulaQuestionResult::getRangeMinBase ( )

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

756 {
757 if(is_numeric($this->getRangeMin()))
758 {
759 if(is_object($this->getUnit()))
760 {
761 include_once "./Services/Math/classes/class.ilMath.php";
762 return ilMath::_mul($this->getRangeMin(), $this->getUnit()->getFactor(), 100);
763 }
764 }
765 return $this->getRangeMin();
766 }

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

Referenced by findValidRandomVariables().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getRangeMinTxt()

assFormulaQuestionResult::getRangeMinTxt ( )

◆ getRatingSign()

assFormulaQuestionResult::getRatingSign ( )

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

859 {
860 return $this->rating_sign;
861 }

References $rating_sign.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getRatingSimple()

assFormulaQuestionResult::getRatingSimple ( )

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

849 {
851 }

References $rating_simple.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getRatingUnit()

assFormulaQuestionResult::getRatingUnit ( )

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

879 {
880 return $this->rating_unit;
881 }

References $rating_unit.

Referenced by getReachedPoints(), and getResultInfo().

+ Here is the caller graph for this function:

◆ getRatingValue()

assFormulaQuestionResult::getRatingValue ( )

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

869 {
870 return $this->rating_value;
871 }

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 489 of file class.assFormulaQuestionResult.php.

490 {
491 global $ilLog;
492 if($this->getRatingSimple())
493 {
494 if($this->isCorrect($variables, $results, $value, $units[$unit]))
495 {
496 return $this->getPoints();
497 }
498 else
499 {
500 return 0;
501 }
502 }
503 else
504 {
505 $points = 0;
506 include_once "./Services/Math/classes/class.EvalMath.php";
507 include_once "./Services/Math/classes/class.ilMath.php";
508 $formula = $this->substituteFormula($variables, $results);
509
510 if(preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches))
511 {
512 foreach($matches[1] as $variable)
513 {
514 $varObj = $variables[$variable];
515 if(!is_object($varObj))
516 {
517 continue;
518 }
519 if($varObj->getUnit() != NULL)
520 {
521 //convert unit and value to baseunit
522 if($varObj->getUnit()->getBaseUnit() != -1)
523 {
524 $tmp_value = $varObj->getValue() * $varObj->getUnit()->getFactor();
525 }
526 else
527 {
528 $tmp_value = $varObj->getValue();
529 }
530 }
531 else
532 {
533 $tmp_value = $varObj->getValue();
534 }
535 $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(".$tmp_value.")" . "\\1", $formula);
536 }
537 }
538
539 $math = new EvalMath();
540 $math->suppress_errors = TRUE;
541 $result = $math->evaluate($formula);
542
543// result_type extension
544 switch($this->getResultType())
545 {
547 if((substr_count($value, '.') == 1) || (substr_count($value, ',') == 1))
548 {
549 $exp_val = $value;
550 $frac_value = str_replace(',', '.', $exp_val);
551 }
552 else
553 {
554 $frac_value = $value;
555 }
556 $check_fraction = TRUE;
557 break;
559 $exp_val = explode('/', $value);
560 if(count($exp_val) == 1)
561 {
562 $frac_value = ilMath::_div($exp_val[0], 1, $this->getPrecision());
563 if( ilMath::_equals(abs($frac_value), abs($result), $this->getPrecision()) )
564 {
565 $check_fraction = TRUE;
566 }
567 else
568 {
569 $check_fraction = FALSE;
570 }
571 }
572 else
573 {
574 $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
575 if(ilMath::_equals(abs($frac_value), abs($result), $this->getPrecision()) )
576 {
577 $check_fraction = TRUE;
578 }
579 }
580 break;
582 $exp_val = explode('/', $value);
583 if(count($exp_val) == 1)
584 {
585 $check_fraction = FALSE;
586 }
587 else
588 {
589 $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
590 if(self::isCoprimeFraction($exp_val[0], $exp_val[1]))
591 {
592 $check_fraction = TRUE;
593 }
594 }
595 break;
597 default:
598 $check_fraction = TRUE;
599 break;
600 }
601
602 // result unit!!
603 if(is_object($this->getUnit()))
604 {
605 // if expected resultunit != baseunit convert to resultunit
606 if($this->getUnit()->getBaseUnit() != -1)
607 {
608 $result = ilMath::_div($result, $this->getUnit()->getFactor(), $this->getPrecision());
609 }
610 else
611 {
612 //if resultunit == baseunit calculate to get correct precision
613 $result = ilMath::_mul($result, $this->getUnit()->getFactor(), $this->getPrecision());
614 }
615 }
616
617 if(is_object($unit))
618 {
619 if(isset($frac_value))
620 {
621 $value = ilMath::_mul($frac_value, $unit->getFactor(), 100);
622 }
623 }
624
625 if($this->checkSign($result, $value))
626 {
627 $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingSign(), 100));
628 }
629 if($this->isInTolerance(abs($value), abs($result), $this->getTolerance()))
630 {
631 $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingValue(), 100));
632 }
633 if(is_object($this->getUnit()))
634 {
635 $base1 = $units[$unit];
636 if(is_object($base1)) $base1 = $units[$base1->getBaseUnit()];
637 $base2 = $units[$this->getUnit()->getBaseUnit()];
638 if(is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId())
639 {
640 $points += ilMath::_mul($this->getPoints(), ilMath::_div($this->getRatingUnit(), 100));
641 }
642 }
643 return $points;
644 }
645 }
isCorrect($variables, $results, $value, $unit=NULL)
static _equals($value1, $value2, $scale)

References $formula, $ilLog, $points, $result, $results, $unit, ilMath\_div(), ilMath\_equals(), 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().

+ Here is the call graph for this function:

◆ getResult()

assFormulaQuestionResult::getResult ( )

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

728 {
729 return $this->result;
730 }

References $result.

Referenced by substituteFormula().

+ Here is the caller graph for this function:

◆ getResultInfo()

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

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

648 {
649 if($this->getRatingSimple())
650 {
651 if($this->isCorrect($variables, $results, $value, $units[$unit]))
652 {
653 return array("points" => $this->getPoints());
654 }
655 else
656 {
657 return array("points" => 0);
658 }
659 }
660 else
661 {
662 include_once "./Services/Math/classes/class.EvalMath.php";
663 include_once "./Services/Math/classes/class.ilMath.php";
664 $totalpoints = 0;
665 $formula = $this->substituteFormula($variables, $results);
666 if(preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches))
667 {
668 foreach($matches[1] as $variable)
669 {
670 $varObj = $variables[$variable];
671 $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(".$varObj->getBaseValue().")" . "\\1", $formula);
672 }
673 }
674 $math = new EvalMath();
675 $math->suppress_errors = TRUE;
676 $result = $math->evaluate($formula);
677 if(is_object($this->getUnit()))
678 {
679 $result = ilMath::_mul($result, $this->getUnit()->getFactor(), 100);
680 }
681 if(is_object($unit))
682 {
683 $value = ilMath::_mul($value, $unit->getFactor(), 100);
684 }
685 else
686 {
687 }
688 $details = array();
689 if($this->checkSign($result, $value))
690 {
691 $points = ilMath::_mul($this->getPoints(), $this->getRatingSign()/100);
692 $totalpoints += $points;
693 $details['sign'] = $points;
694 }
695 if($this->isInTolerance(abs($value), abs($result), $this->getTolerance()))
696 {
697 $points = ilMath::_mul($this->getPoints(), $this->getRatingValue()/100);
698 $totalpoints += $points;
699 $details['value'] = $points;
700 }
701 if(is_object($this->getUnit()))
702 {
703 $base1 = $units[$unit];
704 if(is_object($base1)) $base1 = $units[$base1->getBaseUnit()];
705 $base2 = $units[$this->getUnit()->getBaseUnit()];
706 if(is_object($base1) && is_object($base2) && $base1->getId() == $base2->getId())
707 {
708 $points = ilMath::_mul($this->getPoints(), $this->getRatingUnit()/100);
709 $totalpoints += $points;
710 $details['unit'] = $points;
711 }
712 }
713 $details['points'] = $totalpoints;
714 return $details;
715 }
716 }

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

+ Here is the call graph for this function:

◆ getResultType()

assFormulaQuestionResult::getResultType ( )

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

899 {
900 return (int)$this->result_type;
901 }

References $result_type.

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

+ Here is the caller graph for this function:

◆ getResultTypeByQstId()

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

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

924 {
925 global $ilDB;
926
927 $res = $ilDB->queryF('
928 SELECT result_type
929 FROM il_qpl_qst_fq_res
930 WHERE question_fi = %s
931 AND result = %s',
932 array('integer', 'text'),
933 array($a_qst_id, $a_result));
934
935 $row = $ilDB->fetchAssoc($res);
936
937 return $row['result_type'];
938
939 }

References $ilDB, $res, and $row.

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

+ Here is the caller graph for this function:

◆ getTolerance()

assFormulaQuestionResult::getTolerance ( )

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

809 {
810 return $this->tolerance;
811 }

References $tolerance.

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

+ Here is the caller graph for this function:

◆ getUnit()

assFormulaQuestionResult::getUnit ( )

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

819 {
820 return $this->unit;
821 }

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 941 of file class.assFormulaQuestionResult.php.

942 {
943 $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
944
945 return $gcd == 1 ? true : false;
946 }

References getGreatestCommonDivisor().

+ Here is the call graph for this function:

◆ 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 248 of file class.assFormulaQuestionResult.php.

249 {
250 // The user did not answer the question ....
251 if($value === NULL || 0 == strlen($value))
252 {
253 return false;
254 }
255 $value=str_replace(' ', '',$value);
256
257 include_once "./Services/Math/classes/class.EvalMath.php";
258 include_once "./Services/Math/classes/class.ilMath.php";
259 $formula = $this->substituteFormula($variables, $results);
260
261 $check_valid_chars = true;
262
263 if(preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches))
264 {
265 foreach($matches[1] as $variable)
266 {
267 $varObj = $variables[$variable];
268 if(!is_object($varObj))
269 {
270 continue;
271 }
272
273 if($varObj->getUnit() != NULL)
274 {
275 //convert unit and value to baseunit.... because vars could have different units
276 if($varObj->getUnit()->getBaseUnit() != -1) #$this->getUnit() != NULL)
277 {
278 $tmp_value = $varObj->getValue() * $varObj->getUnit()->getFactor();
279 }
280 else
281 {
282 $tmp_value = $varObj->getValue();
283 }
284 }
285 else
286 {
287 $tmp_value = $varObj->getValue();
288 }
289
290 $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(".$tmp_value.")" . "\\1", $formula);
291 }
292 }
293
294 $math = new EvalMath();
295 $math->suppress_errors = true;
296 $result = $math->evaluate($formula); // baseunit-result!!
297
299
300 // check for valid chars ("0-9",",|.|/","0-9","e|E","+|-","0-9")
301 $has_valid_chars = preg_match("/^-?([0-9]*)(,|\\.|\\/){0,1}([0-9]*)([eE][\\+|-]([0-9])+)?$/", $value, $matches);
302 if(!$has_valid_chars)
303 {
304 $check_valid_chars = false;
305 }
306 else if($matches[2] == '/' && strtolower($matches[4]) == "e" && (!strlen($matches[1]) || !strlen($matches[3]) || $matches[3] == 0))
307 {
308 $check_valid_chars = false;
309 }
310// result_type extension
311 switch($this->getResultType())
312 {
314 if(substr_count($value, '.') == 1 || substr_count($value, ',') == 1)
315 {
316 $exp_val = $value;
317 $frac_value = str_replace(',', '.', $exp_val);
318 }
319 else
320 {
321 $frac_value = $value;
322 }
323
324 $frac_value = ilMath::_round($frac_value, $this->getPrecision());
325
326 if(substr_count($value, '/') >= 1)
327 {
328 $check_fraction = FALSE;
329 }
330 else
331 {
332 $check_fraction = TRUE;
333 }
334 break;
335
338 $exp_val = explode('/', $value);
339 if(count($exp_val) == 1)
340 {
341 $frac_value = ilMath::_div($exp_val[0], 1, $this->getPrecision());
342 if( ilMath::_equals($frac_value, $result, $this->getPrecision()) )
343 {
344 $check_fraction = TRUE;
345 }
346 else
347 {
348 $check_fraction = FALSE;
349 }
350 }
351 else
352 {
353 $frac_value = ilMath::_div($exp_val[0], $exp_val[1]);
354 $frac_value = ilMath::_round($frac_value, $this->getPrecision());
355 $frac_value = str_replace(',', '.', $frac_value);
356
357 if( ilMath::_equals($frac_value, $result, $this->getPrecision()) )
358 {
359 $check_fraction = TRUE;
360 }
361
363 {
364 if(!self::isCoprimeFraction($exp_val[0], $exp_val[1]))
365 {
366 $check_fraction = FALSE;
367 }
368 }
369 }
370
371 if(substr_count($value, '.') >= 1 || substr_count($value, ',') >= 1)
372 {
373 $check_fraction = FALSE;
374 }
375 break;
376
378 default:
379 if(substr_count($value, '.') == 1 || substr_count($value, ',') == 1)
380 {
381 $frac_value = str_replace(',', '.', $value);
382 }
383 elseif( substr_count($value, '/') == 1 )
384 {
385 $exp_val = explode('/', $value);
386 $frac_value = ilMath::_div($exp_val[0], $exp_val[1], $this->getPrecision());
387 }
388 else
389 {
390 $frac_value = $value;
391 }
392 $frac_value = ilMath::_round($frac_value, $this->getPrecision());
393 $check_fraction = TRUE;
394 break;
395 }
396
397 // result unit!!
398 if(is_object($this->getUnit()))
399 {
400 //there is a "fix" result_unit defined!
401
402 // if expected resultunit != baseunit convert to "fix" result_unit
403 if($this->getUnit()->getBaseUnit() != -1)
404 {
405 $result = ilMath::_div($result, $this->getUnit()->getFactor(), $this->getPrecision());
406 }
407 else
408 {
409 //if resultunit == baseunit calculate to get correct precision
410 $result = ilMath::_mul($result, $this->getUnit()->getFactor(), $this->getPrecision());
411 }
412 }
413 else if($this->getUnit() == NULL && $unit != NULL)
414 {
415 // there is no "fix" result_unit defined, but the user has selected a unit ...
416 // so .... there are "available resultunits" in multi-selectbox selected
417 // -> check if selected user-unit is baseunit
418
419 if((int)$unit->getFactor() == 1)
420 {
421 // result is already calculated to baseunit.... -> get correct precision..
423 }
424 else
425 {
426 $result = ilMath::_div($result, $unit->getFactor(), 100);
427 }
428 }
429 if(is_object($unit))
430 {
431 if(isset($frac_value))
432 {
433 $value = ilMath::_mul($frac_value, $unit->getFactor(), 100);
434 }
435 }
436
437 $checkvalue = FALSE;
438 if(isset($frac_value))
439 {
440 if($this->isInTolerance($frac_value, $result, $this->getTolerance()))
441 {
442 $checkvalue = TRUE;
443 }
444 }
445 else
446 {
447 if($this->isInTolerance($value, $result, $this->getTolerance()))
448 {
449 $checkvalue = TRUE;
450 }
451 }
452
453 $checkunit = TRUE;
454 if(is_object($this->getUnit()))
455 {
456 if(is_object($unit))
457 {
458 if($unit->getId() != $this->getUnit()->getId())
459 {
460 $checkunit = FALSE;
461 }
462 }
463 }
464 return $checkvalue && $checkunit && $check_fraction && $check_valid_chars;
465 }

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

Referenced by getReachedPoints(), and getResultInfo().

+ 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 467 of file class.assFormulaQuestionResult.php.

468 {
469 include_once "./Services/Math/classes/class.ilMath.php";
470 $v1 = ilMath::_mul($v1, 1, $this->getPrecision());
471 $b1 = ilMath::_sub($v2, abs(ilMath::_div(ilMath::_mul($p, $v2, 100), 100)), $this->getPrecision());
472 $b2 = ilMath::_add($v2, abs(ilMath::_div(ilMath::_mul($p, $v2, 100), 100)), $this->getPrecision());
473 if(($b1 <= $v1) && ($b2 >= $v1)) return TRUE;
474 else return FALSE;
475 }
static _sub($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setFormula()

assFormulaQuestionResult::setFormula (   $formula)

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

824 {
825 $this->formula = $formula;
826 }

References $formula.

◆ setPoints()

assFormulaQuestionResult::setPoints (   $points)

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

834 {
835 $this->points = $points;
836 }

References $points.

◆ setPrecision()

assFormulaQuestionResult::setPrecision (   $precision)

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

884 {
885 $this->precision = $precision;
886 }

References $precision.

◆ setRangeMax()

assFormulaQuestionResult::setRangeMax (   $range_max)

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

769 {
770// include_once "./Services/Math/classes/class.EvalMath.php";
771// $math = new EvalMath();
772// $math->suppress_errors = TRUE;
773// $result = $math->evaluate($range_max);
774// $val = (strlen($result) > 8) ? strtoupper(sprintf("%e", $result)) : $result;
775// $this->range_max = $val;
776
777 include_once "./Services/Math/classes/class.EvalMath.php";
778 $math = new EvalMath();
779 $math->suppress_errors = TRUE;
780 $result = $math->evaluate($range_max);
781 $this->range_max = $result;
782
783 }

References $range_max, and $result.

Referenced by __construct(), and suggestRange().

+ Here is the caller graph for this function:

◆ setRangeMaxTxt()

assFormulaQuestionResult::setRangeMaxTxt (   $range_max_txt)

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

904 {
905 $this->range_max_txt = $range_max_txt;
906 }

References $range_max_txt.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ setRangeMin()

assFormulaQuestionResult::setRangeMin (   $range_min)

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

733 {
734// include_once "./Services/Math/classes/class.EvalMath.php";
735// $math = new EvalMath();
736// $math->suppress_errors = TRUE;
737// $result = $math->evaluate($range_min);
738// $val = (strlen($result) > 8) ? strtoupper(sprintf("%e", $result)) : $result;
739// $this->range_min = $val;
740
741 include_once "./Services/Math/classes/class.EvalMath.php";
742 $math = new EvalMath();
743 $math->suppress_errors = TRUE;
744 $result = $math->evaluate($range_min);
745 $this->range_min = $result;
746
747
748 }

References $range_min, and $result.

Referenced by __construct(), and suggestRange().

+ Here is the caller graph for this function:

◆ setRangeMinTxt()

assFormulaQuestionResult::setRangeMinTxt (   $range_min_txt)

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

914 {
915 $this->range_min_txt = $range_min_txt;
916 }

References $range_min_txt.

Referenced by __construct().

+ Here is the caller graph for this function:

◆ setRatingSign()

assFormulaQuestionResult::setRatingSign (   $rating_sign)

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

854 {
855 $this->rating_sign = $rating_sign;
856 }

References $rating_sign.

◆ setRatingSimple()

assFormulaQuestionResult::setRatingSimple (   $rating_simple)

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

844 {
845 $this->rating_simple = $rating_simple;
846 }

References $rating_simple.

◆ setRatingUnit()

assFormulaQuestionResult::setRatingUnit (   $rating_unit)

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

874 {
875 $this->rating_unit = $rating_unit;
876 }

References $rating_unit.

◆ setRatingValue()

assFormulaQuestionResult::setRatingValue (   $rating_value)

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

864 {
865 $this->rating_value = $rating_value;
866 }

References $rating_value.

◆ setResult()

assFormulaQuestionResult::setResult (   $result)

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

723 {
724 $this->result = $result;
725 }

References $result.

◆ setResultType()

assFormulaQuestionResult::setResultType (   $a_result_type)

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

894 {
895 $this->result_type = $a_result_type;
896 }

◆ setTolerance()

assFormulaQuestionResult::setTolerance (   $tolerance)

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

804 {
805 $this->tolerance = $tolerance;
806 }

References $tolerance.

◆ setUnit()

assFormulaQuestionResult::setUnit (   $unit)

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

814 {
815 $this->unit = $unit;
816 }

References $unit.

◆ substituteFormula()

assFormulaQuestionResult::substituteFormula (   $variables,
  $results 
)

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

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

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

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

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ suggestRange()

assFormulaQuestionResult::suggestRange (   $variables,
  $results 
)

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

203 {
204
205// @todo Check this
206 include_once "./Services/Math/classes/class.EvalMath.php";
207 $range_min = NULL;
208 $range_max = NULL;
209 for($i = 0; $i < 1000; $i++)
210 {
211 $formula = $this->substituteFormula($variables, $results);
212 if(preg_match_all("/(\\\$v\\d+)/ims", $formula, $matches))
213 {
214 foreach($matches[1] as $variable)
215 {
216 $varObj = $variables[$variable];
217 if(!is_object($varObj))
218 {
219 continue;
220 }
221 $varObj->setRandomValue();
222 $formula = preg_replace("/\\\$" . substr($variable, 1) . "(?![0-9]+)/", "(".$varObj->getBaseValue().")" . "\\1", $formula);
223 }
224 }
225 $math = new EvalMath();
226 $math->suppress_errors = TRUE;
227 $result = $math->evaluate($formula);
228 if(($range_min == NULL) || ($result < $range_min)) $range_min = $result;
229 if(($range_max == NULL) || ($result > $range_max)) $range_max = $result;
230 }
231 include_once "./Services/Math/classes/class.ilMath.php";
232 if(is_object($this->getUnit()))
233 {
234 $range_min = ilMath::_div($range_min, $this->getUnit()->getFactor());
235 $range_max = ilMath::_div($range_max, $this->getUnit()->getFactor());
236 }
237 $this->setRangeMin(ilMath::_mul($range_min, 1, $this->getPrecision()));
238 $this->setRangeMax(ilMath::_mul($range_max, 1, $this->getPrecision()));
239 }

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

+ 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

const assFormulaQuestionResult::RESULT_CO_FRAC = 3

◆ RESULT_DEC

const assFormulaQuestionResult::RESULT_DEC = 1

◆ RESULT_FRAC

const assFormulaQuestionResult::RESULT_FRAC = 2

◆ RESULT_NO_SELECTION

const assFormulaQuestionResult::RESULT_NO_SELECTION = 0

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