Example #1
0
    public function test_within_tolerance_geometric() {
        $answer = new qtype_numerical_answer(13, 7.0, 1.0, '', FORMAT_MOODLE, 1.0);
        $answer->tolerancetype = 3;

        $this->assertFalse($answer->within_tolerance(3.49));
        $this->assertTrue($answer->within_tolerance(3.5));
        $this->assertTrue($answer->within_tolerance(7));
        $this->assertTrue($answer->within_tolerance(14));
        $this->assertFalse($answer->within_tolerance(14.01));
    }
Example #2
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);
 }
Example #3
0
    public function get_possible_responses($questiondata) {
        $responses = array();

        $unit = $this->get_default_numerical_unit($questiondata);

        $starfound = false;
        foreach ($questiondata->options->answers as $aid => $answer) {
            $responseclass = $answer->answer;

            if ($responseclass === '*') {
                $starfound = true;
            } else {
                $responseclass = $this->add_unit($questiondata, $responseclass, $unit);

                $ans = new qtype_numerical_answer($answer->id, $answer->answer, $answer->fraction,
                        $answer->feedback, $answer->feedbackformat, $answer->tolerance);
                list($min, $max) = $ans->get_tolerance_interval();
                $responseclass .= " ($min..$max)";
            }

            $responses[$aid] = new question_possible_response($responseclass,
                    $answer->fraction);
        }

        if (!$starfound) {
            $responses[0] = new question_possible_response(
                    get_string('didnotmatchanyanswer', 'question'), 0);
        }

        $responses[null] = question_possible_response::no_response();

        return array($questiondata->id => $responses);
    }
Example #4
0
    public function get_possible_responses($questiondata) {
        $responses = array();

        $unit = $this->get_default_numerical_unit($questiondata);

        foreach ($questiondata->options->answers as $aid => $answer) {
            $responseclass = $answer->answer;

            if ($responseclass != '*') {
                $responseclass = $this->add_unit($questiondata, $responseclass, $unit);

                $ans = new qtype_numerical_answer($answer->id, $answer->answer, $answer->fraction,
                        $answer->feedback, $answer->feedbackformat, $answer->tolerance);
                list($min, $max) = $ans->get_tolerance_interval();
                $responseclass .= " ($min..$max)";
            }

            $responses[$aid] = new question_possible_response($responseclass,
                    $answer->fraction);
        }
        $responses[null] = question_possible_response::no_response();

        return array($questiondata->id => $responses);
    }