| … | |
… | |
| 770 | sub check_strings { |
770 | sub check_strings { |
| 771 | my ($rh_ans, %options) = @_; |
771 | my ($rh_ans, %options) = @_; |
| 772 | |
772 | |
| 773 | # if the student's answer is a number, simply return the answer hash (unchanged). |
773 | # if the student's answer is a number, simply return the answer hash (unchanged). |
| 774 | |
774 | |
|
|
775 | |
| 775 | if ( $rh_ans->{student_ans} =~ m/[\d+\-*\/^(){}\[\]]|^\s*e\s*$|^\s*pi\s*$/) { |
776 | if ( $rh_ans->{student_ans} =~ m/[\d+\-*\/^(){}\[\]]|^\s*e\s*$|^\s*pi\s*$/) { |
| 776 | if ( $rh_ans->{answerIsString} == 1) { |
777 | if ( $rh_ans->{answerIsString} == 1) { |
| 777 | $rh_ans->throw_error('STRING','Incorrect Answer'); # student's answer is a number |
778 | #$rh_ans->throw_error('STRING','Incorrect Answer'); # student's answer is a number |
| 778 | } |
779 | } |
| 779 | return $rh_ans; |
780 | return $rh_ans; |
| 780 | } |
781 | } |
| 781 | # the student's answer is recognized as a string |
782 | # the student's answer is recognized as a string |
| 782 | my $ans = $rh_ans->{student_ans}; |
783 | my $ans = $rh_ans->{student_ans}; |
| … | |
… | |
| 823 | } |
824 | } |
| 824 | |
825 | |
| 825 | |
826 | |
| 826 | |
827 | |
| 827 | sub check_units { |
828 | sub check_units { |
|
|
829 | |
| 828 | my ($rh_ans, %options) = @_; |
830 | my ($rh_ans, %options) = @_; |
| 829 | |
831 | |
| 830 | |
|
|
| 831 | my %correct_units = %{$rh_ans-> {rh_correct_units}}; |
832 | my %correct_units = %{$rh_ans-> {rh_correct_units}}; |
| 832 | |
833 | |
| 833 | my $ans = $rh_ans->{student_ans}; |
834 | my $ans = $rh_ans->{student_ans}; |
| 834 | # $ans = '' unless defined ($ans); |
835 | # $ans = '' unless defined ($ans); |
| 835 | $ans = str_filters ($ans, 'trim_whitespace'); |
836 | $ans = str_filters ($ans, 'trim_whitespace'); |
| … | |
… | |
| 1402 | my $PGanswerMessage = ''; |
1403 | my $PGanswerMessage = ''; |
| 1403 | |
1404 | |
| 1404 | my ($inVal,$correctVal,$PG_eval_errors,$PG_full_error_report); |
1405 | my ($inVal,$correctVal,$PG_eval_errors,$PG_full_error_report); |
| 1405 | |
1406 | |
| 1406 | if (defined($correct_num_answer) && $correct_num_answer =~ /\S/ && $corrAnswerIsString == 0 ) { |
1407 | if (defined($correct_num_answer) && $correct_num_answer =~ /\S/ && $corrAnswerIsString == 0 ) { |
| 1407 | ($correctVal, $PG_eval_errors,$PG_full_error_report) = PG_answer_eval($correct_num_answer); |
1408 | ($correctVal, $PG_eval_errors,$PG_full_error_report) = PG_answer_eval($correct_num_answer); |
| 1408 | } |
1409 | } |
| 1409 | else { |
1410 | else { |
| 1410 | $PG_eval_errors = ' '; |
1411 | $PG_eval_errors = ' '; |
| 1411 | } |
1412 | } |
| 1412 | |
1413 | |
| … | |
… | |
| 1444 | |
1445 | |
| 1445 | $answer_evaluator->install_pre_filter(\&check_syntax); |
1446 | $answer_evaluator->install_pre_filter(\&check_syntax); |
| 1446 | |
1447 | |
| 1447 | $answer_evaluator->install_pre_filter(\&math_constants); |
1448 | $answer_evaluator->install_pre_filter(\&math_constants); |
| 1448 | |
1449 | |
| 1449 | |
|
|
| 1450 | |
|
|
| 1451 | |
|
|
| 1452 | if ($mode eq 'std') { |
1450 | if ($mode eq 'std') { |
| 1453 | # do nothing |
1451 | # do nothing |
| 1454 | } elsif ($mode eq 'strict') { |
1452 | } elsif ($mode eq 'strict') { |
| 1455 | $answer_evaluator->install_pre_filter(\&is_a_number); |
1453 | $answer_evaluator->install_pre_filter(\&is_a_number); |
| 1456 | } elsif ($mode eq 'arith') { |
1454 | } elsif ($mode eq 'arith') { |
| … | |
… | |
| 1463 | $formattedSubmittedAnswer = $in; |
1461 | $formattedSubmittedAnswer = $in; |
| 1464 | } |
1462 | } |
| 1465 | |
1463 | |
| 1466 | if ($corrAnswerIsString == 0 ){ # avoiding running compare_numbers when correct answer is a string. |
1464 | if ($corrAnswerIsString == 0 ){ # avoiding running compare_numbers when correct answer is a string. |
| 1467 | $answer_evaluator->install_evaluator(\&compare_numbers, %num_params); |
1465 | $answer_evaluator->install_evaluator(\&compare_numbers, %num_params); |
| 1468 | } |
1466 | } |
|
|
1467 | |
|
|
1468 | |
| 1469 | ############################################################################### |
1469 | ############################################################################### |
| 1470 | # We'll leave these next lines out for now, so that the evaluated versions of the student's and professor's |
1470 | # We'll leave these next lines out for now, so that the evaluated versions of the student's and professor's |
| 1471 | # can be displayed in the answer message. This may still cause a few anomolies when strings are used |
1471 | # can be displayed in the answer message. This may still cause a few anomolies when strings are used |
| 1472 | # |
1472 | # |
| 1473 | ############################################################################### |
1473 | ############################################################################### |
| … | |
… | |
| 1487 | $answer_evaluator; |
1487 | $answer_evaluator; |
| 1488 | } |
1488 | } |
| 1489 | |
1489 | |
| 1490 | sub fix_answers_for_display { |
1490 | sub fix_answers_for_display { |
| 1491 | my ($rh_ans, %options) = @_; |
1491 | my ($rh_ans, %options) = @_; |
|
|
1492 | if ( $rh_ans->{answerIsString} ==1) { |
|
|
1493 | $rh_ans = evaluatesToNumber ($rh_ans, %options); |
|
|
1494 | } |
| 1492 | if (defined ($rh_ans->{student_units})) { |
1495 | if (defined ($rh_ans->{student_units})) { |
| 1493 | $rh_ans->{student_ans} = $rh_ans->{student_ans}. ' '. $rh_ans->{student_units}; |
1496 | $rh_ans->{student_ans} = $rh_ans->{student_ans}. ' '. $rh_ans->{student_units}; |
| 1494 | } |
1497 | } |
| 1495 | $rh_ans->{correct_ans} = $rh_ans->{original_correct_ans}; |
1498 | $rh_ans->{correct_ans} = $rh_ans->{original_correct_ans}; |
| 1496 | $rh_ans; |
1499 | $rh_ans; |
| 1497 | } |
1500 | } |
| 1498 | |
1501 | |
|
|
1502 | |
|
|
1503 | sub evaluatesToNumber { |
|
|
1504 | my ($rh_ans, %options) = @_; |
|
|
1505 | my $testString = $rh_ans->{student_ans}; |
|
|
1506 | my $is_a_number = 0; |
|
|
1507 | my ($inVal,$PG_eval_errors,$PG_full_error_report) = PG_answer_eval($rh_ans->{student_ans}); |
|
|
1508 | if ($PG_eval_errors) { |
|
|
1509 | $is_a_number = 0; |
|
|
1510 | } else { |
|
|
1511 | $is_a_number = 1; |
|
|
1512 | $rh_ans->{student_ans} = prfmt($inVal,$options{format}); |
|
|
1513 | } |
|
|
1514 | |
|
|
1515 | $rh_ans; |
|
|
1516 | } |
| 1499 | |
1517 | |
| 1500 | |
1518 | |
| 1501 | ########################################################################## |
1519 | ########################################################################## |
| 1502 | ########################################################################## |
1520 | ########################################################################## |
| 1503 | ## Function answer evaluators |
1521 | ## Function answer evaluators |