Пример #1
0
 public function comment_on_datasetitems($qtypeobj, $questionid, $questiontext, $answers, $data, $number)
 {
     global $DB;
     $comment = new stdClass();
     $comment->stranswers = array();
     $comment->outsidelimit = false;
     $comment->answers = array();
     // Find a default unit.
     if (!empty($questionid) && ($unit = $DB->get_record('question_numerical_units', array('question' => $questionid, 'multiplier' => 1.0)))) {
         $unit = $unit->unit;
     } else {
         $unit = '';
     }
     $answers = fullclone($answers);
     $delimiter = ': ';
     $virtualqtype = $qtypeobj->get_virtual_qtype();
     foreach ($answers as $key => $answer) {
         $error = qtype_calculated_find_formula_errors($answer->answer);
         if ($error) {
             $comment->stranswers[$key] = $error;
             continue;
         }
         $formula = $this->substitute_variables($answer->answer, $data);
         $formattedanswer = qtype_calculated_calculate_answer($answer->answer, $data, $answer->tolerance, $answer->tolerancetype, $answer->correctanswerlength, $answer->correctanswerformat, $unit);
         if ($formula === '*') {
             $answer->min = ' ';
             $formattedanswer->answer = $answer->answer;
         } else {
             eval('$ansvalue = ' . $formula . ';');
             $ans = new qtype_numerical_answer(0, $ansvalue, 0, '', 0, $answer->tolerance);
             $ans->tolerancetype = $answer->tolerancetype;
             list($answer->min, $answer->max) = $ans->get_tolerance_interval($answer);
         }
         if ($answer->min === '') {
             // This should mean that something is wrong.
             $comment->stranswers[$key] = " {$formattedanswer->answer}" . '<br/><br/>';
         } else {
             if ($formula === '*') {
                 $comment->stranswers[$key] = $formula . ' = ' . get_string('anyvalue', 'qtype_calculated') . '<br/><br/><br/>';
             } else {
                 $formula = shorten_text($formula, 57, true);
                 $comment->stranswers[$key] = $formula . ' = ' . $formattedanswer->answer . '<br/>';
                 $correcttrue = new stdClass();
                 $correcttrue->correct = $formattedanswer->answer;
                 $correcttrue->true = '';
                 if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max) {
                     $comment->outsidelimit = true;
                     $comment->answers[$key] = $key;
                     $comment->stranswers[$key] .= get_string('trueansweroutsidelimits', 'qtype_calculated', $correcttrue);
                 } else {
                     $comment->stranswers[$key] .= get_string('trueanswerinsidelimits', 'qtype_calculated', $correcttrue);
                 }
                 $comment->stranswers[$key] .= '<br/>';
                 $comment->stranswers[$key] .= get_string('min', 'qtype_calculated') . $delimiter . $answer->min . ' --- ';
                 $comment->stranswers[$key] .= get_string('max', 'qtype_calculated') . $delimiter . $answer->max;
             }
         }
     }
     return fullclone($comment);
 }
Пример #2
0
    public function multichoice_comment_on_datasetitems($questionid, $questiontext,
            $answers, $data, $number) {
        global $DB;
        $comment = new stdClass();
        $comment->stranswers = array();
        $comment->outsidelimit = false;
        $comment->answers = array();
        // Find a default unit:
        if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units',
                array('question' => $questionid, 'multiplier' => 1.0))) {
            $unit = $unit->unit;
        } else {
            $unit = '';
        }

        $answers = fullclone($answers);
        $errors = '';
        $delimiter = ': ';
        foreach ($answers as $key => $answer) {
            $answer->answer = $this->substitute_variables($answer->answer, $data);
            //evaluate the equations i.e {=5+4)
            $qtext = '';
            $qtextremaining = $answer->answer;
            while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
                $qtextsplits = explode($regs1[0], $qtextremaining, 2);
                $qtext = $qtext.$qtextsplits[0];
                $qtextremaining = $qtextsplits[1];
                if (empty($regs1[1])) {
                    $str = '';
                } else {
                    if ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
                        $str = $formulaerrors;
                    } else {
                        eval('$str = '.$regs1[1].';');

                        $texteval= qtype_calculated_calculate_answer(
                            $str, $data, $answer->tolerance,
                            $answer->tolerancetype, $answer->correctanswerlength,
                            $answer->correctanswerformat, '');
                        $str = $texteval->answer;

                    }
                }
                $qtext = $qtext.$str;
            }
            $answer->answer = $qtext.$qtextremaining;;
            $comment->stranswers[$key]= $answer->answer;

        }
        return fullclone($comment);
    }
 function get_correct_responses(&$question, &$state)
 {
     $virtualqtype = $this->get_virtual_qtype();
     if ($unit = $virtualqtype->get_default_numerical_unit($question)) {
         $unit = $unit->unit;
     } else {
         $unit = '';
     }
     foreach ($question->options->answers as $answer) {
         if ((int) $answer->fraction === 1) {
             $answernumerical = qtype_calculated_calculate_answer($answer->answer, $state->options->dataset, $answer->tolerance, $answer->tolerancetype, $answer->correctanswerlength, $answer->correctanswerformat, $unit);
             return array('' => $answernumerical->answer);
         }
     }
     return null;
 }
Пример #4
0
 function multichoice_comment_on_datasetitems($questionid, $questiontext, $answers, $data, $number)
 {
     global $DB;
     $comment = new stdClass();
     $comment->stranswers = array();
     $comment->outsidelimit = false;
     $comment->answers = array();
     /// Find a default unit:
     if (!empty($questionid) && ($unit = $DB->get_record('question_numerical_units', array('question' => $questionid, 'multiplier' => 1.0)))) {
         $unit = $unit->unit;
     } else {
         $unit = '';
     }
     $answers = fullclone($answers);
     $strmin = get_string('min', 'quiz');
     $strmax = get_string('max', 'quiz');
     $errors = '';
     $delimiter = ': ';
     foreach ($answers as $key => $answer) {
         $answer->answer = $this->substitute_variables($answer->answer, $data);
         //evaluate the equations i.e {=5+4)
         $qtext = "";
         $qtextremaining = $answer->answer;
         while (preg_match('~\\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
             $qtextsplits = explode($regs1[0], $qtextremaining, 2);
             $qtext = $qtext . $qtextsplits[0];
             $qtextremaining = $qtextsplits[1];
             if (empty($regs1[1])) {
                 $str = '';
             } else {
                 if ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
                     $str = $formulaerrors;
                 } else {
                     eval('$str = ' . $regs1[1] . ';');
                     $texteval = qtype_calculated_calculate_answer($str, $data, $answer->tolerance, $answer->tolerancetype, $answer->correctanswerlength, $answer->correctanswerformat, '');
                     $str = $texteval->answer;
                 }
             }
             $qtext = $qtext . $str;
         }
         $answer->answer = $qtext . $qtextremaining;
         $comment->stranswers[$key] = $answer->answer;
         /*  $formula = $this->substitute_variables($answer->answer,$data);
               $formattedanswer = qtype_calculated_calculate_answer(
                       $answer->answer, $data, $answer->tolerance,
                       $answer->tolerancetype, $answer->correctanswerlength,
                       $answer->correctanswerformat, $unit);
                       if ( $formula === '*'){
                           $answer->min = ' ';
                           $formattedanswer->answer = $answer->answer ;
                       }else {
                           eval('$answer->answer = '.$formula.';') ;
                           $virtualqtype->get_tolerance_interval($answer);
                       }
               if ($answer->min === '') {
                   // This should mean that something is wrong
                   $comment->stranswers[$key] = " $formattedanswer->answer".'<br/><br/>';
               } else if ($formula === '*'){
                   $comment->stranswers[$key] = $formula.' = '.get_string('anyvalue','qtype_calculated').'<br/><br/><br/>';
               }else{
                   $comment->stranswers[$key]= $formula.' = '.$formattedanswer->answer.'<br/>' ;
                   $comment->stranswers[$key] .= $strmin. $delimiter.$answer->min.'---';
                   $comment->stranswers[$key] .= $strmax.$delimiter.$answer->max;
                   $comment->stranswers[$key] .='<br/>';
                   $correcttrue->correct = $formattedanswer->answer ;
                   $correcttrue->true = $answer->answer ;
                   if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max){
                       $comment->outsidelimit = true ;
                       $comment->answers[$key] = $key;
                       $comment->stranswers[$key] .=get_string('trueansweroutsidelimits','qtype_calculated',$correcttrue);//<span class="error">ERROR True answer '..' outside limits</span>';
                   }else {
                       $comment->stranswers[$key] .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
                   }
                   $comment->stranswers[$key] .='';
             }*/
     }
     return fullclone($comment);
 }
Пример #5
0
 function comment_on_datasetitems($questionid, $answers, $data, $number)
 {
     global $DB;
     $comment = new stdClass();
     $comment->stranswers = array();
     $comment->outsidelimit = false;
     $comment->answers = array();
     /// Find a default unit:
     if (!empty($questionid) && ($unit = $DB->get_record('question_numerical_units', array('question' => $questionid, 'multiplier' => 1.0)))) {
         $unit = $unit->unit;
     } else {
         $unit = '';
     }
     $answers = fullclone($answers);
     $strmin = get_string('min', 'quiz');
     $strmax = get_string('max', 'quiz');
     $errors = '';
     $delimiter = ': ';
     $virtualqtype = $this->get_virtual_qtype();
     foreach ($answers as $key => $answer) {
         $formula = $this->substitute_variables($answer->answer, $data);
         $formattedanswer = qtype_calculated_calculate_answer($answer->answer, $data, $answer->tolerance, $answer->tolerancetype, $answer->correctanswerlength, $answer->correctanswerformat, $unit);
         if ($formula === '*') {
             $answer->min = ' ';
             $formattedanswer->answer = $answer->answer;
         } else {
             eval('$answer->answer = ' . $formula . ';');
             $virtualqtype->get_tolerance_interval($answer);
         }
         if ($answer->min === '') {
             // This should mean that something is wrong
             $comment->stranswers[$key] = " {$formattedanswer->answer}" . '<br/><br/>';
         } else {
             if ($formula === '*') {
                 $comment->stranswers[$key] = $formula . ' = ' . get_string('anyvalue', 'qtype_calculated') . '<br/><br/>';
             } else {
                 $comment->stranswers[$key] = $formula . ' = ' . $formattedanswer->answer . '';
                 $comment->stranswers[$key] .= "<br/>" . $strmin . $delimiter . $answer->min . '---';
                 $comment->stranswers[$key] .= $strmax . $delimiter . $answer->max;
                 $comment->stranswers[$key] .= '<br/>';
                 $correcttrue->correct = $formattedanswer->answer;
                 $correcttrue->true = $answer->answer;
                 if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max) {
                     $comment->outsidelimit = true;
                     $comment->answers[$key] = $key;
                     $comment->stranswers[$key] .= get_string('trueansweroutsidelimits', 'qtype_calculated', $correcttrue);
                     //<span class="error">ERROR True answer '..' outside limits</span>';
                 } else {
                     $comment->stranswers[$key] .= get_string('trueanswerinsidelimits', 'qtype_calculated', $correcttrue);
                     //' True answer :'.$calculated->trueanswer.' inside limits';
                 }
                 $comment->stranswers[$key] .= '';
             }
         }
     }
     return fullclone($comment);
 }
Пример #6
0
 function convert_answers(&$question, &$state)
 {
     foreach ($question->options->answers as $key => $answer) {
         $answer->answer = $this->substitute_variables($answer->answer, $state->options->dataset);
         //evaluate the equations i.e {=5+4)
         $qtext = "";
         $qtextremaining = $answer->answer;
         //   while  (preg_match('~\{(=)|%[[:digit]]\.=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
         while (preg_match('~\\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
             $qtextsplits = explode($regs1[0], $qtextremaining, 2);
             $qtext = $qtext . $qtextsplits[0];
             $qtextremaining = $qtextsplits[1];
             if (empty($regs1[1])) {
                 $str = '';
             } else {
                 if ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
                     $str = $formulaerrors;
                 } else {
                     eval('$str = ' . $regs1[1] . ';');
                     $texteval = qtype_calculated_calculate_answer($str, $state->options->dataset, $answer->tolerance, $answer->tolerancetype, $answer->correctanswerlength, $answer->correctanswerformat, '');
                     $str = $texteval->answer;
                 }
             }
             $qtext = $qtext . $str;
         }
         $answer->answer = $qtext . $qtextremaining;
     }
 }