~ubuntu-branches/ubuntu/utopic/moodle/utopic

« back to all changes in this revision

Viewing changes to question/type/calculated/questiontype.php

  • Committer: Package Import Robot
  • Author(s): Thijs Kinkhorst
  • Date: 2014-05-12 16:10:38 UTC
  • mfrom: (36.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20140512161038-puyqf65k4e0s8ytz
Tags: 2.6.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1032
1032
        $answers = $question->options->answers;
1033
1033
 
1034
1034
        foreach ($answers as $key => $answer) {
1035
 
            if (is_string($answer)) {
1036
 
                $strheader .= $delimiter.$answer;
1037
 
            } else {
1038
 
                $strheader .= $delimiter.$answer->answer;
1039
 
            }
 
1035
            $ans = shorten_text($answer->answer, 17, true);
 
1036
            $strheader .= $delimiter.$ans;
1040
1037
            $delimiter = '<br/><br/><br/>';
1041
1038
        }
1042
1039
        return $strheader;
1083
1080
                $comment->stranswers[$key] = $formula . ' = ' .
1084
1081
                        get_string('anyvalue', 'qtype_calculated') . '<br/><br/><br/>';
1085
1082
            } else {
 
1083
                $formula = shorten_text($formula, 57, true);
1086
1084
                $comment->stranswers[$key] = $formula . ' = ' . $formattedanswer->answer . '<br/>';
1087
1085
                $correcttrue = new stdClass();
1088
1086
                $correcttrue->correct = $formattedanswer->answer;
1089
 
                $correcttrue->true = $answer->answer;
 
1087
                $correcttrue->true = '';
1090
1088
                if ($formattedanswer->answer < $answer->min ||
1091
1089
                        $formattedanswer->answer > $answer->max) {
1092
1090
                    $comment->outsidelimit = true;
1106
1104
        }
1107
1105
        return fullclone($comment);
1108
1106
    }
1109
 
    public function multichoice_comment_on_datasetitems($questionid, $questiontext,
1110
 
            $answers, $data, $number) {
1111
 
        global $DB;
1112
 
        $comment = new stdClass();
1113
 
        $comment->stranswers = array();
1114
 
        $comment->outsidelimit = false;
1115
 
        $comment->answers = array();
1116
 
        // Find a default unit.
1117
 
        if (!empty($questionid) && $unit = $DB->get_record('question_numerical_units',
1118
 
                array('question' => $questionid, 'multiplier' => 1.0))) {
1119
 
            $unit = $unit->unit;
1120
 
        } else {
1121
 
            $unit = '';
1122
 
        }
1123
 
 
1124
 
        $answers = fullclone($answers);
1125
 
        $errors = '';
1126
 
        $delimiter = ': ';
1127
 
        foreach ($answers as $key => $answer) {
1128
 
            $answer->answer = $this->substitute_variables($answer->answer, $data);
1129
 
            // Evaluate the equations i.e {=5+4).
1130
 
            $qtext = '';
1131
 
            $qtextremaining = $answer->answer;
1132
 
            while (preg_match('~\{=([^[:space:]}]*)}~', $qtextremaining, $regs1)) {
1133
 
                $qtextsplits = explode($regs1[0], $qtextremaining, 2);
1134
 
                $qtext = $qtext.$qtextsplits[0];
1135
 
                $qtextremaining = $qtextsplits[1];
1136
 
                if (empty($regs1[1])) {
1137
 
                    $str = '';
1138
 
                } else {
1139
 
                    if ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
1140
 
                        $str = $formulaerrors;
1141
 
                    } else {
1142
 
                        eval('$str = '.$regs1[1].';');
1143
 
 
1144
 
                        $texteval= qtype_calculated_calculate_answer(
1145
 
                            $str, $data, $answer->tolerance,
1146
 
                            $answer->tolerancetype, $answer->correctanswerlength,
1147
 
                            $answer->correctanswerformat, '');
1148
 
                        $str = $texteval->answer;
1149
 
 
1150
 
                    }
1151
 
                }
1152
 
                $qtext = $qtext.$str;
1153
 
            }
1154
 
            $answer->answer = $qtext.$qtextremaining;
1155
 
            $comment->stranswers[$key]= $answer->answer;
1156
 
 
1157
 
        }
1158
 
        return fullclone($comment);
1159
 
    }
1160
1107
 
1161
1108
    public function tolerance_types() {
1162
1109
        return array(
1878
1825
        $calculated->answer = NAN;
1879
1826
        return $calculated;
1880
1827
    }
1881
 
    if ('1' == $answerformat) { /* Answer is to have $answerlength decimals */
1882
 
        /*** Adjust to the correct number of decimals ***/
1883
 
        if (stripos($answer, 'e')>0) {
1884
 
            $answerlengthadd = strlen($answer)-stripos($answer, 'e');
1885
 
        } else {
1886
 
            $answerlengthadd = 0;
1887
 
        }
1888
 
        $calculated->answer = round(floatval($answer), $answerlength+$answerlengthadd);
1889
 
 
1890
 
        if ($answerlength) {
1891
 
            /* Try to include missing zeros at the end */
1892
 
 
1893
 
            if (preg_match('~^(.*\\.)(.*)$~', $calculated->answer, $regs)) {
1894
 
                $calculated->answer = $regs[1] . substr(
1895
 
                    $regs[2] . '00000000000000000000000000000000000000000x',
1896
 
                    0, $answerlength)
1897
 
                    . $unit;
1898
 
            } else {
1899
 
                $calculated->answer .=
1900
 
                    substr('.00000000000000000000000000000000000000000x',
1901
 
                        0, $answerlength + 1) . $unit;
1902
 
            }
1903
 
        } else {
1904
 
            /* Attach unit */
1905
 
            $calculated->answer .= $unit;
1906
 
        }
 
1828
    if ('1' == $answerformat) { // Answer is to have $answerlength decimals.
 
1829
        // Decimal places.
 
1830
        $calculated->answer = sprintf('%.' . $answerlength . 'F', $answer);
1907
1831
 
1908
1832
    } else if ($answer) { // Significant figures does only apply if the result is non-zero.
1909
1833
 
1941
1865
            $exponent = 'e'.--$p10;
1942
1866
            $answer *= 10;
1943
1867
            if (1 == $answerlength) {
1944
 
                $calculated->answer = $sign.$answer.$exponent.$unit;
 
1868
                $calculated->answer = $sign.$answer.$exponent;
1945
1869
            } else {
1946
1870
                // Attach additional zeros at the end of $answer.
1947
1871
                $answer .= (1 == strlen($answer) ? '.' : '')
1948
1872
                    . '00000000000000000000000000000000000000000x';
1949
1873
                $calculated->answer = $sign
1950
 
                    .substr($answer, 0, $answerlength +1).$exponent.$unit;
 
1874
                    .substr($answer, 0, $answerlength +1).$exponent;
1951
1875
            }
1952
1876
        } else {
1953
1877
            // Stick to plain numeric format.
1954
1878
            $answer *= "1e$p10";
1955
1879
            if (0.1 <= $answer / "1e$answerlength") {
1956
 
                $calculated->answer = $sign.$answer.$unit;
 
1880
                $calculated->answer = $sign.$answer;
1957
1881
            } else {
1958
1882
                // Could be an idea to add some zeros here.
1959
1883
                $answer .= (preg_match('~^[0-9]*$~', $answer) ? '.' : '')
1960
1884
                    . '00000000000000000000000000000000000000000x';
1961
1885
                $oklen = $answerlength + ($p10 < 1 ? 2-$p10 : 1);
1962
 
                $calculated->answer = $sign.substr($answer, 0, $oklen).$unit;
 
1886
                $calculated->answer = $sign.substr($answer, 0, $oklen);
1963
1887
            }
1964
1888
        }
1965
1889
 
1966
1890
    } else {
1967
1891
        $calculated->answer = 0.0;
1968
1892
    }
 
1893
    if ($unit != '') {
 
1894
            $calculated->answer = $calculated->answer . ' ' . $unit;
 
1895
    }
1969
1896
 
1970
1897
    // Return the result.
1971
1898
    return $calculated;