Browse files

Merge from 1.9 as HEAD is different

MDL-16427 implement the '*' convention as for other questiontypes
MDL-15358 negative data and MDL-16427 handling 0
  • Loading branch information...
1 parent 0455e36 commit 3744e9ef2460c28aaf1b1faaae91725f2e245906 pichetp committed Sep 20, 2008
Showing with 35 additions and 27 deletions.
  1. +35 −27 question/type/calculated/questiontype.php
View
62 question/type/calculated/questiontype.php
@@ -364,11 +364,8 @@ function print_question_formulation_and_controls(&$question, &$state, $cmoptions
$numericalquestion = fullclone($question);
foreach ($numericalquestion->options->answers as $key => $answer) {
$answer = fullclone($numericalquestion->options->answers[$key]);
- $correctanswer = qtype_calculated_calculate_answer(
- $answer->answer, $state->options->dataset, $answer->tolerance,
- $answer->tolerancetype, $answer->correctanswerlength,
- $answer->correctanswerformat, $unit);
- $numericalquestion->options->answers[$key]->answer = $correctanswer->answer;
+ $numericalquestion->options->answers[$key]->answer = $this->substitute_variables($answer->answer,
+ $state->options->dataset);
}
$numericalquestion->questiontext = parent::substitute_variables(
$numericalquestion->questiontext, $state->options->dataset);
@@ -476,8 +473,8 @@ function supports_dataset_item_generation() {
function custom_generator_tools_part(&$mform, $idx, $j){
$minmaxgrp = array();
- $minmaxgrp[] =& $mform->createElement('text', "calcmin[$idx]", get_string('calcmin', 'qtype_datasetdependent'), 'size="8"');
- $minmaxgrp[] =& $mform->createElement('text', "calcmax[$idx]", get_string('calcmax', 'qtype_datasetdependent'), 'size="8"');
+ $minmaxgrp[] =& $mform->createElement('text', "calcmin[$idx]", get_string('calcmin', 'qtype_datasetdependent'));
+ $minmaxgrp[] =& $mform->createElement('text', "calcmax[$idx]", get_string('calcmax', 'qtype_datasetdependent'));
$mform->addGroup($minmaxgrp, 'minmaxgrp', get_string('minmax', 'qtype_datasetdependent'), ' - ', false);
$mform->setType("calcmin[$idx]", PARAM_NUMBER);
$mform->setType("calcmax[$idx]", PARAM_NUMBER);
@@ -671,7 +668,6 @@ function save_dataset_items($question, $fromform){
} else {
$datasetitem->value = '';
}
- //pp echo "<pre>"; print_r( $datasetitem );
if (!insert_record('question_dataset_items', $datasetitem)) {
error("Error: Unable to insert new dataset item");
}
@@ -737,7 +733,7 @@ function comment_header($question) {
} else {
$strheader .= $delimiter.$answer->answer;
}
- $delimiter = '<br/><br/>';
+ $delimiter = '<br/><br/><br/>';
}
return $strheader;
}
@@ -751,34 +747,40 @@ function comment_on_datasetitems($question, $data, $number) {
$unit = '';
}
- $answers = $question->options->answers;
+ $answers = fullclone($question->options->answers);
$stranswers = '';
$strmin = get_string('min', 'quiz');
$strmax = get_string('max', 'quiz');
$errors = '';
$delimiter = ': ';
$virtualqtype = $this->get_virtual_qtype();
foreach ($answers as $answer) {
- $formula = $answer->answer;
- foreach ($data as $name => $value) {
- $formula = str_replace('{'.$name.'}', $value, $formula);
- }
- $calculated = qtype_calculated_calculate_answer(
+ $formula = parent::substitute_variables($answer->answer,$data);
+ $formattedanswer = qtype_calculated_calculate_answer(
$answer->answer, $data, $answer->tolerance,
$answer->tolerancetype, $answer->correctanswerlength,
$answer->correctanswerformat, $unit);
- $calculated->tolerance = $answer->tolerance;
- $calculated->tolerancetype = $answer->tolerancetype;
- $calculated->correctanswerlength = $answer->correctanswerlength;
- $calculated->correctanswerformat = $answer->correctanswerformat;
- $virtualqtype->get_tolerance_interval($calculated);
- if ($calculated->min === '') {
+ if ( $formula === '*'){
+ $answer->min = '' ;
+ }else {
+ eval('$answer->answer = '.$formula.';') ;
+ $virtualqtype->get_tolerance_interval($answer);
+ }
+ if ($answer->min === '') {
// This should mean that something is wrong
- $stranswers .= " -$calculated->answer".'<br/><br/>';
+ $stranswers .= " -$formattedanswer->answer".'<br/><br/>';
+ } else {
+ $stranswers .= $formula.' = '.$formattedanswer->answer.'<br/>' ;
+ $stranswers .= $strmin. $delimiter.$answer->min.'---';
+ $stranswers .= $strmax.$delimiter.$answer->max;
+ $stranswers .='<br/>';
+ $correcttrue->correct = $formattedanswer->answer ;
+ $correcttrue->true = $answer->answer ;
+ if ($formattedanswer->answer < $answer->min || $formattedanswer->answer > $answer->max){
+ $stranswers .=get_string('trueansweroutsidelimits','qtype_calculated',$correcttrue);//<span class="error">ERROR True answer '..' outside limits</span>';
} else {
- $stranswers .= $formula.' = '.$calculated->answer.'<br/>' ;
- $stranswers .= $strmin. $delimiter.$calculated->min.'---';
- $stranswers .= $strmax.$delimiter.$calculated->max;
+ $stranswers .=get_string('trueanswerinsidelimits','qtype_calculated',$correcttrue);//' True answer :'.$calculated->trueanswer.' inside limits';
+ }
$stranswers .='<br/>';
}
}
@@ -869,6 +871,8 @@ function substitute_variables($str, $dataset) {
/// Calculate the correct answer
if (empty($formula)) {
$str = '';
+ } else if ($formula === '*'){
+ $str = '*';
} else {
eval('$str = '.$formula.';');
}
@@ -1088,8 +1092,12 @@ function qtype_calculated_calculate_answer($formula, $individualdata,
$answer = $QTYPES['calculated']->substitute_variables($formula, $individualdata);
if ('1' == $answerformat) { /* Answer is to have $answerlength decimals */
/*** Adjust to the correct number of decimals ***/
-
- $calculated->answer = round($answer, $answerlength);
+ if (stripos($answer,'e')>0 ){
+ $answerlengthadd = strlen($answer)-stripos($answer,'e');
+ }else {
+ $answerlengthadd = 0 ;
+ }
+ $calculated->answer = round(floatval($answer), $answerlength+$answerlengthadd);
if ($answerlength) {
/* Try to include missing zeros at the end */

0 comments on commit 3744e9e

Please sign in to comment.