[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 5556 - (download) (as text) (annotate)
Thu Oct 4 16:40:49 2007 UTC (12 years, 3 months ago) by sh002i
File size: 3102 byte(s)
added standard copyright/license header

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: webwork2/lib/WeBWorK.pm,v 1.100 2007/08/13 22:59:53 sh002i Exp $
    5 #
    6 # This program is free software; you can redistribute it and/or modify it under
    7 # the terms of either: (a) the GNU General Public License as published by the
    8 # Free Software Foundation; either version 2, or (at your option) any later
    9 # version, or (b) the "Artistic License" which comes with this package.
   10 #
   11 # This program is distributed in the hope that it will be useful, but WITHOUT
   12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
   13 # FOR A PARTICULAR PURPOSE.  See either the GNU General Public License or the
   14 # Artistic License for more details.
   15 ################################################################################
   16 
   17 =head1 NAME
   18 
   19 problemPreserveAnswers.pl - Allow sticky answers to preserve special characters.
   20 
   21 =head1 DESCRIPTION
   22 
   23 This file implements a fragile hack to overcome a problem with
   24 PGbasicmacros.pl, which removes special characters from student
   25 answers (in order to prevent EV3 from mishandling them).
   26 
   27 Unfortunately, this means that "sticky" answers will lose
   28 those characters, which makes it very difficult to answer
   29 problems with more than one answer when the student wants
   30 to submit several times while working on later parts.
   31 
   32 The real fix to to rewrite PGbasicmacros.pl to handle
   33 this better, but this hack will handle the situation for
   34 now until that can be accomplished.
   35 
   36 To use this hack, simply load the file using
   37 
   38   loadMacros("problemPreserveAnswers.pl");
   39 
   40 at the top of your PG file.
   41 
   42 =cut
   43 
   44 sub _problemPreserveAnswers_init {PreserveAnswers::Init()}
   45 
   46 package PreserveAnswers;
   47 
   48 #
   49 #  Escape the specials in answers, and then
   50 #  override ENDDOCUMENT() to restore the answers
   51 #  to their original values.
   52 #
   53 sub Init {
   54   PreserveAnswers::EscapeAnswers();
   55   $PreserveAnswers::ENDDOCUMENT = \&main::ENDDOCUMENT;
   56   main::PG_restricted_eval
   57     ('sub ENDDOCUMENT {PreserveAnswers::RestoreAnswers(); &$PreserveAnswers::ENDDOCUMENT(@_)}');
   58 }
   59 
   60 #
   61 #  This is a fragile hack to prevent PG from removing the
   62 #  dollar signs in currency answers (and everything else as well).
   63 #  PGbasicmacros.pl needs to be fixed to allow this without
   64 #  having to do this terrible hack.
   65 #
   66 sub EscapeAnswers {
   67   my $original = $main::inputs_ref_orig = {%{$main::inputs_ref}};
   68   my $inputs   = $main::inputs_ref;
   69   foreach my $id (keys %{$original}) {
   70     my $value = $original->{$id};
   71     next if !defined($value) || ref($value);
   72     $value =~ s/([\\\$@\`"&<>])/EscapeHTMLchar($1)/ge;
   73     $inputs->{$id} = $value;
   74   }
   75 }
   76 
   77 sub EscapeHTMLchar {main::spf(ord(shift),"&#x%02X;")}
   78 
   79 sub RestoreAnswers {
   80   my $original = $main::inputs_ref_orig;
   81   my $inputs   = $main::inputs_ref;
   82   foreach my $id (keys %{$original}) {$inputs->{$id} = $original->{$id}}
   83 }
   84 
   85 our $ENDDOCUMENT; # holds pointer to original ENDDOCUMENT
   86 
   87 ######################################################################
   88 
   89 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9