From: dpvc v. a. <we...@ma...> - 2005-10-10 23:47:49
|
Log Message: ----------- Add a new context flag that controls how the student answer should be displayed. The formatStudentAnswer flag can be set to 'evaluated' (the default), which shows the final numeric answer; 'parsed', which shows the fully parsed version (including extra parentheses for clarity); or 'reduced', which performs constant operations, but doesn't perform function evaluations or named-constant substitutions. For example, if the student answers 1+2+sqrt(3), then 'evaluated' will produce 4.73205, 'reduced' will show 3+sqrt(3), and 'parsed' will show 1+2+sqrt(3). Modified Files: -------------- pg/lib/Parser/Context: Default.pm pg/lib/Value: AnswerChecker.pm Revision Data ------------- Index: Default.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Parser/Context/Default.pm,v retrieving revision 1.32 retrieving revision 1.33 diff -Llib/Parser/Context/Default.pm -Llib/Parser/Context/Default.pm -u -r1.32 -r1.33 --- lib/Parser/Context/Default.pm +++ lib/Parser/Context/Default.pm @@ -196,6 +196,7 @@ reduceConstants => 1, # 1 = automatically combine constants reduceConstantFunctions => 1, # 1 = compute function values of constants showExtraParens => 0, # 1 = make things painfully unambiguous + formatStudentAnswer => 'evaluated', # or 'parsed' or 'reduced' }; ############################################################################ Index: AnswerChecker.pm =================================================================== RCS file: /webwork/cvs/system/pg/lib/Value/AnswerChecker.pm,v retrieving revision 1.68 retrieving revision 1.69 diff -Llib/Value/AnswerChecker.pm -Llib/Value/AnswerChecker.pm -u -r1.68 -r1.69 --- lib/Value/AnswerChecker.pm +++ lib/Value/AnswerChecker.pm @@ -31,6 +31,28 @@ )} # +# Special Context flags to be set for the student answer +# +sub cmp_contextFlags { + my $self = shift; my $ans = shift; + return ( + StringifyAsTeX => 0, # reset this, just in case. + no_parameters => 1, # don't let students enter parameters + showExtraParens => 1, # make student answer painfully unambiguous + reduceConstants => 0, # don't combine student constants + reduceConstantFunctions => 0, # don't reduce constant functions + ($ans->{studentsMustReduceUnions} ? + (reduceUnions => 0, reduceSets => 0, + reduceUnionsForComparison => $ans->{showUnionReduceWarnings}, + reduceSetsForComparison => $ans->{showUnionReduceWarnings}) : + (reduceUnions => 1, reduceSets => 1, + reduceUnionsForComparison => 1, reduceSetsForComparison => 1)), + ($ans->{requireParenMatch}? (): ignoreEndpointTypes => 1), # for Intervals + ); +} + + +# # Create an answer checker for the given type of object # sub cmp { @@ -67,21 +89,7 @@ my $current = $$Value::context; # save it for later my $context = $ans->{correct_value}{context} || $current; Parser::Context->current(undef,$context); # change to correct answser's context - my $flags = contextSet($context, # save old context flags for the below - StringifyAsTeX => 0, # reset this, just in case. - no_parameters => 1, # don't let students enter parameters - showExtraParens => 1, # make student answer painfully unambiguous - reduceConstants => 0, # don't combine student constants - reduceConstantFunctions => 0, # don't reduce constant functions - ($ans->{studentsMustReduceUnions} ? - (reduceUnions => 0, reduceSets => 0, - reduceUnionsForComparison => $ans->{showUnionReduceWarnings}, - reduceSetsForComparison => $ans->{showUnionReduceWarnings}) : - (reduceUnions => 1, reduceSets => 1, - reduceUnionsForComparison => 1, reduceSetsForComparison => 1)), - ($ans->{requireParenMatch}? (): ignoreEndpointTypes => 1), # for Intervals - $self->cmp_contextFlags($ans), # any additional ones from the object itself - ); + my $flags = contextSet($context,$self->cmp_contextFlags($ans)); # save old context flags my $inputs = $self->getPG('$inputs_ref',{action=>""}); $ans->{isPreview} = $inputs->{previewAnswers} || ($inputs->{action} =~ m/^Preview/); $ans->{cmp_class} = $self->cmp_class($ans) unless $ans->{cmp_class}; @@ -105,7 +113,19 @@ unless Value::isValue($ans->{student_value}); $ans->{preview_latex_string} = $ans->{student_formula}->TeX; $ans->{preview_text_string} = protectHTML($ans->{student_formula}->string); - $ans->{student_ans} = protectHTML($ans->{student_value}->string); + # + # Get the string for the student answer + # + for ($ans->{formatStudentAnswer} || $context->flag('formatStudentAnswer')) { + /evaluated/i and do {$ans->{student_ans} = protectHTML($ans->{student_value}->string); last}; + /parsed/i and do {$ans->{student_ans} = $ans->{preview_text_string}; last}; + /reduced/i and do { + my $oldFlags = contextSet($context,reduceConstants=>1,reduceConstantFunctions=>0); + $ans->{student_ans} = protectHTML($ans->{student_formula}->substitute()->string); + contextSet($context,%{$oldFags}); last; + }; + warn "Unkown student answer format |$ans->{formatStudentAnswer}|"; + } if ($self->cmp_collect($ans)) { $self->cmp_equal($ans); $self->cmp_postprocess($ans) if !$ans->{error_message}; @@ -248,7 +268,6 @@ # filled in by sub-classes # sub cmp_postprocess {} -sub cmp_contextFlags {return ()} # # Check for unreduced reduced Unions and Sets |