From: dpvc v. a. <we...@ma...> - 2005-07-04 20:21:45
|
Log Message: ----------- Folded in Mike's changes to checkbox_cmp. Modified Files: -------------- pg/lib/Parser/Legacy: PGanswermacros.pl Revision Data ------------- Index: PGanswermacros.pl =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Legacy/PGanswermacros.pl,v retrieving revision 1.1 retrieving revision 1.2 diff -Llib/Parser/Legacy/PGanswermacros.pl -Llib/Parser/Legacy/PGanswermacros.pl -u -r1.1 -r1.2 --- lib/Parser/Legacy/PGanswermacros.pl +++ lib/Parser/Legacy/PGanswermacros.pl @@ -2483,6 +2483,7 @@ sub ignore_order { my $rh_ans = shift; die "expected an answer hash" unless ref($rh_ans)=~/HASH/i; + $rh_ans->{_filter_name} = 'ignore_order'; $rh_ans->{student_ans} = join( "", lex_sort( split( /\s*/, $rh_ans->{student_ans} ) ) ); $rh_ans->{correct_ans} = join( "", lex_sort( split( /\s*/, $rh_ans->{correct_ans} ) ) ); @@ -2927,40 +2928,117 @@ # rather than as a \0 delimited string. sub checkbox_cmp { my $correctAnswer = shift @_; - $correctAnswer = str_filters( $correctAnswer, 'ignore_order' ); - - my $answer_evaluator = sub { - my $in = shift @_; - $in = '' unless defined $in; #in case no boxes checked - # multiple answers could come in two forms - # either a \0 delimited string or - # an array reference. We handle both. - if (ref($in) eq 'ARRAY') { - $in = join("",@{$in}); # convert array to single no-delimiter string - } else { - my @temp = split( "\0", $in ); #convert "\0"-delimited string to array... - $in = join( "", @temp ); #and then to a single no-delimiter string - } - my $original_student_ans = $in; #well, almost original - $in = str_filters( $in, 'ignore_order' ); - - my $correctQ = ($in eq $correctAnswer) ? 1: 0; - - my $ans_hash = new AnswerHash( - 'score' => $correctQ, - 'correct_ans' => "$correctAnswer", - 'student_ans' => $in, - 'ans_message' => "", - 'type' => "checkbox_cmp", - 'preview_text_string' => $in, - 'preview_latex_string' => $in, - 'original_student_ans' => $original_student_ans - ); - return $ans_hash; - - }; + my %options = @_; + assign_option_aliases( \%options, + ); + set_default_options( \%options, + 'debug' => 0, + 'type' => 'checkbox_cmp', + ); + my $answer_evaluator = new AnswerEvaluator( + correct_ans => $correctAnswer, + type => $options{type}, + ); + # pass along debug requests + $answer_evaluator->{debug} = $options{debug}; + + # join student answer array into a single string if necessary + $answer_evaluator->install_pre_filter(sub { + my $rh_ans = shift; + $rh_ans->{_filter_name} = 'convert student_ans to string'; + $rh_ans->{student_ans} = join("", @{$rh_ans->{student_ans}}) + if ref($rh_ans->{student_ans}) =~/ARRAY/i; + $rh_ans; + }); + # ignore order of check boxes + $answer_evaluator->install_pre_filter(\&ignore_order); + # compare as strings + $answer_evaluator->install_evaluator(sub { + my $rh_ans = shift; + $rh_ans->{_filter_name} = 'compare strings generated by checked boxes'; + $rh_ans->{score} = ($rh_ans->{student_ans} eq $rh_ans->{correct_ans}) ? 1 : 0; + $rh_ans; + }); + # fix up preview displays + $answer_evaluator->install_post_filter( sub { + my $rh_ans = shift; + $rh_ans->{_filter_name} = 'adjust preview strings'; + $rh_ans->{type} = $options{type}; + $rh_ans->{preview_text_string} = '\\text{'.$rh_ans->{student_ans}.'}', + $rh_ans->{preview_latex_string} = '\\text{'.$rh_ans->{student_ans}.'}', + $rh_ans; + + + }); + +# my $answer_evaluator = sub { +# my $in = shift @_; +# $in = '' unless defined $in; #in case no boxes checked +# # multiple answers could come in two forms +# # either a \0 delimited string or +# # an array reference. We handle both. +# if (ref($in) eq 'ARRAY') { +# $in = join("",@{$in}); # convert array to single no-delimiter string +# } else { +# my @temp = split( "\0", $in ); #convert "\0"-delimited string to array... +# $in = join( "", @temp ); #and then to a single no-delimiter string +# } +# my $original_student_ans = $in; #well, almost original +# $in = str_filters( $in, 'ignore_order' ); +# +# my $correctQ = ($in eq $correctAnswer) ? 1: 0; +# +# my $ans_hash = new AnswerHash( +# 'score' => $correctQ, +# 'correct_ans' => "$correctAnswer", +# 'student_ans' => $in, +# 'ans_message' => "", +# 'type' => "checkbox_cmp", +# 'preview_text_string' => $in, +# 'preview_latex_string' => $in, +# 'original_student_ans' => $original_student_ans +# ); +# return $ans_hash; +# +# }; return $answer_evaluator; } +# sub checkbox_cmp { +# my $correctAnswer = shift @_; +# $correctAnswer = str_filters( $correctAnswer, 'ignore_order' ); +# +# my $answer_evaluator = sub { +# my $in = shift @_; +# $in = '' unless defined $in; #in case no boxes checked +# # multiple answers could come in two forms +# # either a \0 delimited string or +# # an array reference. We handle both. +# if (ref($in) eq 'ARRAY') { +# $in = join("",@{$in}); # convert array to single no-delimiter string +# } else { +# my @temp = split( "\0", $in ); #convert "\0"-delimited string to array... +# $in = join( "", @temp ); #and then to a single no-delimiter string +# } +# my $original_student_ans = $in; #well, almost original +# $in = str_filters( $in, 'ignore_order' ); +# +# my $correctQ = ($in eq $correctAnswer) ? 1: 0; +# +# my $ans_hash = new AnswerHash( +# 'score' => $correctQ, +# 'correct_ans' => "$correctAnswer", +# 'student_ans' => $in, +# 'ans_message' => "", +# 'type' => "checkbox_cmp", +# 'preview_text_string' => $in, +# 'preview_latex_string' => $in, +# 'original_student_ans' => $original_student_ans +# ); +# return $ans_hash; +# +# }; +# return $answer_evaluator; +# } #added 6/28/2000 by David Etlinger #exactly the same as strict_str_cmp, |