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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5398 - (download) (as text) (annotate)
Wed Aug 22 19:04:25 2007 UTC (12 years, 6 months ago) by sh002i
File size: 22285 byte(s)
normalized documentation -- still needs a USAGE section though.

    1 ################################################################################
    2 # WeBWorK Program Generation Language
    3 # Copyright � 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader: pg/macros/PG.pl,v 1.32 2007/08/09 23:24:56 jj 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 PGchoicemacros.pl - Macros for multiple choice, matching, and true/false questions.
   20 
   21 =head1 SYNOPSIS
   22 
   23 Matching example:
   24 
   25  loadMacros("PGchoicemacros.pl");
   26 
   27  # create a new match list
   28  $ml = new_match_list();
   29 
   30  # enter three questions and their answers
   31  $ml->qa(
   32   "What color is a rose?",
   33   "Red",
   34   "What color is the sky?",
   35   "Blue",
   36   "What color is the sea?",
   37   "Green",
   38  );
   39 
   40  # choose two of these questions, ordered at random,
   41  # which will be printed in the problem.
   42  $ml->choose(2);
   43 
   44  # print the question and answer choices
   45  BEGIN_TEXT
   46  Match the answers below with these questions: $BR
   47  \{ $ml->print_q \} $BR
   48  Answers:
   49  \{ $ml->print_a \}
   50  END_TEXT
   51 
   52  # register the correct answer
   53  ANS($ml->ra_correct_ans);
   54 
   55 =head1 DESCRIPTION
   56 
   57 There are two types of choice macros. The older versions are simple subroutines.
   58 The newer versions involve the List class and its sub-classes and the use of
   59 objects based on these classes. The list sub-classes are:
   60 
   61 =over
   62 
   63 =item *
   64 
   65 B<Match>, which aids in setting up matching question and answer lists,
   66 
   67 =item *
   68 
   69 B<Select>, which aids in selecting and presenting a subset of questions with short
   70 answers (e.g. true/false questions) from a larger question set, and
   71 
   72 =item *
   73 
   74 B<Multiple>, which aids in setting up a standard one-question-many-answers multiple
   75 choice question.
   76 
   77 =back
   78 
   79 =cut
   80 
   81 BEGIN{
   82   be_strict;
   83 }
   84 
   85 package main;
   86 
   87 BEGIN {
   88   be_strict();
   89 }
   90 sub _PGchoicemacros_init{
   91 }
   92 
   93 =head1 MACROS
   94 
   95 =cut
   96 
   97 ################################################################################
   98 
   99 =head2 Match lists
  100 
  101 =over
  102 
  103 =item new_match_list
  104 
  105  $ml = new_match_list();
  106 
  107 new_match_list() creates a new Match object and initializes it with sensible
  108 defaults. It is equivalent to:
  109 
  110  $ml = new Match(random(1,2000,1), ~~&std_print_q, ~~&std_print_a);
  111 
  112 The first argument is the seed for the match list (choosen at random between 1
  113 and 2000 in the example above). The next two arguments are references to the
  114 print subroutines used to print the questions and the answers. Other printing
  115 methods can be used instead of the standard ones. An example of how to do this
  116 is demonstrated with pop_up_list_print_q() below.
  117 
  118 =cut
  119 
  120 sub new_match_list {
  121   new Match(random(1,2000,1), \&std_print_q, \&std_print_a);
  122 }
  123 
  124 =back
  125 
  126 =cut
  127 
  128 ################################################################################
  129 
  130 =head2 Select lists
  131 
  132 =over
  133 
  134 =item new_select_list
  135 
  136  $sl = new_select_list();
  137 
  138 new_select_list() creates a new Select object and initializes it with sensible
  139 defaults. It is equivalent to:
  140 
  141  $sl = new Select(random(1,2000,1), ~~&std_print_q, ~~&std_print_a);
  142 
  143 The parameters to the Select constructor are the same as those for the Match
  144 constrcutor described above under new_match_list().
  145 
  146 See the documentation for the Select class to see how to use this object to
  147 create a true/false question.
  148 
  149 std_print_a is only intended to be used for debugging with select lists, as
  150 there is rarely a reason to print out the answers to a select list.
  151 
  152 =cut
  153 
  154 sub new_select_list {
  155   new Select(random(1,2000,1), \&std_print_q, \&std_print_a);
  156 }
  157 
  158 =item new_pop_up_select_list()
  159 
  160  $sl = new_pop_up_select_list();
  161 
  162 new_popup_select_list() creates a new Select object and initializes it such that
  163 it will render as a popup list. It is equivalent to:
  164 
  165  $selectlist = new Select(random(1,2000,1), ~~&pop_up_list_print_q, ~~&std_print_a);
  166 
  167 =cut
  168 
  169 sub new_pop_up_select_list {
  170   new Select(random(1,2000,1), \&pop_up_list_print_q, \&std_print_a);
  171 }
  172 
  173 =back
  174 
  175 =cut
  176 
  177 ################################################################################
  178 
  179 =head2 Multiple choice quesitons
  180 
  181 =over
  182 
  183 =item new_multiple_choice()
  184 
  185  $mc = new_multiple_choice();
  186 
  187 new_multiple_choice() creates a new Multiple object that presents a question and
  188 a number possible answers, only one of which can be chosen. It is equivalent to:
  189 
  190  $mc = new Multiple(random(1,2000,1), ~~&std_print_q, ~~&radio_print_a);
  191 
  192 The parameters to the Multiple constructor are the same as those for the Match
  193 constrcutor described above under new_match_list().
  194 
  195 =cut
  196 
  197 sub new_multiple_choice {
  198   new Multiple(random(1,2000,1), \&std_print_q, \&radio_print_a);
  199 }
  200 
  201 =item new_checkbox_multiple_choice()
  202 
  203  $mc = new_checkbox_multiple_choice();
  204 
  205 new_checkbox_multiple_choice() creates a new Multiple object that presents a
  206 question and a number possible answers, any number of which can be chosen. It is
  207 equivalent to:
  208 
  209  $mc = new Multiple(random(1,2000,1), ~~&std_print_q, ~~&checkbox_print_a);
  210 
  211 =cut
  212 
  213 sub new_checkbox_multiple_choice {
  214   new Multiple(random(1,2000,1), \&std_print_q, \&checkbox_print_a);
  215 }
  216 
  217 =back
  218 
  219 =cut
  220 
  221 ################################################################################
  222 
  223 =head2 Question printing subroutines
  224 
  225 =over
  226 
  227 =item std_print_q()
  228 
  229  # $list can be a matching list, a select list, or a multiple choice list
  230  $list->rf_print_q(~~&std_print_q);
  231  TEXT($list->print_q);
  232 
  233 This formatting routine is the default method for formatting the way questions
  234 are printed for each of the three List sub-classes. It lists the questions
  235 vertically, numbering them sequentially and providing an answer blank before
  236 each question. std_print_q() checks which mode the user is trying to print the
  237 questions from and returns the appropriately formatted string.
  238 
  239 =cut
  240 
  241 #Standard method of printing questions in a matching or select list
  242 sub std_print_q {
  243     my $self = shift;
  244     my (@questions) = @_;
  245     my $length = $self->{ans_rule_len};
  246     my $out = "";
  247   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  248   if ($main::displayMode =~ /^HTML/) {
  249           my $i=1; my $quest; $out = "\n<P>\n";
  250     foreach $quest (@questions) {
  251        $out.= ans_rule($length) . "&nbsp;<B>$i.</B> $quest<BR>";
  252        $i++;
  253     }
  254   } elsif ($main::displayMode eq 'Latex2HTML') {
  255           my $i=1; my $quest; $out = "\\par\n";
  256     foreach $quest (@questions) {
  257        $out.= ans_rule($length) . "\\begin{rawhtml}<B>$i. </B>\\end{rawhtml} $quest\\begin{rawhtml}<BR>\\end{rawhtml}\n";
  258        $i++;
  259     }
  260   }  elsif ($main::displayMode eq 'TeX') {
  261       $out = "\n\\par\\begin{enumerate}\n";
  262       my $i=1; my $quest;
  263     foreach $quest (@questions) {
  264       $out .= "\\item[" . ans_rule($length) . "$i.] $quest\n";
  265       $i++;
  266     }
  267     $out .= "\\end{enumerate}\n";
  268   } else {
  269     $out = "Error: PGchoicemacros: std_print_q: Unknown displayMode: $main::displayMode.\n";
  270   }
  271   $out;
  272 
  273 }
  274 
  275 =item pop_up_list_print_q()
  276 
  277  $sl->rf_print_q(~~&pop_up_list_print_q);
  278  $sl->ra_pop_up_list([T => 'True', F => 'False']);
  279  TEXT($sl->print_q);
  280 
  281 Alternate method for print questions with pop up lists.
  282 
  283 This printing routine is used to print the questions for a true/false or other
  284 select list with a preceding pop up list of possible answers. A list of values
  285 and labels need to be given to the pop_up_list so that the intended answer is
  286 returned when a student selects an answer form the list. Note the use of => in
  287 the example above to associate the values on the left with the labels on the
  288 right, this means that, for instance, the student will see the word True in the
  289 pop_up_list but the answer that is returned to the grader is T, so that it
  290 corresponds with what the professor typed in as the answer when using
  291 $sl->qa('blah blah', 'T');
  292 
  293 =cut
  294 
  295 sub pop_up_list_print_q {
  296     my $self = shift;
  297     my (@questions) = @_;
  298     my $length = $self->{ans_rule_len};
  299     my @list = @{$self->{ra_pop_up_list} };
  300     my $out = "";
  301 
  302   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  303   if ($main::displayMode =~ /^HTML/) {
  304     my $i=1; my $quest;
  305     foreach $quest (@questions) {
  306        $out.= "\n<p>" . pop_up_list(@list) . "&nbsp;<B>$i.</B> $quest";
  307        $i++;
  308     }
  309     $out .= "<br>\n";
  310   } elsif ($main::displayMode eq 'Latex2HTML') {
  311     my $i=1; my $quest;
  312     foreach $quest (@questions) {
  313        $out.= " \\begin{rawhtml}<p><B>\\end{rawhtml}" . pop_up_list(@list) . " $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest";
  314        $i++;
  315     }
  316     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  317   }  elsif ($main::displayMode eq 'TeX') {
  318       $out = "\n\\par\\begin{enumerate}\n";
  319       my $i=1; my $quest;
  320     foreach $quest (@questions) {
  321       $out .= "\\item[" .  pop_up_list(@list) . "$i.] $quest\n";
  322       $i++;
  323     }
  324     $out .= "\\end{enumerate}\n";
  325   } else {
  326     $out = "Error: PGchoicemacros: pop_up_list_print_q: Unknown displayMode: $main::displayMode.\n";
  327   }
  328   $out;
  329 
  330 }
  331 
  332 
  333 # For graphs in a matching question.
  334 
  335 #sub format_graphs {
  336 # my $self = shift;
  337 # my @in = @_;
  338 # my $out = "";
  339 # while (@in) {
  340 #   $out .= shift(@in). "#" ;
  341 # }
  342 # $out;
  343 #}
  344 
  345 =item quest_first_pop_up_list_print_q()
  346 
  347  $sl->rf_print_q(~~&quest_first_pop_up_list_print_q);
  348  $sl->ra_pop_up_list([T => 'True', F => 'False']);
  349  TEXT($sl->print_q);
  350 
  351 Similar to pop_up_list_print_q(), but places the popup list after the question
  352 text in the output.
  353 
  354 =cut
  355 
  356 # To put pop-up-list at the end of a question.
  357 # contributed by Mark Schmitt 3-6-03
  358 sub quest_first_pop_up_list_print_q {
  359     my $self = shift;
  360     my (@questions) = @_;
  361     my $length = $self->{ans_rule_len};
  362     my @list = @{$self->{ra_pop_up_list} };
  363     my $out = "";
  364 
  365   if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth'
  366         || $main::displayMode eq 'HTML_jsMath'
  367         || $main::displayMode eq 'HTML_asciimath' || $main::displayMode eq 'HTML_LaTeXMathML'
  368               || $main::displayMode eq 'HTML_dpng'|| $main::displayMode eq 'HTML_img') {
  369     my $i=1; my $quest;
  370     foreach $quest (@questions) {
  371        $out.= "\n<p>" .  "&nbsp; $quest" . pop_up_list(@list);
  372        $i++;
  373     }
  374     $out .= "<br>\n";
  375   } elsif ($main::displayMode eq 'Latex2HTML') {
  376     my $i=1; my $quest;
  377     foreach $quest (@questions) {
  378        $out.= " \\begin{rawhtml}<p><B>\\end{rawhtml}" . pop_up_list(@list) . " $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest";
  379        $i++;
  380     }
  381     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  382   }  elsif ($main::displayMode eq 'TeX') {
  383       $out = "\n\\par\\begin{enumerate}\n";
  384       my $i=1; my $quest;
  385     foreach $quest (@questions) {
  386       $out .= "\\item[" .  pop_up_list(@list) . "$i.] $quest\n";
  387       $i++;
  388     }
  389     $out .= "\\end{enumerate}\n";
  390   } else {
  391     $out = "Error: PGchoicemacros: pop_up_list_print_q: Unknown displayMode: $main::displayMode.\n";
  392   }
  393   $out;
  394 
  395 }
  396 
  397 =item ans_in_middle_pop_up_list_print_q()
  398 
  399  $sl->rf_print_q(~~&ans_in_middle_pop_up_list_print_q);
  400  $sl->ra_pop_up_list([T => 'True', F => 'False']);
  401  TEXT($sl->print_q);
  402 
  403 Similar to quest_first_pop_up_list_print_q(), except that no linebreaks are
  404 printed between questions, allowing for the popup list to be placed in the
  405 middle of the text of a problem.
  406 
  407 =cut
  408 
  409 # To put pop-up-list in the middle of a question.
  410 # contributed by Mark Schmitt 3-6-03
  411 sub ans_in_middle_pop_up_list_print_q {
  412     my $self = shift;
  413     my (@questions) = @_;
  414     my $length = $self->{ans_rule_len};
  415     my @list = @{$self->{ra_pop_up_list} };
  416     my $out = "";
  417 
  418   if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth'
  419         || $main::displayMode eq 'HTML_jsMath'
  420         || $main::displayMode eq 'HTML_asciimath' || $main::displayMode eq 'HTML_LaTeXMathML'
  421               || $main::displayMode eq 'HTML_dpng'|| $main::displayMode eq 'HTML_img') {
  422     my $i=1; my $quest;
  423     foreach $quest (@questions) {
  424        $out.= "" .  "&nbsp; $quest" . pop_up_list(@list);
  425        $i++;
  426     }
  427     $out .= "";
  428   } elsif ($main::displayMode eq 'Latex2HTML') {
  429     my $i=1; my $quest;
  430     foreach $quest (@questions) {
  431        $out.= " \\begin{rawhtml}<p><B>\\end{rawhtml}" . pop_up_list(@list) . " $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest";
  432        $i++;
  433     }
  434     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  435   }  elsif ($main::displayMode eq 'TeX') {
  436       $out = "\n\\par\\begin{enumerate}\n";
  437       my $i=1; my $quest;
  438     foreach $quest (@questions) {
  439       $out .= "\\item[" .  pop_up_list(@list) . "$i.] $quest\n";
  440       $i++;
  441     }
  442     $out .= "\\end{enumerate}\n";
  443   } else {
  444     $out = "Error: PGchoicemacros: pop_up_list_print_q: Unknown displayMode: $main::displayMode.\n";
  445   }
  446   $out;
  447 
  448 }
  449 
  450 =item units_list_print_q
  451 
  452 A simple popup question printer. No question text is printed, instead the
  453 pop_up_list contents only are printed as a popup menu.
  454 
  455 =cut
  456 
  457 # Units for physics class
  458 # contributed by Mark Schmitt 3-6-03
  459 sub units_list_print_q {
  460     my $self = shift;
  461     my (@questions) = @_;
  462     my $length = $self->{ans_rule_len};
  463     my @list = @{$self->{ra_pop_up_list} };
  464     my $out = '';
  465 
  466   $out.= pop_up_list(@list);
  467 
  468     $out;
  469 }
  470 
  471 =back
  472 
  473 =cut
  474 
  475 ################################################################################
  476 
  477 =head2 Answer printing subroutines
  478 
  479 =over
  480 
  481 =item std_print_a
  482 
  483  # $list can be a matching list, a select list, or a multiple choice list
  484  $list->rf_print_a(~~&std_print_a);
  485  TEXT($list->print_a);
  486 
  487 This simple formatting routine is the default method for formatting the answers
  488 for matching lists.  It lists the answers vertically lettered sequentially.
  489 
  490 =cut
  491 
  492 #Standard method of printing answers in a matching list
  493 sub std_print_a {
  494   my $self = shift;
  495   my(@array) = @_;
  496   my $i = 0;
  497   my @alpha = ('A'..'Z', 'AA'..'ZZ');
  498   my $letter;
  499   my  $out=   &main::M3(
  500           "\\begin{enumerate}\n",
  501           " \\begin{rawhtml} <OL TYPE=\"A\" VALUE=\"1\"> \\end{rawhtml} ",
  502           # kludge to fix IE/CSS problem
  503           #"<OL COMPACT TYPE=\"A\" START=\"1\">\n"
  504           "<BLOCKQUOTE>\n"
  505   ) ;
  506   my $elem;
  507   foreach $elem (@array) {
  508     $letter = shift @alpha;
  509     $out .= &main::M3(
  510           "\\item[$main::ALPHABET[$i].] $elem\n",
  511           " \\begin{rawhtml} <LI> \\end{rawhtml} $elem  ",
  512           #"<LI> $elem</LI>\n"
  513           "<br /> <b>$letter.</b> $elem\n"
  514     ) ;
  515     $i++;
  516   }
  517   $out .= &main::M3(
  518         "\\end{enumerate}\n",
  519         " \\begin{rawhtml} </OL>\n \\end{rawhtml} ",
  520         #"</OL>\n"
  521         "</BLOCKQUOTE>\n"
  522   ) ;
  523   $out;
  524 
  525 }
  526 
  527 =item radio_print_a()
  528 
  529  # $list can be a matching list, a select list, or a multiple choice list
  530  $list->rf_print_q(~~&radio_print_q);
  531  TEXT($list->print_q);
  532 
  533 This simple printing routine is used to print the answers to multiple choice
  534 questions in a bulleted style with radio buttons preceding each possible answer.
  535 When a multiple choice object is created, a reference to radio_print_a is passed
  536 to that object so that it can be used from within the object later.
  537 
  538 radio_print_a checks which mode the user is trying to print the answers from and
  539 returns the appropriately formatted string.
  540 
  541 =cut
  542 
  543 #Alternate method of printing answers as a list of radio buttons for multiple choice
  544 sub radio_print_a {
  545     my $self = shift;
  546     my (@answers) = @_;
  547     my $out = "";
  548   my $i =0;
  549     my @in = ();
  550   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  551   if ($main::displayMode =~ /^HTML/) {
  552     foreach my $ans (@answers) {
  553       push (@in, ($main::ALPHABET[$i], "<B> $main::ALPHABET[$i]. </B> $ans"));
  554       $i++;
  555     }
  556     my @radio_buttons = ans_radio_buttons(@in);
  557     $out = "\n<BR>" . join "\n<BR>", @radio_buttons;
  558     $out .= "<BR>\n";
  559   } elsif ($main::displayMode eq 'Latex2HTML') {
  560     foreach my $ans (@answers) {
  561       push (@in, ($main::ALPHABET[$i], "\\begin{rawhtml}<B> $main::ALPHABET[$i]. </B> \\end{rawhtml} $ans"));
  562       $i++;
  563     }
  564     my @radio_buttons = ans_radio_buttons(@in);
  565     $out = "\\begin{rawhtml}<BR>\\end{rawhtml}" . join "\\begin{rawhtml}<BR>\\end{rawhtml}", @radio_buttons;
  566     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  567   }  elsif ($main::displayMode eq 'TeX') {
  568     foreach my $ans (@answers) {
  569       push (@in, ($main::ALPHABET[$i], "$main::ALPHABET[$i]. $ans"));
  570       $i++;
  571     }
  572     my @radio_buttons = ans_radio_buttons(@in);
  573     #$out = "\n\\par\\begin{itemize}\n";
  574     $out .= join '', @radio_buttons;
  575     #$out .= "\\end{itemize}\n";
  576   } else {
  577     $out = "Error: PGchoicemacros: radio_print_a: Unknown displayMode: $main::displayMode.\n";
  578   }
  579   $out;
  580 
  581 }
  582 
  583 =item checkbox_print_a()
  584 
  585  # $list can be a matching list, a select list, or a multiple choice list
  586  $list->rf_print_q(~~&radio_print_q);
  587  TEXT($list->print_q);
  588 
  589 This simple printing routine is used to print the answers to multiple choice
  590 questions in a bulleted style with checkboxes preceding each possible answer.
  591 When a multiple choice object is created, a reference to checkbox_print_a is passed
  592 to that object so that it can be used from within the object later.
  593 
  594 checkbox_print_a checks which mode the user is trying to print the answers from and
  595 returns the appropriately formatted string.
  596 
  597 =cut
  598 
  599 #Second alternate method of printing answers as a list of radio buttons for multiple choice
  600 #Method for naming radio buttons is no longer round about and hackish
  601 sub checkbox_print_a {
  602     my $self = shift;
  603     my (@answers) = @_;
  604     my $out = "";
  605   my $i =0;
  606     my @in = ();
  607 #   if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  608   if ($main::displayMode =~ /^HTML/) {
  609     foreach my $ans (@answers) {
  610       push (@in, ($main::ALPHABET[$i], "<B> $main::ALPHABET[$i]. </B> $ans"));
  611       $i++;
  612     }
  613     my @checkboxes = ans_checkbox(@in);
  614     $out = "\n<BR>" . join "\n<BR>", @checkboxes;
  615     $out .= "<BR>\n";
  616   } elsif ($main::displayMode eq 'Latex2HTML') {
  617     foreach my $ans (@answers) {
  618       push (@in, ($main::ALPHABET[$i], "\\begin{rawhtml}<B> $main::ALPHABET[$i]. </B> \\end{rawhtml} $ans"));
  619       $i++;
  620     }
  621     my @checkboxes = ans_checkbox(@in);
  622     $out = "\\begin{rawhtml}<BR>\\end{rawhtml}" . join "\\begin{rawhtml}<BR>\\end{rawhtml}", @checkboxes;
  623     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  624   }  elsif ($main::displayMode eq 'TeX') {
  625     foreach my $ans (@answers) {
  626       push (@in, ($main::ALPHABET[$i], "$main::ALPHABET[$i]. $ans"));
  627       $i++;
  628     }
  629     my @radio_buttons = ans_checkbox(@in);
  630     #$out = "\n\\par\\begin{itemize}\n";
  631     $out .= join '', @radio_buttons ;
  632     #$out .= "\\end{itemize}\n";
  633   } else {
  634     $out = "Error: PGchoicemacros: checkbox_print_a: Unknown displayMode: $main::displayMode.\n";
  635   }
  636   $out;
  637 
  638 }
  639 
  640 =back
  641 
  642 =cut
  643 
  644 ################################################################################
  645 
  646 =head2 Legacy macros
  647 
  648 These are maintained for backward compatibility. They can still be useful in
  649 constructing non-standard lists that don't fit the various list objects. In
  650 general the using the list objects is likely to give better results and is
  651 preferred.
  652 
  653 =over
  654 
  655 =item [DEPRECATED] qa()
  656 
  657  qa($questions, $answers, @new_qa);
  658 
  659 $questions and $answers are references to arrays, and @new_qa is a list of
  660 questions and answers to add to the $questions and $answers arrays.
  661 
  662 =cut
  663 
  664 sub qa {
  665   my($questionsRef,$answersRef,@questANDanswer) = @_;
  666   while (@questANDanswer) {
  667     push(@$questionsRef,shift(@questANDanswer));
  668     push(@$answersRef,shift(@questANDanswer));
  669 
  670   }
  671 }
  672 
  673 =item [DEPRECATED] invert()
  674 
  675  @b = invert(@a);
  676 
  677 Inverts an arrays values and indexes. For example, C<invert(1,2,4,8)> returns
  678 C<undef,0,1,undef,2,undef,undef,undef,4>.
  679 
  680 =cut
  681 
  682 sub invert {
  683   my @array = @_;
  684   my @out = ();
  685   my $i;
  686   for ($i=0;$i<=$#array;$i++) {
  687     $out[$array[$i]]=$i;
  688   }
  689   @out;
  690 }
  691 
  692 =item [DEPRECATED] NchooseK()
  693 
  694  @b = NchooseK($N, $K);
  695 
  696 Selects $K random nonrepeating elements in the range 0 to $N-1.
  697 
  698 =cut
  699 
  700 sub NchooseK {
  701   my($n,$k)=@_;;
  702   my @array = 0..($n-1);
  703   my @out = ();
  704   while (@out<$k) {
  705     push(@out, splice(@array,    random(0,$#array,1) ,         1) );
  706   }
  707   @out;
  708 }
  709 
  710 =item [DEPRECATED] shuffle()
  711 
  712  @b = shuffle($i);
  713 
  714 Returns the integers from 0 to $i-1 in random order.
  715 
  716 =cut
  717 
  718 sub shuffle {
  719   my ($i) = @_;
  720   my @out = &NchooseK($i,$i);
  721   @out;
  722 }
  723 
  724 =item [DEPRECATED] match_questions_list()
  725 
  726 =cut
  727 
  728 sub match_questions_list {
  729   my (@questions) = @_;
  730   my $out = "";
  731   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  732   if ($main::displayMode =~ /^HTML/) {
  733     my $i=1; my $quest;
  734     foreach $quest (@questions) {
  735        $out.= "\n<BR>" . ans_rule(4) . "<B>$i.</B> $quest";
  736        $i++;
  737     }
  738     $out .= "<br>\n";
  739   } elsif ($main::displayMode eq 'Latex2HTML') {
  740     my $i=1; my $quest;
  741     foreach $quest (@questions) {
  742        $out.= " \\begin{rawhtml}<BR>\\end{rawhtml} " . ans_rule(4) . "\\begin{rawhtml}<B>\\end{rawhtml} $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest"; #"$i.   $quest";
  743        $i++;
  744     }
  745     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  746   } elsif ($main::displayMode eq 'TeX') {
  747       $out = "\n\\par\\begin{enumerate}\n";
  748       my $i=1; my $quest;
  749     foreach $quest (@questions) {
  750       $out .= "\\item[" . ans_rule(3) . "$i.] $quest\n";
  751       $i++;
  752       }
  753     $out .= "\\end{enumerate}\n";
  754   } else {
  755     $out = "Error: PGchoicemacros: match_questions_list: Unknown displayMode: $main::displayMode.\n";
  756   }
  757   $out;
  758 }
  759 
  760 =item [DEPRECATED] match_questions_list_varbox()
  761 
  762 =cut
  763 
  764 sub match_questions_list_varbox {
  765   my ($length, @questions) = @_;
  766   my $out = "";
  767   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  768   if ($main::displayMode =~ /^HTML/) {
  769     my $i=1; my $quest;
  770     foreach $quest (@questions) {
  771       $out.=  "\n<BR>" . ans_rule($length) . "<B>$i.</B> $quest";
  772       $i++;
  773     }
  774     $out .= "<br>\n";
  775   } elsif ($main::displayMode eq 'Latex2HTML') {
  776     my $i=1; my $quest;
  777     foreach $quest (@questions) {
  778       $out.=  " \\begin{rawhtml}<BR>\\end{rawhtml} " . ans_rule($length) . "\\begin{rawhtml}<B>\\end{rawhtml} $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest"; #"$i.   $quest";
  779       $i++;
  780     }
  781     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  782   } elsif ($main::displayMode eq 'TeX') {
  783     $out = "\n\\par\\begin{enumerate}\n";
  784     my $i=1; my $quest;
  785     foreach $quest (@questions) {
  786       $out .= "\\item[" . ans_rule($length) . "$i.] $quest\n";
  787       $i++;
  788     }
  789     $out .= "\\end{enumerate}\n";
  790   } else {
  791     $out = "Error: PGchoicemacros: match_questions_list_varbox: Unknown displayMode: $main::displayMode.\n";
  792   }
  793   $out;
  794 }
  795 
  796 =back
  797 
  798 =cut
  799 
  800 1;
  801 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9