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

View of /trunk/pg/macros/parserMultiPart.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3632 - (download) (as text) (annotate)
Sat Sep 17 13:22:19 2005 UTC (14 years, 5 months ago) by dpvc
File size: 14476 byte(s)
Improved layout and labeling of error messages.

    1 sub _parserMultiPart_init {}
    2 
    3 #
    4 #  MultiPart objects let you tie several answer blanks to a single
    5 #  answer checker, so you can have the answer in one blank influence
    6 #  the answer in another.  The MultiPart can produce either a single
    7 #  result in the answer results area, or a separate result for each
    8 #  blank.
    9 #
   10 #  To create a MultiPart pass a list of answers to MultiPart() in the
   11 #  order they will appear in the problem.  For example:
   12 #
   13 #    $mp = MultiPart("x^2",-1,1);
   14 #
   15 #  or
   16 #
   17 #    $mp = MultiPart(Vector(1,1,1),Vector(2,2,2))->with(singleResult=>1);
   18 #
   19 #  Then, use $mp->ans_rule to create answer blanks for the various parts
   20 #  just as you would ans_rule.  You can pass the width of the blank, which
   21 #  defaults to 20 otherwise.  For example:
   22 #
   23 #    BEGIN_TEXT
   24 #      \(f(x)\) = \{$mp->ans_rule(20)\} produces the same value
   25 #      at \(x\) = \{$mp->ans_rule(10)\} as it does at \(x\) = \{$mp->ans_rule(10)\}.
   26 #    END_TEXT
   27 #
   28 #  Finally, call $mp->cmp to produce the answer checker(s) used in the MultiPart.
   29 #  You need to provide a checker routine that will be called to determine if the
   30 #  answers are correct or not.  The checker will only be called if the student
   31 #  answers have no syntax errors and their types match the types of the professor's
   32 #  answers, so you don't ahve to worry about handling bad data from the student
   33 #  (at least as far as typechecking goes).
   34 #
   35 #  The checker routine should accept three parameters:  a reference to the array
   36 #  of correct answers, a reference to the array of student answers, and a reference
   37 #  to the MultiPart itself.  It should do whatever checking it needs to do and
   38 #  then return a score for the MultiPart as a whole (every answer blank will be
   39 #  given the same score), or a reference to an array of scores, one for each
   40 #  blank.  The routine can set error messages via the MultiPart's setMessage()
   41 #  method (e.g., $mp->setMessage(1,"The function can't be the identity") would
   42 #  set the message for the first answer blank of the MultiPart), or can call
   43 #  Value::Error() to generate an error and die.
   44 #
   45 #  The checker routine can be supplied either when the MultiPart is created, or
   46 #  when the cmp() method is called.  For example:
   47 #
   48 #      $mp = MultiPart("x^2",1,-1)->with(
   49 #        singleResult => 1,
   50 #        checker => sub {
   51 #          my ($correct,$student,$self) = @_;  # get the parameters
   52 #          my ($f,$x1,$x2) = @{$student};      # extract the student answers
   53 #          Value::Error("Function can't be the identity") if ($f == 'x');
   54 #          Value::Error("Function can't be constant") if ($f->isConstant);
   55 #          return $f->eval(x=>$x1) == $f->eval(x=>$x2);
   56 #        },
   57 #      );
   58 #           .
   59 #           .
   60 #           .
   61 #      ANS($mp->cmp);
   62 #
   63 #  or
   64 #
   65 #      $mp = MultiPart("x^2",1,-1)->with(singleResult=>1);
   66 #      sub check {
   67 #        my ($correct,$student,$self) = @_;  # get the parameters
   68 #        my ($f,$x1,$x2) = @{$student};      # extract the student answers
   69 #        Value::Error("Function can't be the identity") if ($f == 'x');
   70 #        Value::Error("Function can't be constant") if ($f->isConstant);
   71 #        return $f->eval(x=>$x1) == $f->eval(x=>$x2);
   72 #      };
   73 #           .
   74 #           .
   75 #           .
   76 #      ANS($mp->cmp(checker=>~~&check));
   77 #
   78 ######################################################################
   79 
   80 package MultiPart;
   81 our @ISA = qw(Value);
   82 
   83 our $count = 0;                      # counter for unique identifier for multi-parts
   84 our $answerPrefix = "MuLtIpArT";     # answer rule prefix
   85 our $separator = ';';                # separator for singleResult previews
   86 
   87 #
   88 #  Create a new MultiPart item from a list of items.
   89 #  The items are converted if Value items, if they aren't already.
   90 #  You can set the following fields of the resulting item:
   91 #
   92 #      checker => code            a subroutine to be called to check the
   93 #                                 student answers.  The routine is passed
   94 #                                 three parameters: a reference to the array
   95 #                                 or correct answers, a reference to the
   96 #                                 array of student answers, and a reference
   97 #                                 to the MultiPart object itself.  The routine
   98 #                                 should return either a score or an array of
   99 #                                 scores (one for each student answer).
  100 #
  101 #      singleResult => 0 or 1     whether to show only one entry in the
  102 #                                 results area at the top of the page,
  103 #                                 or one for each answer rule.
  104 #                                 (Default: 0)
  105 #
  106 #      namedRules => 0 or 1       wether to use named rules or default
  107 #                                 rule names.  Use named rules if you need
  108 #                                 to intersperse other rules with the
  109 #                                 ones for the MultiPart, in which case
  110 #                                 you must use NAMED_ANS not ANS.
  111 #                                 (Default: 0)
  112 #
  113 #      checkTypes => 0 or 1       whether the types of the student and
  114 #                                 professor's answers must match exactly
  115 #                                 or just pass the usual type-match error
  116 #                                 checking (in which case, you should check
  117 #                                 the types before you use the data).
  118 #                           (Default: 1)
  119 #
  120 #      separator => string        the string to use between entries in the
  121 #                                 results area when singleResult is set.
  122 #
  123 #      tex_separator => string    same, but for the preview area.
  124 #
  125 my @ans_defaults = (
  126   checker => sub {0},
  127   showCoordinateHints => 0,
  128   showEndpointHints => 0,
  129   showEndTypeHints => 0,
  130 );
  131 
  132 sub new {
  133   my $self = shift; my $class = ref($self) || $self;
  134   my @data = @_; my @cmp;
  135   Value::Error("%s lists can't be empty",$class) if scalar(@data) == 0;
  136   foreach my $x (@data) {
  137     $x = Value::makeValue($x) unless Value::isValue($x);
  138     push(@cmp,$x->cmp(@ans_defaults));
  139   }
  140   bless {
  141     data => [@data], cmp => [@cmp], ans => [],
  142     part => 0, singleResult => 0, namedRules => 0, checkTypes => 1,
  143     tex_separator => $separator.'\,', separator => $separator.' ',
  144     context => $$Value::context, id => $answerPrefix.($count++),
  145   }, $class;
  146 }
  147 
  148 #
  149 #  Creates an answer checker (or array of same) to be passed
  150 #  to ANS() or NAMED_ANS().  Any parameters are passed to
  151 #  the individual answer checkers.
  152 #
  153 sub cmp {
  154   my $self = shift; my %options = @_;
  155   foreach my $id ('checker','separator') {
  156     if (defined($options{$id})) {
  157       $self->{$id} = $options{$id};
  158       delete $options{$id};
  159     }
  160   }
  161   die "You must supply a checker subroutine" unless ref($self->{checker}) eq 'CODE';
  162   my @cmp = ();
  163   if ($self->{singleResult}) {
  164     push(@cmp,$self->ANS_NAME(0)) if $self->{namedRules};
  165     push(@cmp,$self->single_cmp(%options));
  166   } else {
  167     foreach my $i (0..$self->length-1) {
  168       push(@cmp,$self->ANS_NAME($i)) if $self->{namedRules};
  169       push(@cmp,$self->entry_cmp($i,%options));
  170     }
  171   }
  172   return @cmp;
  173 }
  174 
  175 ######################################################################
  176 
  177 #
  178 #  Get the answer checker used for when all the answers are treated
  179 #  as a single result.
  180 #
  181 sub single_cmp {
  182   my $self = shift; my @correct;
  183   foreach my $cmp (@{$self->{cmp}}) {push(@correct,$cmp->{rh_ans}{correct_ans})}
  184   my $ans = new AnswerEvaluator;
  185   $ans->ans_hash(
  186     correct_ans => join($self->{separator},@correct),
  187     type        => "MultiPart",
  188     @_,
  189   );
  190   $ans->install_evaluator(sub {my $ans = shift; (shift)->single_check($ans)},$self);
  191   $ans->install_pre_filter('erase'); # don't do blank check
  192   return $ans;
  193 }
  194 
  195 #
  196 #  Check the answers when they are treated as a single result.
  197 #
  198 #    First, call individual answer checkers to get any type-check errors
  199 #    Then perform the user's checker routine
  200 #    Finally collect the individual answers and errors and combine
  201 #      them for the single result.
  202 #
  203 sub single_check {
  204   my $self = shift; my $ans = shift;
  205   my $inputs = $main::inputs_ref;
  206   $self->{ans}[0] = $self->{cmp}[0]->evaluate($ans->{student_ans});
  207   foreach my $i (1..$self->length-1)
  208     {$self->{ans}[$i] = $self->{cmp}[$i]->evaluate($inputs->{$self->ANS_NAME($i)})}
  209   my $score = 0; my (@errors,@student,@latex,@text);
  210   my $i = 0; my $nonblank = 0;
  211   if ($self->perform_check) {
  212     push(@errors,$self->{ans}[0]{ans_message});
  213     $self->{ans}[0]{ans_message} = "";
  214   }
  215   foreach my $result (@{$self->{ans}}) {
  216     $i++; $nonblank |= ($result->{student_ans} =~ m/\S/);
  217     push(@latex,check_string($result->{preview_latex_string},'\_\_'));
  218     push(@text,check_string($result->{preview_text_string},'__'));
  219     push(@student,check_string($result->{student_ans},'__'));
  220     if ($result->{ans_message}) {
  221       push(@errors,'<TR VALIGN="TOP"><TD STYLE="text-align:right; border:0px" NOWRAP>' .
  222                    "<I>In answer $i</I>:&nbsp;</TD>".
  223                    '<TD STYLE="text-align:left; border:0px">'.$result->{ans_message}.'</TD></TR>');
  224     } else {$score += $result->{score}}
  225   }
  226   $ans->score($score/$self->length);
  227   $ans->{ans_message} = $ans->{error_message} = "";
  228   if (scalar(@errors)) {
  229     $ans->{ans_message} = $ans->{error_message} =
  230       '<TABLE BORDER="0" CELLSPACING="0" CELLPADDING="0" CLASS="ArrayLayout">' .
  231        join('<TR><TD HEIGHT="4"></TD></TR>',@errors).
  232       '</TABLE>';
  233   }
  234   if ($nonblank) {
  235     $ans->{preview_latex_string} = '{'.join('}'.$self->{tex_separator}.'{',@latex).'}';
  236     $ans->{preview_text_string}  = join($self->{separator},@text);
  237     $ans->{student_ans} = join($self->{separator},@student);
  238   }
  239   return $ans;
  240 }
  241 
  242 #
  243 #  Return a given string or a default if it is empty or not defined
  244 #
  245 sub check_string {
  246   my $s = shift;
  247   $s = shift unless defined($s) && $s =~ m/\S/;
  248   return $s;
  249 }
  250 
  251 ######################################################################
  252 
  253 #
  254 #  Answer checker to use for individual entries when singleResult
  255 #  is not in effect.
  256 #
  257 sub entry_cmp {
  258   my $self = shift; my $i = shift;
  259   my $ans = new AnswerEvaluator;
  260   $ans->ans_hash(
  261     correct_ans => $self->{cmp}[$i]{rh_ans}{correct_ans},
  262     part        => $i,
  263     type        => "MultiPart($i)",
  264     @_,
  265   );
  266   $ans->install_evaluator(sub {my $ans = shift; (shift)->entry_check($ans)},$self);
  267   $ans->install_pre_filter('erase'); # don't do blank check
  268   return $ans;
  269 }
  270 
  271 #
  272 #  Call the correct answser's checker to check for syntax and type errors.
  273 #  If this is the last one, perform the user's checker routine as well
  274 #  Return the individual answer (our answer hash is discarded).
  275 #
  276 sub entry_check {
  277   my $self = shift; my $ans = shift;
  278   my $i = $ans->{part};
  279   $self->{ans}[$i] = $self->{cmp}[$i]->evaluate($ans->{student_ans});
  280   $self->{ans}[$i]->score(0);
  281   $self->perform_check if ($i == $self->length - 1);
  282   return $self->{ans}[$i];
  283 }
  284 
  285 ######################################################################
  286 
  287 #
  288 #  Collect together the correct and student answers, and call the
  289 #  user's checker routine.
  290 #
  291 #  If any of the answers produced errors or the types don't match
  292 #    don't call the user's routine.
  293 #  Otherwise, call it, and if there was an error, report that.
  294 #  Set the individual scores based on the result from the user's routine.
  295 #
  296 sub perform_check {
  297   my $self = shift; $self->{context}->clearError;
  298   my @correct; my @student;
  299   foreach my $ans (@{$self->{ans}}) {
  300     push(@correct,$ans->{correct_value});
  301     push(@student,$ans->{student_value});
  302     return if $ans->{ans_message} ne "" || !defined($ans->{student_value});
  303     return if $self->{checkTypes} && $ans->{student_value}->type ne $ans->{correct_value}->type;
  304   }
  305   my $result = Value::cmp_compare([@correct],[@student],$self);
  306   if (!defined($result) && $self->{context}{error}{flag}) {$self->cmp_error($self->{ans}[0]); return 1}
  307   $result = 0 if (!defined($result) || $result eq '');
  308   if (ref($result) eq 'ARRAY') {
  309     die "Checker subroutine returned the wrong number of results"
  310       if (scalar(@{$result}) != $self->length);
  311     foreach my $i (0..$self->length-1) {$self->{ans}[$i]->score($result->[$i])}
  312   } elsif (Value::matchNumber($result)) {
  313     foreach my $ans (@{$self->{ans}}) {$ans->score($result)}
  314   } else {
  315     die "Checker subroutine should return a number or array of numbers ($result)";
  316   }
  317   return;
  318 }
  319 
  320 ######################################################################
  321 
  322 #
  323 #  The user's checker can call setMessage(n,message) to set the error message
  324 #  for the n-th answer blank.
  325 #
  326 sub setMessage {
  327   my $self = shift; my $i = (shift)-1; my $message = shift;
  328   $self->{ans}[$i]->{ans_message} = $self->{ans}[$i]->{error_message} = $message;
  329 }
  330 
  331 
  332 ######################################################################
  333 
  334 #
  335 #  Produce the name for a named answer blank
  336 #
  337 sub ANS_NAME {
  338   my $self = shift; my $i = shift;
  339   $self->{id}.'_'.$i;
  340 }
  341 
  342 #
  343 #  Record an answer-blank name (when using extensions)
  344 #
  345 sub NEW_NAME {
  346   my $self = shift;
  347   main::RECORD_FORM_LABEL(shift);
  348 }
  349 
  350 #
  351 #  Produce an answer rule for the next item in the list,
  352 #    taking care to use names or extensions as needed
  353 #    by the settings of the MultiPart.
  354 #
  355 sub ans_rule {
  356   my $self = shift; my $size = shift || 20;
  357   my $data = $self->{data}[$self->{part}];
  358   my $name = $self->ANS_NAME($self->{part}++);
  359   return $data->named_ans_rule_extension($self->NEW_NAME($name),$size,@_)
  360     if ($self->{singleResult} && $self->{part} > 1);
  361   return $data->ans_rule($size,@_) unless $self->{namedRules};
  362   return $data->named_ans_rule($name,$size,@_);
  363 }
  364 
  365 #
  366 #  Do the same, but for answer arrays, which are generated by the
  367 #    Value objects automatically sized to suit their data.
  368 #    Reset the correct_ans once the array is made
  369 #
  370 sub ans_array {
  371   my $self = shift; my $size = shift || 5; my $HTML;
  372   my $data = $self->{data}[$self->{part}];
  373   my $name = $self->ANS_NAME($self->{part}++);
  374   if ($self->{singleResult} && $self->{part} > 1) {
  375     $HTML = $data->named_ans_array_extension($self->NEW_NAME($name),$size,@_);
  376   } elsif (!$self->{namedRules}) {
  377     $HTML = $data->ans_array($size,@_);
  378   } else {
  379     $HTML = $data->named_ans_array($name,$size,@_);
  380   }
  381   $self->{cmp}[$self->{part}-1] = $data->cmp(@ans_defaults);
  382   return $HTML;
  383 }
  384 
  385 ######################################################################
  386 
  387 package main;
  388 
  389 #
  390 #  Main routine to create MultiPart items.
  391 #
  392 sub MultiPart {MultiPart->new(@_)};
  393 
  394 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9