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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 : sh002i 1050
2 : apizer 1080
3 : sh002i 1050 use strict;
4 :    
5 :     ## $ENV{'PATH'} .= ':/usr/math/bin';
6 :    
7 :     my $debug = 0;
8 :     $debug = 1 if $Global::imageDebugMode;
9 :     ## if $debug =1, log, etc. files created by
10 :     ## latex2html are not deleted
11 :    
12 :     ##############################################################
13 :     # File: DisplayMacros.pl
14 :     # This contains the subroutines for creating problem files
15 :     ##############################################################
16 :    
17 :     ################################################################
18 :     # Copyright @1995-1998 by Michael E. Gage, Arnold K. Pizer and
19 :     # WeBWorK at the University of Rochester. All rights reserved.
20 :     ################################################################
21 :    
22 :    
23 :     ## To add or delete displayModes edit this file
24 :    
25 :     sub displaySelectModeLine_string
26 :     # called from probSet.pl
27 :     # displays the option line for selecting display modes
28 :     {
29 :     my ($displayMode) =@_ ;
30 :     my $out = "Display Mode: <BR>";
31 :    
32 :     # A list of the available modes.
33 :     my $mode_list = $Global::available_mode_list; ## ref to a list of available modes
34 :     ## The format is [internal symbol, external name]
35 :     # A list of the available modes.
36 :     # Format is [internal symbol, external name, ""], where the third
37 :     # argument is changed to checked below for the current displayMode
38 :     # my $mode_list = [
39 :     # ['HTML', 'text', ""],
40 :     # ['HTML_tth', 'formatted-text',""],
41 :     # ['HTML_dpng' ,'dvipng',""],
42 :     # ['Latex2HTML', 'typeset',""]
43 :     # ];
44 :    
45 :     # Make the format [internal symbol, external name, '']
46 :     # The third argument is changed to checked below for the current displayMode
47 :     my $j;
48 :     for $j (0..(scalar(@{$mode_list})-1)) {
49 :     push @{$mode_list->[$j]},'';
50 :     }
51 :    
52 :     if (! defined($displayMode) ) {$displayMode = $Global::htmldisplayModeDefault;}
53 :    
54 :    
55 :     my $found = 0;
56 :     # Search through all modes to match for displayMode
57 :     # If we don't find one, found=0 will trigger warn message below
58 :     for $j (0..(scalar(@{$mode_list})-1)) {
59 :     if($mode_list->[$j]->[0] eq $displayMode) {
60 :     $mode_list->[$j]->[2] = "CHECKED";
61 :     $found=1;
62 :     last;
63 :     }
64 :     }
65 :    
66 :     for $j (@{$mode_list}) {
67 :     $out .= qq!<INPUT TYPE=RADIO NAME="Mode" VALUE="$j->[0]" $j->[2]>$j->[1]<BR>\n!;
68 :     }
69 :     if(! $found) {
70 :     my $wstr = " Error: displayMacros.pl: sub displaySelectModeLine. Unrecognized mode |$displayMode| . The acceptable modes are: ";
71 :     for $j (@{$mode_list}) {
72 :     $wstr .= " $j->[0] ";
73 :     }
74 :     warn $wstr;
75 :     }
76 :     $out;
77 :     }
78 :    
79 :     sub displaySelectModeLine {
80 :     print displaySelectModeLine_string(@_);
81 :     }
82 :     ##################################################################################################################
83 :     # Does the initial processing of the problem.
84 :     # Returns an array containing the rendered problem. #
85 :     ##################################################################################################################
86 :    
87 :     sub createDisplayedProblem {
88 :    
89 :     my ($setNumber,$probNum,$psvn,$printlinesref,$rh_flags)= @_;
90 :     my @printlines;
91 :    
92 :    
93 :     my $coursel2hDirectory = getCoursel2hDirectory();
94 :     unless(-e $coursel2hDirectory ) {
95 :     &createDirectory($coursel2hDirectory, $Global::l2h_set_directory_permission,
96 :     $Global::numericalGroupID);
97 :     }
98 :    
99 :     unless(-e "${coursel2hDirectory}set$setNumber") {
100 :     &createDirectory("${coursel2hDirectory}set$setNumber",$Global::l2h_set_directory_permission,
101 :     $Global::numericalGroupID);
102 :     }
103 :    
104 :    
105 :     my $PROBDIR = convertPath("${coursel2hDirectory}set$setNumber/$probNum-$psvn/");
106 :     my $TMPPROBDIR = convertPath("${coursel2hDirectory}$probNum-$psvn/");
107 :    
108 :     if (! -e $PROBDIR) { # no gifs of equations have been created
109 :     &l2hcreate($setNumber,$probNum,$psvn,$printlinesref);
110 :    
111 :     } else { # determine if the gifs are older than the modifications of the source file
112 :     #&attachProbSetRecord($psvn);
113 :     my $fileName = &getProblemFileName($probNum,$psvn);
114 :     $fileName = "${Global::templateDirectory}$fileName";
115 :     #print "\n\n The filename is $fileName \n\n";
116 :     my @probDirStat = stat $PROBDIR;
117 :     my @sourceFileStat = stat $fileName;
118 :     #print "\n\n The source file age is $sourceFileStat[9] \n\n";
119 :     #print "\n\n The prob dir age is $probDirStat[9] \n\n";
120 :    
121 :     if (($sourceFileStat[9] > $probDirStat[9] ) or
122 :     $rh_flags->{'refreshCachedImages'}) {
123 :     ## source file is newer or solutions should be shown recreate the l2h cache
124 :     rmDirectoryAndFiles($PROBDIR);
125 :     &l2hcreate($setNumber,$probNum,$psvn,$printlinesref);
126 :     }
127 :    
128 :    
129 :     }
130 :     #the problem has been rendered by Latex2HTML into this file:
131 :     # open(TEXXX, "${PROBDIR}${psvn}output.html") || die "Can't open ${PROBDIR}${psvn}output.html";
132 :     open(TEXXX, "${PROBDIR}${psvn}output.html") or
133 :     warn "ERROR: $0".
134 :     "Can't open the HTML file: \n ${PROBDIR}${psvn}output.html\n(allegedly)".
135 :     "translated by latex2HTML\n at displayMacros.pl, line" . __LINE__ ;
136 :    
137 :     @printlines = <TEXXX>;
138 :     push(@printlines, "The file ${PROBDIR}${psvn}output.html was empty") unless @printlines;
139 :     #print "PRINTLINES",@printlines;
140 :     close(TEXXX);
141 :    
142 :     @printlines;
143 :     }
144 :    
145 :    
146 :    
147 :     ###########################################################################################
148 :     # Formats and displays the responses to submitted answers to the problem. Returns a string. #
149 :     ###########################################################################################
150 :    
151 :     sub display_answers { # this will be put in displayMacros.pl soon.
152 :     #my ($displayCorrectAnswersQ,$showPartialCorrectAnswers,$rh_answer_results,$rh_problem_result) = @_;
153 :     my ($rh_answer_results,$rh_problem_result,$rh_flags) = @_;
154 :     my $displayCorrectAnswersQ = $rh_flags ->{displayCorrectAnswersQ};
155 :     my $showPartialCorrectAnswers = $rh_flags -> {showPartialCorrectAnswers};
156 :     my @answer_entry_order = @{$rh_flags -> {ANSWER_ENTRY_ORDER} };
157 :     my $ANSWER_PREFIX = $rh_flags -> {ANSWER_PREFIX};
158 :     my $allAnswersCorrectQ = 1;
159 :     my $printedResponse='';
160 :     ###### Print appropriate response to submitted answers
161 :     my ($i,$answerIsCorrectQ, $normalizedSubmittedAnswer,$normalizedCorrectAnswer,$ans_name,$errors);
162 :     $i=0;
163 : apizer 1351 # $printedResponse .= "\n<table border=0 cellpadding=0 cellspacing=0 bgcolor=\"#cccccc\">\n";
164 :     # replace above line by next two lines as per Davide Cervone. AKP.
165 :     $printedResponse .= "\n<table border=0 cellpadding=7 cellspacing=0 bgcolor=\"#cccccc\">\n";
166 :     $printedResponse .= "<tr><td><table border=0 cellpadding=0 cellspacing=0>\n";
167 : sh002i 1050 foreach my $key ( @answer_entry_order ) {
168 :    
169 :     $i++;
170 :     $answerIsCorrectQ = $rh_answer_results ->{$key} -> {score};
171 :     $normalizedSubmittedAnswer = $rh_answer_results ->{$key} -> {student_ans};
172 :     $normalizedSubmittedAnswer = '' if ($normalizedSubmittedAnswer =~ /^error:\s+empty/);
173 : gage 1071 $normalizedCorrectAnswer = $rh_answer_results ->{$key} -> {original_correct_ans};
174 :    
175 :     ## Handle the case where the answer evaluator does not return original_correct_ans
176 :     if ((!defined $normalizedCorrectAnswer) or (!$normalizedCorrectAnswer =~ /\S/)) {
177 :     $normalizedCorrectAnswer = $rh_answer_results ->{$key} -> {correct_ans};
178 :     }
179 :    
180 : sh002i 1050 $errors = $rh_answer_results ->{$key} -> {ans_message};
181 :     $errors = '' if ($errors eq 'empty');
182 :     #$ans_name = $rh_answer_results ->{$key} -> {ans_name};
183 :     #$ans_name =~ s/$ANSWER_PREFIX//; # this handles implicitly defined answer names.
184 :     $ans_name = $i; # just number the answers in order
185 :     $allAnswersCorrectQ = $allAnswersCorrectQ && $answerIsCorrectQ;
186 :     $printedResponse .= "\n<TR><TD align=left COLSPAN =2><em>Answer $ans_name entered:</em>--&gt; $normalizedSubmittedAnswer &lt;-- ";
187 :     $printedResponse .= "<B>Correct. </B></TD></TR>" if ($answerIsCorrectQ && $showPartialCorrectAnswers );
188 :     $printedResponse .= "<B>Incorrect. </B></TD></TR>" if (!($answerIsCorrectQ) && $showPartialCorrectAnswers);
189 : apizer 1351 $errors =~ s/\n/<BR>/g; ## convert newlines to <BR> in error messages as per Davide Cervone
190 : sh002i 1050 # change 9/2/00 by MEG -- give width in pixels rather than %.
191 :     # Some browsers break with % widht which is not the standard
192 : apizer 1351 $printedResponse .= "\n<TR> <TD align=left WIDTH = \"50\" >&nbsp;</TD><TD align=left>$errors</TD></TR>" if ($errors =~ /\w/);
193 : sh002i 1050
194 : apizer 1351 $printedResponse .= "\n<TR><TD align=left WIDTH = \"50\">&nbsp;</TD> <TD align=left><em>Correct answer:</em> $normalizedCorrectAnswer</TD></TR>" if ($displayCorrectAnswersQ);
195 : sh002i 1050
196 :     }
197 :     if ($i == 1) {
198 :     $printedResponse .= "\n<TR><TD align=left COLSPAN =2><B>The above answer is correct.</B><BR>" if ($allAnswersCorrectQ);
199 :     $printedResponse .= "\n<TR><TD align=left COLSPAN =2><B>The above answer is NOT correct.</B><BR>" if (!($allAnswersCorrectQ));
200 :     }
201 :     else {
202 :     $printedResponse .= "\n<TR><TD align=left COLSPAN =2><B>All of the above answers are correct.</B><BR>" if ($allAnswersCorrectQ);
203 :     $printedResponse .= "\n<TR><TD align=left COLSPAN =2><B>At least one of the above answers is NOT correct.</B><BR>" if (!($allAnswersCorrectQ));
204 :     }
205 :     my $percentCorr = int(100*$rh_problem_result->{score} +.5);
206 :    
207 :     $printedResponse .="\n<TR><TD align=left COLSPAN =2><B>Your score on this attempt is ${percentCorr}\%.</B><BR>";
208 : apizer 1351 # $printedResponse .= "\n</table>\n";
209 :     # replace above line by next line as per Davide Cervone. AKP.
210 :     $printedResponse .= "</td></tr>\n</table>\n</table>\n";
211 : sh002i 1050 # $printedResponse .="\n problem grader is ".$rh_problem_result->{type}." and the score is ".$rh_problem_result->{score}."<BR>\n";
212 :     $printedResponse;
213 :     }
214 :    
215 :     ###########################################################################################
216 :     # Previews submitted answers to the problem. Returns a string. #
217 :     ###########################################################################################
218 :    
219 :     sub preview_answers {
220 :     my ($rh_answer_results,$rh_problem_result,$rh_flags) = @_;
221 :     my @answer_entry_order = @{$rh_flags -> {ANSWER_ENTRY_ORDER} };
222 :     my $ANSWER_PREFIX = $rh_flags -> {ANSWER_PREFIX};
223 :     my $printedResponse ='';
224 :     ###### Print appropriate response to submitted answers
225 :     my ($i,$original_student_ans,$normalizedSubmittedAnswer,$errors,$ans_name,$preview_text_string,$preview_latex_string);
226 :     my ($ans_evaluator_type, $value_word, $error_word, $show_value);
227 :    
228 :     $i=0;
229 :     $printedResponse .= "\n<table border=0 cellpadding=0 cellspacing=0 >\n";
230 :     foreach my $key ( @answer_entry_order ) {
231 :     $i++;
232 :     $ans_name = $rh_answer_results ->{$key} -> {ans_name};
233 :     #$ans_name =~ s/$ANSWER_PREFIX//; # this handles implicitly defined answer names. #commented out by DME 6/6/2000
234 :     $original_student_ans = $rh_answer_results ->{$key} -> {original_student_ans};
235 :     $normalizedSubmittedAnswer = $rh_answer_results ->{$key} -> {student_ans};
236 :     $errors = $rh_answer_results ->{$key} -> {ans_message};
237 : apizer 1351 $errors =~ s/\n/<BR>/g; ## convert newlines to <BR> in error messages as per Davide Cervone
238 : sh002i 1050 $preview_text_string ='';
239 :     $preview_text_string = $rh_answer_results ->{$key} -> {preview_text_string}
240 :     if defined $rh_answer_results ->{$key} -> {preview_text_string};
241 :     $preview_latex_string ='';
242 :     $preview_latex_string = $rh_answer_results ->{$key} -> {preview_latex_string}
243 :     if defined $rh_answer_results ->{$key} -> {preview_latex_string};
244 :     $ans_evaluator_type = $rh_answer_results ->{$key} -> {type};
245 :     $value_word = 'value:';
246 :     $show_value = 0;
247 :     $show_value = 1 if ((($ans_evaluator_type =~ /number/) and ($normalizedSubmittedAnswer =~ /\w/)) or ($normalizedSubmittedAnswer =~ /^error/));
248 :     $show_value = 0 if ($normalizedSubmittedAnswer =~ /^error:\s+empty/);
249 :     $value_word = '' if ($normalizedSubmittedAnswer =~ /^error/);
250 :     $error_word = 'error:';
251 :     $error_word = '' if ($errors =~ /^error:/);
252 :     $printedResponse .= "\n<TR><TD align=left>Ans $i </TD>";
253 :     #$printedResponse .= "\n<TD align=left><INPUT TYPE=\"text\" NAME=\"${ANSWER_PREFIX}${ans_name}\" VALUE=\"$original_student_ans\" SIZE=70></TD></TR>"; #commented out by DME 6/6/2000
254 :     $printedResponse .= "\n<TD align=left><INPUT TYPE=\"text\" NAME=\"${ans_name}\" VALUE=\"$original_student_ans\" SIZE=70></TD></TR>";
255 :     $printedResponse .= "\n<TR> <TD align=left WIDTH = \"7%\" ></TD><TD align=left>parsed: $preview_text_string</TD></TR>" if ($preview_text_string =~ /\w/);
256 :     $printedResponse .= "\n<TR> <TD align=left WIDTH = \"7%\" ></TD><TD align=left>${value_word} $normalizedSubmittedAnswer</TD></TR>" if $show_value == 1;
257 :     $printedResponse .= "\n<TR> <TD align=left WIDTH = \"7%\" ></TD><TD align=left>${error_word} $errors</TD></TR>" if (($errors =~ /\w/) and ($errors ne 'empty')) ;
258 :     if ($preview_latex_string =~ /\w/) {
259 :     $printedResponse .= "\n<TR> <TD align=left WIDTH = \"7%\" ></TD><TD align=left>";
260 :     $printedResponse .= "\n <APPLET CODE=\"HotEqn.class\" HEIGHT=\"80\" WIDTH=\"500\" ARCHIVE=\"HotEqn.zip\" NAME=\"Equation\" ALIGN=\"middle\" CODEBASE=\"$Global::appletsURL\"> ";
261 :     $printedResponse .= "\n <PARAM NAME=\"equation\" VALUE=\"$preview_latex_string\"></APPLET></TD></TR> ";
262 :     }
263 :     $printedResponse .= "\n<TR Height = 5></TR>";
264 :     }
265 :    
266 :     $printedResponse .= "\n</table>\n";
267 :     $printedResponse;
268 :     }
269 :    
270 :    
271 :     sub lc_sort { # this sorts strings with letters and number groups, alternately lexigraphically and numerically
272 :     # (lc stands for library of congress as in QA617.34R45)
273 :     my($left,$right) = @_;
274 :     # format "abcd345.57def34ABC";
275 :     # string assumed to begin with alpha
276 :     # string is split into alternating alpha and numeric groups
277 :     # numeric groups match [\d\.]+
278 :     # numeric groups assumed to contain at least one digit, ( a period alone will cause and error)
279 :     # alpha groups can contain any characters except digits and the period
280 :     # spaces in alpha groups will cause unexpected behavior
281 :     # sort is not case sensitive
282 :     # _ sorts after alpha characters
283 :    
284 :     # not case sensitive
285 :    
286 :     my @a = split( /([\d\.]+)/, $left);
287 :    
288 :     my @b = split( /([\d\.]+)/, $right);
289 :    
290 :     my $out = undef;
291 :     my $mode = 0; # even is lexic and odd is numeric
292 :     my($l,$r);
293 :     while (@a) {
294 :     $l = shift @a;
295 :     $r = shift @b;
296 :     $out = ($mode++ % 2 == 0) ? uc($l) cmp uc($r) : $l <=> $r; # lexic or numeric compare
297 :     last unless $out==0; # stop unless $l and $r are different.
298 :    
299 :     }
300 :     $out;
301 :     }
302 :    
303 :     #####################################################################
304 :     # Creates an insert which appears on the probSet page. #
305 :     #####################################################################
306 :     sub createDisplayedInsert
307 :     {
308 :     #my ($mode,$setNumber,$fileName,$psvn,$courseName,$printlinesref)= @_;
309 :     my ($setNumber,$fileName,$psvn,$courseName,$printlinesref)= @_;
310 :    
311 :     my @printlines=@$printlinesref;
312 :     my $PROBDIR;
313 :    
314 :     # if($mode eq "HTML" || $mode eq 'HTML_tth') {
315 :     # @printlines = &createProblem2($mode,$fileName,$psvn,$courseName,$sourceref);
316 :     #
317 :     # } elsif ($mode eq 'Latex2HTML') {
318 :     #latex2html processing
319 :     my $coursel2hDirectory = getCoursel2hDirectory();
320 :     unless(-e $coursel2hDirectory ) {
321 :     &createDirectory($coursel2hDirectory, $Global::l2h_set_directory_permission,
322 :     $Global::numericalGroupID);
323 :     }
324 :    
325 :     unless(-e "${coursel2hDirectory}set$setNumber") {
326 :     &createDirectory("${coursel2hDirectory}set$setNumber",$Global::l2h_set_directory_permission,
327 :     $Global::numericalGroupID);
328 :     }
329 :    
330 :     my $shortFileName = $fileName;
331 :     $shortFileName =~ s|^.*?([^\/]*)$|$1|;
332 :     $shortFileName =~ s|\..*$||;
333 :     $PROBDIR = convertPath("${coursel2hDirectory}set$setNumber/$shortFileName-$psvn/");
334 :     if (! -e $PROBDIR) {
335 :     &l2hcreate($setNumber,$shortFileName,$psvn,$printlinesref);
336 :     } else {
337 :     #&attachProbSetRecord($psvn);
338 :     my $fullFileName = "${Global::templateDirectory}$fileName";
339 :     #print "\n\n The full filename is $fullFileName \n\n";
340 :     my @probDirStat = stat $PROBDIR;
341 :     my @sourceFileStat = stat $fullFileName;
342 :     #print "\n\n The source file age is $sourceFileStat[9] \n\n";
343 :     #print "\n\n The prob dir age is $probDirStat[9] \n\n";
344 :     if ($sourceFileStat[9] > $probDirStat[9] ) { ## source file is newer
345 :     rmDirectoryAndFiles($PROBDIR);
346 :     &l2hcreate($setNumber,$shortFileName,$psvn,$printlinesref);
347 :     }
348 :     #else {&createProblem2($mode, $fileName, $psvn,$courseName,$sourceref);} ##initialize problem
349 :    
350 :     }
351 :    
352 :    
353 :     open(TEXXX, "${PROBDIR}${psvn}output.html") or
354 :     wwerror("ERROR: $0", "Can't open ${PROBDIR}${psvn}output.html",'','', '');
355 :     @printlines = <TEXXX>;
356 :     close(TEXXX);
357 :     # } else {
358 :     #
359 :     # @printlines="createDisplayedProblem: Error: Mode is not HTML, HTML_tthHTML_tth or Latex2HTML.";
360 :     #
361 :     #
362 :     # }
363 :     @printlines;
364 :     }
365 :    
366 :     ##do not need this subroutine anymore
367 :     #sub l2hcreateProb {
368 :     # my ($setNumber,$probNum,$psvn,$printlinesref)= @_;
369 :     # #my ($setNumber,$probNum,$psvn,$courseName,$printlinesref)= @_;
370 :     # #my $mode = 'Latex2HTML';
371 :     #
372 :     # #my @printlines = &createProblem($mode, $probNum, $psvn, $courseName,$sourceref,$refSubmittedAnswers);
373 :     # #my $printlinesref = \@printlines;
374 :     # my $tmpDirectory = "tmp/l2h/set$setNumber/$probNum-$psvn/";
375 :     # l2hcreate($setNumber,$probNum,$psvn,$printlinesref)
376 :     #}
377 :    
378 :     #do not use this subroutine anymore
379 :     #sub l2hcreateInsert {
380 :     # my ($setNumber,$shortFileName,$psvn,$printlinesref)= @_;
381 :     # #my $mode = 'Latex2HTML';
382 :     # #my @printlines = &createProblem2($mode, $fileName, $psvn,$courseName,$sourceref);
383 :     # #my $printlinesref = \@printlines;
384 :     # #my $shortFileName = $fileName;
385 :     # #$shortFileName =~ s|^.*?([^\/]*)$|$1|;
386 :     # #my $tmpDirectory = "tmp/l2h/set$setNumber/$shortFileName-$psvn/";
387 :     # l2hcreate($setNumber,$shortFileName,$psvn,$printlinesref)
388 :     #}
389 :    
390 :     sub l2hcreate { ## for latex2HTML 96.1 and 98.1
391 :     my ($setNumber,$probNum,$psvn,$printlinesref) = @_;
392 :    
393 :     # warn "l2hcreate is being executed displaymacros.pl line ".__LINE__;
394 :    
395 :     my $PROBDIR = convertPath(&getCoursel2hDirectory."set$setNumber/$probNum-$psvn/");
396 :     my $TMPPROBDIR = convertPath(&getCoursel2hDirectory."$probNum-$psvn/");
397 :     my $PROBURL = &getCoursel2hURL."set$setNumber/$probNum-$psvn/";
398 :    
399 :     &createDirectory($TMPPROBDIR,$Global::l2h_prob_directory_permission,$Global::numericalGroupID)
400 :     unless(-e "$TMPPROBDIR");
401 :    
402 :     open(OUTTEXFILE, ">$TMPPROBDIR${psvn}output.tex") or wwerror($0, "Can't open temporary file $TMPPROBDIR${psvn}output.tex");
403 :    
404 :     print OUTTEXFILE &texInput($Global::TEX_PROB_PREAMBLE);
405 :     print OUTTEXFILE &texInput($Global::TEX_PROB_HEADER);
406 :     print OUTTEXFILE @$printlinesref;
407 :     print OUTTEXFILE &texInput($Global::TEX_PROB_FOOTER);
408 :     close(OUTTEXFILE);
409 :    
410 :     ## Give this temporary file permission 666 in case the process dies before it it deleted 60 lines further down
411 :     chmod(0666, "$TMPPROBDIR${psvn}output.tex");
412 :    
413 :     ## system("/usr/math/bin/latex2html -init_file ${Global::mainDirectory}latex2html.init -dir $PROBDIR -prefix $psvn ${htmlDirectory}tmp/l2h/${psvn}output.tex > ${htmlDirectory}tmp/l2h/${psvn}l2h.log");
414 :     my $latex2HTML_result = &makeL2H($TMPPROBDIR, $psvn) ;
415 :     warn( "LaTeX2HTML failed. Returned with status: $latex2HTML_result\n" ) if $latex2HTML_result ;
416 : gage 1071
417 : sh002i 1050 ##Get rid of all unwanted stuff in html document created by latex2html
418 :     unless(-e "${TMPPROBDIR}${psvn}output.html") {
419 :     warn "Can't rename ${TMPPROBDIR}${psvn}output.html";
420 :     return (0); ### there was a failure in latex2html processing
421 :     ### we just give a warning so that so that l2hPrecreateSet.pl can continue
422 :     }
423 :    
424 :     rename("${TMPPROBDIR}${psvn}output.html","${TMPPROBDIR}${psvn}output.html.org") or
425 :     warn "Can't rename ${TMPPROBDIR}${psvn}output.html at ". __LINE__;
426 :     open(TEXORG, "${TMPPROBDIR}${psvn}output.html.org") or
427 :     warn "Can't open ${TMPPROBDIR}${psvn}output.html.org";
428 :     my @l2hOutputArray;
429 :    
430 :    
431 :    
432 :    
433 :     BLK: { # This is protection to make absolutely sure that the line separater is set properly.
434 :     # It's still a mystery as to where this becomes defined to be something else.
435 :     local($/);
436 :     $/ = "\n";
437 :     @l2hOutputArray = <TEXORG>;
438 :    
439 :    
440 :     }
441 :    
442 :     close(TEXORG);
443 :     open(TEXNEW, ">${TMPPROBDIR}${psvn}output.html") or
444 :     wwerror($0, "Can't open ${TMPPROBDIR}${psvn}output.html",'','', '');
445 :    
446 :    
447 :     foreach (@l2hOutputArray) {
448 :     if($_ =~ /^<META/) {next;}
449 :     if($_ =~ /^<!DOCTYPE HTML PUBLIC/) {next;}
450 :     if($_ =~ /^<HTML>/) {next;}
451 :     if($_ =~ /^<HEAD>/) {next;}
452 :     if($_ =~ /^<TITLE>/) {next;}
453 :     if($_ =~ /^<LINK REL/) {next;}
454 :     if($_ =~ /^<\/HEAD>/) {next;}
455 :     if($_ =~ /^<BODY/) {next;}
456 :     if($_ =~ /^<\/BODY>/) {next;}
457 :     if($_ =~ /^<\/HTML>/) {next;}
458 :     if($_ =~ /^<BR> <HR>/) {next;}
459 :    
460 :     print TEXNEW ;
461 :     }
462 :    
463 :    
464 :     close(TEXNEW);
465 :    
466 :     ## Now do global multiline changes on whole file
467 :    
468 :     open(TEXNEW, "${TMPPROBDIR}${psvn}output.html") or
469 :     wwerror("$0", "Can't open ${TMPPROBDIR}${psvn}output.html",'','', '');
470 :     @l2hOutputArray = <TEXNEW>;
471 :     close(TEXNEW);
472 :     my $l2hOutputString = join('',@l2hOutputArray);
473 :    
474 :     ## make gif images created by latex2html locatable by server
475 :     ## NOTE: $htmlURL is defined in webworkCourse.ph . Often this will
476 :     ## will be a link appearing in a public_html_docs directory.
477 :     ## The $htmlURL, any links, and the next line must be coordinated.
478 :    
479 :     $l2hOutputString =~ s|${psvn}img|${PROBURL}${psvn}img|g;
480 :    
481 :     ## remove multiline comments
482 :     $l2hOutputString =~ s|<!--.*?-->\n||sg;
483 :    
484 :     open(TEXNEW, ">${TMPPROBDIR}${psvn}output.html") or
485 :     wwerror("$0", "Can't open ${TMPPROBDIR}${psvn}output.html",'','', '');
486 :     print TEXNEW $l2hOutputString;
487 :     close(TEXNEW);
488 :    
489 :     ## remove unneeded files
490 :    
491 :     unless ($debug) {unlink("${TMPPROBDIR}${psvn}output.html.org");}
492 :     unless ($debug) {unlink(<${TMPPROBDIR}*images.*>);}
493 :     unless ($debug) {unlink(<${TMPPROBDIR}.*.db>);}
494 :     unless ($debug) {unlink(<${TMPPROBDIR}*.db>);}
495 :     unless ($debug) {unlink(<${TMPPROBDIR}IMG_PARAMS.*>);}
496 :     unless ($debug) {unlink(<${TMPPROBDIR}*.pl>);}
497 :     unless ($debug) {unlink(<${TMPPROBDIR}*.css>);}
498 :     unless ($debug) {unlink("${TMPPROBDIR}index.html");}
499 :     unless ($debug) {unlink("${TMPPROBDIR}${psvn}output.tex");}
500 :     unless ($debug) {unlink("${TMPPROBDIR}${psvn}l2h.log");}
501 :     unless ($debug) {
502 :     my @allfiles = ();
503 :     opendir( DIRHANDLE, "$TMPPROBDIR") || warn qq/Can't read directory $TMPPROBDIR $!/;
504 :     @allfiles = map "$TMPPROBDIR$_", grep( /^l2h/, readdir DIRHANDLE);
505 :     closedir(DIRHANDLE);
506 :     my $l2hTempDir = $allfiles[0];
507 :     if (defined $l2hTempDir) {
508 :     unlink(<$l2hTempDir/*>);
509 :     rmdir ($l2hTempDir);
510 :     }
511 :     }
512 :    
513 :     ## change permission and group on remaining files
514 :     chmod($Global::l2h_data_permission, glob("${TMPPROBDIR}*"));
515 :     chown(-1,$Global::numericalGroupID,glob("${TMPPROBDIR}*"));
516 :    
517 :     ## Now that all the processing has been done, rename the $TMPPROBDIR TO $PROBDIR
518 :    
519 :     rename("$TMPPROBDIR","$PROBDIR") or
520 :     warn "Can't rename the temporary problem directory:\n $TMPPROBDIR to $PROBDIR\n at displayMacros.pl , line: " . __LINE__ ;
521 :    
522 :     }
523 :    
524 :    
525 :     #########################################################################################################
526 :     ##Subroutine that makes answers sticky in l2h mode #
527 :     # #
528 :     # INPUT: $rh_submittedAnswers Reference to a hash containing the answers submitted #
529 :     # $ra_printLines Reference to an array containing the (HTML) text to be output #
530 :     # $rh_flags Reference to a hash containing flags; specifically a #
531 :     # reference to an array containing the answer field labels #
532 :     # #
533 :     # OUTPUT: @printLines An array containing the (modified) text to be output #
534 :     # #
535 :     # OVERVIEW: l2h_sticky_answers is given HTML text, a list of submitted answers, and a list of #
536 :     # answer field labels. Its job is to retain the user's answers between submissions #
537 :     # when in typeset mode (this is handled elsewhere in the text modes). Basically, its #
538 :     # job is to act as a "filter" for the HTML text, replacing the answer fields that have #
539 :     # been reset with fields containing the previously entered answers, returning the #
540 :     # modified text. A brief high-level overview of the algorithm follows: #
541 :     # #
542 :     # ALGORITHM: The references are first dereferenced. The incoming text is first joined into #
543 :     # one string. It is then split up again, but not by line. Rather, the text is split #
544 :     # such that each array entry is either text which can be ignored, or a single #
545 :     # <INPUT...> tag. Each entry is then processed. If it is an <INPUT> tag, then it #
546 :     # must be checked for the presence of each answer field label for which a value was #
547 :     # submitted (there are many <INPUT> fields which are not answer fields, so we can't #
548 :     # assume that consecutive <INPUT> fields correspond to consecutive answer labels). #
549 :     # If a label is found, the blank value space is replaced with the appropriate #
550 :     # submitted answer (note that we can assume that there is a one-to-one correspondence #
551 :     # between answer labels and submitted answers; this is guaranteed by the specs). Radio #
552 :     # buttons and checkboxes are handled specially; see below. The modified text is then #
553 :     # added to the output string, which is split on a placeholder such that the output #
554 :     # array has the same number of entries as the input array (this is not required, but #
555 :     # might avoid some subtle bug in the future). #
556 :     # #
557 :     # NOTE: The specifications seem to require that the input text array consist of one #
558 :     # field for each line of text. However, it appears that the input is actually one #
559 :     # field, with newline characters separating lines. This function should accept #
560 :     # either form of input, although the "correct" form of one field per line has not #
561 :     # been tested. It is possible that, if input is received in this form AND the #
562 :     # newline characters have been truncated, the output could be garbled. #
563 :     # #
564 :     # --David Etlinger 6/7/2000 #
565 :     # #
566 :     # ADDED: Added a few lines of code to properly handle radio buttons. Checkboxes still need #
567 :     # to be implemented. #
568 :     # --David Etlinger 6/14/2000 #
569 :     # #
570 :     # ADDED: Added code to handle checkboxes. This is complicated because the submitted checkboxes #
571 :     # are originally stored as a single string with "\0" as a delimiter. If the input type #
572 :     # is determined to be checkboxes, the string is first split into an array. A hash key #
573 :     # in a special checkbox array is then made to point to the array. This is done because #
574 :     # there might be more than one checkbox set in a single question. Each time an input line #
575 :     # of type checkbox appears, the next value in this array is popped into a temp variable. #
576 :     # If it is determined that the line being processed corresponds to this value, the line #
577 :     # is processed (made "sticky"); otherwise, the value is pushed back on the array. The #
578 :     # fact that the number of checked cehckboxes is known but the total number of checkboxes #
579 :     # is not means that a given line of input type checkbox might or might not correspond #
580 :     # to the next value in the checkbox array. (I hope this explanation is clear enough!) #
581 :     # --David Etlinger 6/28/2000 #
582 :     #########################################################################################################
583 :    
584 :     sub l2h_sticky_answers {
585 :     my ( $rh_submittedAnswers, $ra_printLines, $rh_flags ) = @_;
586 :    
587 :     #warn ("rh_submittedAnswers = \@rh_submittedAnswers");
588 :     #warn ("ra_printLines = \@{ra_printLines}");
589 :     #warn ("rh_flags = \@{rh_flags}");
590 :    
591 :     my %submittedAnswers = %{$rh_submittedAnswers};
592 :     my @printLines = @{$ra_printLines};
593 :     my @answerLabels = @{$rh_flags -> {ANSWER_ENTRY_ORDER}};
594 :    
595 :     my $line; # holds the text of each line
596 :     my $label; # holds each answer label
597 :     my $counter = 0; # holds the index of the current answer
598 :     my $output; # holds the text the subroutine returns
599 :    
600 :     my $answer_value;
601 :    
602 :     my %checkboxAns; # holder for the checkbox multi-part answers
603 :     my $nextCheckboxAns; # temp holder for the next checkbox answer to be processed
604 :    
605 :     my $placeholder = "\x253"; # unused hex character to join text lines with
606 :    
607 :     #first, convert the array of text lines to one string...
608 :     my $text = join( "$placeholder", @printLines );
609 :    
610 :     #then, split it such that a line consists of either text
611 :     #or a single <INPUT> tag (case insensitive; note also that
612 :     #whitespace within the <INPUT> tag is accounted for).
613 :     # NOTE -- the regular expression searches for "<", then any
614 :     # amount of whitespace, then "INPUT", then any number of
615 :     # characters that aren't ">", then ">". I think that instead of
616 :     # searching for characters that aren't ">", I could have instead
617 :     # searched to match a minimal number of characters (using ?), and
618 :     # then ">". I don't know regular expressions well enough to tell
619 :     # if this might lead to some subtle difference.
620 :     my @textLines = split( m|(<\s*INPUT[^>]*>)|is, $text );
621 :     #my @textLines = split( m|(<\s*INPUT.*?>)|is, $text );
622 :    
623 :     foreach $line ( @textLines ) {
624 :     if( $line =~ m|<\s*INPUT|i ) {
625 :     foreach $label ( @answerLabels ) {
626 :     next unless exists( $submittedAnswers{$label} ); # skip if no answer was submitted.
627 :     if( $line =~ m|NAME\s*=\s*"$label"|i ) {
628 :     if( $line =~ m|TYPE\s*=\s*RADIO|i ) { #handle radio buttons
629 :     $line =~ s|VALUE\s*=\s*"$submittedAnswers{$label}"|VALUE = "$submittedAnswers{$label}" CHECKED|i;
630 :     }
631 :     elsif( $line =~ m|TYPE\s*=\s*CHECKBOX|i ) {
632 :     #make the hash key point to an anonymous array
633 :     $checkboxAns{$label} = [ split( "\0", $submittedAnswers{$label} ) ] if not exists( $checkboxAns{$label} );
634 :     if( defined $checkboxAns{$label}[0] ) {
635 :     $nextCheckboxAns = shift @{$checkboxAns{$label}};
636 :     if( $line !~ s|VALUE\s*=\s*"$nextCheckboxAns"|VALUE = "$nextCheckboxAns" CHECKED|i ) {
637 :     unshift( @{$checkboxAns{$label}}, $nextCheckboxAns ); #put the unused answer back on the list
638 :     }
639 :     }
640 :     }
641 :     else {
642 :     # we'll assume this is something else, like one or more fields.
643 :     # if it's several fields, we need to take only one answer at a time
644 :     # \0 are used to delimeter between entries.
645 :     if ($submittedAnswers{$label} =~ /\0/ ) {
646 :     my @answers = split("\0", $submittedAnswers{$label});
647 :     $answer_value = shift(@answers); # use up the first answer
648 :     $submittedAnswers{$label}=join "\0", @answers; # store the rest
649 :     $answer_value= '' unless defined($answer_value);
650 :    
651 :     }
652 :     else {
653 :     $answer_value = $submittedAnswers{$label};
654 :     }
655 :    
656 :     $line =~ s|VALUE\s*=\s*""|VALUE = "$answer_value"|i;
657 :     }
658 :     }
659 :     }
660 :     } #end if test for "<INPUT"
661 :    
662 :     $output .= $line;
663 :     } #end foreach
664 :    
665 :     @printLines = split( m|$placeholder|, $output );
666 :     return @printLines;
667 :     } #end l2h_sticky_answers()
668 :    
669 :     ## This is the old system (but newer than the one below).
670 :     ## It has been replaced for two reasons:
671 :     ## 1) It is complicated and difficult to understand or modify
672 :     ## 2) It does not work for several situations that rarely come up,
673 :     ## but must be handled properly. Specifically, it doesn't handle
674 :     ## text with more than one <INPUT> tag on a given line very well.
675 :     ## there are probably other problems, but that is the biggest.
676 :     ## --DME 6/7/2000
677 :     # # the following doubly nested loop iterates over each line,
678 :     # # and for each line searches for each answer label. Technically,
679 :     # # it might have been faster to join each entry in @printlines
680 :     # # into one string, search on that, and split it back up, but I
681 :     # # felt that the slight theoretical speed gain was not worth the
682 :     # # added complexity.
683 :     # warn "answerLabels = @answerLabels"; #DEBUG
684 :     # foreach $line ( @printLines ) {
685 :     # warn "Line is $line"; #DEBUG
686 :     # foreach $label ( @answerLabels ) {
687 :     # if( $line =~ m|<INPUT TYPE=TEXT.*NAME="$label| ) {
688 :     # while ($line =~ /VALUE = ""/) {
689 :     # # Put trailing space in displayed answer so that while loop will
690 :     # # always end. We are using the form of the s/// operator which
691 :     # # evaluates its right hand side
692 :     # $line =~ s|NAME="$label" VALUE = ""|
693 :     # $counter++;
694 :     # $submittedAnswers[$counter]=" " unless defined ($submittedAnswers[$counter])
695 :     # && not $submittedAnswers[$counter] =~ /^\s*$/;
696 :     # qq{ NAME="$label" VALUE = "$submittedAnswers[$counter]" } |e;
697 :     # # This insures that in VALUE = "$submittedAnswers[$counter]"
698 :     # # the quantity $submittedAnswers[$counter]
699 :     # # is never empty. This is required in order to terminate the loop.
700 :     # } #end while
701 :     # push( @output, $line );
702 :     # } #end if
703 :     # else {
704 :     # push( @output, $line );
705 :     # }
706 :     # } #end foreach over @answerLabels
707 :     # } #end foreach over @printLines
708 :     #
709 :     # @printLines = @output;
710 :     # } #end outer if
711 :     #
712 :     # return @printLines;
713 :     # } #end l2h_sticky_answers()
714 :    
715 :     ##subroutine that makes answers sticky in l2h mode
716 :     # this is an old version of this routine, which assumes (incorrectly)
717 :     # that answer labels begin with "AnSwEr". I've left it here just in case...
718 :     # DME 6/6/2000
719 :     #sub l2h_sticky_answers {
720 :     # my ($refSubmittedAnswers, $refprintlines)=@_;
721 :     # my @printlines=@$refprintlines;
722 :     # if ((@{$refSubmittedAnswers}!=0)) {
723 :     # my $line;
724 :     # my @output=();
725 :     # foreach $line (@printlines) {
726 :     # if ($line =~ m|<INPUT TYPE=TEXT.*NAME="AnSwEr|) {
727 :     # #print "<P>line doesn't exists<P>\n" unless defined($line);
728 :     # while ($line =~ /VALUE = ""/) {
729 :     # ## Put trailing space in displayed answer so that while loop will
730 :     # ## always end. We are using the form of the s/// operator which evaluates its right hand side
731 :     # $line =~ s|NAME="AnSwEr(\d*)" VALUE = ""|
732 :     # my $tttemp = $1;
733 :     # ${$refSubmittedAnswers}[$tttemp-1]=" " unless defined (${$refSubmittedAnswers}[$tttemp-1])
734 :     # && not ${$refSubmittedAnswers}[$tttemp-1] =~ /^\s*$/;
735 :     #
736 :     # qq{ NAME="AnSwEr$tttemp" VALUE = "${$refSubmittedAnswers}[$tttemp-1]" } |e;
737 :     # # This insures that in VALUE = "${$refSubmittedAnswers}[$tttemp-1]" the quantity ${$refSubmittedAnswers}[$tttemp-1]
738 :     # # is never empty. This is required in order to terminate the loop.
739 :     # }
740 :     # push(@output, $line);
741 :     # }
742 :     # else {
743 :     # push(@output, $line);
744 :     # }
745 :     # }
746 :     #
747 :     # @printlines = @output;
748 :     # }
749 :     #
750 :     # @printlines;
751 :     #}
752 :    
753 :     ##subroutine that updates current keys in the l2h mode
754 :    
755 :     # sub l2h_update_keys {
756 :     # my ($sessionKey, $refprintlines)= @_;
757 :     # my @printlines=@$refprintlines;
758 :     # my $line;
759 :     # my @output=();
760 :     # #my $sessionKey = $main::sessionKey;
761 :     # warn "hi lines = ",join("",@printlines);
762 :     # foreach $line (@printlines) {
763 :     # if ($line =~ m|^\s*<A(.*?)\&key=[^&]*&user|) { #<A.*&key=.*?&user
764 :     # #grab the session key from the CGI input or make it blank
765 :     # $line =~ s|^\s*<A(.*?)&key=[^&]*&user|<A$1&key=$sessionKey&user|;
766 :     # warn "line = $line<BR>";
767 :     # push(@output, $line);
768 :     # }else{
769 :     # push(@output, $line);
770 :     # }
771 :     #
772 :     # }
773 :     # @printlines;
774 :     #
775 :     # }
776 :    
777 :    
778 :     sub makeL2H {
779 :     my ($TMPPROBDIR,$psvn) =@_;
780 :     $ENV{PATH} .= "$Global::extendedPath";
781 :     if($Global::externalLaTeX2HTMLVersion eq "98.1p1") {
782 :     system("$Global::externalLaTeX2HTMLPath -no_math -init_file $Global::externalLaTeX2HTMLInit -dir $TMPPROBDIR -prefix $psvn $TMPPROBDIR${psvn}output.tex > $TMPPROBDIR${psvn}l2h.log 2>&1");
783 :     } elsif($Global::externalLaTeX2HTMLVersion eq "96.1") {
784 :     system("$Global::externalLaTeX2HTMLPath -init_file $Global::externalLaTeX2HTMLInit -dir $TMPPROBDIR -prefix $psvn $TMPPROBDIR${psvn}output.tex > $TMPPROBDIR${psvn}l2h.log");
785 :     } else {
786 :     die "Unknown LaTeX2HTML version: \$Global::externalLaTeX2HTMLVersion = $Global::externalLaTeX2HTMLVersion";
787 :     }
788 :     }
789 :    
790 :     1;

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9