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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6192 - (download) (as text) (annotate)
Fri Jan 29 10:52:10 2010 UTC (9 years, 11 months ago) by dpvc
File size: 1784 byte(s)
A context that allows students to enter any string

    1 ####################################################################
    2 #
    3 #  Implements a context in which the student's answer is treated as a
    4 #  literal string, and not parsed further.  The real answer checking
    5 #  should be performed in a custom checker passed to the answer
    6 #  string's cmp() method.  E.g.,
    7 #
    8 #        loadMacros("contextArbitraryString.pl");
    9 #        Context("ArbitraryString");
   10 #
   11 #        ANS(Compute("The string I want")->cmp(checker => sub {
   12 #          my ($correct,$student,$ans) = @_;
   13 #          $correct = $correct->value; # get perl string from String object
   14 #          $student = $student->value; # ditto
   15 #          ##
   16 #          ## do your checking here, and return true if correct
   17 #          ## or false if incorrect.  For example
   18 #          ##   return $correct eq $student;
   19 #          ##
   20 #          return $score;
   21 #        }));
   22 #
   23 
   24 sub _contextArbitraryString_init {
   25   my $context = $main::context{ArbitraryString} = Parser::Context->getCopy("Numeric");
   26   $context->{name} = "ArbitraryString";
   27   $context->parens->clear();
   28   $context->variables->clear();
   29   $context->constants->clear();
   30   $context->operators->clear();
   31   $context->functions->clear();
   32   $context->strings->clear();
   33   $context->{pattern}{number} = "^\$";
   34   $context->variables->{patterns} = {};
   35   $context->strings->{patterns}{".*"} = [-20,'str'];
   36   $context->{value}{"String()"} = "context::ArbitraryString";
   37   $context->{parser}{String} = "context::ArbitraryString::String";
   38   $context->update;
   39 }
   40 
   41 package context::ArbitraryString;
   42 sub new {shift; main::Compute(@_)}
   43 
   44 package context::ArbitraryString::String;
   45 our @ISA = ('Parser::String');
   46 
   47 sub new {
   48   my $self = shift;
   49   my ($equation,$value,$ref) = @_;
   50   $value = $equation->{string};
   51   $self->SUPER::new($equation,$value,$ref);
   52 }
   53 
   54 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9