[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 5707 - (download) (as text) (annotate)
Fri Jun 20 14:40:35 2008 UTC (11 years, 8 months ago) by gage
File size: 23033 byte(s)
An experimental collection of macros designed as a first attempt
to make writing sequential problems easier (Davide Cervone principal author)
The rules for using these macros may well change as we gain experience writing
sequential problems.  (See also PGsequentialmacros.pl for some additional, lower level tools.)

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9