[system] / trunk / pg / macros / PGanswermacros.pl Repository:
ViewVC logotype

Diff of /trunk/pg/macros/PGanswermacros.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 3345 Revision 3346
2171} 2171}
2172 2172
2173sub ignore_order { 2173sub ignore_order {
2174 my $rh_ans = shift; 2174 my $rh_ans = shift;
2175 die "expected an answer hash" unless ref($rh_ans)=~/HASH/i; 2175 die "expected an answer hash" unless ref($rh_ans)=~/HASH/i;
2176 $rh_ans->{_filter_name} = 'ignore_order';
2176 $rh_ans->{student_ans} = join( "", lex_sort( split( /\s*/, $rh_ans->{student_ans} ) ) ); 2177 $rh_ans->{student_ans} = join( "", lex_sort( split( /\s*/, $rh_ans->{student_ans} ) ) );
2177 $rh_ans->{correct_ans} = join( "", lex_sort( split( /\s*/, $rh_ans->{correct_ans} ) ) ); 2178 $rh_ans->{correct_ans} = join( "", lex_sort( split( /\s*/, $rh_ans->{correct_ans} ) ) );
2178 2179
2179 return $rh_ans; 2180 return $rh_ans;
2180} 2181}
2615#added 2/26/2003 by Mike Gage 2616#added 2/26/2003 by Mike Gage
2616# handled the case where multiple answers are passed as an array reference 2617# handled the case where multiple answers are passed as an array reference
2617# rather than as a \0 delimited string. 2618# rather than as a \0 delimited string.
2618sub checkbox_cmp { 2619sub checkbox_cmp {
2619 my $correctAnswer = shift @_; 2620 my $correctAnswer = shift @_;
2620 $correctAnswer = str_filters( $correctAnswer, 'ignore_order' ); 2621 my %options = @_;
2621 2622 assign_option_aliases( \%options,
2623 );
2624 set_default_options( \%options,
2625 'debug' => 0,
2626 'type' => 'checkbox_cmp',
2627 );
2628 my $answer_evaluator = new AnswerEvaluator(
2629 correct_ans => $correctAnswer,
2630 type => $options{type},
2631 );
2632 # pass along debug requests
2633 $answer_evaluator->{debug} = $options{debug};
2634
2635 # join student answer array into a single string if necessary
2636 $answer_evaluator->install_pre_filter(sub {
2637 my $rh_ans = shift;
2638 $rh_ans->{_filter_name} = 'convert student_ans to string';
2639 $rh_ans->{student_ans} = join("", @{$rh_ans->{student_ans}})
2640 if ref($rh_ans->{student_ans}) =~/ARRAY/i;
2641 $rh_ans;
2642 });
2643 # ignore order of check boxes
2644 $answer_evaluator->install_pre_filter(\&ignore_order);
2645 # compare as strings
2646 $answer_evaluator->install_evaluator(sub {
2647 my $rh_ans = shift;
2648 $rh_ans->{_filter_name} = 'compare strings generated by checked boxes';
2649 $rh_ans->{score} = ($rh_ans->{student_ans} eq $rh_ans->{correct_ans}) ? 1 : 0;
2650 $rh_ans;
2651 });
2652 # fix up preview displays
2653 $answer_evaluator->install_post_filter( sub {
2654 my $rh_ans = shift;
2655 $rh_ans->{_filter_name} = 'adjust preview strings';
2656 $rh_ans->{type} = $options{type};
2657 $rh_ans->{preview_text_string} = '\\text{'.$rh_ans->{student_ans}.'}',
2658 $rh_ans->{preview_latex_string} = '\\text{'.$rh_ans->{student_ans}.'}',
2659 $rh_ans;
2660
2661
2662 });
2663
2622 my $answer_evaluator = sub { 2664# my $answer_evaluator = sub {
2623 my $in = shift @_; 2665# my $in = shift @_;
2624 $in = '' unless defined $in; #in case no boxes checked 2666# $in = '' unless defined $in; #in case no boxes checked
2625 # multiple answers could come in two forms 2667# # multiple answers could come in two forms
2626 # either a \0 delimited string or 2668# # either a \0 delimited string or
2627 # an array reference. We handle both. 2669# # an array reference. We handle both.
2628 if (ref($in) eq 'ARRAY') { 2670# if (ref($in) eq 'ARRAY') {
2629 $in = join("",@{$in}); # convert array to single no-delimiter string 2671# $in = join("",@{$in}); # convert array to single no-delimiter string
2630 } else { 2672# } else {
2631 my @temp = split( "\0", $in ); #convert "\0"-delimited string to array... 2673# my @temp = split( "\0", $in ); #convert "\0"-delimited string to array...
2632 $in = join( "", @temp ); #and then to a single no-delimiter string 2674# $in = join( "", @temp ); #and then to a single no-delimiter string
2633 } 2675# }
2634 my $original_student_ans = $in; #well, almost original 2676# my $original_student_ans = $in; #well, almost original
2635 $in = str_filters( $in, 'ignore_order' ); 2677# $in = str_filters( $in, 'ignore_order' );
2636 2678#
2637 my $correctQ = ($in eq $correctAnswer) ? 1: 0; 2679# my $correctQ = ($in eq $correctAnswer) ? 1: 0;
2638 2680#
2639 my $ans_hash = new AnswerHash( 2681# my $ans_hash = new AnswerHash(
2640 'score' => $correctQ, 2682# 'score' => $correctQ,
2641 'correct_ans' => "$correctAnswer", 2683# 'correct_ans' => "$correctAnswer",
2642 'student_ans' => $in, 2684# 'student_ans' => $in,
2643 'ans_message' => "", 2685# 'ans_message' => "",
2644 'type' => "checkbox_cmp", 2686# 'type' => "checkbox_cmp",
2645 'preview_text_string' => $in, 2687# 'preview_text_string' => $in,
2646 'preview_latex_string' => $in, 2688# 'preview_latex_string' => $in,
2647 'original_student_ans' => $original_student_ans 2689# 'original_student_ans' => $original_student_ans
2648 ); 2690# );
2649 return $ans_hash; 2691# return $ans_hash;
2650 2692#
2651 }; 2693# };
2652 return $answer_evaluator; 2694 return $answer_evaluator;
2653} 2695}
2696# sub checkbox_cmp {
2697# my $correctAnswer = shift @_;
2698# $correctAnswer = str_filters( $correctAnswer, 'ignore_order' );
2699#
2700# my $answer_evaluator = sub {
2701# my $in = shift @_;
2702# $in = '' unless defined $in; #in case no boxes checked
2703# # multiple answers could come in two forms
2704# # either a \0 delimited string or
2705# # an array reference. We handle both.
2706# if (ref($in) eq 'ARRAY') {
2707# $in = join("",@{$in}); # convert array to single no-delimiter string
2708# } else {
2709# my @temp = split( "\0", $in ); #convert "\0"-delimited string to array...
2710# $in = join( "", @temp ); #and then to a single no-delimiter string
2711# }
2712# my $original_student_ans = $in; #well, almost original
2713# $in = str_filters( $in, 'ignore_order' );
2714#
2715# my $correctQ = ($in eq $correctAnswer) ? 1: 0;
2716#
2717# my $ans_hash = new AnswerHash(
2718# 'score' => $correctQ,
2719# 'correct_ans' => "$correctAnswer",
2720# 'student_ans' => $in,
2721# 'ans_message' => "",
2722# 'type' => "checkbox_cmp",
2723# 'preview_text_string' => $in,
2724# 'preview_latex_string' => $in,
2725# 'original_student_ans' => $original_student_ans
2726# );
2727# return $ans_hash;
2728#
2729# };
2730# return $answer_evaluator;
2731# }
2654 2732
2655#added 6/28/2000 by David Etlinger 2733#added 6/28/2000 by David Etlinger
2656#exactly the same as strict_str_cmp, 2734#exactly the same as strict_str_cmp,
2657#but more intuitive to the user 2735#but more intuitive to the user
2658 2736

Legend:
Removed from v.3345  
changed lines
  Added in v.3346

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9