[system] / trunk / webwork / system / courseScripts / PGbasicmacros.pl Repository:
ViewVC logotype

Diff of /trunk/webwork/system/courseScripts/PGbasicmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 29 Revision 30
82 $CARET, 82 $CARET,
83 $PI, 83 $PI,
84 $E, 84 $E,
85 @ALPHABET, 85 @ALPHABET,
86 ); 86 );
87_PGbasicmacros_init(); 87#_PGbasicmacros_init();
88sub _PGbasicmacros_init { 88sub _PGbasicmacros_init {
89 PG_restricted_eval( <<'EOF'); 89 PG_restricted_eval( <<'EOF');
90 $main::PAR = PAR(); 90 $main::PAR = PAR();
91 $main::BR = BR(); 91 $main::BR = BR();
92 $main::LQ = LQ(); 92 $main::LQ = LQ();
554#added 6/14/2000 by David Etlinger 554#added 6/14/2000 by David Etlinger
555sub ans_checkbox { 555sub ans_checkbox {
556 my $name = NEW_ANS_NAME( ++$main::ans_rule_count ); 556 my $name = NEW_ANS_NAME( ++$main::ans_rule_count );
557 my @checkboxes = NAMED_ANS_CHECKBOX_BUTTONS( $name, @_ ); 557 my @checkboxes = NAMED_ANS_CHECKBOX_BUTTONS( $name, @_ );
558 558
559 if ($main::displayMode eq 'TeX') { 559 if ($displayMode eq 'TeX') {
560 $checkboxes[0] = "\n\\begin{itemize}\n" . $checkboxes[0]; 560 $checkboxes[0] = "\n\\begin{itemize}\n" . $checkboxes[0];
561 $checkboxes[$#checkboxes] .= "\n\\end{itemize}\n"; 561 $checkboxes[$#checkboxes] .= "\n\\end{itemize}\n";
562 } 562 }
563 563
564 (wantarray) ? @checkboxes: join(" ", @checkboxes); 564 (wantarray) ? @checkboxes: join(" ", @checkboxes);
647 my @list = @_; 647 my @list = @_;
648 $name = RECORD_ANS_NAME($name); # record answer name 648 $name = RECORD_ANS_NAME($name); # record answer name
649 my $answer_value = ''; 649 my $answer_value = '';
650 $answer_value = ${$main::inputs_ref}{$name} if defined(${$main::inputs_ref}{$name}); 650 $answer_value = ${$main::inputs_ref}{$name} if defined(${$main::inputs_ref}{$name});
651 my $out = ""; 651 my $out = "";
652 if ($main::displayMode eq "HTML" or $main::displayMode eq "HTML_tth") { 652 if ($displayMode eq "HTML" or $displayMode eq "HTML_tth") {
653 $out = qq!<SELECT NAME = "$name" SIZE=1> \n!; 653 $out = qq!<SELECT NAME = "$name" SIZE=1> \n!;
654 my $i; 654 my $i;
655 foreach ($i=0; $i< @list; $i=$i+2) { 655 foreach ($i=0; $i< @list; $i=$i+2) {
656 my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : ""; 656 my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : "";
657 $out .= qq!<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\n!; 657 $out .= qq!<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\n!;
658 }; 658 };
659 $out .= " </SELECT>\n"; 659 $out .= " </SELECT>\n";
660 } elsif ( $main::displayMode eq "Latex2HTML") { 660 } elsif ( $displayMode eq "Latex2HTML") {
661 $out = qq! \\begin{rawhtml}<SELECT NAME = "$name" SIZE=1> \\end{rawhtml} \n !; 661 $out = qq! \\begin{rawhtml}<SELECT NAME = "$name" SIZE=1> \\end{rawhtml} \n !;
662 my $i; 662 my $i;
663 foreach ($i=0; $i< @list; $i=$i+2) { 663 foreach ($i=0; $i< @list; $i=$i+2) {
664 my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : ""; 664 my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : "";
665 $out .= qq!\\begin{rawhtml}<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\\end{rawhtml}\n!; 665 $out .= qq!\\begin{rawhtml}<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\\end{rawhtml}\n!;
666 }; 666 };
667 $out .= " \\begin{rawhtml}</SELECT>\\end{rawhtml}\n"; 667 $out .= " \\begin{rawhtml}</SELECT>\\end{rawhtml}\n";
668 } elsif ( $main::displayMode eq "TeX") { 668 } elsif ( $displayMode eq "TeX") {
669 $out .= "\\fbox{?}"; 669 $out .= "\\fbox{?}";
670 } 670 }
671 671
672} 672}
673 673
683 683
684 solution('text','text2',...); 684 solution('text','text2',...);
685 SOLUTION('text','text2',...); # equivalent to TEXT(solution(...)); 685 SOLUTION('text','text2',...); # equivalent to TEXT(solution(...));
686 686
687 hint('text', 'text2', ...); 687 hint('text', 'text2', ...);
688 HINT('text', 'text2',...); # equivalent to TEXT("$main::BR$main::HINT" . hint(@_) . "$main::BR") if hint(@_); 688 HINT('text', 'text2',...); # equivalent to TEXT("$BR$HINT" . hint(@_) . "$BR") if hint(@_);
689 689
690Solution prints its concatenated input when the check box named 'ShowSol' is set and 690Solution prints its concatenated input when the check box named 'ShowSol' is set and
691the time is after the answer date. (The variable C<$envir{'inputs_ref'}->>C<{'ShowSol'}> is set.) 691the time is after the answer date. (The variable C<$envir{'inputs_ref'}->>C<{'ShowSol'}> is set.)
692 692
693PG_FLAGS{'solutionExists'} is set to 1 when a solution is available for viewing. 693PG_FLAGS{'solutionExists'} is set to 1 when a solution is available for viewing.
694 694
695Hints are shown only after the number of attempts is greater than $main::showHint. 695Hints are shown only after the number of attempts is greater than $:showHint.
696($main::showHint defaults to 1.) 696($:showHint defaults to 1.)
697 697
698=cut 698=cut
699 699
700 700
701 701
735 my @in = @_; 735 my @in = @_;
736 my $out = ''; 736 my $out = '';
737 737
738 $main::showHint = 1 unless defined($main::showHint); 738 $main::showHint = 1 unless defined($main::showHint);
739 $main::numOfAttempts = 0 unless defined($main::numOfAttempts); 739 $main::numOfAttempts = 0 unless defined($main::numOfAttempts);
740 if ($main::displayMode eq 'TeX') { 740 if ($displayMode eq 'TeX') {
741 $out = ''; # do nothing since hints are not available for download 741 $out = ''; # do nothing since hints are not available for download
742 742
743 } elsif ($main::numOfAttempts >= $main::showHint ) { #numOfAttempts is only defined in interactive mode 743 } elsif ($main::numOfAttempts >= $main::showHint ) { #numOfAttempts is only defined in interactive mode
744 if ( ${$main::inputs_ref}{'ShowHint'} ) { 744 if ( ${$main::inputs_ref}{'ShowHint'} ) {
745 $out = join(' ',@in); # show hint 745 $out = join(' ',@in); # show hint
746 } elsif ($main::displayMode eq 'HTML' or $main::displayMode eq 'HTML_tth') { 746 } elsif ($displayMode eq 'HTML' or $displayMode eq 'HTML_tth') {
747 $out = checkbox(-name=>'ShowHint', 747 $out = checkbox(-name=>'ShowHint',
748 -value=>1, 748 -value=>1,
749 -label=>"Show Hint", 749 -label=>"Show Hint",
750 -override => 1 750 -override => 1
751 ) ; 751 ) ;
752 } elsif ($main::displayMode eq 'Latex2HTML') { 752 } elsif ($displayMode eq 'Latex2HTML') {
753 $out = '\\begin{rawhtml}' . checkbox(-name=>'ShowHint', 753 $out = '\\begin{rawhtml}' . checkbox(-name=>'ShowHint',
754 -value=>1, 754 -value=>1,
755 -label=>"Show Hint", 755 -label=>"Show Hint",
756 -override => 1 756 -override => 1
757 ) . '\\end{rawhtml}' ; 757 ) . '\\end{rawhtml}' ;
758 } else { 758 } else {
759 warn "Error: The subroutine hints doesn't know what to do in display mode $main::displayMode."; 759 warn "Error: The subroutine hints doesn't know what to do in display mode $displayMode.";
760 $out = ''; 760 $out = '';
761 } 761 }
762 } else { 762 } else {
763 # do nothing since the $numOf Attempts is not yet large enough. 763 # do nothing since the $numOf Attempts is not yet large enough.
764 $out = ''; 764 $out = '';
855 855
856# This replaces M3. You can add new modes at will to this one. 856# This replaces M3. You can add new modes at will to this one.
857 857
858sub MODES { 858sub MODES {
859 my %options = @_; 859 my %options = @_;
860 return $options{$main::displayMode} 860 return $options{$displayMode}
861 if defined( $options{$main::displayMode} ); 861 if defined( $options{$displayMode} );
862 862
863 # default searches. 863 # default searches.
864 if ($main::displayMode eq "Latex2HTML") { 864 if ($displayMode eq "Latex2HTML") {
865 return $options{TeX} 865 return $options{TeX}
866 if defined( $options{TeX} ); 866 if defined( $options{TeX} );
867 return $options{HTML} 867 return $options{HTML}
868 if defined( $options{HTML} ); 868 if defined( $options{HTML} );
869 die " ERROR in using MODES: 'HTML' and 'TeX' options not defined for 'Latex2HTML'"; 869 die " ERROR in using MODES: 'HTML' and 'TeX' options not defined for 'Latex2HTML'";
870 } 870 }
871 871
872 if ($main::displayMode eq "HTML_tth") { 872 if ($displayMode eq "HTML_tth") {
873 return $options{HTML} 873 return $options{HTML}
874 if defined( $options{HTML} ); 874 if defined( $options{HTML} );
875 die " ERROR in using MODES: 'HTML' option not defined for HTML_tth"; 875 die " ERROR in using MODES: 'HTML' option not defined for HTML_tth";
876 876
877 } 877 }
923 923
924=cut 924=cut
925 925
926 926
927 927
928
929
928# A utility variable. Notice that "B"=$ALPHABET[1] and 930# A utility variable. Notice that "B"=$ALPHABET[1] and
929# "ABCD"=@ALPHABET[0..3]. 931# "ABCD"=@ALPHABET[0..3].
930 932
931sub ALPHABET { 933sub ALPHABET {
932 ('A'..'ZZ')[@_]; 934 ('A'..'ZZ')[@_];
1174sub math_ev3 { 1176sub math_ev3 {
1175 my $in = shift; #print "in=$in<BR>"; 1177 my $in = shift; #print "in=$in<BR>";
1176 my ($out,$PG_eval_errors,$PG_full_error_report); 1178 my ($out,$PG_eval_errors,$PG_full_error_report);
1177 $in = FEQ($in); 1179 $in = FEQ($in);
1178 $in =~ s/%/\\%/g; # % causes trouble in TeX and HTML_tth it usually (always?) indicates an error, not comment 1180 $in =~ s/%/\\%/g; # % causes trouble in TeX and HTML_tth it usually (always?) indicates an error, not comment
1179 return("$main::BM $in $main::EM") unless ($main::displayMode eq 'HTML_tth'); 1181 return("$main::BM $in $main::EM") unless ($displayMode eq 'HTML_tth');
1180 $in = "\\(" . $in . "\\)"; 1182 $in = "\\(" . $in . "\\)";
1181 $out = tth($in); 1183 $out = tth($in);
1182 ($out,$PG_eval_errors,$PG_full_error_report); 1184 ($out,$PG_eval_errors,$PG_full_error_report);
1183 1185
1184} 1186}
1186sub display_math_ev3 { 1188sub display_math_ev3 {
1187 my $in = shift; #print "in=$in<BR>"; 1189 my $in = shift; #print "in=$in<BR>";
1188 my ($out,$PG_eval_errors,$PG_full_error_report); 1190 my ($out,$PG_eval_errors,$PG_full_error_report);
1189 $in = FEQ($in); 1191 $in = FEQ($in);
1190 $in =~ s/%/\\%/g; 1192 $in =~ s/%/\\%/g;
1191 return("$main::BDM $in $main::EDM") unless $main::displayMode eq 'HTML_tth' ; 1193 return("$main::BDM $in $main::EDM") unless $displayMode eq 'HTML_tth' ;
1192 $in = "\\[" . $in . "\\]"; 1194 $in = "\\[" . $in . "\\]";
1193 $out =tth($in); 1195 $out =tth($in);
1194 ($out,$PG_eval_errors,$PG_full_error_report); 1196 ($out,$PG_eval_errors,$PG_full_error_report);
1195} 1197}
1196 1198
1281 1283
1282# kludge to clean up path names 1284# kludge to clean up path names
1283 ## allow underscore character in set and section names and also allows line breaks at / 1285 ## allow underscore character in set and section names and also allows line breaks at /
1284sub protect_underbar { 1286sub protect_underbar {
1285 my $in = shift; 1287 my $in = shift;
1286 if ($main::displayMode eq 'TeX') { 1288 if ($displayMode eq 'TeX') {
1287 1289
1288 $in =~ s|_|\\\_|g; 1290 $in =~ s|_|\\\_|g;
1289 $in =~ s|/|\\\-/|g; # allows an optional hyphenation of the path (in tex) 1291 $in =~ s|/|\\\-/|g; # allows an optional hyphenation of the path (in tex)
1290 } 1292 }
1291 $in; 1293 $in;
1374 my @out = keys %temp; # sort is causing trouble with Safe.?? 1376 my @out = keys %temp; # sort is causing trouble with Safe.??
1375 @out; 1377 @out;
1376} 1378}
1377 1379
1378sub lex_sort { 1380sub lex_sort {
1379 PGsort sub {$_[0] cmp $_[1]}, @_; 1381 PGsort sub {$_[0] cmp $_[1]}, @_;
1380} 1382}
1381sub num_sort { 1383sub num_sort {
1382 PGsort sub {$_[0] <=> $_[1]}, @_; 1384 PGsort sub {$_[0] <=> $_[1]}, @_;
1383} 1385}
1384 1386
1412sub begintable { 1414sub begintable {
1413 my ($number)=shift; #number of columns in table 1415 my ($number)=shift; #number of columns in table
1414 my %options = @_; 1416 my %options = @_;
1415 warn "begintable(cols) requires a number indicating the number of columns" unless defined($number); 1417 warn "begintable(cols) requires a number indicating the number of columns" unless defined($number);
1416 my $out = ""; 1418 my $out = "";
1417 if ($main::displayMode eq 'TeX') { 1419 if ($displayMode eq 'TeX') {
1418 $out .= "\n\\par\\smallskip\\begin{center}\\begin{tabular}{" . "|c" x $number . "|} \\hline\n"; 1420 $out .= "\n\\par\\smallskip\\begin{center}\\begin{tabular}{" . "|c" x $number . "|} \\hline\n";
1419 } 1421 }
1420 elsif ($main::displayMode eq 'Latex2HTML') { 1422 elsif ($displayMode eq 'Latex2HTML') {
1421 $out .= "\n\\begin{rawhtml} <TABLE , BORDER=1>\n\\end{rawhtml}"; 1423 $out .= "\n\\begin{rawhtml} <TABLE , BORDER=1>\n\\end{rawhtml}";
1422 } 1424 }
1423 elsif ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') { 1425 elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth') {
1424 $out .= "<TABLE BORDER=1>\n" 1426 $out .= "<TABLE BORDER=1>\n"
1425 } 1427 }
1426 else { 1428 else {
1427 $out = "Error: PGchoicemacros: begintable: Unknown displayMode: $main::displayMode.\n"; 1429 $out = "Error: PGchoicemacros: begintable: Unknown displayMode: $displayMode.\n";
1428 } 1430 }
1429 $out; 1431 $out;
1430 } 1432 }
1431 1433
1432sub endtable { 1434sub endtable {
1433 my $out = ""; 1435 my $out = "";
1434 if ($main::displayMode eq 'TeX') { 1436 if ($displayMode eq 'TeX') {
1435 $out .= "\n\\end {tabular}\\end{center}\\par\\smallskip\n"; 1437 $out .= "\n\\end {tabular}\\end{center}\\par\\smallskip\n";
1436 } 1438 }
1437 elsif ($main::displayMode eq 'Latex2HTML') { 1439 elsif ($displayMode eq 'Latex2HTML') {
1438 $out .= "\n\\begin{rawhtml} </TABLE >\n\\end{rawhtml}"; 1440 $out .= "\n\\begin{rawhtml} </TABLE >\n\\end{rawhtml}";
1439 } 1441 }
1440 elsif ($main::displayMode eq 'HTML' || $main::displayMode eq 'HTML_tth') { 1442 elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth') {
1441 $out .= "</TABLE>\n"; 1443 $out .= "</TABLE>\n";
1442 } 1444 }
1443 else { 1445 else {
1444 $out = "Error: PGchoicemacros: endtable: Unknown displayMode: $main::displayMode.\n"; 1446 $out = "Error: PGchoicemacros: endtable: Unknown displayMode: $displayMode.\n";
1445 } 1447 }
1446 $out; 1448 $out;
1447 } 1449 }
1448 1450
1449 1451
1450sub row { 1452sub row {
1451 my @elements = @_; 1453 my @elements = @_;
1452 my $out = ""; 1454 my $out = "";
1453 if ($main::displayMode eq 'TeX') { 1455 if ($displayMode eq 'TeX') {
1454 while (@elements) { 1456 while (@elements) {
1455 $out .= shift(@elements) . " &"; 1457 $out .= shift(@elements) . " &";
1456 } 1458 }
1457 chop($out); # remove last & 1459 chop($out); # remove last &
1458 $out .= "\\\\ \\hline \n"; 1460 $out .= "\\\\ \\hline \n";
1459 # carriage returns must be added manually for tex 1461 # carriage returns must be added manually for tex
1460 } 1462 }
1461 elsif ($main::displayMode eq 'Latex2HTML') { 1463 elsif ($displayMode eq 'Latex2HTML') {
1462 $out .= "\n\\begin{rawhtml}\n<TR>\n\\end{rawhtml}\n"; 1464 $out .= "\n\\begin{rawhtml}\n<TR>\n\\end{rawhtml}\n";
1463 while (@elements) { 1465 while (@elements) {
1464 $out .= " \n\\begin{rawhtml}\n<TD> \n\\end{rawhtml}\n" . shift(@elements) . " \n\\begin{rawhtml}\n</TD> \n\\end{rawhtml}\n"; 1466 $out .= " \n\\begin{rawhtml}\n<TD> \n\\end{rawhtml}\n" . shift(@elements) . " \n\\begin{rawhtml}\n</TD> \n\\end{rawhtml}\n";
1465 } 1467 }
1466 $out .= " \n\\begin{rawhtml}\n</TR> \n\\end{rawhtml}\n"; 1468 $out .= " \n\\begin{rawhtml}\n</TR> \n\\end{rawhtml}\n";

Legend:
Removed from v.29  
changed lines
  Added in v.30

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9