[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 5373 - (download) (as text) (annotate)
Sun Aug 19 02:01:57 2007 UTC (12 years, 3 months ago) by dpvc
File size: 2190 byte(s)
Normalized comments and headers to that they will format their POD
documentation properly.  (I know that the POD processing was supposed
to strip off the initial #, but that doesn't seem to happen, so I've
added a space throughout.)

    1 
    2 sub _problemPreserveAnswers_init {PreserveAnswers::Init()}
    3 
    4 =head1 PreserveAnswers();
    5 
    6 ######################################################################
    7 #
    8 #  This file implements a fragile hack to overcome a problem with
    9 #  PGbasicmacros.pl, which removes special characters from student
   10 #  answers (in order to prevent EV3 from mishandling them).
   11 #
   12 #  Unfortunately, this means that "sticky" answers will lose
   13 #  those characters, which makes it very difficult to answer
   14 #  problems with more than one answer when the student wants
   15 #  to submit several times while working on later parts.
   16 #
   17 #  The real fix to to rewrite PGbasicmacros.pl to handle
   18 #  this better, but this hack will handle the situation for
   19 #  now until that can be accomplished.
   20 #
   21 #  To use this hack, simply load the file using
   22 #
   23 #    loadMacros("problemPreserveAnswers.pl");
   24 #
   25 #  at the top of your PG file.
   26 #
   27 
   28 =cut
   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; # holds pointer to original ENDDOCUMENT
   70 
   71 ######################################################################
   72 
   73 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9