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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5924 - (download) (as text) (annotate)
Thu Oct 2 10:50:02 2008 UTC (11 years, 3 months ago) by dpvc
File size: 3071 byte(s)
avoid side-effect of loading String context that sets the string
context to being the current context.  (This fixes bug 1520)

    1 ################################################################################
    2 # WeBWorK Online Homework Delivery System
    3 # Copyright  2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/parserPopUp.pl,v 1.7 2007/10/04 16:40:49 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 parserPopUp.pl - Pop-up menus compatible with Value objects.
   20 
   21 =head1 DESCRIPTION
   22 
   23 This file implements a pop-up menu object that is compatible
   24 with Value objects, and in particular, with the MultiAnswer object.
   25 
   26 To create a PopUp object, use
   27 
   28   $popup = PopUp([choices,...],correct);
   29 
   30 where "choices" are the strings for the items in the popup menu,
   31 and "correct" is the choice that is the correct answer for the
   32 popup.
   33 
   34 To insert the popup menu into the problem text, use
   35 
   36   BEGIN_TEXT
   37   \{$popup->menu\}
   38   END_TEXT
   39 
   40 and then
   41 
   42   ANS($popup->cmp);
   43 
   44 to get the answer checker for the popup.
   45 
   46 You can use the PopUp menu object in MultiAnswer objects.  This is
   47 the reason for the pop-up menu's ans_rule method (since that is what
   48 MultiAnswer calls to get answer rules).
   49 
   50 =cut
   51 
   52 loadMacros('MathObjects.pl');
   53 
   54 sub _parserPopUp_init {parserPopUp::Init()}; # don't reload this file
   55 
   56 #
   57 #  The package that implements pop-up menus
   58 #
   59 package parserPopUp;
   60 our @ISA = qw(Value::String);
   61 
   62 #
   63 #  Setup the main:: namespace
   64 #
   65 sub Init {
   66   ### Hack to get around context change in contextString.pl
   67   ### FIXME:  when context definitions don't set context, put loadMacros with MathObject.pl above again
   68   my $context = main::Context();
   69   main::loadMacros('contextString.pl');
   70   main::Context($context);
   71   main::PG_restricted_eval('sub PopUp {parserPopUp->new(@_)}');
   72 }
   73 
   74 #
   75 #  Create a new PopUp object
   76 #
   77 sub new {
   78   my $self = shift; my $class = ref($self) || $self;
   79   my $choices = shift; my $value = shift;
   80   Value::Error("A PopUp's first argument should be a list of menu items")
   81     unless ref($choices) eq 'ARRAY';
   82   Value::Error("A PopUp's second argument should be the correct menu choice")
   83     unless defined($value) && $value ne "";
   84   my $context = Parser::Context->getCopy("String");
   85   $context->strings->add(map {$_=>{}} @{$choices});
   86   my $self = bless $context->Package("String")->new($context,$value)->with(choices => $choices), $class;
   87   return $self;
   88 }
   89 
   90 #
   91 #  Create the menu list
   92 #
   93 sub menu {
   94   my $self = shift;
   95   main::pop_up_list($self->{choices});
   96 }
   97 
   98 #
   99 #  Answer rule is the menu list
  100 #
  101 sub ans_rule {shift->menu(@_)}
  102 
  103 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9