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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1050 - (download) (as text) (annotate)
Fri Jun 6 21:39:42 2003 UTC (16 years, 8 months ago) by sh002i
File size: 35242 byte(s)
moved PG modules and macro files from webwork-modperl to pg
-sam

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9