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