ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.assFormulaQuestionResult.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
11 {
13  const RESULT_DEC = 1;
14  const RESULT_FRAC = 2;
15  const RESULT_CO_FRAC = 3;
16 
17  private $result;
18  private $range_min;
19  private $range_max;
20  private $tolerance;
21  private $unit;
22  private $formula;
23  private $rating_simple;
24  private $rating_sign;
25  private $rating_value;
26  private $rating_unit;
27  private $points;
28  private $precision;
29  private $result_type;
30  private $range_min_txt;
31  private $range_max_txt;
32 
33  private $available_units = array();
34 
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  }
73 
74  public function substituteFormula($variables, $results)
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  }
98 
99  public function calculateFormula($variables, $results, $question_id = 0, $use_precision = true)
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  }
140 
141  public function findValidRandomVariables($variables, $results)
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  }
177 
178  public function suggestRange($variables, $results)
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  }
215 
223  public function isCorrect($variables, $results, $value, $unit = null)
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  }
381 
382  protected function isInTolerance($v1, $v2, $p)
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  }
394 
395  protected function checkSign($v1, $v2)
396  {
397  if ((($v1 >= 0) && ($v2 >= 0)) || (($v1 <= 0) && ($v2 <= 0))) {
398  return true;
399  } else {
400  return false;
401  }
402  }
403 
404  public function getReachedPoints($variables, $results, $value, $unit, $units)
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  }
523 
524  public function getResultInfo($variables, $results, $value, $unit, $units)
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  }
580 
581  /************************************
582  * Getter and Setter
583  ************************************/
584 
585  public function setResult($result)
586  {
587  $this->result = $result;
588  }
589 
590  public function getResult()
591  {
592  return $this->result;
593  }
594 
595  public function setRangeMin($range_min)
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  }
610 
611  public function getRangeMin()
612  {
613  return $this->range_min;
614  }
615 
616  public function getRangeMinBase()
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  }
626 
627  public function setRangeMax($range_max)
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  }
642 
643  public function getRangeMax()
644  {
645  return $this->range_max;
646  }
647 
648  public function getRangeMaxBase()
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  }
658 
659  public function setTolerance($tolerance)
660  {
661  $this->tolerance = $tolerance;
662  }
663 
664  public function getTolerance()
665  {
666  return $this->tolerance;
667  }
668 
669  public function setUnit($unit)
670  {
671  $this->unit = $unit;
672  }
673 
674  public function getUnit()
675  {
676  return $this->unit;
677  }
678 
679  public function setFormula($formula)
680  {
681  $this->formula = $formula;
682  }
683 
684  public function getFormula()
685  {
686  return $this->formula;
687  }
688 
689  public function setPoints($points)
690  {
691  $this->points = $points;
692  }
693 
694  public function getPoints()
695  {
696  return $this->points;
697  }
698 
700  {
701  $this->rating_simple = $rating_simple;
702  }
703 
704  public function getRatingSimple()
705  {
706  return $this->rating_simple;
707  }
708 
709  public function setRatingSign($rating_sign)
710  {
711  $this->rating_sign = $rating_sign;
712  }
713 
714  public function getRatingSign()
715  {
716  return $this->rating_sign;
717  }
718 
719  public function setRatingValue($rating_value)
720  {
721  $this->rating_value = $rating_value;
722  }
723 
724  public function getRatingValue()
725  {
726  return $this->rating_value;
727  }
728 
729  public function setRatingUnit($rating_unit)
730  {
731  $this->rating_unit = $rating_unit;
732  }
733 
734  public function getRatingUnit()
735  {
736  return $this->rating_unit;
737  }
738 
739  public function setPrecision($precision)
740  {
741  $this->precision = $precision;
742  }
743 
744  public function getPrecision()
745  {
746  return (int) $this->precision;
747  }
748 
749  public function setResultType($a_result_type)
750  {
751  $this->result_type = $a_result_type;
752  }
753 
754  public function getResultType()
755  {
756  return (int) $this->result_type;
757  }
758 
760  {
761  $this->range_max_txt = $range_max_txt;
762  }
763 
764  public function getRangeMaxTxt()
765  {
766  return $this->range_max_txt;
767  }
768 
770  {
771  $this->range_min_txt = $range_min_txt;
772  }
773 
774  public function getRangeMinTxt()
775  {
776  return $this->range_min_txt;
777  }
778 
779  public static function getResultTypeByQstId($a_qst_id, $a_result)
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  }
797 
798  public static function isCoprimeFraction($numerator, $denominator)
799  {
800  $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
801 
802  return $gcd == 1 ? true : false;
803  }
804 
805  public static function convertDecimalToCoprimeFraction($decimal_value, $tolerance = 1.e-9)
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  }
845 
846  public static function getGreatestCommonDivisor($a, $b)
847  {
848  if ($b > 0) {
849  return self::getGreatestCommonDivisor($b, $a % $b);
850  } else {
851  return $a;
852  }
853  }
854 
855 
856  public function getAvailableResultUnits($question_id)
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  }
876 }
Add rich text string
calculateFormula($variables, $results, $question_id=0, $use_precision=true)
static _div($left_operand, $right_operand, $scale=50)
static getResultTypeByQstId($a_qst_id, $a_result)
getResultInfo($variables, $results, $value, $unit, $units)
static isCoprimeFraction($numerator, $denominator)
static _add($left_operand, $right_operand, $scale=50)
foreach($_POST as $key=> $value) $res
findValidRandomVariables($variables, $results)
e($cmd)
Definition: flush.php:14
isCorrect($variables, $results, $value, $unit=null)
Create styles array
The data for the language used.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static _mul($left_operand, $right_operand, $scale=50)
getReachedPoints($variables, $results, $value, $unit, $units)
$results
Definition: svg-scanner.php:47
global $lng
Definition: privfeed.php:17
global $ilDB
$i
Definition: disco.tpl.php:19
static _sub($left_operand, $right_operand, $scale=50)
static convertDecimalToCoprimeFraction($decimal_value, $tolerance=1.e-9)
__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