[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 2274 - (download) (as text) (annotate)
Mon Jun 7 23:53:45 2004 UTC (15 years, 8 months ago) by sh002i
File size: 36288 byte(s)
got rid of remaining calls to "wwerror". Fixes bug #530.

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

aubreyja at gmail dot com
ViewVC Help
Powered by ViewVC 1.0.9