[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 6418 - (download) (as text) (annotate)
Tue Sep 7 21:19:14 2010 UTC (9 years, 4 months ago) by apizer
File size: 23022 byte(s)
Add MathJax display mode

    1 ################################################################################
    2 # WeBWorK Program Generation Language
    3 # Copyright � 2000-2007 The WeBWorK Project, http://openwebwork.sf.net/
    4 # $CVSHeader$
    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_MathJax'
  392    || $main::displayMode eq 'HTML_dpng'
  393    || $main::displayMode eq 'HTML'
  394    || $main::displayMode eq 'HTML_tth'
  395    || $main::displayMode eq 'HTML_jsMath'
  396    || $main::displayMode eq 'HTML_asciimath'
  397    || $main::displayMode eq 'HTML_LaTeXMathML'
  398    || $main::displayMode eq 'HTML_img') {
  399     my $i=1; my $quest;
  400     foreach $quest (@questions) {
  401        $out.= "\n<p>" .  "&nbsp; $quest" . pop_up_list(@list);
  402        $i++;
  403     }
  404     $out .= "<br>\n";
  405   } elsif ($main::displayMode eq 'Latex2HTML') {
  406     my $i=1; my $quest;
  407     foreach $quest (@questions) {
  408        $out.= " \\begin{rawhtml}<p><B>\\end{rawhtml}" . pop_up_list(@list) . " $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest";
  409        $i++;
  410     }
  411     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  412   }  elsif ($main::displayMode eq 'TeX') {
  413       $out = "\n\\par\\begin{enumerate}\n";
  414       my $i=1; my $quest;
  415     foreach $quest (@questions) {
  416       $out .= "\\item[" .  pop_up_list(@list) . "$i.] $quest\n";
  417       $i++;
  418     }
  419     $out .= "\\end{enumerate}\n";
  420   } else {
  421     $out = "Error: PGchoicemacros: pop_up_list_print_q: Unknown displayMode: $main::displayMode.\n";
  422   }
  423   $out;
  424 
  425 }
  426 
  427 =item ans_in_middle_pop_up_list_print_q()
  428 
  429  $sl->rf_print_q(~~&ans_in_middle_pop_up_list_print_q);
  430  $sl->ra_pop_up_list([T => 'True', F => 'False']);
  431  TEXT($sl->print_q);
  432 
  433 Similar to quest_first_pop_up_list_print_q(), except that no linebreaks are
  434 printed between questions, allowing for the popup list to be placed in the
  435 middle of the text of a problem.
  436 
  437 =cut
  438 
  439 # To put pop-up-list in the middle of a question.
  440 # contributed by Mark Schmitt 3-6-03
  441 
  442 # ^function ans_in_middle_pop_up_list_print_q
  443 
  444 sub ans_in_middle_pop_up_list_print_q {
  445     my $self = shift;
  446     my (@questions) = @_;
  447     my $length = $self->{ans_rule_len};
  448     my @list = @{$self->{ra_pop_up_list} };
  449     my $out = "";
  450 
  451   if ($main::displayMode eq 'HTML_MathJax'
  452    || $main::displayMode eq 'HTML_dpng'
  453    || $main::displayMode eq 'HTML'
  454    || $main::displayMode eq 'HTML_tth'
  455    || $main::displayMode eq 'HTML_jsMath'
  456    || $main::displayMode eq 'HTML_asciimath'
  457    || $main::displayMode eq 'HTML_LaTeXMathML'
  458    || $main::displayMode eq 'HTML_img') {
  459     my $i=1; my $quest;
  460     foreach $quest (@questions) {
  461        $out.= "" .  "&nbsp; $quest" . pop_up_list(@list);
  462        $i++;
  463     }
  464     $out .= "";
  465   } elsif ($main::displayMode eq 'Latex2HTML') {
  466     my $i=1; my $quest;
  467     foreach $quest (@questions) {
  468        $out.= " \\begin{rawhtml}<p><B>\\end{rawhtml}" . pop_up_list(@list) . " $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest";
  469        $i++;
  470     }
  471     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  472   }  elsif ($main::displayMode eq 'TeX') {
  473       $out = "\n\\par\\begin{enumerate}\n";
  474       my $i=1; my $quest;
  475     foreach $quest (@questions) {
  476       $out .= "\\item[" .  pop_up_list(@list) . "$i.] $quest\n";
  477       $i++;
  478     }
  479     $out .= "\\end{enumerate}\n";
  480   } else {
  481     $out = "Error: PGchoicemacros: pop_up_list_print_q: Unknown displayMode: $main::displayMode.\n";
  482   }
  483   $out;
  484 
  485 }
  486 
  487 =item units_list_print_q
  488 
  489 A simple popup question printer. No question text is printed, instead the
  490 pop_up_list contents only are printed as a popup menu.
  491 
  492 =cut
  493 
  494 # Units for physics class
  495 # contributed by Mark Schmitt 3-6-03
  496 
  497 # ^function units_list_print_q
  498 
  499 sub units_list_print_q {
  500     my $self = shift;
  501     my (@questions) = @_;
  502     my $length = $self->{ans_rule_len};
  503     my @list = @{$self->{ra_pop_up_list} };
  504     my $out = '';
  505 
  506   $out.= pop_up_list(@list);
  507 
  508     $out;
  509 }
  510 
  511 =back
  512 
  513 =cut
  514 
  515 ################################################################################
  516 
  517 =head2 Answer printing subroutines
  518 
  519 =over
  520 
  521 =item std_print_a
  522 
  523  # $list can be a matching list, a select list, or a multiple choice list
  524  $list->rf_print_a(~~&std_print_a);
  525  TEXT($list->print_a);
  526 
  527 This simple formatting routine is the default method for formatting the answers
  528 for matching lists.  It lists the answers vertically lettered sequentially.
  529 
  530 =cut
  531 
  532 #Standard method of printing answers in a matching list
  533 # ^function std_print_a
  534 sub std_print_a {
  535   my $self = shift;
  536   my(@array) = @_;
  537   my $i = 0;
  538   my @alpha = ('A'..'Z', 'AA'..'ZZ');
  539   my $letter;
  540   my  $out=   &main::M3(
  541           "\\begin{enumerate}\n",
  542           " \\begin{rawhtml} <OL TYPE=\"A\" VALUE=\"1\"> \\end{rawhtml} ",
  543           # kludge to fix IE/CSS problem
  544           #"<OL COMPACT TYPE=\"A\" START=\"1\">\n"
  545           "<BLOCKQUOTE>\n"
  546   ) ;
  547   my $elem;
  548   foreach $elem (@array) {
  549     $letter = shift @alpha;
  550     $out .= &main::M3(
  551           "\\item[$main::ALPHABET[$i].] $elem\n",
  552           " \\begin{rawhtml} <LI> \\end{rawhtml} $elem  ",
  553           #"<LI> $elem</LI>\n"
  554           "<br /> <b>$letter.</b> $elem\n"
  555     ) ;
  556     $i++;
  557   }
  558   $out .= &main::M3(
  559         "\\end{enumerate}\n",
  560         " \\begin{rawhtml} </OL>\n \\end{rawhtml} ",
  561         #"</OL>\n"
  562         "</BLOCKQUOTE>\n"
  563   ) ;
  564   $out;
  565 
  566 }
  567 
  568 =item radio_print_a()
  569 
  570  # $list can be a matching list, a select list, or a multiple choice list
  571  $list->rf_print_q(~~&radio_print_q);
  572  TEXT($list->print_q);
  573 
  574 This simple printing routine is used to print the answers to multiple choice
  575 questions in a bulleted style with radio buttons preceding each possible answer.
  576 When a multiple choice object is created, a reference to radio_print_a is passed
  577 to that object so that it can be used from within the object later.
  578 
  579 radio_print_a checks which mode the user is trying to print the answers from and
  580 returns the appropriately formatted string.
  581 
  582 =cut
  583 
  584 #Alternate method of printing answers as a list of radio buttons for multiple choice
  585 #Method for naming radio buttons is no longer round about and hackish
  586 
  587 # ^function radio_print_a
  588 sub radio_print_a {
  589     my $self = shift;
  590     my (@answers) = @_;
  591     my $out = "";
  592   my $i =0;
  593     my @in = ();
  594   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  595   if ($main::displayMode =~ /^HTML/) {
  596     foreach my $ans (@answers) {
  597       push (@in, ($main::ALPHABET[$i], "<B> $main::ALPHABET[$i]. </B> $ans"));
  598       $i++;
  599     }
  600     my @radio_buttons = ans_radio_buttons(@in);
  601     $out = "\n<BR>" . join "\n<BR>", @radio_buttons;
  602     $out .= "<BR>\n";
  603   } elsif ($main::displayMode eq 'Latex2HTML') {
  604     foreach my $ans (@answers) {
  605       push (@in, ($main::ALPHABET[$i], "\\begin{rawhtml}<B> $main::ALPHABET[$i]. </B> \\end{rawhtml} $ans"));
  606       $i++;
  607     }
  608     my @radio_buttons = ans_radio_buttons(@in);
  609     $out = "\\begin{rawhtml}<BR>\\end{rawhtml}" . join "\\begin{rawhtml}<BR>\\end{rawhtml}", @radio_buttons;
  610     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  611   }  elsif ($main::displayMode eq 'TeX') {
  612     foreach my $ans (@answers) {
  613       push (@in, ($main::ALPHABET[$i], "$main::ALPHABET[$i]. $ans"));
  614       $i++;
  615     }
  616     my @radio_buttons = ans_radio_buttons(@in);
  617     #$out = "\n\\par\\begin{itemize}\n";
  618     $out .= join '', @radio_buttons;
  619     #$out .= "\\end{itemize}\n";
  620   } else {
  621     $out = "Error: PGchoicemacros: radio_print_a: Unknown displayMode: $main::displayMode.\n";
  622   }
  623   $out;
  624 
  625 }
  626 
  627 =item checkbox_print_a()
  628 
  629  # $list can be a matching list, a select list, or a multiple choice list
  630  $list->rf_print_q(~~&radio_print_q);
  631  TEXT($list->print_q);
  632 
  633 This simple printing routine is used to print the answers to multiple choice
  634 questions in a bulleted style with checkboxes preceding each possible answer.
  635 When a multiple choice object is created, a reference to checkbox_print_a is passed
  636 to that object so that it can be used from within the object later.
  637 
  638 checkbox_print_a checks which mode the user is trying to print the answers from and
  639 returns the appropriately formatted string.
  640 
  641 =cut
  642 
  643 
  644 
  645 # ^function checkbox_print_a
  646 sub checkbox_print_a {
  647     my $self = shift;
  648     my (@answers) = @_;
  649     my $out = "";
  650   my $i =0;
  651     my @in = ();
  652 #   if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  653   if ($main::displayMode =~ /^HTML/) {
  654     foreach my $ans (@answers) {
  655       push (@in, ($main::ALPHABET[$i], "<B> $main::ALPHABET[$i]. </B> $ans"));
  656       $i++;
  657     }
  658     my @checkboxes = ans_checkbox(@in);
  659     $out = "\n<BR>" . join "\n<BR>", @checkboxes;
  660     $out .= "<BR>\n";
  661   } elsif ($main::displayMode eq 'Latex2HTML') {
  662     foreach my $ans (@answers) {
  663       push (@in, ($main::ALPHABET[$i], "\\begin{rawhtml}<B> $main::ALPHABET[$i]. </B> \\end{rawhtml} $ans"));
  664       $i++;
  665     }
  666     my @checkboxes = ans_checkbox(@in);
  667     $out = "\\begin{rawhtml}<BR>\\end{rawhtml}" . join "\\begin{rawhtml}<BR>\\end{rawhtml}", @checkboxes;
  668     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  669   }  elsif ($main::displayMode eq 'TeX') {
  670     foreach my $ans (@answers) {
  671       push (@in, ($main::ALPHABET[$i], "$main::ALPHABET[$i]. $ans"));
  672       $i++;
  673     }
  674     my @radio_buttons = ans_checkbox(@in);
  675     #$out = "\n\\par\\begin{itemize}\n";
  676     $out .= join '', @radio_buttons ;
  677     #$out .= "\\end{itemize}\n";
  678   } else {
  679     $out = "Error: PGchoicemacros: checkbox_print_a: Unknown displayMode: $main::displayMode.\n";
  680   }
  681   $out;
  682 
  683 }
  684 
  685 =back
  686 
  687 =cut
  688 
  689 ################################################################################
  690 
  691 =head2 Legacy macros
  692 
  693 These are maintained for backward compatibility. They can still be useful in
  694 constructing non-standard lists that don't fit the various list objects. In
  695 general the using the list objects is likely to give better results and is
  696 preferred.
  697 
  698 =over
  699 
  700 =item [DEPRECATED] qa()
  701 
  702  qa($questions, $answers, @new_qa);
  703 
  704 $questions and $answers are references to arrays, and @new_qa is a list of
  705 questions and answers to add to the $questions and $answers arrays.
  706 
  707 =cut
  708 
  709 # ^function qa   [DEPRECATED]
  710 #
  711 sub qa {
  712   my($questionsRef,$answersRef,@questANDanswer) = @_;
  713   while (@questANDanswer) {
  714     push(@$questionsRef,shift(@questANDanswer));
  715     push(@$answersRef,shift(@questANDanswer));
  716 
  717   }
  718 }
  719 
  720 =item [DEPRECATED] invert()
  721 
  722  @b = invert(@a);
  723 
  724 Inverts an arrays values and indexes. For example, C<invert(1,2,4,8)> returns
  725 C<undef,0,1,undef,2,undef,undef,undef,4>.
  726 
  727 =cut
  728 
  729 # ^function invert   [DEPRECATED]
  730 sub invert {
  731   my @array = @_;
  732   my @out = ();
  733   my $i;
  734   for ($i=0;$i<=$#array;$i++) {
  735     $out[$array[$i]]=$i;
  736   }
  737   @out;
  738 }
  739 
  740 =item [DEPRECATED] NchooseK()
  741 
  742  @b = NchooseK($N, $K);
  743 
  744 Selects $K random nonrepeating elements in the range 0 to $N-1.
  745 
  746 =cut
  747 
  748 # ^function NchooseK   [DEPRECATED]
  749 
  750 sub NchooseK {
  751   my($n,$k)=@_;;
  752   my @array = 0..($n-1);
  753   my @out = ();
  754   while (@out<$k) {
  755     push(@out, splice(@array,    random(0,$#array,1) ,         1) );
  756   }
  757   @out;
  758 }
  759 
  760 =item [DEPRECATED] shuffle()
  761 
  762  @b = shuffle($i);
  763 
  764 Returns the integers from 0 to $i-1 in random order.
  765 
  766 =cut
  767 
  768 # ^function shuffle   [DEPRECATED]
  769 
  770 sub shuffle {
  771   my ($i) = @_;
  772   my @out = &NchooseK($i,$i);
  773   @out;
  774 }
  775 
  776 =item [DEPRECATED] match_questions_list()
  777 
  778 =cut
  779 
  780 # ^function match_questions_list   [DEPRECATED]
  781 
  782 sub match_questions_list {
  783   my (@questions) = @_;
  784   my $out = "";
  785   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  786   if ($main::displayMode =~ /^HTML/) {
  787     my $i=1; my $quest;
  788     foreach $quest (@questions) {
  789        $out.= "\n<BR>" . ans_rule(4) . "<B>$i.</B> $quest";
  790        $i++;
  791     }
  792     $out .= "<br>\n";
  793   } elsif ($main::displayMode eq 'Latex2HTML') {
  794     my $i=1; my $quest;
  795     foreach $quest (@questions) {
  796        $out.= " \\begin{rawhtml}<BR>\\end{rawhtml} " . ans_rule(4) . "\\begin{rawhtml}<B>\\end{rawhtml} $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest"; #"$i.   $quest";
  797        $i++;
  798     }
  799     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  800   } elsif ($main::displayMode eq 'TeX') {
  801       $out = "\n\\par\\begin{enumerate}\n";
  802       my $i=1; my $quest;
  803     foreach $quest (@questions) {
  804       $out .= "\\item[" . ans_rule(3) . "$i.] $quest\n";
  805       $i++;
  806       }
  807     $out .= "\\end{enumerate}\n";
  808   } else {
  809     $out = "Error: PGchoicemacros: match_questions_list: Unknown displayMode: $main::displayMode.\n";
  810   }
  811   $out;
  812 }
  813 
  814 =item [DEPRECATED] match_questions_list_varbox()
  815 
  816 =cut
  817 
  818 # ^function match_questions_list_varbox   [DEPRECATED]
  819 
  820 sub match_questions_list_varbox {
  821   my ($length, @questions) = @_;
  822   my $out = "";
  823   #if ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') {
  824   if ($main::displayMode =~ /^HTML/) {
  825     my $i=1; my $quest;
  826     foreach $quest (@questions) {
  827       $out.=  "\n<BR>" . ans_rule($length) . "<B>$i.</B> $quest";
  828       $i++;
  829     }
  830     $out .= "<br>\n";
  831   } elsif ($main::displayMode eq 'Latex2HTML') {
  832     my $i=1; my $quest;
  833     foreach $quest (@questions) {
  834       $out.=  " \\begin{rawhtml}<BR>\\end{rawhtml} " . ans_rule($length) . "\\begin{rawhtml}<B>\\end{rawhtml} $i. \\begin{rawhtml}</B>\\end{rawhtml}   $quest"; #"$i.   $quest";
  835       $i++;
  836     }
  837     $out .= " \\begin{rawhtml}<BR>\\end{rawhtml} ";
  838   } elsif ($main::displayMode eq 'TeX') {
  839     $out = "\n\\par\\begin{enumerate}\n";
  840     my $i=1; my $quest;
  841     foreach $quest (@questions) {
  842       $out .= "\\item[" . ans_rule($length) . "$i.] $quest\n";
  843       $i++;
  844     }
  845     $out .= "\\end{enumerate}\n";
  846   } else {
  847     $out = "Error: PGchoicemacros: match_questions_list_varbox: Unknown displayMode: $main::displayMode.\n";
  848   }
  849   $out;
  850 }
  851 
  852 =back
  853 
  854 =cut
  855 
  856 1;
  857 

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9