1032
1032
$answers = $question->options->answers;
1034
1034
foreach ($answers as $key => $answer) {
1035
if (is_string($answer)) {
1036
$strheader .= $delimiter.$answer;
1038
$strheader .= $delimiter.$answer->answer;
1035
$ans = shorten_text($answer->answer, 17, true);
1036
$strheader .= $delimiter.$ans;
1040
1037
$delimiter = '<br/><br/><br/>';
1042
1039
return $strheader;
1083
1080
$comment->stranswers[$key] = $formula . ' = ' .
1084
1081
get_string('anyvalue', 'qtype_calculated') . '<br/><br/><br/>';
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;
1107
1105
return fullclone($comment);
1109
public function multichoice_comment_on_datasetitems($questionid, $questiontext,
1110
$answers, $data, $number) {
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;
1124
$answers = fullclone($answers);
1127
foreach ($answers as $key => $answer) {
1128
$answer->answer = $this->substitute_variables($answer->answer, $data);
1129
// Evaluate the equations i.e {=5+4).
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])) {
1139
if ($formulaerrors = qtype_calculated_find_formula_errors($regs1[1])) {
1140
$str = $formulaerrors;
1142
eval('$str = '.$regs1[1].';');
1144
$texteval= qtype_calculated_calculate_answer(
1145
$str, $data, $answer->tolerance,
1146
$answer->tolerancetype, $answer->correctanswerlength,
1147
$answer->correctanswerformat, '');
1148
$str = $texteval->answer;
1152
$qtext = $qtext.$str;
1154
$answer->answer = $qtext.$qtextremaining;
1155
$comment->stranswers[$key]= $answer->answer;
1158
return fullclone($comment);
1161
1108
public function tolerance_types() {
1878
1825
$calculated->answer = NAN;
1879
1826
return $calculated;
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');
1886
$answerlengthadd = 0;
1888
$calculated->answer = round(floatval($answer), $answerlength+$answerlengthadd);
1890
if ($answerlength) {
1891
/* Try to include missing zeros at the end */
1893
if (preg_match('~^(.*\\.)(.*)$~', $calculated->answer, $regs)) {
1894
$calculated->answer = $regs[1] . substr(
1895
$regs[2] . '00000000000000000000000000000000000000000x',
1899
$calculated->answer .=
1900
substr('.00000000000000000000000000000000000000000x',
1901
0, $answerlength + 1) . $unit;
1905
$calculated->answer .= $unit;
1828
if ('1' == $answerformat) { // Answer is to have $answerlength decimals.
1830
$calculated->answer = sprintf('%.' . $answerlength . 'F', $answer);
1908
1832
} else if ($answer) { // Significant figures does only apply if the result is non-zero.
1941
1865
$exponent = 'e'.--$p10;
1943
1867
if (1 == $answerlength) {
1944
$calculated->answer = $sign.$answer.$exponent.$unit;
1868
$calculated->answer = $sign.$answer.$exponent;
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;
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;
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);
1967
1891
$calculated->answer = 0.0;
1894
$calculated->answer = $calculated->answer . ' ' . $unit;
1970
1897
// Return the result.
1971
1898
return $calculated;