| … | |
… | |
| 3463 | check_strings ($rh_ans, %options) |
3463 | check_strings ($rh_ans, %options) |
| 3464 | returns $rh_ans |
3464 | returns $rh_ans |
| 3465 | |
3465 | |
| 3466 | =cut |
3466 | =cut |
| 3467 | |
3467 | |
|
|
3468 | # Changed logic to: |
|
|
3469 | # if student answer matches a "strings" value, treat it as a string |
|
|
3470 | # and mark it right/wrong depending on if it matches the right answer |
|
|
3471 | # if it is not an official string, treat it as a number |
|
|
3472 | # This makes it so that strings=>['I', '-I'] will work regardless of |
|
|
3473 | # whether one of these is the right answer or not |
|
|
3474 | |
| 3468 | sub check_strings { |
3475 | sub check_strings { |
| 3469 | my ($rh_ans, %options) = @_; |
3476 | my ($rh_ans, %options) = @_; |
| 3470 | |
3477 | |
| 3471 | # if the student's answer is a number, simply return the answer hash (unchanged). |
3478 | # We treat the student answer as a string iff it matches an acceptable string |
| 3472 | |
3479 | my $sensibleAnswer=0; |
| 3473 | if ( $rh_ans->{student_ans} =~ m/[\d+\-*\/^(){}\[\]]|^\s*e\s*$|^\s*pi\s*$/) { |
3480 | my $ans = $rh_ans->{student_ans}; |
| 3474 | if ( $rh_ans->{answerIsString} == 1) { |
3481 | $ans = str_filters( $ans, 'compress_whitespace' ); # remove trailing, leading, and double spaces. |
| 3475 | #$rh_ans->throw_error('STRING','Incorrect Answer'); # student's answer is a number |
3482 | my $legalString; # find out if string makes sense |
|
|
3483 | my @legalStrings = @{$options{strings}}; |
|
|
3484 | foreach $legalString (@legalStrings) { |
|
|
3485 | if ( uc($ans) eq uc($legalString) ) { |
|
|
3486 | $sensibleAnswer = 1; |
|
|
3487 | last; |
| 3476 | } |
3488 | } |
| 3477 | return $rh_ans; |
|
|
| 3478 | } |
3489 | } |
| 3479 | # the student's answer is recognized as a string |
3490 | $sensibleAnswer = 1 unless $ans =~ /\S/; ## empty answers are sensible |
| 3480 | my $ans = $rh_ans->{student_ans}; |
3491 | if(! $sensibleAnswer) { return $rh_ans; } |
| 3481 | |
3492 | |
| 3482 | # OVERVIEW of reminder of function: |
3493 | # OVERVIEW of reminder of function: |
| 3483 | # if answer is correct, return correct. (adjust score to 1) |
3494 | # if answer is correct, return correct. (adjust score to 1) |
| 3484 | # if answer is incorect: |
3495 | # if answer is incorect: |
| 3485 | # 1) determine if the answer is sensible. if it is, return incorrect. |
3496 | # 1) determine if the answer is sensible. if it is, return incorrect. |
| 3486 | # 2) if the answer is not sensible (and incorrect), then return an error message indicating so. |
3497 | # 2) if the answer is not sensible (and incorrect), then return an error message indicating so. |
| 3487 | # no matter what: throw a 'STRING' error to skip numerical evaluations. (error flag skips remainder of pre_filters and evaluators) |
3498 | # no matter what: throw a 'STRING' error to skip numerical evaluations. (error flag skips remainder of pre_filters and evaluators) |
| 3488 | # last: 'STRING' post_filter will clear the error (avoiding pink screen.) |
3499 | # last: 'STRING' post_filter will clear the error (avoiding pink screen.) |
| 3489 | |
3500 | |
| 3490 | my $sensibleAnswer = 0; |
3501 | # we only get this far if the student's answer was sensible |
| 3491 | $ans = str_filters( $ans, 'compress_whitespace' ); # remove trailing, leading, and double spaces. |
3502 | |
| 3492 | my ($ans_eval) = str_cmp($rh_ans->{correct_ans}); |
3503 | my ($ans_eval) = str_cmp($rh_ans->{correct_ans}); |
| 3493 | my $temp_ans_hash = &$ans_eval($ans); |
3504 | my $temp_ans_hash = &$ans_eval($ans); |
| 3494 | $rh_ans->{test} = $temp_ans_hash; |
3505 | $rh_ans->{test} = $temp_ans_hash; |
| 3495 | if ($temp_ans_hash->{score} ==1 ) { # students answer matches the correct answer. |
3506 | if ($temp_ans_hash->{score} ==1 ) { # students answer matches the correct answer. |
| 3496 | $rh_ans->{score} = 1; |
3507 | $rh_ans->{score} = 1; |
| 3497 | $sensibleAnswer = 1; |
|
|
| 3498 | } else { # students answer does not match the correct answer. |
|
|
| 3499 | my $legalString = ''; # find out if string makes sense |
|
|
| 3500 | my @legalStrings = @{$options{strings}}; |
|
|
| 3501 | foreach $legalString (@legalStrings) { |
|
|
| 3502 | if ( uc($ans) eq uc($legalString) ) { |
|
|
| 3503 | $sensibleAnswer = 1; |
|
|
| 3504 | last; |
|
|
| 3505 | } |
|
|
| 3506 | } |
|
|
| 3507 | $sensibleAnswer = 1 unless $ans =~ /\S/; ## empty answers are sensible |
|
|
| 3508 | $rh_ans->throw_error('EVAL', "Your answer is not a recognized answer") unless ($sensibleAnswer); |
|
|
| 3509 | # $temp_ans_hash -> setKeys( 'ans_message' => 'Your answer is not a recognized answer' ) unless ($sensibleAnswer); |
|
|
| 3510 | # $temp_ans_hash -> setKeys( 'student_ans' => uc($ans) ); |
|
|
| 3511 | } |
3508 | } |
| 3512 | $rh_ans->{student_ans} = $ans; |
3509 | $rh_ans->{student_ans} = $ans; |
| 3513 | if ($sensibleAnswer) { |
|
|
| 3514 | $rh_ans->throw_error('STRING', "The student's answer $rh_ans->{student_ans} is interpreted as a string."); |
3510 | $rh_ans->throw_error('STRING', "The student's answer $rh_ans->{student_ans} is interpreted as a string."); |
| 3515 | } |
|
|
| 3516 | # warn ("\$rh_ans->{answerIsString} = $rh_ans->{answerIsString}"); |
3511 | # warn ("\$rh_ans->{answerIsString} = $rh_ans->{answerIsString}"); |
| 3517 | $rh_ans; |
3512 | $rh_ans; |
| 3518 | } |
3513 | } |
| 3519 | |
3514 | |
| 3520 | =head4 check_units |
3515 | =head4 check_units |