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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5342 - (download) (as text) (annotate)
Fri Aug 17 01:59:33 2007 UTC (12 years, 6 months ago) by dpvc
File size: 2129 byte(s)
This is a temporary hack that allows sticky answers to preserve all
the characters the student typed.  PGbasicmacros.pl currently removes
several characters, including $, which is needed for monitary
answers.  Eventually, PGbasicmacros.pl should be fixed, but for now
this will work, and it should not cause trouble after PGbasicmacros.pl
is fixed.  In the end, this file can be emptied, then it should issue
a warning message, and finally it can be removed.

The hack is to copy the inputs_ref and modify the answers so that the
PGbasicmacros.pl macros will not remove those characters (the key is
to convert them to HTML entities instead).  Then after the problem has
been processed, we put the original answers back so that the answer
checkers will work properly on them.  This is accomplished by
replacing ENDDOCUMENT with a new routine that first puts back the
answers and then calls the old ENDDOCUMENT.  This has the unwanted
side effect of putting error messages into the error log, but I can't
seem to work around that.

    1 
    2 =pod
    3 
    4 ######################################################################
    5 #
    6 #  This file implements a fragile hack to overcome a problem with
    7 #  PGbasicmacros.pl, which removes special characters from student
    8 #  answers (in order to prevent EV3 from mishandling them).
    9 #
   10 #  Unfortunately, this means that "sticky" answers will lose
   11 #  those characters, which makes it very difficult to answer
   12 #  problems with more than one answer when the student wants
   13 #  to submit several times while working on later parts.
   14 #
   15 #  The real fix to to rewrite PGbasicmacros.pl to handle
   16 #  this better, but this hack will handle the situation for
   17 #  now until that can be accomplished.
   18 #
   19 #  To use this hack, simply load the file using
   20 #
   21 #    loadMacros("problemPreserveAnswers.pl");
   22 #
   23 #  at the top of your PG file.
   24 #
   25 
   26 =cut
   27 
   28 sub _problemPreserveAnswers_init {PreserveAnswers::Init()}
   29 
   30 package PreserveAnswers;
   31 
   32 #
   33 #  Escape the specials in answers, and then
   34 #  override ENDDOCUMENT() to restore the answers
   35 #  to their original values.
   36 #
   37 sub Init {
   38   PreserveAnswers::EscapeAnswers();
   39   $PreserveAnswers::ENDDOCUMENT = \&main::ENDDOCUMENT;
   40   main::PG_restricted_eval
   41     ('sub ENDDOCUMENT {PreserveAnswers::RestoreAnswers(); &$PreserveAnswers::ENDDOCUMENT(@_)}');
   42 }
   43 
   44 #
   45 #  This is a fragile hack to prevent PG from removing the
   46 #  dollar signs in currency answers (and everything else as well).
   47 #  PGbasicmacros.pl needs to be fixed to allow this without
   48 #  having to do this terrible hack.
   49 #
   50 sub EscapeAnswers {
   51   my $original = $main::inputs_ref_orig = {%{$main::inputs_ref}};
   52   my $inputs   = $main::inputs_ref;
   53   foreach my $id (keys %{$original}) {
   54     my $value = $original->{$id};
   55     next if !defined($value) || ref($value);
   56     $value =~ s/([\\\$@\`])/EscapeHTMLchar($1)/ge;
   57     $inputs->{$id} = $value;
   58   }
   59 }
   60 
   61 sub EscapeHTMLchar {main::spf(ord(shift),"&#x%02X;")}
   62 
   63 sub RestoreAnswers {
   64   my $original = $main::inputs_ref_orig;
   65   my $inputs   = $main::inputs_ref;
   66   foreach my $id (keys %{$original}) {$inputs->{$id} = $original->{$id}}
   67 }
   68 
   69 our $ENDDOCUMENT;
   70 
   71 ######################################################################
   72 
   73 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9