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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 6168 - (view) (download) (as text)

1 : sh002i 5556 ################################################################################
2 :     # WeBWorK Online Homework Delivery System
3 :     # Copyright © 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
4 : dpvc 6168 # $CVSHeader: pg/macros/parserPopUp.pl,v 1.10 2009/06/25 23:28:44 gage Exp $
5 : sh002i 5556 #
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 : sh002i 5553 =head1 NAME
18 : dpvc 3723
19 : sh002i 5553 parserPopUp.pl - Pop-up menus compatible with Value objects.
20 : dpvc 3723
21 : gage 4997 =head1 DESCRIPTION
22 :    
23 : sh002i 5553 This file implements a pop-up menu object that is compatible
24 :     with Value objects, and in particular, with the MultiAnswer object.
25 : dpvc 3723
26 : sh002i 5553 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 : gage 4997 =cut
51 :    
52 : dpvc 5924 loadMacros('MathObjects.pl');
53 : sh002i 5553
54 : dpvc 6168 sub _parserPopUp_init {parser::PopUp::Init()}; # don't reload this file
55 : sh002i 5553
56 : dpvc 3723 #
57 :     # The package that implements pop-up menus
58 :     #
59 : dpvc 6168 package parser::PopUp;
60 : dpvc 3723 our @ISA = qw(Value::String);
61 : dpvc 6168 my $context;
62 : dpvc 3723
63 : dpvc 5392 #
64 : dpvc 6168 # Setup the context and the PopUp() command
65 : dpvc 5392 #
66 : dpvc 5924 sub Init {
67 : dpvc 6168 #
68 :     # make a context in which arbitrary strings can be entered
69 :     #
70 :     $context = Parser::Context->getCopy("Numeric");
71 :     $context->{name} = "PopUp";
72 :     $context->parens->clear();
73 :     $context->variables->clear();
74 :     $context->constants->clear();
75 :     $context->operators->clear();
76 :     $context->functions->clear();
77 :     $context->strings->clear();
78 :     $context->{pattern}{number} = "^\$";
79 :     $context->variables->{patterns} = {};
80 :     $context->strings->{patterns}{".*"} = [-20,'str'];
81 :     $context->{parser}{String} = "parser::PopUp::String";
82 :     $context->update;
83 :     main::PG_restricted_eval('sub PopUp {parser::PopUp->new(@_)}');
84 : dpvc 5924 }
85 : dpvc 5392
86 :     #
87 :     # Create a new PopUp object
88 :     #
89 : dpvc 3723 sub new {
90 :     my $self = shift; my $class = ref($self) || $self;
91 : dpvc 6027 shift if Value::isContext($_[0]); # remove context, if given (it is not used)
92 : dpvc 3723 my $choices = shift; my $value = shift;
93 :     Value::Error("A PopUp's first argument should be a list of menu items")
94 :     unless ref($choices) eq 'ARRAY';
95 :     Value::Error("A PopUp's second argument should be the correct menu choice")
96 :     unless defined($value) && $value ne "";
97 : dpvc 6168 my %choice; map {$choice{$_} = 1} @$choices;
98 :     Value::Error("The correct choice must be one of the PopUp menu items")
99 :     unless $choice{$value};
100 :     my $self = bless {data => [$value], context => $context, choices => $choices}, $class;
101 : dpvc 3723 return $self;
102 :     }
103 :    
104 : dpvc 5392 #
105 :     # Create the menu list
106 :     #
107 : dpvc 3723 sub menu {
108 :     my $self = shift;
109 :     main::pop_up_list($self->{choices});
110 :     }
111 :    
112 : dpvc 5392 #
113 :     # Answer rule is the menu list
114 :     #
115 : dpvc 3723 sub ans_rule {shift->menu(@_)}
116 :    
117 : dpvc 6168 ##################################################
118 :     #
119 :     # Replacement for Parser::String that takes the
120 :     # complete parse string as its value
121 :     #
122 :     package parser::PopUp::String;
123 :     our @ISA = ('Parser::String');
124 :    
125 :     sub new {
126 :     my $self = shift;
127 :     my ($equation,$value,$ref) = @_;
128 :     $value = $equation->{string};
129 :     $self->SUPER::new($equation,$value,$ref);
130 :     }
131 :    
132 :     ##################################################
133 :    
134 : dpvc 3723 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9