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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1280 - (download) (as text) (annotate)
Thu Jun 26 17:42:18 2003 UTC (16 years, 7 months ago) by malsyned
File size: 15660 byte(s)
Merged the changes needed to work with the new WW2 translator
optimizations into the main pg branch.

    1 #!/usr/local/bin/webwork-perl
    2 
    3 # This file provided the fundamental macros for the pg language
    4 # These macros define the interface between the problems written by
    5 # the professor and the processing which occurs in the script
    6 # processProblem.pl
    7 
    8 
    9 BEGIN {
   10   be_strict();
   11 }
   12 
   13 sub _PG_init{
   14 
   15 }
   16 
   17 #package PG;
   18 
   19 
   20 =head1 NAME
   21 
   22   PG.pl --- located in the courseScripts directory.
   23   Defines the Program Generating language at the most basic level.
   24 
   25 =head1 SYNPOSIS
   26 
   27   The basic PG problem structure:
   28 
   29   DOCUMENT();          # should be the first statment in the problem
   30   loadMacros(.....);   # (optional) load other macro files if needed.
   31                        # (loadMacros is defined in F<dangerousMacros.pl>)
   32 
   33   HEADER_TEXT(...);    # (optional) used only for inserting javaScript into problems.
   34 
   35   #            #  insert text of problems
   36   TEXT("Problem text to be",
   37        "displayed. Enter 1 in this blank:",
   38        ANS_RULE(1,30)  #  ANS_RULE() defines an answer blank 30 characters long.
   39                        #  It is defined in F<PGbasicmacros.pl>
   40        );
   41 
   42 
   43   ANS( answer_evalutors);  # see F<PGanswermacros.pl> for examples of answer evaluatiors.
   44 
   45   ENDDOCUMENT()        # must be the last statement in the problem
   46 
   47 
   48 
   49 =head1 DESCRIPTION
   50 
   51 As described in the synopsis, this file and the macros C<DOCUMENT()> and C<ENDDOCUMENT()> determine
   52 the interface between problems written in the PG language and the rest of B<WeBWorK>, in particular
   53 the subroutine C<createPGtext(()> in the file F<translate.pl>.
   54 
   55 C<DOCUMENT()> must be the first statement in each problem template.
   56 It  initializes variables,
   57 in particular all of the contents of the
   58 environment variable  become defined in the problem enviroment.
   59 (See
   60 L</webwork_system_html/docs/techdescription/pglanguage/PGenvironment.html>)
   61 
   62 ENDDOCUMENT() must the last executable statement in any problem template.  It returns
   63 the rendered problem, answer evaluators and other flags to the rest of B<WeBWorK>, specificially
   64 to the routine C<createPGtext()> defined in F<translate.pl>
   65 
   66 
   67 The C<HEADER_TEXT()>, C<TEXT()>, and C<ANS()> functions load the
   68 header text string, the problem text string.
   69 and the answer evaulator queue respectively.
   70 
   71 
   72 =cut
   73 
   74 
   75 #  Private variables for the PG.pl file.
   76 
   77 my ($STRINGforOUTPUT, $STRINGforHEADER_TEXT, @PG_ANSWERS, @PG_UNLABELED_ANSWERS);
   78 my %PG_ANSWERS_HASH ;
   79 
   80 # my variables are unreliable if two DOCUMENTS were to be called before and ENDDOCUMENT
   81 # there could be conflicts.  As I understand the behavior of the Apache child
   82 # this cannot occur -- a child finishes with one request before obtaining the next
   83 
   84 #   DOCUMENT must come early in every .pg file, before any answers or text are
   85 # defined.  It initializes the variables.
   86 # It can appear only once.
   87 
   88 =head2 DOCUMENT()
   89 
   90 C<DOCUMENT()> must be the first statement in each problem template.  It can
   91 only be used once in each problem.
   92 
   93 C<DOCUMENT()> initializes some empty variables and via C<INITIALIZE_PG()> unpacks the
   94 variables in the C<%envir> variable which is implicitly passed to the problem. It must
   95 be the first statement in any problem template. It
   96 also unpacks any answers submitted and places them in the C<@submittedAnswer> list,
   97 saves the problem seed in C<$PG_original_problemSeed> in case you need it later, and
   98 initializes the pseudo random number generator object in C<$PG_random_generator>.
   99 
  100 You can reset the standard number generator using the command:
  101 
  102   $PG_random_generator->srand($new_seed_value);
  103 
  104 (See also C<SRAND> in the L<PGbasicmacros.pl> file.)
  105 
  106 The
  107 environment variable contents is defined in
  108 L</webwork_system_html/docs/techdescription/pglanguage/PGenvironment.html>
  109 
  110 
  111 =cut
  112 
  113 sub DOCUMENT {
  114 
  115   $STRINGforOUTPUT ="";
  116     $STRINGforHEADER_TEXT ="";
  117   @PG_ANSWERS=();
  118 
  119   @PG_UNLABELED_ANSWERS = ();
  120   %PG_ANSWERS_HASH = ();
  121   # FIXME:  We are initializing these variables into both Safe::Root1 (the cached safe compartment)
  122   # and Safe::Root2 (the current one)
  123   # There is a good chance they won't be properly updated in one or the other of these compartments.
  124 
  125 #   @main::PG_ANSWER_ENTRY_ORDER = ();
  126 #   $main::ANSWER_PREFIX = 'AnSwEr';
  127 #   %main::PG_FLAGS=();  #global flags
  128 #   $main::showPartialCorrectAnswers = 0 unless defined($main::showPartialCorrectAnswers );
  129 #   $main::showHint = 1 unless defined($main::showHint);
  130 #   $main::solutionExists =0;
  131 #   $main::hintExists =0;
  132 #   %main::gifs_created = ();
  133   eval(q!
  134   @main::PG_ANSWER_ENTRY_ORDER = ();
  135   $main::ANSWER_PREFIX = 'AnSwEr';
  136   %main::PG_FLAGS=();  #global flags
  137   $main::showPartialCorrectAnswers = 0 unless defined($main::showPartialCorrectAnswers );
  138   $main::showHint = 1 unless defined($main::showHint);
  139   $main::solutionExists =0;
  140   $main::hintExists =0;
  141   %main::gifs_created = ();
  142 
  143     !);
  144 #    warn eval(q! "PG.pl:  The envir variable $main::{envir} is".join(" ",%main::envir)!);
  145     my $rh_envir = eval(q!\%main::envir!);
  146     my %envir    = %$rh_envir;
  147     no strict;
  148     foreach  my  $var (keys %envir) {
  149       eval(q!$main::!.$var.q! = $main::envir{!.$var.q!}! );  #whew!! makes sure $var is interpolated but $main:: is evaluated at run time.
  150     #    warn eval(q! "var $var is defined ". $main::!.$var);
  151         warn "Problem defining ", q{\$main::}.$var, " while initializing the PG problem: $@" if $@;
  152     }
  153     use strict;
  154 
  155 
  156 
  157     eval(q!
  158   @main::submittedAnswers = @{$main::refSubmittedAnswers} if defined($main::refSubmittedAnswers);
  159   $main::PG_original_problemSeed = $main::problemSeed;
  160   $main::PG_random_generator = new PGrandom($main::problemSeed) || die "Can't create random number generator.";
  161   $main::ans_rule_count = 0;  # counts questions
  162 
  163     # end unpacking of environment variables.
  164     $main::QUIZ_PREFIX = '' unless defined($main::QUIZ_PREFIX)
  165 
  166   !);
  167 #   @main::submittedAnswers = @{$main::refSubmittedAnswers} if defined($main::refSubmittedAnswers);
  168 #   $main::PG_original_problemSeed = $main::problemSeed;
  169 #   $main::PG_random_generator = new PGrandom($main::problemSeed) || die "Can't create random number generator.";
  170 #   $main::ans_rule_count = 0;  # counts questions
  171 
  172     # end unpacking of environment variables.
  173 #   $main::QUIZ_PREFIX = '' unless defined($main::QUIZ_PREFIX)
  174 
  175 }
  176 
  177 sub inc_ans_rule_count {
  178   eval(q!++$main::ans_rule_count!); # evalute at runtime to get correct main::
  179 }
  180 # HEADER_TEXT is for material which is destined to be placed in the header of the html problem -- such
  181 #   as javaScript code.
  182 
  183 =head2 HEADER_TEXT()
  184 
  185 
  186   HEADER_TEXT("string1", "string2", "string3");
  187 
  188 The C<HEADER_TEXT()>
  189 function concatenates its arguments and places them in the output
  190 header text string.  It is used for material which is destined to be placed in
  191 the header of the html problem -- such as javaScript code.
  192  It can be used more than once in a file.
  193 
  194 
  195 =cut
  196 
  197 sub HEADER_TEXT {
  198   my @in = @_;
  199   $STRINGforHEADER_TEXT .= join(" ",@in);
  200   }
  201 
  202 # TEXT is the function which defines text which will appear in the problem.
  203 # All text must be an argument to this function.  Any other statements
  204 #   are calculations (done in perl) which will not directly appear in the
  205 # output.  Think of this as the "print" function for the .pg language.
  206 # It can be used more than once in a file.
  207 
  208 =head2 TEXT()
  209 
  210   TEXT("string1", "string2", "string3");
  211 
  212 The C<TEXT()> function concatenates its arguments and places them in the output
  213 text string. C<TEXT()> is the function which defines text which will appear in the problem.
  214 All text must be an argument to this function.  Any other statements
  215 are calculations (done in perl) which will not directly appear in the
  216 output.  Think of this as the "print" function for the .pg language.
  217 It can be used more than once in a file.
  218 
  219 =cut
  220 
  221 sub TEXT {
  222   my @in = @_;
  223   $STRINGforOUTPUT .= join(" ",@in);
  224   }
  225 
  226 
  227 
  228 =head2 ANS()
  229 
  230   ANS(answer_evaluator1, answer_evaluator2, answer_evaluator3,...)
  231 
  232 Places the answer evaluators in the unlabeled answer_evaluator queue.  They will be paired
  233 with unlabeled answer rules (answer entry blanks) in the order entered.  This is the standard
  234 method for entering answers.
  235 
  236   LABELED_ANS(answer_evaluater_name1, answer_evaluator1, answer_evaluater_name2,answer_evaluator2,...)
  237 
  238 Places the answer evaluators in the labeled answer_evaluator hash.  This allows pairing of
  239 labeled answer evaluators and labeled answer rules which may not have been entered in the same
  240 order.
  241 
  242 =cut
  243 
  244 sub ANS{             # store answer evaluators which have not been explicitly labeled
  245   my @in = @_;
  246   while (@in ) {
  247          warn("<BR><B>Error in ANS:$in[0]</B> -- inputs must be references to
  248                       subroutines<BR>")
  249       unless ref($in[0]);
  250       push(@PG_ANSWERS, shift @in );
  251       }
  252 }
  253 sub LABELED_ANS {  #a better alias for NAMED_ANS
  254   &NAMED_ANS;
  255 }
  256 
  257 sub NAMED_ANS{     # store answer evaluators which have been explicitly labeled (submitted in a hash)
  258   my @in = @_;
  259   while (@in ) {
  260     my $label = shift @in;
  261     $label = eval(q!$main::QUIZ_PREFIX.$label!);
  262     my $ans_eval = shift @in;
  263     TEXT("<BR><B>Error in NAMED_ANS:$in[0]</B>
  264           -- inputs must be references to subroutines<BR>")
  265       unless ref($ans_eval);
  266     $PG_ANSWERS_HASH{$label}= $ans_eval;
  267   }
  268 }
  269 sub RECORD_ANS_NAME {     # this maintains the order in which the answer rules are printed.
  270   my $label = shift;
  271   eval(q!push(@main::PG_ANSWER_ENTRY_ORDER, $label)!);
  272   $label;
  273 }
  274 
  275 sub NEW_ANS_NAME {        # this keeps track of the answers which are entered implicitly,
  276                           # rather than with a specific label
  277     my $number=shift;
  278     my $prefix = eval(q!$main::QUIZ_PREFIX.$main::ANSWER_PREFIX!);
  279     my $label = $prefix.$number;
  280     push(@PG_UNLABELED_ANSWERS,$label);
  281     $label;
  282 }
  283 sub ANS_NUM_TO_NAME {     # This converts a number to an answer label for use in
  284                           # radio button and check box answers. No new answer
  285                           # name is recorded.
  286     my $number=shift;
  287     my $label = eval(q!$main::QUIZ_PREFIX$main::ANSWER_PREFIX$number"!);
  288     $label;
  289 }
  290 
  291 my $vecnum;
  292 
  293 sub NEW_ANS_ARRAY_NAME {        # this keeps track of the answers which are entered implicitly,
  294                           # rather than with a specific label
  295     my $number=shift;
  296     $vecnum = 0;
  297     my $row = shift;
  298     my $col = shift;
  299     my $label = "ArRaY"."$number"."["."$vecnum".","."$row".","."$col"."]";
  300     push(@PG_UNLABELED_ANSWERS,$label);
  301     $label;
  302 }
  303 
  304 sub NEW_ANS_ARRAY_NAME_EXTENSION {        # this keeps track of the answers which are entered implicitly,
  305                           # rather than with a specific label
  306     my $number=shift;
  307     my $row = shift;
  308     my $col = shift;
  309     if( $row == 0 && $col == 0 ){
  310       $vecnum += 1;
  311     }
  312     my $label = "ArRaY"."$number"."["."$vecnum".","."$row".","."$col"."]";
  313     $label;
  314 }
  315 
  316 # ENDDOCUMENT must come at the end of every .pg file.
  317 #   It exports the resulting text of the problem, the text to be used in HTML header material
  318 #   (for javaScript), the list of answer evaluators and any other flags.  It can appear only once and
  319 #   it MUST be the last statement in the problem.
  320 
  321 =head2 ENDDOCUMENT()
  322 
  323 ENDDOCUMENT() must the last executable statement in any problem template.  It can
  324 only appear once.  It returns
  325 an array consisting of
  326 
  327   A reference to a string containing the rendered text of the problem.
  328   A reference to a string containing text to be placed in the header
  329                (for javaScript)
  330   A reference to the array containing the answer evaluators.
  331                (May be changed to a hash soon.)
  332   A reference to an associative array (hash) containing various flags.
  333 
  334   The following flags are set by ENDDOCUMENT:
  335   (1) showPartialCorrectAnswers  -- determines whether students are told which
  336       of their answers in a problem are wrong.
  337   (2) recordSubmittedAnswers  -- determines whether students submitted answers
  338       are saved.
  339   (3) refreshCachedImages  -- determines whether the cached image of the problem
  340       in typeset mode is always refreshed (i.e. setting this to 1 means cached
  341       images are not used).
  342   (4) solutionExits   -- indicates the existence of a solution.
  343   (5) hintExits   -- indicates the existence of a hint.
  344   (6) showHintLimit -- determines the number of attempts after which hint(s) will be shown
  345 
  346   (7) PROBLEM_GRADER_TO_USE -- chooses the problem grader to be used in this order
  347     (a) A problem grader specified by the problem using:
  348         install_problem_grader(\&grader);
  349     (b) One of the standard problem graders defined in PGanswermacros.pl when set to
  350         'std_problem_grader' or 'avg_problem_grader' by the environment variable
  351         $PG_environment{PROBLEM_GRADER_TO_USE}
  352     (c) A subroutine referenced by $PG_environment{PROBLEM_GRADER_TO_USE}
  353     (d) The default &std_problem_grader defined in PGanswermacros.pl
  354 
  355 
  356 =cut
  357 
  358 sub ENDDOCUMENT {
  359 
  360     my $index=0;
  361     foreach my $label (@PG_UNLABELED_ANSWERS) {
  362         if ( defined($PG_ANSWERS[$index]) ) {
  363         $PG_ANSWERS_HASH{"$label"}= $PG_ANSWERS[$index];
  364       #warn "recording answer label = $label";
  365       } else {
  366         warn "No answer provided by instructor for answer $label";
  367       }
  368       $index++;
  369     }
  370 
  371     $STRINGforOUTPUT .="\n";
  372    eval q{  #make sure that "main" points to the current safe compartment by evaluating these lines.
  373     $main::PG_FLAGS{'showPartialCorrectAnswers'} = $main::showPartialCorrectAnswers;
  374     $main::PG_FLAGS{'recordSubmittedAnswers'} = $main::recordSubmittedAnswers;
  375     $main::PG_FLAGS{'refreshCachedImages'} = $main::refreshCachedImages;
  376     $main::PG_FLAGS{'hintExists'} = $main::hintExists;
  377     $main::PG_FLAGS{'showHintLimit'} = $main::showHint;
  378     $main::PG_FLAGS{'solutionExists'} = $main::solutionExists;
  379     $main::PG_FLAGS{ANSWER_ENTRY_ORDER} = \@main::PG_ANSWER_ENTRY_ORDER;
  380     $main::PG_FLAGS{ANSWER_PREFIX} = $main::ANSWER_PREFIX;
  381     # install problem grader
  382     if (defined($main::PG_FLAGS{PROBLEM_GRADER_TO_USE}) ) {
  383       # problem grader defined within problem -- no further action needed
  384     } elsif ( defined( $main::envir{PROBLEM_GRADER_TO_USE} ) ) {
  385       if (ref($main::envir{PROBLEM_GRADER_TO_USE}) eq 'CODE' ) {         # user defined grader
  386         $main::PG_FLAGS{PROBLEM_GRADER_TO_USE} = $main::envir{PROBLEM_GRADER_TO_USE};
  387       } elsif ($main::envir{PROBLEM_GRADER_TO_USE} eq 'std_problem_grader' ) {
  388         if (defined(&std_problem_grader) ){
  389           $main::PG_FLAGS{PROBLEM_GRADER_TO_USE} = \&std_problem_grader; # defined in PGanswermacros.pl
  390         } # std_problem_grader is the default in any case so don't give a warning.
  391       } elsif ($main::envir{PROBLEM_GRADER_TO_USE} eq 'avg_problem_grader' ) {
  392         if (defined(&avg_problem_grader) ){
  393           $main::PG_FLAGS{PROBLEM_GRADER_TO_USE} = \&avg_problem_grader; # defined in PGanswermacros.pl
  394         }
  395         #else { # avg_problem_grader will be installed by PGtranslator so there is no need for a warning.
  396         # warn "The problem grader 'avg_problem_grader' has not been defined.  Has PGanswermacros.pl been loaded?";
  397         #}
  398       } else {
  399         warn "Error:  $main::PG_FLAGS{PROBLEM_GRADER_TO_USE} is not a known program grader.";
  400       }
  401     } elsif (defined(&std_problem_grader)) {
  402       $main::PG_FLAGS{PROBLEM_GRADER_TO_USE} = \&std_problem_grader; # defined in PGanswermacros.pl
  403     } else {
  404       # PGtranslator will install its default problem grader
  405     }
  406 
  407     warn "ERROR: The problem grader is not a subroutine" unless ref( $main::PG_FLAGS{PROBLEM_GRADER_TO_USE}) eq 'CODE'
  408                      or $main::PG_FLAGS{PROBLEM_GRADER_TO_USE} = 'std_problem_grader'
  409                      or $main::PG_FLAGS{PROBLEM_GRADER_TO_USE} = 'avg_problem_grader';
  410      # return results
  411     };
  412 
  413   (\$STRINGforOUTPUT, \$STRINGforHEADER_TEXT,\%PG_ANSWERS_HASH,eval(q!\%main::PG_FLAGS!));
  414 }
  415 
  416 
  417 
  418 =head2 INITIALIZE_PG()
  419 
  420 This is executed each C<DOCUMENT()> is called.  For backward compatibility
  421 C<loadMacros> also checks whether the C<macroDirectory> has been defined
  422 and if not, it runs C<INITIALIZE_PG()> and issues a warning.
  423 
  424 =cut
  425 
  426 
  427 1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9