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

Annotation of /trunk/pg/macros/PGbasicmacros.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5903 - (view) (download) (as text)

1 : sh002i 1050
2 : apizer 1080
3 : sh002i 1050 ####################################################################
4 :     # Copyright @ 1995-1998 University of Rochester
5 :     # All Rights Reserved
6 :     ####################################################################
7 :    
8 :     =head1 NAME
9 :    
10 :     PGbasicmacros.pl --- located in the courseScripts directory
11 :    
12 :     =head1 SYNPOSIS
13 :    
14 :    
15 :    
16 :     =head1 DESCRIPTION
17 :    
18 :    
19 :    
20 :     =cut
21 :    
22 :     # this is equivalent to use strict, but can be used within the Safe compartment.
23 :     BEGIN{
24 : gage 1267 be_strict;
25 : sh002i 1050 }
26 :    
27 :    
28 : gage 1251 my $displayMode;
29 : sh002i 1050
30 :     my ($PAR,
31 :     $BR,
32 :     $LQ,
33 :     $RQ,
34 :     $BM,
35 :     $EM,
36 :     $BDM,
37 :     $EDM,
38 :     $LTS,
39 :     $GTS,
40 :     $LTE,
41 :     $GTE,
42 :     $BEGIN_ONE_COLUMN,
43 :     $END_ONE_COLUMN,
44 :     $SOL,
45 :     $SOLUTION,
46 :     $HINT,
47 : jj 3520 $COMMENT,
48 : sh002i 1050 $US,
49 :     $SPACE,
50 :     $BBOLD,
51 : apizer 1080 $EBOLD,
52 : sh002i 1050 $BITALIC,
53 :     $EITALIC,
54 :     $BCENTER,
55 :     $ECENTER,
56 :     $HR,
57 :     $LBRACE,
58 :     $RBRACE,
59 :     $LB,
60 :     $RB,
61 :     $DOLLAR,
62 :     $PERCENT,
63 :     $CARET,
64 :     $PI,
65 :     $E,
66 :     @ALPHABET,
67 : gage 1251 $envir,
68 : gage 1253 $PG_random_generator,
69 : gage 1267 $inputs_ref,
70 : gage 1462 $rh_sticky_answers,
71 :     $r_ans_rule_count,
72 : sh002i 1050 );
73 :    
74 :     sub _PGbasicmacros_init {
75 : gage 1251 # The big problem is that at compile time in the cached Safe compartment
76 :     # main:: has one definition, probably Safe::Root1::
77 :     # At runtime main has another definition Safe::Rootx:: where x is > 1
78 : apizer 1314
79 :     # It is important to
80 : gage 1251 # initialize the my variable version of $displayMode from the "runtime" version
81 :     # of main::displayMode
82 : apizer 1314
83 : gage 1251 $displayMode = main::PG_restricted_eval(q!$main::displayMode!);
84 :    
85 :     # This is initializes the remaining variables in the runtime main:: compartment.
86 : apizer 1314
87 : gage 1251 main::PG_restricted_eval( <<'EndOfFile');
88 : gage 1267 $displayMode = $displayMode;
89 : gage 1251
90 : gage 1286 $main::PAR = PAR();
91 :     $main::BR = BR();
92 : sh002i 1050 $main::LQ = LQ();
93 :     $main::RQ = RQ();
94 : gage 1286 $main::BM = BM();
95 :     $main::EM = EM();
96 : sh002i 1050 $main::BDM = BDM();
97 :     $main::EDM = EDM();
98 :     $main::LTS = LTS();
99 :     $main::GTS = GTS();
100 :     $main::LTE = LTE();
101 :     $main::GTE = GTE();
102 :     $main::BEGIN_ONE_COLUMN = BEGIN_ONE_COLUMN();
103 :     $main::END_ONE_COLUMN = END_ONE_COLUMN();
104 :     $main::SOL = SOLUTION_HEADING();
105 :     $main::SOLUTION = SOLUTION_HEADING();
106 :     $main::HINT = HINT_HEADING();
107 :     $main::US = US();
108 :     $main::SPACE = SPACE();
109 : gage 1286 $main::BBOLD = BBOLD();
110 :     $main::EBOLD = EBOLD();
111 : sh002i 1050 $main::BITALIC = BITALIC();
112 :     $main::EITALIC = EITALIC();
113 :     $main::BCENTER = BCENTER();
114 :     $main::ECENTER = ECENTER();
115 :     $main::HR = HR();
116 :     $main::LBRACE = LBRACE();
117 :     $main::RBRACE = RBRACE();
118 :     $main::LB = LB();
119 :     $main::RB = RB();
120 :     $main::DOLLAR = DOLLAR();
121 :     $main::PERCENT = PERCENT();
122 :     $main::CARET = CARET();
123 :     $main::PI = PI();
124 :     $main::E = E();
125 :     @main::ALPHABET = ('A'..'ZZ');
126 : gage 1462 %main::STICKY_ANSWERS = ();
127 : apizer 1080
128 : gage 1251
129 :     EndOfFile
130 :    
131 :     # Next we transfer the correct definitions in the main:: compartment to the local my variables
132 :     # This can't be done inside the eval above because my variables seem to be invisible inside the eval
133 :    
134 :    
135 : gage 1286 $PAR = PAR();
136 :     $BR = BR();
137 :     $LQ = LQ();
138 :     $RQ = RQ();
139 :     $BM = BM();
140 :     $EM = EM();
141 :     $BDM = BDM();
142 :     $EDM = EDM();
143 :     $LTS = LTS();
144 :     $GTS = GTS();
145 :     $LTE = LTE();
146 :     $GTE = GTE();
147 :     $BEGIN_ONE_COLUMN = BEGIN_ONE_COLUMN();
148 :     $END_ONE_COLUMN = END_ONE_COLUMN();
149 :     $SOL = SOLUTION_HEADING();
150 :     $SOLUTION = SOLUTION_HEADING();
151 :     $HINT = HINT_HEADING();
152 :     $US = US();
153 :     $SPACE = SPACE();
154 :     $BBOLD = BBOLD();
155 :     $EBOLD = EBOLD();
156 :     $BITALIC = BITALIC();
157 :     $EITALIC = EITALIC();
158 :     $BCENTER = BCENTER();
159 :     $ECENTER = ECENTER();
160 :     $HR = HR();
161 :     $LBRACE = LBRACE();
162 :     $RBRACE = RBRACE();
163 :     $LB = LB();
164 :     $RB = RB();
165 :     $DOLLAR = DOLLAR();
166 :     $PERCENT = PERCENT();
167 :     $CARET = CARET();
168 :     $PI = PI();
169 :     $E = E();
170 :     @ALPHABET = ('A'..'ZZ');
171 : sh002i 1050
172 : gage 1253 $envir = PG_restricted_eval(q!\%main::envir!);
173 :     $PG_random_generator = PG_restricted_eval(q!$main::PG_random_generator!);
174 : gage 1267 $inputs_ref = $envir{inputs_ref};
175 : gage 1462 $rh_sticky_answers = PG_restricted_eval(q!\%main::STICKY_ANSWERS!);
176 :     $r_ans_rule_count = PG_restricted_eval(q!\$ans_rule_count!);
177 : sh002i 1050 }
178 :    
179 :     =head2 Answer blank macros:
180 :    
181 :     These produce answer blanks of various sizes or pop up lists or radio answer buttons.
182 :     The names for the answer blanks are
183 :     generated implicitly.
184 :    
185 :     ans_rule( width )
186 :     tex_ans_rule( width )
187 :     ans_radio_buttons(value1=>label1, value2,label2 => value3,label3=>...)
188 : jj 3704 pop_up_list(@list) # list consists of (value => label, PR => "Product rule",...)
189 :     pop_up_list([@list]) # list consists of values
190 : sh002i 1050
191 : jj 3704 In the last case, one can use C<pop_up_list(['?', 'yes', 'no'])> to produce a
192 :     pop-up list containing the three strings listed, and then use str_cmp to check
193 :     the answer.
194 :    
195 : sh002i 1050 To indicate the checked position of radio buttons put a '%' in front of the value: C<ans_radio_buttons(1, 'Yes','%2','No')>
196 :     will have 'No' checked. C<tex_ans_rule> works inside math equations in C<HTML_tth> mode. It does not work in C<Latex2HTML> mode
197 :     since this mode produces gif pictures.
198 :    
199 :    
200 :     The following method is defined in F<PG.pl> for entering the answer evaluators corresponding
201 :     to answer rules with automatically generated names. The answer evaluators are matched with the
202 :     answer rules in the order in which they appear on the page.
203 :    
204 :     ANS(ans_evaluator1, ans_evaluator2,...);
205 :    
206 :     These are more primitive macros which produce answer blanks for specialized cases when complete
207 :     control over the matching of answers blanks and answer evaluators is desired.
208 :     The names of the answer blanks must be generated manually, and it is best if they do NOT begin
209 :     with the default answer prefix (currently AnSwEr).
210 :    
211 :     labeled_ans_rule(name, width) # an alias for NAMED_ANS_RULE where width defaults to 20 if omitted.
212 :    
213 :     NAMED_ANS_RULE(name, width)
214 :     NAMED_ANS_BOX(name, rows, cols)
215 :     NAMED_ANS_RADIO(name, value,label,)
216 :     NAMED_ANS_RADIO_EXTENSION(name, value,label)
217 :     NAMED_ANS_RADIO_BUTTONS(name,value1,label1,value2,label2,...)
218 :     check_box('-name' =>answer5,'-value' =>'statement3','-label' =>'I loved this course!' )
219 :     NAMED_POP_UP_LIST($name, @list) # list consists of (value => tag, PR => "Product rule",...)
220 : jj 3704 NAMED_POP_UP_LIST($name, [@list]) # list consists of a list of values (and each tag will be set to the corresponding value)
221 : sh002i 1050
222 :     (Name is the name of the variable, value is the value given to the variable when this option is selected,
223 :     and label is the text printed next to the button or check box. Check box variables can have multiple values.)
224 :    
225 :     NAMED_ANS_RADIO_BUTTONS creates a sequence of NAMED_ANS_RADIO and NAMED_ANS_RADIO_EXTENSION items which
226 :     are output either as an array or, in scalar context, as the array glued together with spaces. It is
227 :     usually easier to use this than to manually construct the radio buttons by hand. However, sometimes
228 :     extra flexibility is desiredin which case:
229 :    
230 :     When entering radio buttons using the "NAMED" format, you should use NAMED_ANS_RADIO button for the first button
231 :     and then use NAMED_ANS_RADIO_EXTENSION for the remaining buttons. NAMED_ANS_RADIO requires a matching answer evalutor,
232 :     while NAMED_ANS_RADIO_EXTENSION does not. The name used for NAMED_ANS_RADIO_EXTENSION should match the name
233 :     used for NAMED_ANS_RADIO (and the associated answer evaluator).
234 :    
235 :    
236 :     The following method is defined in F<PG.pl> for entering the answer evaluators corresponding
237 :     to answer rules with automatically generated names. The answer evaluators are matched with the
238 :     answer rules in the order in which they appear on the page.
239 :    
240 :     NAMED_ANS(name1 => ans_evaluator1, name2 => ans_evaluator2,...);
241 :    
242 :     These auxiliary macros are defined in PG.pl
243 :    
244 :    
245 :     NEW_ANS_NAME( number ); # produces a new answer blank name from a number by adding a prefix (AnSwEr)
246 :     # and registers this name as an implicitly labeled answer
247 :     # Its use is paired with each answer evaluator being entered using ANS()
248 :    
249 :     ANS_NUM_TO_NAME(number); # adds the prefix (AnSwEr) to the number, but does nothing else.
250 :    
251 :     RECORD_ANS_NAME( name ); # records the order in which the answer blank is rendered
252 :     # This is called by all of the constructs above, but must
253 :     # be called explicitly if an input blank is constructed explictly
254 :     # using HTML code.
255 :    
256 :     These are legacy macros:
257 :    
258 :     ANS_RULE( number, width ); # equivalent to NAMED_ANS_RULE( NEW_ANS_NAME(number), width)
259 :     ANS_BOX( question_number,height, width ); # equivalent to NAMED_ANS_BOX( NEW_ANS_NAME(number), height, width)
260 :     ANS_RADIO( question_number, value,tag ); # equivalent to NAMED_ANS_RADIO( NEW_ANS_NAME(number), value,tag)
261 :     ANS_RADIO_OPTION( question_number, value,tag ); # equivalent to NAMED_ANS_RADIO_EXTENSION( ANS_NUM_TO_NAME(number), value,tag)
262 :    
263 :    
264 :     =cut
265 :    
266 : gage 1267
267 :    
268 : sh002i 1050 sub labeled_ans_rule { # syntactic sugar for NAMED_ANS_RULE
269 :     my($name,$col) = @_;
270 :     $col = 20 unless defined($col);
271 :     NAMED_ANS_RULE($name,$col);
272 :     }
273 :    
274 :     sub NAMED_ANS_RULE {
275 :     my($name,$col) = @_;
276 :     my $answer_value = '';
277 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
278 : sh002i 1050 if ($answer_value =~ /\0/ ) {
279 :     my @answers = split("\0", $answer_value);
280 :     $answer_value = shift(@answers); # use up the first answer
281 : gage 1462 $rh_sticky_answers->{$name}=\@answers;
282 : apizer 1314 # store the rest -- beacuse this stores to a main:; variable
283 :     # it must be evaluated at run time
284 : sh002i 1050 $answer_value= '' unless defined($answer_value);
285 :     } elsif (ref($answer_value) eq 'ARRAY') {
286 :     my @answers = @{ $answer_value};
287 :     $answer_value = shift(@answers); # use up the first answer
288 : gage 1462 $rh_sticky_answers->{$name}=\@answers;
289 : apizer 1314 # store the rest -- beacuse this stores to a main:; variable
290 :     # it must be evaluated at run time
291 : sh002i 1050 $answer_value= '' unless defined($answer_value);
292 : apizer 1080 }
293 :    
294 : sh002i 5175 $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
295 : jj 3543 $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer
296 : sh002i 1050 $name = RECORD_ANS_NAME($name);
297 : apizer 1379
298 : apizer 1385 my $tcol = $col/2 > 3 ? $col/2 : 3; ## get max
299 :     $tcol = $tcol < 40 ? $tcol : 40; ## get min
300 : apizer 1379
301 : sh002i 1050 MODES(
302 : apizer 1379 TeX => "\\mbox{\\parbox[t]{${tcol}ex}{\\hrulefill}}",
303 :     Latex2HTML => qq!\\begin{rawhtml}<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"\">\\end{rawhtml}!,
304 : dpvc 2292 HTML => qq!<INPUT TYPE=TEXT SIZE=$col NAME="$name" VALUE="$answer_value">!.
305 :     qq!<INPUT TYPE=HIDDEN NAME="previous_$name" VALUE="$answer_value">!
306 : sh002i 1050 );
307 :     }
308 :    
309 : gage 5626 sub NAMED_HIDDEN_ANS_RULE { # this is used to hold information being passed into and out of applets
310 :     # -- preserves state -- identical to NAMED_ANS_RULE except input type "hidden"
311 :     my($name,$col) = @_;
312 :     my $answer_value = '';
313 :     $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
314 :     if ($answer_value =~ /\0/ ) {
315 :     my @answers = split("\0", $answer_value);
316 :     $answer_value = shift(@answers); # use up the first answer
317 :     $rh_sticky_answers->{$name}=\@answers;
318 :     # store the rest -- beacuse this stores to a main:; variable
319 :     # it must be evaluated at run time
320 :     $answer_value= '' unless defined($answer_value);
321 :     } elsif (ref($answer_value) eq 'ARRAY') {
322 :     my @answers = @{ $answer_value};
323 :     $answer_value = shift(@answers); # use up the first answer
324 :     $rh_sticky_answers->{$name}=\@answers;
325 :     # store the rest -- beacuse this stores to a main:; variable
326 :     # it must be evaluated at run time
327 :     $answer_value= '' unless defined($answer_value);
328 :     }
329 :    
330 :     $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
331 :     $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer
332 :     $name = RECORD_ANS_NAME($name);
333 :    
334 :     my $tcol = $col/2 > 3 ? $col/2 : 3; ## get max
335 :     $tcol = $tcol < 40 ? $tcol : 40; ## get min
336 :    
337 :     MODES(
338 :     TeX => "\\mbox{\\parbox[t]{${tcol}ex}{\\hrulefill}}",
339 :     Latex2HTML => qq!\\begin{rawhtml}<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"\">\\end{rawhtml}!,
340 :     HTML => qq!<INPUT TYPE=HIDDEN SIZE=$col NAME="$name" VALUE="$answer_value">!.
341 :     qq!<INPUT TYPE=HIDDEN NAME="previous_$name" VALUE="$answer_value">!
342 :     );
343 :     }
344 : sh002i 1050 sub NAMED_ANS_RULE_OPTION { # deprecated
345 :     &NAMED_ANS_RULE_EXTENSION;
346 :     }
347 :    
348 :     sub NAMED_ANS_RULE_EXTENSION {
349 :     my($name,$col) = @_;
350 :     my $answer_value = '';
351 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
352 : gage 1462 if ( defined( $rh_sticky_answers->{$name} ) ) {
353 :     $answer_value = shift( @{ $rh_sticky_answers->{$name} });
354 : sh002i 1050 $answer_value = '' unless defined($answer_value);
355 :     }
356 : sh002i 5175 $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
357 : jj 3543 $answer_value =~ s/\s+/ /g; ## remove excessive whitespace from student answer
358 : dpvc 3268 my $tcol = $col/2 > 3 ? $col/2 : 3; ## get max
359 :     $tcol = $tcol < 40 ? $tcol : 40; ## get min
360 : sh002i 1050 MODES(
361 : dpvc 3268 TeX => "\\mbox{\\parbox[t]{${tcol}ex}{\\hrulefill}}",
362 : sh002i 1050 Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"\">\n\\end{rawhtml}\n!,
363 : dpvc 3268 HTML => qq!<INPUT TYPE=TEXT SIZE=$col NAME = "$name" VALUE = "$answer_value">!.
364 :     qq!<INPUT TYPE=HIDDEN NAME="previous_$name" VALUE = "$answer_value">!
365 : sh002i 1050 );
366 :     }
367 :    
368 :     sub ANS_RULE { #deprecated
369 :     my($number,$col) = @_;
370 :     my $name = NEW_ANS_NAME($number);
371 :     NAMED_ANS_RULE($name,$col);
372 :     }
373 :    
374 :    
375 :     sub NAMED_ANS_BOX {
376 :     my($name,$row,$col) = @_;
377 :     $row = 10 unless defined($row);
378 :     $col = 80 unless defined($col);
379 :     $name = RECORD_ANS_NAME($name);
380 :     my $height = .07*$row;
381 :     my $answer_value = '';
382 : gage 1267 $answer_value = $inputs_ref->{$name} if defined( $inputs_ref->{$name} );
383 : gage 5816 # $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
384 : sh002i 1050 my $out = M3(
385 :     qq!\\vskip $height in \\hrulefill\\quad !,
386 :     qq!\\begin{rawhtml}<TEXTAREA NAME="$name" ROWS="$row" COLS="$col"
387 :     WRAP="VIRTUAL">$answer_value</TEXTAREA>\\end{rawhtml}!,
388 :     qq!<TEXTAREA NAME="$name" ROWS="$row" COLS="$col"
389 : gage 2061 WRAP="VIRTUAL">$answer_value</TEXTAREA>
390 :     <INPUT TYPE=HIDDEN NAME="previous_$name" VALUE = "$answer_value">
391 :     !
392 : sh002i 1050 );
393 :     $out;
394 :     }
395 :    
396 :     sub ANS_BOX { #deprecated
397 :     my($number,$row,$col) = @_;
398 :     my $name = NEW_ANS_NAME($number);
399 :     NAMED_ANS_BOX($name,$row,$col);
400 :     }
401 :    
402 :     sub NAMED_ANS_RADIO {
403 :     my $name = shift;
404 :     my $value = shift;
405 :     my $tag =shift;
406 :     $name = RECORD_ANS_NAME($name);
407 :     my $checked = '';
408 :     if ($value =~/^\%/) {
409 :     $value =~ s/^\%//;
410 :     $checked = 'CHECKED'
411 :     }
412 : gage 1267 if (defined($inputs_ref->{$name}) ) {
413 :     if ($inputs_ref->{$name} eq $value) {
414 : sh002i 1050 $checked = 'CHECKED'
415 :     } else {
416 :     $checked = '';
417 :     }
418 :    
419 :     }
420 :    
421 :     MODES(
422 :     TeX => qq!\\item{$tag}\n!,
423 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
424 :     HTML => qq!<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>$tag!
425 :     );
426 :    
427 :     }
428 :    
429 :     sub NAMED_ANS_RADIO_OPTION { #deprecated
430 :     &NAMED_ANS_RADIO_EXTENSION;
431 :     }
432 :    
433 :     sub NAMED_ANS_RADIO_EXTENSION {
434 :     my $name = shift;
435 :     my $value = shift;
436 :     my $tag =shift;
437 :    
438 :    
439 :     my $checked = '';
440 :     if ($value =~/^\%/) {
441 :     $value =~ s/^\%//;
442 :     $checked = 'CHECKED'
443 :     }
444 : gage 1267 if (defined($inputs_ref->{$name}) ) {
445 :     if ($inputs_ref->{$name} eq $value) {
446 : sh002i 1050 $checked = 'CHECKED'
447 :     } else {
448 :     $checked = '';
449 :     }
450 :    
451 :     }
452 :    
453 :     MODES(
454 :     TeX => qq!\\item{$tag}\n!,
455 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
456 :     HTML => qq!<INPUT TYPE=RADIO NAME="$name" VALUE="$value" $checked>$tag!
457 :     );
458 :    
459 :     }
460 :    
461 :     sub NAMED_ANS_RADIO_BUTTONS {
462 :     my $name =shift;
463 :     my $value = shift;
464 :     my $tag = shift;
465 :    
466 :    
467 :     my @out = ();
468 :     push(@out, NAMED_ANS_RADIO($name, $value,$tag));
469 :     my @buttons = @_;
470 :     while (@buttons) {
471 :     $value = shift @buttons; $tag = shift @buttons;
472 :     push(@out, NAMED_ANS_RADIO_OPTION($name, $value,$tag));
473 :     }
474 :     (wantarray) ? @out : join(" ",@out);
475 :     }
476 :     sub ANS_RADIO {
477 :     my $number = shift;
478 :     my $value = shift;
479 :     my $tag =shift;
480 :     my $name = NEW_ANS_NAME($number);
481 :     NAMED_ANS_RADIO($name,$value,$tag);
482 :     }
483 :    
484 :     sub ANS_RADIO_OPTION {
485 :     my $number = shift;
486 :     my $value = shift;
487 :     my $tag =shift;
488 :    
489 :    
490 :     my $name = ANS_NUM_TO_NAME($number);
491 :     NAMED_ANS_RADIO_OPTION($name,$value,$tag);
492 :     }
493 :     sub ANS_RADIO_BUTTONS {
494 :     my $number =shift;
495 :     my $value = shift;
496 :     my $tag = shift;
497 :    
498 :    
499 :     my @out = ();
500 :     push(@out, ANS_RADIO($number, $value,$tag));
501 :     my @buttons = @_;
502 :     while (@buttons) {
503 :     $value = shift @buttons; $tag = shift @buttons;
504 :     push(@out, ANS_RADIO_OPTION($number, $value,$tag));
505 :     }
506 :     (wantarray) ? @out : join(" ",@out);
507 :     }
508 : gage 1784 ##############################################
509 :     # contained_in( $elem, $array_reference or null separated string);
510 :     # determine whether element is equal
511 :     # ( in the sense of eq, not ==, ) to an element in the array.
512 :     ##############################################
513 :     sub contained_in {
514 :     my $element = shift;
515 :     my @input_list = @_;
516 :     my @output_list = ();
517 :     # Expand the list -- convert references to arrays to arrays
518 :     # Convert null separated strings to arrays
519 :     foreach my $item (@input_list ) {
520 :     if ($item =~ /\0/) {
521 :     push @output_list, split('\0', $item);
522 :     } elsif (ref($item) =~/ARRAY/) {
523 :     push @output_list, @{$item};
524 :     } else {
525 :     push @output_list, $item;
526 :     }
527 :     }
528 :    
529 :     my @match_list = grep {$element eq $_ } @output_list;
530 :     if ( @match_list ) {
531 :     return 1;
532 :     } else {
533 :     return 0;
534 :     }
535 :     }
536 : sh002i 1050
537 : gage 1784 ##########################
538 :     # If multiple boxes are checked then the $inputs_ref->{name }will be a null separated string
539 :     # or a reference to an array.
540 :     ##########################
541 :    
542 : sh002i 1050 sub NAMED_ANS_CHECKBOX {
543 :     my $name = shift;
544 :     my $value = shift;
545 :     my $tag =shift;
546 :     $name = RECORD_ANS_NAME($name);
547 :    
548 :     my $checked = '';
549 :     if ($value =~/^\%/) {
550 :     $value =~ s/^\%//;
551 :     $checked = 'CHECKED'
552 :     }
553 :    
554 : gage 1267 if (defined($inputs_ref->{$name}) ) {
555 : gage 1784 if ( contained_in($value, $inputs_ref->{$name} ) ) {
556 : sh002i 1050 $checked = 'CHECKED'
557 :     }
558 :     else {
559 :     $checked = '';
560 :     }
561 :    
562 :     }
563 :    
564 :     MODES(
565 :     TeX => qq!\\item{$tag}\n!,
566 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
567 :     HTML => qq!<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>$tag!
568 :     );
569 :    
570 :     }
571 :    
572 :     sub NAMED_ANS_CHECKBOX_OPTION {
573 :     my $name = shift;
574 :     my $value = shift;
575 :     my $tag =shift;
576 :    
577 :     my $checked = '';
578 :     if ($value =~/^\%/) {
579 :     $value =~ s/^\%//;
580 :     $checked = 'CHECKED'
581 :     }
582 :    
583 : gage 1267 if (defined($inputs_ref->{$name}) ) {
584 : gage 1784 if ( contained_in($value, $inputs_ref->{$name}) ) {
585 : sh002i 1050 $checked = 'CHECKED'
586 :     }
587 :     else {
588 :     $checked = '';
589 :     }
590 :    
591 :     }
592 :    
593 :     MODES(
594 :     TeX => qq!\\item{$tag}\n!,
595 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>\\end{rawhtml}$tag!,
596 :     HTML => qq!<INPUT TYPE=CHECKBOX NAME="$name" VALUE="$value" $checked>$tag!
597 :     );
598 :    
599 :     }
600 :    
601 :     sub NAMED_ANS_CHECKBOX_BUTTONS {
602 :     my $name =shift;
603 :     my $value = shift;
604 :     my $tag = shift;
605 :    
606 :     my @out = ();
607 :     push(@out, NAMED_ANS_CHECKBOX($name, $value,$tag));
608 :    
609 :     my @buttons = @_;
610 :     while (@buttons) {
611 :     $value = shift @buttons; $tag = shift @buttons;
612 :     push(@out, NAMED_ANS_CHECKBOX_OPTION($name, $value,$tag));
613 :     }
614 :    
615 :     (wantarray) ? @out : join(" ",@out);
616 :     }
617 :    
618 :     sub ANS_CHECKBOX {
619 :     my $number = shift;
620 :     my $value = shift;
621 :     my $tag =shift;
622 :     my $name = NEW_ANS_NAME($number);
623 :    
624 :     NAMED_ANS_CHECKBOX($name,$value,$tag);
625 :     }
626 :    
627 :     sub ANS_CHECKBOX_OPTION {
628 :     my $number = shift;
629 :     my $value = shift;
630 :     my $tag =shift;
631 :     my $name = ANS_NUM_TO_NAME($number);
632 :    
633 :     NAMED_ANS_CHECKBOX_OPTION($name,$value,$tag);
634 :     }
635 :    
636 : gage 1784
637 :    
638 : sh002i 1050 sub ANS_CHECKBOX_BUTTONS {
639 :     my $number =shift;
640 :     my $value = shift;
641 :     my $tag = shift;
642 :    
643 :     my @out = ();
644 :     push(@out, ANS_CHECKBOX($number, $value, $tag));
645 :    
646 :     my @buttons = @_;
647 :     while (@buttons) {
648 :     $value = shift @buttons; $tag = shift @buttons;
649 :     push(@out, ANS_CHECKBOX_OPTION($number, $value,$tag));
650 :     }
651 :    
652 :     (wantarray) ? @out : join(" ",@out);
653 :     }
654 :    
655 :     sub ans_rule {
656 :     my $len = shift; # gives the optional length of the answer blank
657 :     $len = 20 unless $len ;
658 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
659 : sh002i 1050 NAMED_ANS_RULE($name ,$len);
660 :     }
661 :     sub ans_rule_extension {
662 :     my $len = shift;
663 :     $len = 20 unless $len ;
664 : gage 1462 my $name = NEW_ANS_NAME($$r_ans_rule_count); # don't update the answer name
665 : sh002i 1050 NAMED_ANS_RULE($name ,$len);
666 :     }
667 :     sub ans_radio_buttons {
668 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
669 : sh002i 1050 my @radio_buttons = NAMED_ANS_RADIO_BUTTONS($name, @_);
670 :    
671 :     if ($displayMode eq 'TeX') {
672 :     $radio_buttons[0] = "\n\\begin{itemize}\n" . $radio_buttons[0];
673 :     $radio_buttons[$#radio_buttons] .= "\n\\end{itemize}\n";
674 :     }
675 :    
676 :     (wantarray) ? @radio_buttons: join(" ", @radio_buttons);
677 :     }
678 :    
679 :     #added 6/14/2000 by David Etlinger
680 :     sub ans_checkbox {
681 : gage 1267 my $name = NEW_ANS_NAME( inc_ans_rule_count() );
682 : sh002i 1050 my @checkboxes = NAMED_ANS_CHECKBOX_BUTTONS( $name, @_ );
683 :    
684 :     if ($displayMode eq 'TeX') {
685 :     $checkboxes[0] = "\n\\begin{itemize}\n" . $checkboxes[0];
686 :     $checkboxes[$#checkboxes] .= "\n\\end{itemize}\n";
687 :     }
688 :    
689 :     (wantarray) ? @checkboxes: join(" ", @checkboxes);
690 :     }
691 :    
692 :    
693 :     ## define a version of ans_rule which will work inside TeX math mode or display math mode -- at least for tth mode.
694 :     ## This is great for displayed fractions.
695 :     ## This will not work with latex2HTML mode since it creates gif equations.
696 :    
697 :     sub tex_ans_rule {
698 :     my $len = shift;
699 :     $len = 20 unless $len ;
700 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
701 : sh002i 1050 my $answer_rule = NAMED_ANS_RULE($name ,$len); # we don't want to create three answer rules in different modes.
702 :     my $out = MODES(
703 :     'TeX' => $answer_rule,
704 :     'Latex2HTML' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
705 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
706 :     'HTML_dpng' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
707 :     'HTML' => $answer_rule
708 :     );
709 : apizer 1080
710 : sh002i 1050 $out;
711 :     }
712 :     sub tex_ans_rule_extension {
713 :     my $len = shift;
714 :     $len = 20 unless $len ;
715 : gage 1462 my $name = NEW_ANS_NAME($$r_ans_rule_count);
716 : sh002i 1050 my $answer_rule = NAMED_ANS_RULE($name ,$len); # we don't want to create three answer rules in different modes.
717 :     my $out = MODES(
718 :     'TeX' => $answer_rule,
719 :     'Latex2HTML' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
720 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
721 :     'HTML_dpng' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
722 :     'HTML' => $answer_rule
723 :     );
724 : apizer 1080
725 : sh002i 1050 $out;
726 :     }
727 :     # still needs some cleanup.
728 :     sub NAMED_TEX_ANS_RULE {
729 :     my $name = shift;
730 :     my $len = shift;
731 :     $len = 20 unless $len ;
732 :     my $answer_rule = NAMED_ANS_RULE($name ,$len); # we don't want to create three answer rules in different modes.
733 :     my $out = MODES(
734 :     'TeX' => $answer_rule,
735 :     'Latex2HTML' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
736 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
737 :     'HTML_dpng' => '\\fbox{Answer boxes cannot be placed inside typeset equations}',
738 :     'HTML' => $answer_rule
739 :     );
740 : apizer 1080
741 : sh002i 1050 $out;
742 :     }
743 :     sub NAMED_TEX_ANS_RULE_EXTENSION {
744 :     my $name = shift;
745 :     my $len = shift;
746 :     $len = 20 unless $len ;
747 :     my $answer_rule = NAMED_ANS_RULE_EXTENSION($name ,$len); # we don't want to create three answer rules in different modes.
748 :     my $out = MODES(
749 :     'TeX' => $answer_rule,
750 :     'Latex2HTML' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
751 :     'HTML_tth' => '\\begin{rawhtml} '. $answer_rule.'\\end{rawhtml}',
752 :     'HTML_dpng' => '\fbox{Answer boxes cannot be placed inside typeset equations}',
753 :     'HTML' => $answer_rule
754 :     );
755 : apizer 1080
756 : sh002i 1050 $out;
757 :     }
758 :     sub ans_box {
759 :     my $row = shift;
760 :     my $col =shift;
761 :     $row = 5 unless $row;
762 :     $col = 80 unless $col;
763 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count());
764 : sh002i 1050 NAMED_ANS_BOX($name ,$row,$col);
765 :     }
766 :    
767 :     #this is legacy code; use ans_checkbox instead
768 :     sub checkbox {
769 :     my %options = @_;
770 :     qq!<INPUT TYPE="checkbox" NAME="$options{'-name'}" VALUE="$options{'-value'}">$options{'-label'}!
771 :     }
772 :    
773 :    
774 :     sub NAMED_POP_UP_LIST {
775 :     my $name = shift;
776 :     my @list = @_;
777 : jj 3704 if(ref($list[0]) eq 'ARRAY') {
778 :     my @list1 = @{$list[0]};
779 :     @list = map { $_ => $_ } @list1;
780 :     }
781 : sh002i 1050 $name = RECORD_ANS_NAME($name); # record answer name
782 :     my $answer_value = '';
783 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
784 : sh002i 1050 my $out = "";
785 :     if ($displayMode eq 'HTML' or $displayMode eq 'HTML_tth' or
786 : dpvc 4386 $displayMode eq 'HTML_dpng' or $displayMode eq 'HTML_img' or $displayMode eq 'HTML_jsMath' or
787 :     $displayMode eq 'HTML_asciimath' or $displayMode eq 'HTML_LaTeXMathML') {
788 : sh002i 1050 $out = qq!<SELECT NAME = "$name" SIZE=1> \n!;
789 :     my $i;
790 :     foreach ($i=0; $i< @list; $i=$i+2) {
791 :     my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : "";
792 :     $out .= qq!<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\n!;
793 :     };
794 :     $out .= " </SELECT>\n";
795 :     } elsif ( $displayMode eq "Latex2HTML") {
796 :     $out = qq! \\begin{rawhtml}<SELECT NAME = "$name" SIZE=1> \\end{rawhtml} \n !;
797 :     my $i;
798 :     foreach ($i=0; $i< @list; $i=$i+2) {
799 :     my $select_flag = ($list[$i] eq $answer_value) ? "SELECTED" : "";
800 :     $out .= qq!\\begin{rawhtml}<OPTION $select_flag VALUE ="$list[$i]" > $list[$i+1] </OPTION>\\end{rawhtml}\n!;
801 :     };
802 :     $out .= " \\begin{rawhtml}</SELECT>\\end{rawhtml}\n";
803 :     } elsif ( $displayMode eq "TeX") {
804 :     $out .= "\\fbox{?}";
805 :     }
806 :    
807 :     }
808 :    
809 :     sub pop_up_list {
810 :     my @list = @_;
811 : gage 1267 my $name = NEW_ANS_NAME(inc_ans_rule_count()); # get new answer name
812 : sh002i 1050 NAMED_POP_UP_LIST($name, @list);
813 :     }
814 :    
815 : lr003k 1120
816 :    
817 :     =head5 answer_matrix
818 :    
819 :     Usage \[ \{ answer_matrix(rows,columns,width_of_ans_rule, @options) \} \]
820 : apizer 1314
821 : lr003k 1120 Creates an array of answer blanks and passes it to display_matrix which returns
822 :     text which represents the matrix in TeX format used in math display mode. Answers
823 :     are then passed back to whatever answer evaluators you write at the end of the problem.
824 :     (note, if you have an m x n matrix, you will need mn answer evaluators, and they will be
825 : apizer 1314 returned to the evaluaters starting in the top left hand corner and proceed to the left
826 : lr003k 1120 and then at the end moving down one row, just as you would read them.)
827 : apizer 1314
828 : lr003k 1120 The options are passed on to display_matrix.
829 :    
830 :    
831 :     =cut
832 :    
833 :    
834 :     sub answer_matrix{
835 :     my $m = shift;
836 :     my $n = shift;
837 :     my $width = shift;
838 :     my @options = @_;
839 :     my @array=();
840 :     for( my $i = 0; $i < $m; $i+=1)
841 :     {
842 :     my @row_array = ();
843 : apizer 1314
844 : lr003k 1120 for( my $i = 0; $i < $n; $i+=1)
845 :     {
846 :     push @row_array, ans_rule($width);
847 : apizer 1314 }
848 : lr003k 1120 my $r_row_array = \@row_array;
849 :     push @array, $r_row_array;
850 :     }
851 : gage 1251 # display_matrix hasn't been loaded into the cache safe compartment
852 :     # so we need to refer to the subroutine in this way to make
853 :     # sure that main is defined correctly.
854 :     my $ra_local_display_matrix=PG_restricted_eval(q!\&main::display_matrix!);
855 :     &$ra_local_display_matrix( \@array, @options );
856 : apizer 1314
857 : lr003k 1120 }
858 :    
859 :     sub NAMED_ANS_ARRAY_EXTENSION{
860 : apizer 1314
861 : lr003k 1120 my $name = shift;
862 :     my $col = shift;
863 :     $col = 20 unless $col;
864 :     my $answer_value = '';
865 : apizer 1314
866 : gage 1267 $answer_value = ${$inputs_ref}{$name} if defined(${$inputs_ref}{$name});
867 : lr003k 1120 if ($answer_value =~ /\0/ ) {
868 :     my @answers = split("\0", $answer_value);
869 : apizer 1314 $answer_value = shift(@answers);
870 : lr003k 1120 $answer_value= '' unless defined($answer_value);
871 :     } elsif (ref($answer_value) eq 'ARRAY') {
872 :     my @answers = @{ $answer_value};
873 : apizer 1314 $answer_value = shift(@answers);
874 : lr003k 1120 $answer_value= '' unless defined($answer_value);
875 :     }
876 : apizer 1314
877 : sh002i 5175 $answer_value =~ tr/\\$@`//d; #`## make sure student answers can not be interpolated by e.g. EV3
878 : lr003k 1120 MODES(
879 :     TeX => "\\mbox{\\parbox[t]{10pt}{\\hrulefill}}\\hrulefill\\quad ",
880 :     Latex2HTML => qq!\\begin{rawhtml}\n<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"\">\n\\end{rawhtml}\n!,
881 :     HTML => "<INPUT TYPE=TEXT SIZE=$col NAME=\"$name\" VALUE = \"$answer_value\">\n"
882 :     );
883 :     }
884 :    
885 :     sub ans_array{
886 :     my $m = shift;
887 :     my $n = shift;
888 :     my $col = shift;
889 :     $col = 20 unless $col;
890 : gage 1267 my $num = inc_ans_rule_count() ;
891 : lr003k 1120 my $name = NEW_ANS_ARRAY_NAME($num,0,0);
892 :     my @options = @_;
893 :     my @array=();
894 :     my $string;
895 :     my $answer_value = "";
896 : apizer 1314
897 : lr003k 1120 $array[0][0] = NAMED_ANS_RULE($name,$col);
898 : apizer 1314
899 : lr003k 1120 for( my $i = 1; $i < $n; $i+=1)
900 :     {
901 :     $name = NEW_ANS_ARRAY_NAME_EXTENSION($num,0,$i);
902 :     $array[0][$i] = NAMED_ANS_ARRAY_EXTENSION($name,$col);
903 : apizer 1314
904 : lr003k 1120 }
905 : apizer 1314
906 : lr003k 1120 for( my $j = 1; $j < $m; $j+=1 ){
907 : apizer 1314
908 : lr003k 1120 for( my $i = 0; $i < $n; $i+=1)
909 :     {
910 :     $name = NEW_ANS_ARRAY_NAME_EXTENSION($num,$j,$i);
911 :     $array[$j][$i] = NAMED_ANS_ARRAY_EXTENSION($name,$col);
912 : apizer 1314
913 : lr003k 1120 }
914 : apizer 1314
915 : lr003k 1120 }
916 : gage 1251 my $ra_local_display_matrix=PG_restricted_eval(q!\&main::display_matrix!);
917 :     &$ra_local_display_matrix( \@array, @options );
918 : apizer 1314
919 : lr003k 1120 }
920 :    
921 :     sub ans_array_extension{
922 :     my $m = shift;
923 :     my $n = shift;
924 :     my $col = shift;
925 :     $col = 20 unless $col;
926 : gage 1267 my $num = PG_restricted_eval(q!$main::ans_rule_count!);
927 : lr003k 1120 my @options = @_;
928 :     my $name;
929 :     my @array=();
930 :     my $string;
931 :     my $answer_value = "";
932 : apizer 1314
933 : lr003k 1120 for( my $j = 0; $j < $m; $j+=1 ){
934 : apizer 1314
935 : lr003k 1120 for( my $i = 0; $i < $n; $i+=1)
936 :     {
937 :     $name = NEW_ANS_ARRAY_NAME_EXTENSION($num,$j,$i);
938 :     $array[$j][$i] = NAMED_ANS_ARRAY_EXTENSION($name,$col);
939 : apizer 1314
940 : lr003k 1120 }
941 : apizer 1314
942 : lr003k 1120 }
943 : gage 1251 my $ra_local_display_matrix=PG_restricted_eval(q!\&main::display_matrix!);
944 :     &$ra_local_display_matrix( \@array, @options );
945 : apizer 1314
946 : lr003k 1120 }
947 :    
948 :    
949 : sh002i 1050 # end answer blank macros
950 :    
951 :     =head2 Hints and solutions macros
952 :    
953 :     solution('text','text2',...);
954 :     SOLUTION('text','text2',...); # equivalent to TEXT(solution(...));
955 :    
956 :     hint('text', 'text2', ...);
957 :     HINT('text', 'text2',...); # equivalent to TEXT("$BR$HINT" . hint(@_) . "$BR") if hint(@_);
958 :    
959 :     Solution prints its concatenated input when the check box named 'ShowSol' is set and
960 :     the time is after the answer date. The check box 'ShowSol' is visible only after the
961 :     answer date or when the problem is viewed by a professor.
962 :    
963 : gage 1251 $main::envir{'displaySolutionsQ'} is set to 1 when a solution is to be displayed.
964 : sh002i 1050
965 :     Hints are shown only after the number of attempts is greater than $:showHint
966 :     ($main::showHint defaults to 1) and the check box named 'ShowHint' is set. The check box
967 :     'ShowHint' is visible only after the number of attempts is greater than $main::showHint.
968 :    
969 : gage 1251 $main::envir{'displayHintsQ'} is set to 1 when a hint is to be displayed.
970 : sh002i 1050
971 :    
972 :     =cut
973 :    
974 :    
975 :    
976 :     # solution prints its input when $displaySolutionsQ is set.
977 :     # use as TEXT(solution("blah, blah");
978 :     # \$solutionExists
979 :     # is passed to processProblem which displays a "show Solution" button
980 :     # when a solution is available for viewing
981 :    
982 :    
983 :     sub solution {
984 :     my @in = @_;
985 :     my $out = '';
986 : gage 1267 PG_restricted_eval(q!$main::solutionExists =1!);
987 :     if (PG_restricted_eval(q!$main::envir{'displaySolutionsQ'}!)) {$out = join(' ',@in);}
988 : sh002i 1050 $out;
989 :     }
990 :    
991 :    
992 :     sub SOLUTION {
993 :     TEXT( solution(@_)) ;
994 :     }
995 :    
996 :    
997 :     sub hint {
998 :     my @in = @_;
999 :     my $out = '';
1000 : gage 5901 my $permissionLevel = PG_restricted_eval(q!$main::envir{permissionLevel}!);
1001 :     my $PRINT_FILE_NAMES_PERMISSION_LEVEL = PG_restricted_eval(q!$envir->{'PRINT_FILE_NAMES_PERMISSION_LEVEL'}!);
1002 :     my $printHintForInstructor = $permissionLevel >= $PRINT_FILE_NAMES_PERMISSION_LEVEL;
1003 :     my $showHint = PG_restricted_eval(q!$main::showHint!);
1004 :     my $displayHint = PG_restricted_eval(q!$envir->{'displayHintsQ'}!);
1005 :     PG_restricted_eval(q!$main::hintExists =1!);
1006 : gage 5903 PG_restricted_eval(q!$main::numOfAttempts = 0 unless defined($main::numOfAttempts);!);
1007 :     my $attempts = PG_restricted_eval(q!$main::numOfAttempts!);
1008 : apizer 1080
1009 : gage 1267 if ($displayMode eq 'TeX') {
1010 : gage 5901 if ($printHintForInstructor) {
1011 :     $out = join(' ',@in, "$BR(Show hint after $showHint attempts. ) $BR");
1012 :     } else {
1013 :     $out = ''; # do nothing since hints are not available for download for students
1014 :     }
1015 :     } elsif ($printHintForInstructor) { # always print hints for instructor types
1016 :     $out = join(' ',@in, "$BR(Show hint after $showHint attempts. )$BR ");
1017 :     } elsif ( $displayHint and ( $attempts > $showHint )) {
1018 : apizer 1080
1019 : sh002i 1050 ## the second test above prevents a hint being shown if a doctored form is submitted
1020 : apizer 1080
1021 : gage 5901 $out = join(' ',@in);
1022 :     } # show hint
1023 : apizer 1080
1024 : sh002i 1050 $out ;
1025 :     }
1026 :    
1027 :    
1028 :     sub HINT {
1029 : gage 1267 TEXT("$BR" . hint(@_) . "$BR") if hint(@_);
1030 : sh002i 1050 }
1031 :    
1032 :    
1033 :    
1034 :     # End hints and solutions macros
1035 :     #################################
1036 :    
1037 : jj 3520 =head2 Comments to instructors
1038 :    
1039 :     COMMENT('text','text2',...);
1040 :    
1041 :     Takes the text to be lines of a comment to be shown only
1042 :     in the Library Browser below the rendered problem.
1043 :    
1044 :     The function COMMENT stores the needed html in the variable
1045 :     pgComment, which gets transfered to the flag 'comment' in PG_FLAGS.
1046 :    
1047 :     =cut
1048 :    
1049 :     # Add a comment which will display in the Library browser
1050 :     # Currently, the only output is html
1051 :    
1052 :     sub COMMENT {
1053 :     my @in = @_;
1054 :     my $out = join("$BR", @in);
1055 :     my $out = '<div class=\"AuthorComment\">'.$out.'</div>';
1056 :    
1057 :     PG_restricted_eval(q!$main::pgComment = "!.$out.q!"!);
1058 :     return('');
1059 :     }
1060 :    
1061 :     #################################
1062 : sh002i 1050 # Produces a random number between $begin and $end with increment 1.
1063 :     # You do not have to worry about integer or floating point types.
1064 :    
1065 :     =head2 Pseudo-random number generator
1066 :    
1067 :     Usage:
1068 :     random(0,5,.1) # produces a random number between 0 and 5 in increments of .1
1069 :     non_zero_random(0,5,.1) # gives a non-zero random number
1070 :    
1071 :     list_random(2,3,5,6,7,8,10) # produces random value from the list
1072 :     list_random(2,3, (5..8),10) # does the same thing
1073 :    
1074 :     SRAND(seed) # resets the main random generator -- use very cautiously
1075 :    
1076 :    
1077 :     SRAND(time) will create a different problem everytime it is called. This makes it difficult
1078 :     to check the answers :-).
1079 :    
1080 : gage 1251 SRAND($envir->{'inputs_ref'}->{'key'} ) will create a different problem for each login session.
1081 : sh002i 1050 This is probably what is desired.
1082 :    
1083 :     =cut
1084 :    
1085 :    
1086 :     sub random {
1087 :     my ($begin, $end, $incr) = @_;
1088 : gage 1267 $PG_random_generator->random($begin,$end,$incr);
1089 : sh002i 1050 }
1090 :    
1091 :    
1092 :     sub non_zero_random { ##gives a non-zero random number
1093 :     my (@arguments)=@_;
1094 :     my $a=0;
1095 :     my $i=100; #safety counter
1096 :     while ($a==0 && ( 0 < $i-- ) ) {
1097 :     $a=random(@arguments);
1098 :     }
1099 :     $a;
1100 :     }
1101 :    
1102 :     sub list_random {
1103 :     my(@li) = @_;
1104 :     return $li[random(1,scalar(@li))-1];
1105 :     }
1106 :    
1107 :     sub SRAND { # resets the main random generator -- use cautiously
1108 :     my $seed = shift;
1109 : gage 1328 $PG_random_generator -> srand($seed);
1110 : sh002i 1050 }
1111 :    
1112 :     # display macros
1113 :    
1114 :     =head2 Display Macros
1115 :    
1116 :     These macros produce different output depending on the display mode being used to show
1117 :     the problem on the screen, or whether the problem is being converted to TeX to produce
1118 :     a hard copy output.
1119 :    
1120 :     MODES ( TeX => "Output this in TeX mode",
1121 :     HTML => "output this in HTML mode",
1122 :     HTML_tth => "output this in HTML_tth mode",
1123 :     HTML_dpng => "output this in HTML_dpng mode",
1124 :     Latex2HTML => "output this in Latex2HTML mode",
1125 :     )
1126 :    
1127 :     TEX (tex_version, html_version) #obsolete
1128 :    
1129 :     M3 (tex_version, latex2html_version, html_version) #obsolete
1130 :    
1131 :    
1132 :    
1133 :     =cut
1134 :    
1135 :    
1136 :     sub TEX {
1137 :     my ($tex, $html ) = @_;
1138 :     MODES(TeX => $tex, HTML => $html, HTML_tth => $html, HTML_dpng => $html);
1139 :     }
1140 :    
1141 :    
1142 :     sub M3 {
1143 :     my($tex,$l2h,$html) = @_;
1144 :     MODES(TeX => $tex, Latex2HTML => $l2h, HTML => $html, HTML_tth => $html, HTML_dpng => $html);
1145 :     }
1146 :    
1147 : sh002i 2370 # MODES() is now table driven
1148 :     our %DISPLAY_MODE_FAILOVER = (
1149 : dpvc 4386 TeX => [],
1150 :     HTML => [],
1151 :     HTML_tth => [ "HTML", ],
1152 :     HTML_dpng => [ "HTML_tth", "HTML", ],
1153 :     HTML_jsMath => [ "HTML_dpng", "HTML_tth", "HTML", ],
1154 :     HTML_asciimath => [ "HTML_dpng", "HTML_tth", "HTML", ],
1155 :     HTML_LaTeXMathML => [ "HTML_dpng", "HTML_tth", "HTML", ],
1156 : sh002i 2370 # legacy modes -- these are not supported, but some problems might try to
1157 :     # set the display mode to one of these values manually and some macros may
1158 :     # provide rendered versions for these modes but not the one we want.
1159 :     Latex2HTML => [ "TeX", "HTML", ],
1160 :     HTML_img => [ "HTML_dpng", "HTML_tth", "HTML", ],
1161 :     );
1162 : sh002i 1050
1163 : sh002i 2370 # This replaces M3. You can add new modes at will to this one.
1164 : sh002i 1050 sub MODES {
1165 :     my %options = @_;
1166 : sh002i 2370
1167 :     # is a string supplied for the current display mode? if so, return it
1168 : sh002i 2193 return $options{$displayMode} if defined $options{$displayMode};
1169 :    
1170 : sh002i 2370 # otherwise, fail over to backup modes
1171 :     my @backup_modes;
1172 :     if (exists $DISPLAY_MODE_FAILOVER{$displayMode}) {
1173 :     @backup_modes = @{$DISPLAY_MODE_FAILOVER{$displayMode}};
1174 : sh002i 2193 foreach my $mode (@backup_modes) {
1175 :     return $options{$mode} if defined $options{$mode};
1176 :     }
1177 :     }
1178 : sh002i 2370 die "ERROR in defining MODES: neither display mode $displayMode nor",
1179 :     " any fallback modes (", join(", ", @backup_modes), ") supplied.\n";
1180 : sh002i 1050 }
1181 :    
1182 :     # end display macros
1183 :    
1184 :    
1185 :     =head2 Display constants
1186 :    
1187 :     @ALPHABET ALPHABET() capital letter alphabet -- ALPHABET[0] = 'A'
1188 :     $PAR PAR() paragraph character (\par or <p>)
1189 :     $BR BR() line break character
1190 :     $LQ LQ() left double quote
1191 :     $RQ RQ() right double quote
1192 :     $BM BM() begin math
1193 :     $EM EM() end math
1194 :     $BDM BDM() begin display math
1195 :     $EDM EDM() end display math
1196 :     $LTS LTS() strictly less than
1197 :     $GTS GTS() strictly greater than
1198 :     $LTE LTE() less than or equal
1199 :     $GTE GTE() greater than or equal
1200 :     $BEGIN_ONE_COLUMN BEGIN_ONE_COLUMN() begin one-column mode
1201 :     $END_ONE_COLUMN END_ONE_COLUMN() end one-column mode
1202 :     $SOL SOLUTION_HEADING() solution headline
1203 :     $SOLUTION SOLUTION_HEADING() solution headline
1204 :     $HINT HINT_HEADING() hint headline
1205 :     $US US() underscore character
1206 :     $SPACE SPACE() space character (tex and latex only)
1207 :     $BBOLD BBOLD() begin bold typeface
1208 :     $EBOLD EBOLD() end bold typeface
1209 :     $BITALIC BITALIC() begin italic typeface
1210 :     $EITALIC EITALIC() end italic typeface
1211 :     $BCENTER BCENTER() begin centered environment
1212 : apizer 1080 $ECENTER ECENTER() end centered environment
1213 : sh002i 1050 $HR HR() horizontal rule
1214 :     $LBRACE LBRACE() left brace
1215 :     $LB LB () left brace
1216 :     $RBRACE RBRACE() right brace
1217 :     $RB RB () right brace
1218 :     $DOLLAR DOLLAR() a dollar sign
1219 :     $PERCENT PERCENT() a percent sign
1220 :     $CARET CARET() a caret sign
1221 :     $PI PI() the number pi
1222 :     $E E() the number e
1223 :    
1224 :     =cut
1225 :    
1226 :    
1227 :    
1228 :    
1229 :    
1230 :     # A utility variable. Notice that "B"=$ALPHABET[1] and
1231 :     # "ABCD"=@ALPHABET[0..3].
1232 :    
1233 :     sub ALPHABET {
1234 :     ('A'..'ZZ')[@_];
1235 :     }
1236 :    
1237 :     ###############################################################
1238 :     # Some constants which are different in tex and in HTML
1239 :     # The order of arguments is TeX, Latex2HTML, HTML
1240 : apizer 1390 # Adopted Davide Cervone's improvements to PAR, LTS, GTS, LTE, GTE, LBRACE, RBRACE, LB, RB. 7-14-03 AKP
1241 : apizer 1379 sub PAR { MODES( TeX => '\\par ', Latex2HTML => '\\begin{rawhtml}<P>\\end{rawhtml}', HTML => '<P>'); };
1242 : gage 5816 #sub BR { MODES( TeX => '\\par\\noindent ', Latex2HTML => '\\begin{rawhtml}<BR>\\end{rawhtml}', HTML => '<BR>'); };
1243 : apizer 1390 # Alternate definition of BR which is slightly more flexible and gives more white space in printed output
1244 :     # which looks better but kills more trees.
1245 : gage 5816 sub BR { MODES( TeX => '\\leavevmode\\\\\\relax ', Latex2HTML => '\\begin{rawhtml}<BR>\\end{rawhtml}', HTML => '<BR>'); };
1246 : apizer 5858 sub LQ { MODES( TeX => "\\lq\\lq{}", Latex2HTML => '"', HTML => '&quot;' ); };
1247 :     sub RQ { MODES( TeX => "\\rq\\rq{}", Latex2HTML => '"', HTML => '&quot;' ); };
1248 : sh002i 1050 sub BM { MODES(TeX => '\\(', Latex2HTML => '\\(', HTML => ''); }; # begin math mode
1249 :     sub EM { MODES(TeX => '\\)', Latex2HTML => '\\)', HTML => ''); }; # end math mode
1250 :     sub BDM { MODES(TeX => '\\[', Latex2HTML => '\\[', HTML => '<P ALIGN=CENTER>'); }; #begin displayMath mode
1251 :     sub EDM { MODES(TeX => '\\]', Latex2HTML => '\\]', HTML => '</P>'); }; #end displayMath mode
1252 : apizer 1379 sub LTS { MODES(TeX => '<', Latex2HTML => '\\lt ', HTML => '&lt;', HTML_tth => '<' ); };
1253 :     sub GTS { MODES(TeX => '>', Latex2HTML => '\\gt ', HTML => '&gt;', HTML_tth => '>' ); };
1254 :     sub LTE { MODES(TeX => '\\le ', Latex2HTML => '\\le ', HTML => '<U>&lt;</U>', HTML_tth => '\\le ' ); };
1255 :     sub GTE { MODES(TeX => '\\ge ', Latex2HTML => '\\ge ', HTML => '<U>&gt;</U>', HTML_tth => '\\ge ' ); };
1256 : sh002i 1050 sub BEGIN_ONE_COLUMN { MODES(TeX => " \\end{multicols}\n", Latex2HTML => " ", HTML => " "); };
1257 :     sub END_ONE_COLUMN { MODES(TeX =>
1258 :     " \\begin{multicols}{2}\n\\columnwidth=\\linewidth\n",
1259 :     Latex2HTML => ' ', HTML => ' ');
1260 :    
1261 :     };
1262 :     sub SOLUTION_HEADING { MODES( TeX => '\\par {\\bf Solution:}',
1263 :     Latex2HTML => '\\par {\\bf Solution:}',
1264 :     HTML => '<P><B>Solution:</B>');
1265 :     };
1266 :     sub HINT_HEADING { MODES( TeX => "\\par {\\bf Hint:}", Latex2HTML => "\\par {\\bf Hint:}", HTML => "<P><B>Hint:</B>"); };
1267 :     sub US { MODES(TeX => '\\_', Latex2HTML => '\\_', HTML => '_');}; # underscore, e.g. file${US}name
1268 :     sub SPACE { MODES(TeX => '\\ ', Latex2HTML => '\\ ', HTML => '&nbsp;');}; # force a space in latex, doesn't force extra space in html
1269 :     sub BBOLD { MODES(TeX => '{\\bf ', Latex2HTML => '{\\bf ', HTML => '<B>'); };
1270 :     sub EBOLD { MODES( TeX => '}', Latex2HTML => '}',HTML => '</B>'); };
1271 :     sub BITALIC { MODES(TeX => '{\\it ', Latex2HTML => '{\\it ', HTML => '<I>'); };
1272 :     sub EITALIC { MODES(TeX => '} ', Latex2HTML => '} ', HTML => '</I>'); };
1273 :     sub BCENTER { MODES(TeX => '\\begin{center} ', Latex2HTML => ' \\begin{rawhtml} <div align="center"> \\end{rawhtml} ', HTML => '<div align="center">'); };
1274 :     sub ECENTER { MODES(TeX => '\\end{center} ', Latex2HTML => ' \\begin{rawhtml} </div> \\end{rawhtml} ', HTML => '</div>'); };
1275 :     sub HR { MODES(TeX => '\\par\\hrulefill\\par ', Latex2HTML => '\\begin{rawhtml} <HR> \\end{rawhtml}', HTML => '<HR>'); };
1276 : apizer 1379 sub LBRACE { MODES( TeX => '\{', Latex2HTML => '\\lbrace', HTML => '{' , HTML_tth=> '\\lbrace' ); };
1277 :     sub RBRACE { MODES( TeX => '\}', Latex2HTML => '\\rbrace', HTML => '}' , HTML_tth=> '\\rbrace',); };
1278 :     sub LB { MODES( TeX => '\{', Latex2HTML => '\\lbrace', HTML => '{' , HTML_tth=> '\\lbrace' ); };
1279 :     sub RB { MODES( TeX => '\}', Latex2HTML => '\\rbrace', HTML => '}' , HTML_tth=> '\\rbrace',); };
1280 : gage 4806 sub DOLLAR { MODES( TeX => '\\$', Latex2HTML => '&#36;', HTML => '&#36;' ); };
1281 : sh002i 1050 sub PERCENT { MODES( TeX => '\\%', Latex2HTML => '\\%', HTML => '%' ); };
1282 :     sub CARET { MODES( TeX => '\\verb+^+', Latex2HTML => '\\verb+^+', HTML => '^' ); };
1283 :     sub PI {4*atan2(1,1);};
1284 :     sub E {exp(1);};
1285 :    
1286 :     ###############################################################
1287 :     ## Evaluation macros
1288 :    
1289 :    
1290 :     =head2 TEXT macros
1291 :    
1292 :     Usage:
1293 :     TEXT(@text);
1294 :    
1295 :     This is the simplest way to print text from a problem. The strings in the array C<@text> are concatenated
1296 :     with spaces between them and printed out in the text of the problem. The text is not processed in any other way.
1297 :     C<TEXT> is defined in PG.pl.
1298 :    
1299 :     Usage:
1300 :     BEGIN_TEXT
1301 :     text.....
1302 :     END_TEXT
1303 :    
1304 :     This is the most common way to enter text into the problem. All of the text between BEGIN_TEXT and END_TEXT
1305 :     is processed by the C<EV3> macro described below and then printed using the C<TEXT> command. The two key words
1306 :     must appear on lines by themselves. The preprocessing that makes this construction work is done in F<PGtranslator.pm>.
1307 :     See C<EV3> below for details on the processing.
1308 :    
1309 :    
1310 :     =cut
1311 :    
1312 :     =head2 Evaluation macros
1313 :    
1314 :     =head3 EV3
1315 :    
1316 :     TEXT(EV3("This is a formulat \( \int_0^5 x^2 \, dx \) ");
1317 :     TEXT(EV3(@text));
1318 :    
1319 :     TEXT(EV3(<<'END_TEXT'));
1320 :     text stuff...
1321 :     END_TEXT
1322 :    
1323 :    
1324 :     The BEGIN_TEXT/END_TEXT construction is translated into the construction above by PGtranslator.pm. END_TEXT must appear
1325 :     on a line by itself and be left justified. (The << construction is known as a "here document" in UNIX and in PERL.)
1326 :    
1327 :     The single quotes around END_TEXT mean that no automatic interpolation of variables takes place in the text.
1328 :     Using EV3 with strings which have been evaluated by double quotes may lead to unexpected results.
1329 :    
1330 :    
1331 :     The evaluation macro E3 first evaluates perl code inside the braces: C<\{ code \}>.
1332 :     Any perl statment can be put inside the braces. The
1333 :     result of the evaluation (i.e. the last statement evaluated) replaces the C<\{ code \}> construction.
1334 :    
1335 :     Next interpolation of all variables (e.g. C<$var or @array> ) is performed.
1336 :    
1337 :     Then mathematical formulas in TeX are evaluated within the
1338 :     C<\( tex math mode \)> and
1339 :     C<\[ tex display math mode \] >
1340 :     constructions, in that order:
1341 :    
1342 :     =head3 FEQ
1343 :    
1344 :     FEQ($string); # processes and outputs the string
1345 :    
1346 :    
1347 :     The mathematical formulas are run through the macro C<FEQ> (Format EQuations) which performs
1348 :     several substitutions (see below).
1349 :     In C<HTML_tth> mode the resulting code is processed by tth to obtain an HTML version
1350 :     of the formula. (In the future processing by WebEQ may be added here as another option.)
1351 :     The Latex2HTML mode does nothing
1352 :     at this stage; it creates the entire problem before running it through
1353 :     TeX and creating the GIF images of the equations.
1354 :    
1355 :     The resulting string is output (and usually fed into TEXT to be printed in the problem).
1356 :    
1357 :     Usage:
1358 :    
1359 :     $string2 = FEQ($string1);
1360 :    
1361 :     This is a filter which is used to format equations by C<EV2> and C<EV3>, but can also be used on its own. It is best
1362 :     understood with an example.
1363 :    
1364 :     $string1 = "${a}x^2 + ${b}x + {$c:%.1f}"; $a = 3;, $b = -2; $c = -7.345;
1365 :    
1366 :     when interpolated becomes:
1367 :    
1368 :     $string1 = '3x^2 + -2x + {-7.345:%0.1f}
1369 :    
1370 :     FEQ first changes the number of decimal places displayed, so that the last term becomes -7.3 Then it removes the
1371 :     extraneous plus and minus signs, so that the final result is what you want:
1372 :    
1373 :     $string2 = '3x^2 - 2x -7.3';
1374 :    
1375 :     (The %0.1f construction
1376 :     is the same formatting convention used by Perl and nearly identical to the one used by the C printf statement. Some common
1377 :     usage: %0.3f 3 decimal places, fixed notation; %0.3e 3 significant figures exponential notation; %0.3g uses either fixed
1378 :     or exponential notation depending on the size of the number.)
1379 :    
1380 :     Two additional legacy formatting constructions are also supported:
1381 :    
1382 : gage 5688 C<!{$c:%0.3f} > will give a number with 3 decimal places and a negative
1383 :     sign if the number is negative, no sign if the number is positive. Since this is
1384 :     identical to the behavior of C<{$c:%0.3f}> the use of this syntax is depricated.
1385 : sh002i 1050
1386 : gage 5688 C<?{$c:%0.3f}> determines the sign and prints it
1387 :     whether the number is positive or negative. You can use this
1388 :     to force an expression such as C<+5.456>.
1389 : sh002i 1050
1390 :     =head3 EV2
1391 :    
1392 :     TEXT(EV2(@text));
1393 :    
1394 :     TEXT(EV2(<<END_OF_TEXT));
1395 :     text stuff...
1396 :     END_OF_TEXT
1397 :    
1398 :     This is a precursor to EV3. In this case the constants are interpolated first, before the evaluation of the \{ ...code...\}
1399 :     construct. This can lead to unexpected results. For example C<\{ join(" ", @text) \}> with C<@text = ("Hello","World");> becomes,
1400 :     after interpolation, C<\{ join(" ",Hello World) \}> which then causes an error when evaluated because Hello is a bare word.
1401 :     C<EV2> can still be useful if you allow for this, and in particular it works on double quoted strings, which lead to
1402 :     unexpected results with C<EV3>. Using single quoted strings with C<EV2> may lead to unexpected results.
1403 :    
1404 :     The unexpected results have to do with the number of times backslashed constructions have to be escaped. It is quite messy. For
1405 :     more details get a good Perl book and then read the code. :-)
1406 :    
1407 :    
1408 :    
1409 :    
1410 :     =cut
1411 :    
1412 :    
1413 :     sub ev_substring {
1414 :     my $string = shift;
1415 :     my $start_delim = shift;
1416 :     my $end_delim = shift;
1417 :     my $actionRef = shift;
1418 :     my ($eval_out,$PG_eval_errors,$PG_full_error_report)=();
1419 :     my $out = "";
1420 : apizer 1379 #
1421 :     # DPVC -- 2001/12/07
1422 :     # original "while ($string)" fails to process the string "0" correctly
1423 :     #
1424 :     while ($string ne "") {
1425 :     #
1426 :     # end DPVC
1427 :     #
1428 : sh002i 1050 if ($string =~ /\Q$start_delim\E/s) {
1429 :     #print "$start_delim $end_delim evaluating_substring=$string<BR>";
1430 :     $string =~ s/^(.*?)\Q$start_delim\E//s; # get string up to next \{ ---treats string as a single line, ignoring returns
1431 :     $out .= $1;
1432 :     #print "$start_delim $end_delim substring_out=$out<BR>";
1433 :     $string =~ s/^(.*?)\Q$end_delim\E//s; # get perl code up to \} ---treats string as a single line, ignoring returns
1434 :     #print "$start_delim $end_delim evaluate_string=$1<BR>";
1435 :     ($eval_out,$PG_eval_errors,$PG_full_error_report) = &$actionRef($1);
1436 :     $eval_out = "$start_delim $eval_out $end_delim" if $PG_full_error_report;
1437 :     $out = $out . $eval_out;
1438 :     #print "$start_delim $end_delim new substring_out=$out<BR><p><BR>";
1439 : gage 1267 $out .="$PAR ERROR $0 in ev_substring, PGbasicmacros.pl:$PAR <PRE> $@ </PRE>$PAR" if $@;
1440 : sh002i 1050 }
1441 :     else {
1442 :     $out .= $string; # flush the last part of the string
1443 :     last;
1444 :     }
1445 :    
1446 :     }
1447 :     $out;
1448 :     }
1449 :     sub safe_ev {
1450 :     my ($out,$PG_eval_errors,$PG_full_error_report) = &old_safe_ev; # process input by old_safe_ev first
1451 :     $out =~s/\\/\\\\/g; # protect any new backslashes introduced.
1452 :     ($out,$PG_eval_errors,$PG_full_error_report)
1453 :     }
1454 :    
1455 :     sub old_safe_ev {
1456 :     my $in = shift;
1457 :     my ($out,$PG_eval_errors,$PG_full_error_report) = PG_restricted_eval("$in;");
1458 :     # the addition of the ; seems to provide better error reporting
1459 :     if ($PG_eval_errors) {
1460 :     my @errorLines = split("\n",$PG_eval_errors);
1461 : gage 1267 #$out = "<PRE>$PAR % ERROR in $0:old_safe_ev, PGbasicmacros.pl: $PAR % There is an error occuring inside evaluation brackets \\{ ...code... \\} $BR % somewhere in an EV2 or EV3 or BEGIN_TEXT block. $BR % Code evaluated:$BR $in $BR % $BR % $errorLines[0]\n % $errorLines[1]$BR % $BR % $BR </PRE> ";
1462 : sh002i 1050 warn " ERROR in old_safe_ev, PGbasicmacros.pl: <PRE>
1463 :     ## There is an error occuring inside evaluation brackets \\{ ...code... \\}
1464 :     ## somewhere in an EV2 or EV3 or BEGIN_TEXT block.
1465 :     ## Code evaluated:
1466 :     ## $in
1467 :     ##" .join("\n ", @errorLines). "
1468 : gage 1267 ##</PRE>$BR
1469 : sh002i 1050 ";
1470 : gage 1267 $out ="$PAR $BBOLD $in $EBOLD $PAR";
1471 : sh002i 1050
1472 :    
1473 :     }
1474 :    
1475 :     ($out,$PG_eval_errors,$PG_full_error_report);
1476 :     }
1477 :    
1478 :     sub FEQ { # Format EQuations
1479 :     my $in = shift;
1480 :     # formatting numbers -- the ?{} and !{} constructions
1481 :     $in =~s/\?\s*\{([.\-\$\w\d]+):?([%.\da-z]*)\}/${ \( &sspf($1,$2) )}/g;
1482 :     $in =~s/\!\s*\{([.\-\$\w\d]+):?([%.\da-z]*)\}/${ \( &spf($1,$2) )}/g;
1483 :    
1484 :     # more formatting numbers -- {number:format} constructions
1485 :     $in =~ s/\{(\s*[\+\-\d\.]+[eE]*[\+\-]*\d*):(\%\d*.\d*\w)}/${ \( &spf($1,$2) )}/g;
1486 :     $in =~ s/\+\s*\-/ - /g;
1487 :     $in =~ s/\-\s*\+/ - /g;
1488 :     $in =~ s/\+\s*\+/ + /g;
1489 :     $in =~ s/\-\s*\-/ + /g;
1490 :     $in;
1491 :     }
1492 :    
1493 :     #sub math_ev3 {
1494 :     # my $in = shift; #print "in=$in<BR>";
1495 :     # my ($out,$PG_eval_errors,$PG_full_error_report);
1496 :     # $in = FEQ($in);
1497 :     # $in =~ s/%/\\%/g; # % causes trouble in TeX and HTML_tth it usually (always?) indicates an error, not comment
1498 : gage 1267 # return("$BM $in $EM") unless ($displayMode eq 'HTML_tth');
1499 : sh002i 1050 # $in = "\\(" . $in . "\\)";
1500 :     # $out = tth($in);
1501 :     # ($out,$PG_eval_errors,$PG_full_error_report);
1502 :     #
1503 :     #}
1504 :     #
1505 :     #sub display_math_ev3 {
1506 :     # my $in = shift; #print "in=$in<BR>";
1507 :     # my ($out,$PG_eval_errors,$PG_full_error_report);
1508 :     # $in = FEQ($in);
1509 :     # $in =~ s/%/\\%/g;
1510 :     # return("$main::BDM $in $main::EDM") unless $displayMode eq 'HTML_tth' ;
1511 :     # $in = "\\[" . $in . "\\]";
1512 :     # $out =tth($in);
1513 :     # ($out,$PG_eval_errors,$PG_full_error_report);
1514 :     #}
1515 :    
1516 :     sub math_ev3 {
1517 :     my $in = shift;
1518 :     return general_math_ev3($in, "inline");
1519 :     }
1520 :    
1521 :     sub display_math_ev3 {
1522 :     my $in = shift;
1523 :     return general_math_ev3($in, "display");
1524 :     }
1525 :    
1526 :     sub general_math_ev3 {
1527 :     my $in = shift;
1528 :     my $mode = shift || "inline";
1529 : apizer 1080
1530 : sh002i 1155 $in = FEQ($in); # Format EQuations
1531 :     $in =~ s/%/\\%/g; # avoid % becoming TeX comments
1532 : apizer 1314
1533 : apizer 1379 ## remove leading and trailing spaces so that HTML mode will
1534 :     ## not include unwanted spaces as per Davide Cervone.
1535 :     $in =~ s/^\s+//;
1536 :     $in =~ s/\s+$//;
1537 : jj 1758 ## If it ends with a backslash, there should be another space
1538 :     ## at the end
1539 :     if($in =~ /\\$/) { $in .= ' ';}
1540 : apizer 1379
1541 : sh002i 1155 # some modes want the delimiters, some don't
1542 :     my $in_delim = $mode eq "inline"
1543 :     ? "\\($in\\)"
1544 :     : "\\[$in\\]";
1545 : apizer 1314
1546 : sh002i 1050 my $out;
1547 : gage 1071 if($displayMode eq "HTML_tth") {
1548 :     $out = tth($in_delim);
1549 : apizer 1379 ## remove leading and trailing spaces as per Davide Cervone.
1550 : dpvc 2392 $out =~ s/^\s+//;
1551 :     $out =~ s/\s+$//;
1552 : gage 1071 } elsif ($displayMode eq "HTML_dpng") {
1553 : sh002i 1158 # for jj's version of ImageGenerator
1554 : gage 1251 $out = $envir->{'imagegen'}->add($in_delim);
1555 : sh002i 1158 # for my version of ImageGenerator
1556 : gage 1251 #$out = $envir->{'imagegen'}->add($in, $mode);
1557 : sh002i 1050 } elsif ($displayMode eq "HTML_img") {
1558 :     $out = math2img($in, $mode);
1559 : dpvc 2166 } elsif ($displayMode eq "HTML_jsMath") {
1560 : dpvc 2292 $in =~ s/</&lt;/g; $in =~ s/>/&gt;/g;
1561 : dpvc 2166 $out = '<SPAN CLASS="math">'.$in.'</SPAN>' if $mode eq "inline";
1562 :     $out = '<DIV CLASS="math">'.$in.'</DIV>' if $mode eq "display";
1563 : dpvc 2199 } elsif ($displayMode eq "HTML_asciimath") {
1564 :     $out = "`$in`" if $mode eq "inline";
1565 :     $out = '<DIV ALIGN="CENTER">`'.$in.'`</DIV>' if $mode eq "display";
1566 : dpvc 4386 } elsif ($displayMode eq "HTML_LaTeXMathML") {
1567 :     $in = '{'.$in.'}';
1568 :     $in =~ s/</\\lt/g; $in =~ s/>/\\gt/g;
1569 :     $in =~ s/\{\s*(\\(display|text|script|scriptscript)style)/$1\{/g;
1570 :     $out = '$$'.$in.'$$' if $mode eq "inline";
1571 :     $out = '<DIV ALIGN="CENTER">$$\displaystyle{'.$in.'}$$</DIV>' if $mode eq "display";
1572 : sh002i 1050 } else {
1573 :     $out = "\\($in\\)" if $mode eq "inline";
1574 :     $out = "\\[$in\\]" if $mode eq "display";
1575 :     }
1576 :     return $out;
1577 :     }
1578 :    
1579 :     sub EV2 {
1580 :     my $string = join(" ",@_);
1581 :     # evaluate code inside of \{ \} (no nesting allowed)
1582 :     $string = ev_substring($string,"\\{","\\}",\&old_safe_ev);
1583 :     $string = ev_substring($string,"\\<","\\>",\&old_safe_ev);
1584 :     $string = ev_substring($string,"\\(","\\)",\&math_ev3);
1585 :     $string = ev_substring($string,"\\[","\\]",\&display_math_ev3);
1586 :     # macros for displaying math
1587 : gage 1267 $string =~ s/\\\(/$BM/g;
1588 :     $string =~ s/\\\)/$EM/g;
1589 :     $string =~ s/\\\[/$BDM/g;
1590 :     $string =~ s/\\\]/$EDM/g;
1591 : sh002i 1050 $string;
1592 :     }
1593 :    
1594 :     sub EV3{
1595 :     my $string = join(" ",@_);
1596 :     # evaluate code inside of \{ \} (no nesting allowed)
1597 :     $string = ev_substring($string,"\\\\{","\\\\}",\&safe_ev); # handles \{ \} in single quoted strings of PG files
1598 :     # interpolate variables
1599 :     my ($evaluated_string,$PG_eval_errors,$PG_full_errors) = PG_restricted_eval("<<END_OF_EVALUATION_STRING\n$string\nEND_OF_EVALUATION_STRING\n");
1600 :     if ($PG_eval_errors) {
1601 :     my @errorLines = split("\n",$PG_eval_errors);
1602 :     $string =~ s/</&lt;/g; $string =~ s/>/&gt;/g;
1603 : gage 1267 $evaluated_string = "<PRE>$PAR % ERROR in $0:EV3, PGbasicmacros.pl: $PAR % There is an error occuring in the following code:$BR $string $BR % $BR % $errorLines[0]\n % $errorLines[1]$BR % $BR % $BR </PRE> ";
1604 : sh002i 1050 $@="";
1605 :     }
1606 :     $string = $evaluated_string;
1607 :     $string = ev_substring($string,"\\(","\\)",\&math_ev3);
1608 :     $string = ev_substring($string,"\\[","\\]",\&display_math_ev3);
1609 :     $string;
1610 :     }
1611 :    
1612 : gage 1483 sub EV4{
1613 :     if ($displayMode eq "HTML_dpng") {
1614 :     my $string = join(" ",@_);
1615 :     my ($evaluated_string,$PG_eval_errors,$PG_full_errors) = PG_restricted_eval("<<END_OF_EVALUATION_STRING\n$string\nEND_OF_EVALUATION_STRING\n");
1616 :     if ($PG_eval_errors) {
1617 :     my @errorLines = split("\n",$PG_eval_errors);
1618 :     $string =~ s/</&lt;/g; $string =~ s/>/&gt;/g;
1619 :     $evaluated_string = "<PRE>$PAR % ERROR in $0:EV3, PGbasicmacros.pl:".
1620 :     "$PAR % There is an error occuring in the following code:$BR ".
1621 : gage 1490 "$string $BR % $BR % $errorLines[0]\n % $errorLines[1]$BR ".
1622 : gage 1483 "% $BR % $BR </PRE> ";
1623 :     }
1624 :     $string = $evaluated_string;
1625 :     $string = $envir{'imagegen'}->add($string);
1626 :     $string;
1627 :     } else {
1628 :     EV3(@_);
1629 :     }
1630 :     }
1631 :    
1632 : gage 5792 =head3 EV3P
1633 : gage 1483
1634 : gage 5792 ######################################################################
1635 :     #
1636 :     # New version of EV3 that allows `...` and ``...`` to insert TeX produced
1637 :     # by the new Parser (in math and display modes).
1638 :     #
1639 :     # Format: EV3P(string,...);
1640 :     # EV3P({options},string,...);
1641 :     #
1642 :     # `x^2/5` will become \(\frac{x^2}{5}\) and then rendered for hardcopy or screen output
1643 :     #
1644 :     # where options can include:
1645 :     #
1646 :     # processCommands => 0 or 1 Indicates if the student's answer will
1647 :     # be allowed to process \{...\}.
1648 :     # Default: 1
1649 :     #
1650 :     # processVariables => 0 1 Indicates whether variable substitution
1651 :     # should be performed on the student's
1652 :     # answer.
1653 :     # Default: 1
1654 :     #
1655 :     # processMath => 0 or 1 Indicates whether \(...\), \[...\],
1656 :     # `...` and ``...`` will be processed
1657 :     # in the student's answer.
1658 :     # Default: 1
1659 :     #
1660 :     # processParser => 0 or 1 Indicates if `...` and ``...`` should
1661 :     # be processed when math is being
1662 :     # processed.
1663 :     # Default: 1
1664 :     #
1665 :     # fixDollars => 0 or 1 Specifies whether dollar signs not followed
1666 :     # by a letter should be replaced by ${DOLLAR}
1667 :     # prior to variable substitution (to prevent
1668 :     # accidental substitution of strange Perl
1669 :     # values).
1670 :     # Default: 1
1671 :     #
1672 :    
1673 :     =cut
1674 :    
1675 :     sub EV3P {
1676 :     my $option_ref = {}; $option_ref = shift if ref($_[0]) eq 'HASH';
1677 :     my %options = (
1678 :     processCommands => 1,
1679 :     processVariables => 1,
1680 :     processParser => 1,
1681 :     processMath => 1,
1682 :     fixDollars => 1,
1683 :     %{$option_ref},
1684 :     );
1685 :     my $string = join(" ",@_);
1686 :     $string = ev_substring($string,"\\\\{","\\\\}",\&safe_ev) if $options{processCommands};
1687 :     if ($options{processVariables}) {
1688 :     my $eval_string = $string;
1689 : gage 5816 $eval_string =~ s/\$(?![a-z\{])/\${DOLLAR}/gi if $options{fixDollars};
1690 : gage 5792 my ($evaluated_string,$PG_eval_errors,$PG_full_errors) =
1691 :     PG_restricted_eval("<<END_OF_EVALUATION_STRING\n$eval_string\nEND_OF_EVALUATION_STRING\n");
1692 :     if ($PG_eval_errors) {
1693 :     my $error = (split("\n",$PG_eval_errors))[0]; $error =~ s/at \(eval.*//gs;
1694 :     $string =~ s/&/&amp;/g; $string =~ s/</&lt;/g; $string =~ s/>/&gt;/g;
1695 :     $evaluated_string = $BBOLD."(Error: $error in '$string')".$EBOLD;
1696 :     }
1697 :     $string = $evaluated_string;
1698 :     }
1699 :     if ($options{processMath}) {
1700 :     $string = EV3P_parser($string) if $options{processParser};
1701 :     $string = ev_substring($string,"\\(","\\)",\&math_ev3);
1702 :     $string = ev_substring($string,"\\[","\\]",\&display_math_ev3);
1703 :     }
1704 :     return $string;
1705 :     }
1706 :    
1707 :     #
1708 :     # Look through a string for ``...`` or `...` and use
1709 :     # the parser to produce TeX code for the specified mathematics.
1710 :     # ``...`` does display math, `...` does in-line math. They
1711 :     # can also be used within math mode already, in which case they
1712 :     # use whatever mode is already in effect.
1713 :     #
1714 :     sub EV3P_parser {
1715 :     my $string = shift;
1716 :     return $string unless $string =~ m/`/;
1717 :     my $start = ''; my %end = ('\('=>'\)','\['=>'\]');
1718 :     my @parts = split(/(``.*?``\*?|`.+?`\*?|(?:\\[()\[\]]))/s,$string);
1719 :     foreach my $part (@parts) {
1720 :     if ($part =~ m/^(``?)(.*)\1(\*?)$/s) {
1721 :     my ($delim,$math,$star) = ($1,$2,$3);
1722 :     my $f = Parser::Formula($math);
1723 :     if (defined($f)) {
1724 :     $f = $f->reduce if $star;
1725 :     $part = $f->TeX;
1726 :     $part = ($delim eq '`' ? '\('.$part.'\)': '\['.$part.'\]') if (!$start);
1727 :     } else {
1728 :     ## FIXME: use context->{error}{ref} to highlight error in $math.
1729 :     $part = $BBOLD."(Error: $$Value::context->{error}{message} '$math')".$EBOLD;
1730 :     $part = $end{$start}." ".$part." ".$start if $start;
1731 :     }
1732 :     }
1733 :     elsif ($start) {$start = '' if $part eq $end{$start}}
1734 :     elsif ($end{$part}) {$start = $part}
1735 :     }
1736 :     return join('',@parts);
1737 :     }
1738 :    
1739 :    
1740 : sh002i 1050 =head2 Formatting macros
1741 :    
1742 :     beginproblem() # generates text listing number and the point value of
1743 :     # the problem. It will also print the file name containing
1744 :     # the problem for users listed in the PRINT_FILE_NAMES_FOR PG_environment
1745 :     # variable.
1746 :     OL(@array) # formats the array as an Ordered List ( <OL> </OL> ) enumerated by letters.
1747 :    
1748 :     htmlLink($url, $text)
1749 :     # Places a reference to the URL with the specified text in the problem.
1750 :     # A common usage is \{ htmlLink(alias('prob1_help.html') \}, 'for help')
1751 :     # where alias finds the full address of the prob1_help.html file in the same directory
1752 :     # as the problem file
1753 : gage 5209 appletLink( { name => "xFunctions",
1754 :     codebase => '', # use this to specify the complete url
1755 :     # otherwise libraries specified in global.conf are searched
1756 :     archive => 'xFunctions.zip', # name the archive containing code (.jar files go here also)
1757 :     code => 'xFunctionsLauncher.class',
1758 :     width => 100,
1759 :     height => 14,
1760 :     params => { param1 =>value1, param2 => value2},
1761 :     }
1762 :     );
1763 :     helpLink() allows site specific help specified in global.conf or course.conf
1764 :     the parameter localHelpURL must be defined in the environment
1765 :     currently works only for 'interval notation' and 'units'
1766 :     NEEDS REFINEMENT
1767 :    
1768 :     ########################
1769 :     deprecated coding method
1770 :     appletLink ($url, $parameters)
1771 : sh002i 1050 # For example
1772 :     # appletLink(q! archive="http: //webwork.math.rochester.edu/gage/xFunctions/xFunctions.zip"
1773 :     code="xFunctionsLauncher.class" width=100 height=14!,
1774 :     " parameter text goes here")
1775 :     # will link to xFunctions.
1776 :    
1777 :     low level:
1778 :    
1779 :     spf($number, $format) # prints the number with the given format
1780 :     sspf($number, $format) # prints the number with the given format, always including a sign.
1781 : jj 2155 nicestring($coefficients, $terms) # print a linear combinations of terms using coefficients
1782 :     nicestring($coefficients) # uses the coefficients to make a polynomial
1783 :     # For example
1784 :     # nicestring([1,-2, 0]) produces 'x^2-2x'
1785 :     # nicestring([2,0,-1],['', 't', 't^2']) produces '2-t^2'
1786 : sh002i 1050 protect_underbar($string) # protects the underbar (class_name) in strings which may have to pass through TeX.
1787 :    
1788 :     =cut
1789 :    
1790 :     sub beginproblem {
1791 :     my $out = "";
1792 : gage 1267 my $problemValue = $envir->{problemValue};
1793 : gage 1298 my $fileName = $envir->{fileName};
1794 : gage 1267 my $probNum = $envir->{probNum};
1795 :     my $TeXFileName = protect_underbar($envir->{fileName});
1796 :     my $l2hFileName = protect_underbar($envir->{fileName});
1797 : sh002i 1050 my %inlist;
1798 :     my $points ='pts';
1799 : apizer 1314
1800 : gage 1267 $points = 'pt' if $problemValue == 1;
1801 : sh002i 1050 ## Prepare header for the problem
1802 : gage 1251 grep($inlist{$_}++,@{ $envir->{'PRINT_FILE_NAMES_FOR'} });
1803 : gage 5816 my $permissionLevel = $envir->{permissionLevel};
1804 :     my $PRINT_FILE_NAMES_PERMISSION_LEVEL = $envir->{'PRINT_FILE_NAMES_PERMISSION_LEVEL'};
1805 :     my $studentLogin = $envir->{studentLogin};
1806 :     my $print_path_name_flag =
1807 :     (defined($permissionLevel) && defined($PRINT_FILE_NAMES_PERMISSION_LEVEL) && $permissionLevel >= $PRINT_FILE_NAMES_PERMISSION_LEVEL)
1808 :     || ( defined($inlist{ $studentLogin }) and ( $inlist{ $studentLogin }>0 ) ) ;
1809 :    
1810 :     if ( $print_path_name_flag ) {
1811 : dpvc 2392 $out = &M3("{\\bf ${probNum}. {\\footnotesize ($problemValue $points) $TeXFileName}}\\newline ",
1812 : gage 1267 " \\begin{rawhtml} ($problemValue $points) <B>$l2hFileName</B><BR>\\end{rawhtml}",
1813 :     "($problemValue $points) <B>$fileName</B><BR>"
1814 : jj 1964 ) if ($problemValue ne "");
1815 : dpvc 2392 } else {
1816 :     $out = &M3("{\\bf ${probNum}.} ($problemValue $points) ",
1817 : gage 1267 "($problemValue $points) ",
1818 :     "($problemValue $points) "
1819 : jj 1964 ) if ($problemValue ne "");
1820 : sh002i 1050 }
1821 : jj 3553 $out .= MODES(%{main::PG_restricted_eval(q!$main::problemPreamble!)});
1822 : sh002i 1050 $out;
1823 :    
1824 :     }
1825 :    
1826 : jj 2155 sub nicestring {
1827 :     my($thingy) = shift;
1828 :     my(@coefs) = @{$thingy};
1829 :     my $n = scalar(@coefs);
1830 :     $thingy = shift;
1831 :     my(@others);
1832 :     if(defined($thingy)) {
1833 :     @others = @{$thingy};
1834 :     } else {
1835 :     my($j);
1836 :     for $j (1..($n-2)) {
1837 :     $others[$j-1] = "x^".($n-$j);
1838 :     }
1839 :     if($n>=2) { $others[$n-2] = "x";}
1840 :     $others[$n-1] = "";
1841 :     }
1842 :     my($j, $k)=(0,0);
1843 :     while(($k<$n) && ($coefs[$k]==0)) {$k++;}
1844 :     if($k==$n) {return("0");}
1845 :     my $ans;
1846 :     if($coefs[$k]==1) {$ans = ($others[$k]) ? "$others[$k]" : "1";}
1847 :     elsif($coefs[$k]== -1) {$ans = ($others[$k]) ? "- $others[$k]" : "-1"}
1848 :     else { $ans = "$coefs[$k] $others[$k]";}
1849 :     $k++;
1850 :     for $j ($k..($n-1)) {
1851 :     if($coefs[$j] != 0) {
1852 :     if($coefs[$j] == 1) {
1853 :     $ans .= ($others[$j]) ? "+ $others[$j]" : "+ 1";
1854 :     } elsif($coefs[$j] == -1) {
1855 :     $ans .= ($others[$j]) ? "- $others[$j]" : "-1";
1856 :     } else {
1857 :     $ans .= "+ $coefs[$j] $others[$j]";
1858 :     }
1859 :     }
1860 :     }
1861 :     return($ans);
1862 :     }
1863 :    
1864 : sh002i 1050 # kludge to clean up path names
1865 :     ## allow underscore character in set and section names and also allows line breaks at /
1866 :     sub protect_underbar {
1867 :     my $in = shift;
1868 :     if ($displayMode eq 'TeX') {
1869 :    
1870 :     $in =~ s|_|\\\_|g;
1871 :     $in =~ s|/|\\\-/|g; # allows an optional hyphenation of the path (in tex)
1872 :     }
1873 :     $in;
1874 :     }
1875 :    
1876 :    
1877 :     # An example of a macro which prints out a list (with letters)
1878 :     sub OL {
1879 :     my(@array) = @_;
1880 :     my $i = 0;
1881 : apizer 3158 my @alpha = ('A'..'Z', 'AA'..'ZZ');
1882 : jj 3136 my $letter;
1883 : sh002i 1050 my $out= &M3(
1884 :     "\\begin{enumerate}\n",
1885 :     " \\begin{rawhtml} <OL TYPE=\"A\" VALUE=\"1\"> \\end{rawhtml} ",
1886 : jj 3136 # kludge to fix IE/CSS problem
1887 :     #"<OL TYPE=\"A\" VALUE=\"1\">\n"
1888 :     "<BLOCKQUOTE>\n"
1889 : sh002i 1050 ) ;
1890 :     my $elem;
1891 :     foreach $elem (@array) {
1892 : jj 3136 $letter = shift @alpha;
1893 : sh002i 1050 $out .= MODES(
1894 : gage 1267 TeX=> "\\item[$ALPHABET[$i].] $elem\n",
1895 : sh002i 1050 Latex2HTML=> " \\begin{rawhtml} <LI> \\end{rawhtml} $elem ",
1896 : jj 3136 #HTML=> "<LI> $elem\n",
1897 :     HTML=> "<br /> <b>$letter.</b> $elem\n",
1898 :     #HTML_dpng=> "<LI> $elem <br /> <br /> \n"
1899 :     HTML_dpng=> "<br /> <b>$letter.</b> $elem \n"
1900 : sh002i 1050 );
1901 :     $i++;
1902 :     }
1903 :     $out .= &M3(
1904 :     "\\end{enumerate}\n",
1905 :     " \\begin{rawhtml} </OL>\n \\end{rawhtml} ",
1906 : jj 3136 #"</OL>\n"
1907 :     "</BLOCKQUOTE>\n"
1908 : sh002i 1050 ) ;
1909 :     }
1910 :    
1911 :     sub htmlLink {
1912 :     my $url = shift;
1913 :     my $text = shift;
1914 :     my $options = shift;
1915 :     $options = "" unless defined($options);
1916 : gage 1267 return "$BBOLD\[ broken link: $text \] $EBOLD" unless defined($url);
1917 : dpvc 2392 M3( "{\\bf \\underline{$text}}",
1918 :     "\\begin{rawhtml}<A HREF=\"$url\" $options>$text</A>\\end{rawhtml}",
1919 :     "<A HREF=\"$url\" $options>$text</A>"
1920 : sh002i 1050 );
1921 :     }
1922 : gage 1267
1923 : jj 2173
1924 :     sub helpLink {
1925 :     my $type1 = shift;
1926 :     return "" if(not defined($envir{'localHelpURL'}));
1927 :     my $type = lc($type1);
1928 :     my %typeHash = (
1929 :     'interval notation' => 'IntervalNotation.html',
1930 :     'units' => 'Units.html',
1931 :     );
1932 :    
1933 :     my $infoRef = $typeHash{$type};
1934 :     return htmlLink( $envir{'localHelpURL'}.$infoRef, $type1,
1935 :     'target="ww_help" onclick="window.open(this.href,this.target,\'width=550,height=350,scrollbars=yes,resizable=on\'); return false;"');
1936 :     }
1937 :    
1938 : sh002i 1050 sub appletLink {
1939 : gage 4088 my $url = $_[0];
1940 :     return oldAppletLink(@_) unless ref($url) ; # handle legacy where applet link completely defined
1941 :     # search for applet
1942 :     # get fileName of applet
1943 :     my $applet = shift;
1944 :     my $options = shift;
1945 :     my $archive = $applet ->{archive};
1946 :     my $codebase = $applet ->{codebase};
1947 : gage 4098 my $code = $applet ->{code};
1948 : gage 4088 my $appletHeader = '';
1949 :     # find location of applet
1950 : gage 4098 if (defined($codebase) and $codebase =~/\S/) {
1951 :     # do nothing
1952 :     } elsif(defined($archive) and $archive =~/\S/) {
1953 :     $codebase = findAppletCodebase($archive )
1954 :     } elsif (defined($code) and $code =~/\S/) {
1955 :     $codebase = findAppletCodebase($code )
1956 :     } else {
1957 :     warn "Must define the achive (.jar file) or code (.class file) where the applet code is to be found";
1958 :     return;
1959 :     }
1960 :    
1961 : gage 4088 if ( $codebase =~/^Error/) {
1962 :     warn $codebase;
1963 :     return;
1964 :     } else {
1965 : gage 4098 # we are set to include the applet
1966 : gage 4088 }
1967 :     my $appletHeader = qq! archive = "$archive " codebase = "$codebase" !;
1968 :     foreach my $key ('name', 'code','width','height', ) {
1969 :     if ( defined($applet->{$key}) ) {
1970 :     $appletHeader .= qq! $key = "!.$applet->{$key}.q!" ! ;
1971 :     } else {
1972 :     warn " $key is not defined for applet ".$applet->{name};
1973 :     # technically name is not required, but all of the other parameters are
1974 :     }
1975 :     }
1976 :     # add parameters to options
1977 :     if (defined($applet->{params}) ) {
1978 :     foreach my $key (keys %{ $applet->{params} }) {
1979 :     my $value = $applet->{params}->{$key};
1980 : gage 4098 $options .= qq{<PARAM NAME = "$key" VALUE = "$value" >\n};
1981 : gage 4088 }
1982 :    
1983 :    
1984 :     }
1985 :     MODES( TeX => "{\\bf \\underline{APPLET} }".$applet->{name},
1986 :     Latex2HTML => "\\begin{rawhtml} <APPLET $appletHeader> $options </APPLET>\\end{rawhtml}",
1987 :     HTML => "<APPLET\n $appletHeader> \n $options \n </APPLET>",
1988 :     #HTML => qq!<OBJECT $appletHeader codetype="application/java"> $options </OBJECT>!
1989 :     );
1990 :     }
1991 :    
1992 :     sub oldAppletLink {
1993 : sh002i 1050 my $url = shift;
1994 :     my $options = shift;
1995 :     $options = "" unless defined($options);
1996 : gage 4088 MODES( TeX => "{\\bf \\underline{APPLET} }",
1997 :     Latex2HTML => "\\begin{rawhtml} <APPLET $url> $options </APPLET>\\end{rawhtml}",
1998 :     HTML => "<APPLET $url> $options </APPLET>"
1999 : sh002i 1050 );
2000 :     }
2001 :     sub spf {
2002 :     my($number,$format) = @_; # attention, the order of format and number are reversed
2003 :     $format = "%4.3g" unless $format; # default value for format
2004 :     sprintf($format, $number);
2005 :     }
2006 :     sub sspf {
2007 :     my($number,$format) = @_; # attention, the order of format and number are reversed
2008 :     $format = "%4.3g" unless $format; # default value for format
2009 :     my $sign = $number>=0 ? " + " : " - ";
2010 :     $number = $number>=0 ? $number : -$number;
2011 :     $sign .sprintf($format, $number);
2012 :     }
2013 :    
2014 :     =head2 Sorting and other list macros
2015 :    
2016 :    
2017 :    
2018 :     Usage:
2019 :     lex_sort(@list); # outputs list in lexigraphic (alphabetical) order
2020 :     num_sort(@list); # outputs list in numerical order
2021 :     uniq( @list); # outputs a list with no duplicates. Order is unspecified.
2022 :    
2023 :     PGsort( \&sort_subroutine, @list);
2024 : apizer 2143 # &sort_subroutine defines order. It's output must be 1 or 0 (true or false)
2025 : sh002i 1050
2026 :     =cut
2027 :    
2028 :     # uniq gives unique elements of a list:
2029 :     sub uniq {
2030 :     my (@in) =@_;
2031 :     my %temp = ();
2032 :     while (@in) {
2033 :     $temp{shift(@in)}++;
2034 :     }
2035 :     my @out = keys %temp; # sort is causing trouble with Safe.??
2036 :     @out;
2037 :     }
2038 :    
2039 :     sub lex_sort {
2040 : apizer 2143 PGsort sub {$_[0] lt $_[1]}, @_;
2041 : sh002i 1050 }
2042 :     sub num_sort {
2043 : apizer 2143 PGsort sub {$_[0] < $_[1]}, @_;
2044 : sh002i 1050 }
2045 :    
2046 :    
2047 :     =head2 Macros for handling tables
2048 :    
2049 :     Usage:
2050 :     begintable( number_of_columns_in_table)
2051 :     row(@dataelements)
2052 :     endtable()
2053 :    
2054 :     Example of useage:
2055 :    
2056 :     BEGIN_TEXT
2057 :     This problem tests calculating new functions from old ones:$BR
2058 :     From the table below calculate the quantities asked for:$BR
2059 :     \{begintable(scalar(@firstrow)+1)\}
2060 :     \{row(" \(x\) ",@firstrow)\}
2061 :     \{row(" \(f(x)\) ", @secondrow)\}
2062 :     \{row(" \(g(x)\) ", @thirdrow)\}
2063 :     \{row(" \(f'(x)\) ", @fourthrow)\}
2064 :     \{row(" \(g'(x)\) ", @fifthrow)\}
2065 :     \{endtable()\}
2066 :    
2067 :     (The arrays contain numbers which are placed in the table.)
2068 :    
2069 :     END_TEXT
2070 :    
2071 :     =cut
2072 :    
2073 :     sub begintable {
2074 :     my ($number)=shift; #number of columns in table
2075 :     my %options = @_;
2076 :     warn "begintable(cols) requires a number indicating the number of columns" unless defined($number);
2077 :     my $out = "";
2078 :     if ($displayMode eq 'TeX') {
2079 :     $out .= "\n\\par\\smallskip\\begin{center}\\begin{tabular}{" . "|c" x $number . "|} \\hline\n";
2080 :     }
2081 :     elsif ($displayMode eq 'Latex2HTML') {
2082 :     $out .= "\n\\begin{rawhtml} <TABLE , BORDER=1>\n\\end{rawhtml}";
2083 :     }
2084 : dpvc 4386 elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng' ||
2085 :     $displayMode eq 'HTML_img' || $displayMode eq 'HTML_jsMath' ||
2086 :     $displayMode eq 'HTML_asciimath' || $displayMode eq 'HTML_LaTeXMathML') {
2087 : sh002i 1050 $out .= "<TABLE BORDER=1>\n"
2088 :     }
2089 : apizer 1080 else {
2090 : apizer 1379 $out = "Error: PGbasicmacros: begintable: Unknown displayMode: $displayMode.\n";
2091 : sh002i 1050 }
2092 :     $out;
2093 :     }
2094 :    
2095 :     sub endtable {
2096 :     my $out = "";
2097 :     if ($displayMode eq 'TeX') {
2098 :     $out .= "\n\\end {tabular}\\end{center}\\par\\smallskip\n";
2099 :     }
2100 :     elsif ($displayMode eq 'Latex2HTML') {
2101 :     $out .= "\n\\begin{rawhtml} </TABLE >\n\\end{rawhtml}";
2102 :     }
2103 : dpvc 4386 elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng' ||
2104 :     $displayMode eq 'HTML_img' || $displayMode eq 'HTML_jsMath' ||
2105 :     $displayMode eq 'HTML_asciimath' || $displayMode eq 'HTML_LaTeXMathML') {
2106 : sh002i 1050 $out .= "</TABLE>\n";
2107 :     }
2108 :     else {
2109 : apizer 1379 $out = "Error: PGbasicmacros: endtable: Unknown displayMode: $displayMode.\n";
2110 : sh002i 1050 }
2111 :     $out;
2112 :     }
2113 :    
2114 :    
2115 :     sub row {
2116 :     my @elements = @_;
2117 :     my $out = "";
2118 :     if ($displayMode eq 'TeX') {
2119 :     while (@elements) {
2120 :     $out .= shift(@elements) . " &";
2121 :     }
2122 :     chop($out); # remove last &
2123 :     $out .= "\\\\ \\hline \n";
2124 :     # carriage returns must be added manually for tex
2125 :     }
2126 :     elsif ($displayMode eq 'Latex2HTML') {
2127 :     $out .= "\n\\begin{rawhtml}\n<TR>\n\\end{rawhtml}\n";
2128 :     while (@elements) {
2129 :     $out .= " \n\\begin{rawhtml}\n<TD> \n\\end{rawhtml}\n" . shift(@elements) . " \n\\begin{rawhtml}\n</TD> \n\\end{rawhtml}\n";
2130 :     }
2131 :     $out .= " \n\\begin{rawhtml}\n</TR> \n\\end{rawhtml}\n";
2132 :     }
2133 : dpvc 4386 elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng'||
2134 :     $displayMode eq 'HTML_img' || $displayMode eq 'HTML_jsMath' ||
2135 :     $displayMode eq 'HTML_asciimath' || $displayMode eq 'HTML_LaTeXMathML') {
2136 : sh002i 1050 $out .= "<TR>\n";
2137 :     while (@elements) {
2138 :     $out .= "<TD>" . shift(@elements) . "</TD>";
2139 :     }
2140 :     $out .= "\n</TR>\n";
2141 :     }
2142 :     else {
2143 : apizer 1379 $out = "Error: PGbasicmacros: row: Unknown displayMode: $displayMode.\n";
2144 : sh002i 1050 }
2145 :     $out;
2146 :     }
2147 :    
2148 :     =head2 Macros for displaying static images
2149 :    
2150 :     Usage:
2151 :     $string = image($image, width => 100, height => 100, tex_size => 800)
2152 : jj 2443 $string = image($image, width => 100, height => 100, extra_html_tags => 'align="middle"', tex_size => 800)
2153 : sh002i 1050 $string = image([$image1, $image2], width => 100, height => 100, tex_size => 800)
2154 :     $string = caption($string);
2155 :     $string = imageRow([$image1, $image2 ], [$caption1, $caption2]);
2156 :     # produces a complete table with rows of pictures.
2157 :    
2158 :    
2159 :     =cut
2160 :    
2161 :     # More advanced macros
2162 :     sub image {
2163 :     my $image_ref = shift;
2164 :     my @opt = @_;
2165 :     unless (scalar(@opt) % 2 == 0 ) {
2166 :     warn "ERROR in image macro. A list of macros must be inclosed in square brackets.";
2167 :     }
2168 :     my %in_options = @opt;
2169 :     my %known_options = (
2170 :     width => 100,
2171 :     height => 100,
2172 :     tex_size => 800,
2173 : jj 2443 extra_html_tags => '',
2174 : sh002i 1050 );
2175 :     # handle options
2176 :     my %out_options = %known_options;
2177 :     foreach my $opt_name (keys %in_options) {
2178 :     if ( exists( $known_options{$opt_name} ) ) {
2179 :     $out_options{$opt_name} = $in_options{$opt_name} if exists( $in_options{$opt_name} ) ;
2180 :     } else {
2181 :     die "Option $opt_name not defined for image. " .
2182 :     "Default options are:<BR> ", display_options2(%known_options);
2183 :     }
2184 :     }
2185 :     my $width = $out_options{width};
2186 :     my $height = $out_options{height};
2187 :     my $tex_size = $out_options{tex_size};
2188 :     my $width_ratio = $tex_size*(.001);
2189 :     my @image_list = ();
2190 :    
2191 :     if (ref($image_ref) =~ /ARRAY/ ) {
2192 :     @image_list = @{$image_ref};
2193 :     } else {
2194 :     push(@image_list,$image_ref);
2195 :     }
2196 : apizer 1080
2197 : gage 1071 my @output_list = ();
2198 : sh002i 1050 while(@image_list) {
2199 :     my $imageURL = alias(shift @image_list);
2200 :     my $out="";
2201 : apizer 1080
2202 : gage 1267 if ($displayMode eq 'TeX') {
2203 : sh002i 1050 my $imagePath = $imageURL; # in TeX mode, alias gives us a path, not a URL
2204 : sh002i 1550 if (defined $envir->{texDisposition} and $envir->{texDisposition} eq "pdf") {
2205 : sh002i 1050 # We're going to create PDF files with our TeX (using pdflatex), so
2206 :     # alias should have given us the path to a PNG image. What we need
2207 :     # to do is find out the dimmensions of this image, since pdflatex
2208 :     # is too dumb to live.
2209 : apizer 1080
2210 : sh002i 1050 #my ($height, $width) = getImageDimmensions($imagePath);
2211 :     ##warn "&image: $imagePath $height $width\n";
2212 :     #unless ($height and $width) {
2213 :     # warn "Couldn't get the dimmensions of image $imagePath.\n"
2214 :     #}
2215 :     #$out = "\\includegraphics[bb=0 0 $height $width,width=$width_ratio\\linewidth]{$imagePath}\n";
2216 :     $out = "\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}\n";
2217 :     } else {
2218 :     # Since we're not creating PDF files, alias should have given us the
2219 :     # path to an EPS file. latex can get its dimmensions no problem!
2220 : apizer 1080
2221 : sh002i 1050 $out = "\\includegraphics[width=$width_ratio\\linewidth]{$imagePath}\n";
2222 :     }
2223 : gage 1267 } elsif ($displayMode eq 'Latex2HTML') {
2224 : jj 2215 my $wid = ($envir->{onTheFlyImageSize} || 0)+ 30;
2225 :     $out = qq!\\begin{rawhtml}\n<A HREF= "$imageURL" TARGET="_blank" onclick="window.open(this.href,this.target, 'width=$wid,height=$wid,scrollbars=yes,resizable=on'); return false;"><IMG SRC="$imageURL" WIDTH="$width" HEIGHT="$height"></A>\n
2226 : sh002i 1050 \\end{rawhtml}\n !
2227 : dpvc 4386 } elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng' ||
2228 :     $displayMode eq 'HTML_img' || $displayMode eq 'HTML_jsMath' ||
2229 :     $displayMode eq 'HTML_asciimath' || $displayMode eq 'HTML_LaTeXMathML') {
2230 : jj 2215 my $wid = ($envir->{onTheFlyImageSize} || 0) +30;
2231 : jj 2443 $out = qq!<A HREF= "$imageURL" TARGET="_blank" onclick="window.open(this.href,this.target, 'width=$wid,height=$wid,scrollbars=yes,resizable=on'); return false;"><IMG SRC="$imageURL" WIDTH="$width" HEIGHT="$height" $out_options{extra_html_tags} ></A>
2232 : sh002i 1050 !
2233 :     } else {
2234 : apizer 1379 $out = "Error: PGbasicmacros: image: Unknown displayMode: $displayMode.\n";
2235 : sh002i 1050 }
2236 :     push(@output_list, $out);
2237 :     }
2238 :     return wantarray ? @output_list : $output_list[0];
2239 :     }
2240 :    
2241 :     # This is legacy code.
2242 :     sub images {
2243 :     my @in = @_;
2244 :     my @outlist = ();
2245 :     while (@in) {
2246 :     push(@outlist,&image( shift(@in) ) );
2247 :     }
2248 :     @outlist;
2249 :     }
2250 :    
2251 :    
2252 :     sub caption {
2253 :     my ($out) = @_;
2254 : gage 1267 $out = " $out \n" if $displayMode eq 'TeX';
2255 :     $out = " $out " if $displayMode eq 'HTML';
2256 :     $out = " $out " if $displayMode eq 'HTML_tth';
2257 :     $out = " $out " if $displayMode eq 'HTML_dpng';
2258 :     $out = " $out " if $displayMode eq 'HTML_img';
2259 : dpvc 2166 $out = " $out " if $displayMode eq 'HTML_jsMath';
2260 : dpvc 2199 $out = " $out " if $displayMode eq 'HTML_asciimath';
2261 : dpvc 4386 $out = " $out " if $displayMode eq 'HTML_LaTeXMathML';
2262 : gage 1267 $out = " $out " if $displayMode eq 'Latex2HTML';
2263 : sh002i 1050 $out;
2264 :     }
2265 :    
2266 :     sub captions {
2267 :     my @in = @_;
2268 :     my @outlist = ();
2269 :     while (@in) {
2270 :     push(@outlist,&caption( shift(@in) ) );
2271 :     }
2272 :     @outlist;
2273 :     }
2274 :    
2275 :     sub imageRow {
2276 :    
2277 :     my $pImages = shift;
2278 :     my $pCaptions=shift;
2279 :     my $out = "";
2280 :     my @images = @$pImages;
2281 :     my @captions = @$pCaptions;
2282 :     my $number = @images;
2283 :     # standard options
2284 :     my %options = ( 'tex_size' => 200, # width for fitting 4 across
2285 :     'height' => 100,
2286 :     'width' => 100,
2287 :     @_ # overwrite any default options
2288 :     );
2289 :    
2290 : gage 1267 if ($displayMode eq 'TeX') {
2291 : sh002i 1050 $out .= "\n\\par\\smallskip\\begin{center}\\begin{tabular}{" . "|c" x $number . "|} \\hline\n";
2292 :     while (@images) {
2293 :     $out .= &image( shift(@images),%options ) . '&';
2294 :     }
2295 :     chop($out);
2296 :     $out .= "\\\\ \\hline \n";
2297 :     while (@captions) {
2298 :     $out .= &caption( shift(@captions) ) . '&';
2299 :     }
2300 :     chop($out);
2301 :     $out .= "\\\\ \\hline \n\\end {tabular}\\end{center}\\par\\smallskip\n";
2302 : gage 1267 } elsif ($displayMode eq 'Latex2HTML'){
2303 : sh002i 1050
2304 :     $out .= "\n\\begin{rawhtml} <TABLE BORDER=1><TR>\n\\end{rawhtml}\n";
2305 :     while (@images) {
2306 :     $out .= "\n\\begin{rawhtml} <TD>\n\\end{rawhtml}\n" . &image( shift(@images),%options )
2307 :     . "\n\\begin{rawhtml} </TD>\n\\end{rawhtml}\n" ;
2308 :     }
2309 :    
2310 :     $out .= "\n\\begin{rawhtml}</TR><TR>\\end{rawhtml}\n";
2311 :     while (@captions) {
2312 :     $out .= "\n\\begin{rawhtml} <TH>\n\\end{rawhtml}\n".&caption( shift(@captions) )
2313 :     . "\n\\begin{rawhtml} </TH>\n\\end{rawhtml}\n" ;
2314 :     }
2315 :    
2316 :     $out .= "\n\\begin{rawhtml} </TR> </TABLE >\n\\end{rawhtml}";
2317 : dpvc 4386 } elsif ($displayMode eq 'HTML' || $displayMode eq 'HTML_tth' || $displayMode eq 'HTML_dpng' ||
2318 :     $displayMode eq 'HTML_img' || $displayMode eq 'HTML_jsMath' ||
2319 :     $displayMode eq 'HTML_asciimath' || $displayMode eq 'HTML_LaTeXMathML') {
2320 : sh002i 1050 $out .= "<P>\n <TABLE BORDER=2 CELLPADDING=3 CELLSPACING=2 ><TR ALIGN=CENTER VALIGN=MIDDLE>\n";
2321 :     while (@images) {
2322 :     $out .= " \n<TD>". &image( shift(@images),%options ) ."</TD>";
2323 :     }
2324 :     $out .= "</TR>\n<TR>";
2325 :     while (@captions) {
2326 :     $out .= " <TH>". &caption( shift(@captions) ) ."</TH>";
2327 :     }
2328 :     $out .= "\n</TR></TABLE></P>\n"
2329 :     }
2330 :     else {
2331 : apizer 1379 $out = "Error: PGbasicmacros: imageRow: Unknown languageMode: $displayMode.\n";
2332 : sh002i 1050 warn $out;
2333 :     }
2334 :     $out;
2335 :     }
2336 :    
2337 :    
2338 :     ###########
2339 :     # Auxiliary macros
2340 :    
2341 :     sub display_options2{
2342 :     my %options = @_;
2343 :     my $out_string = "";
2344 :     foreach my $key (keys %options) {
2345 :     $out_string .= " $key => $options{$key},<BR>";
2346 :     }
2347 :     $out_string;
2348 :     }
2349 :    
2350 :    
2351 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9