ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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;
24 private $rating_sign;
26 private $rating_unit;
27 private $points;
28 private $precision;
29 private $result_type;
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;
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 {
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 }
104
105 public function calculateFormula($variables, $results, $question_id = 0, $use_precision = true)
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 }
155
156 public function findValidRandomVariables($variables, $results)
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 }
201
202 public function suggestRange($variables, $results)
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 }
240
248 public function isCorrect($variables, $results, $value, $unit = NULL)
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 }
466
467 protected function isInTolerance($v1, $v2, $p)
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 }
476
477 protected function checkSign($v1, $v2)
478 {
479 if((($v1 >= 0) && ($v2 >= 0)) || (($v1 <= 0) && ($v2 <= 0)))
480 {
481 return TRUE;
482 }
483 else
484 {
485 return FALSE;
486 }
487 }
488
489 public function getReachedPoints($variables, $results, $value, $unit, $units)
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 }
646
647 public function getResultInfo($variables, $results, $value, $unit, $units)
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 }
717
718 /************************************
719 * Getter and Setter
720 ************************************/
721
722 public function setResult($result)
723 {
724 $this->result = $result;
725 }
726
727 public function getResult()
728 {
729 return $this->result;
730 }
731
732 public function setRangeMin($range_min)
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 }
749
750 public function getRangeMin()
751 {
752 return $this->range_min;
753 }
754
755 public function getRangeMinBase()
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 }
767
768 public function setRangeMax($range_max)
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 }
784
785 public function getRangeMax()
786 {
787 return $this->range_max;
788 }
789
790 public function getRangeMaxBase()
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 }
802
803 public function setTolerance($tolerance)
804 {
805 $this->tolerance = $tolerance;
806 }
807
808 public function getTolerance()
809 {
810 return $this->tolerance;
811 }
812
813 public function setUnit($unit)
814 {
815 $this->unit = $unit;
816 }
817
818 public function getUnit()
819 {
820 return $this->unit;
821 }
822
823 public function setFormula($formula)
824 {
825 $this->formula = $formula;
826 }
827
828 public function getFormula()
829 {
830 return $this->formula;
831 }
832
833 public function setPoints($points)
834 {
835 $this->points = $points;
836 }
837
838 public function getPoints()
839 {
840 return $this->points;
841 }
842
844 {
845 $this->rating_simple = $rating_simple;
846 }
847
848 public function getRatingSimple()
849 {
851 }
852
854 {
855 $this->rating_sign = $rating_sign;
856 }
857
858 public function getRatingSign()
859 {
860 return $this->rating_sign;
861 }
862
864 {
865 $this->rating_value = $rating_value;
866 }
867
868 public function getRatingValue()
869 {
870 return $this->rating_value;
871 }
872
874 {
875 $this->rating_unit = $rating_unit;
876 }
877
878 public function getRatingUnit()
879 {
880 return $this->rating_unit;
881 }
882
883 public function setPrecision($precision)
884 {
885 $this->precision = $precision;
886 }
887
888 public function getPrecision()
889 {
890 return (int)$this->precision;
891 }
892
893 public function setResultType($a_result_type)
894 {
895 $this->result_type = $a_result_type;
896 }
897
898 public function getResultType()
899 {
900 return (int)$this->result_type;
901 }
902
904 {
905 $this->range_max_txt = $range_max_txt;
906 }
907
908 public function getRangeMaxTxt()
909 {
911 }
912
914 {
915 $this->range_min_txt = $range_min_txt;
916 }
917
918 public function getRangeMinTxt()
919 {
921 }
922
923 public static function getResultTypeByQstId($a_qst_id, $a_result)
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 }
940
941 public static function isCoprimeFraction($numerator, $denominator)
942 {
943 $gcd = self::getGreatestCommonDivisor(abs($numerator), abs($denominator));
944
945 return $gcd == 1 ? true : false;
946 }
947
948 public static function convertDecimalToCoprimeFraction($decimal_value, $tolerance = 1.e-9)
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 }
996
997 public static function getGreatestCommonDivisor($a, $b)
998 {
999 if ($b > 0)
1000 {
1001 return self::getGreatestCommonDivisor($b, $a % $b);
1002 }
1003 else
1004 {
1005 return $a;
1006 }
1007 }
1008
1009
1010 public function getAvailableResultUnits($question_id)
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 }
1028
1029}
1030
1031?>
static getResultTypeByQstId($a_qst_id, $a_result)
calculateFormula($variables, $results, $question_id=0, $use_precision=true)
static isCoprimeFraction($numerator, $denominator)
getResultInfo($variables, $results, $value, $unit, $units)
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
findValidRandomVariables($variables, $results)
getReachedPoints($variables, $results, $value, $unit, $units)
isCorrect($variables, $results, $value, $unit=NULL)
static _mul($left_operand, $right_operand, $scale=50)
static _round($value, $precision=0)
static _div($left_operand, $right_operand, $scale=50)
static _equals($value1, $value2, $scale)
static _sub($left_operand, $right_operand, $scale=50)
static _add($left_operand, $right_operand, $scale=50)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
global $lng
Definition: privfeed.php:40
$results
global $ilDB