From: Mike G. v. a. <we...@ma...> - 2010-05-28 21:53:51
|
Log Message: ----------- added tooltip for the answer preview -- this shows the actual entry -- with this we could get rid of the first "parsed" entry in each row, saving some space. Something similar (but more complicated) would give us a tooltip showing the typeset version of the correct answer. (We want the untypeset version to be available for cutting and pasting when instructors are checking problems for correctness so the method ("title" attribute) used for the answer preview won't work. Modified Files: -------------- webwork2/lib/WeBWorK/ContentGenerator: Problem.pm Revision Data ------------- Index: Problem.pm =================================================================== RCS file: /webwork/cvs/system/webwork2/lib/WeBWorK/ContentGenerator/Problem.pm,v retrieving revision 1.224 retrieving revision 1.225 diff -Llib/WeBWorK/ContentGenerator/Problem.pm -Llib/WeBWorK/ContentGenerator/Problem.pm -u -r1.224 -r1.225 --- lib/WeBWorK/ContentGenerator/Problem.pm +++ lib/WeBWorK/ContentGenerator/Problem.pm @@ -242,6 +242,7 @@ my $preview = ($showAttemptPreview ? $self->previewAnswer($answerResult, $imgGen, \$tthPreambleCache) : ""); + my $correctAnswerPreview = $self->previewCorrectAnswer($answerResult, $imgGen, \$tthPreambleCache); my $correctAnswer = $answerResult->{correct_ans}; my $answerScore = $answerResult->{score}; my $answerMessage = $showMessages ? $answerResult->{ans_message} : ""; @@ -260,8 +261,8 @@ my $row; #$row .= CGI::td($name); $row .= $showAttemptAnswers ? CGI::td($self->nbsp($studentAnswer)) : ""; - $row .= $showAttemptPreview ? CGI::td($self->nbsp($preview)) : ""; - $row .= $showCorrectAnswers ? CGI::td($self->nbsp($correctAnswer)) : ""; + $row .= $showAttemptPreview ? CGI::td({title=>"$studentAnswer"}, $self->nbsp($preview)) : ""; + $row .= $showCorrectAnswers ? CGI::td({title=> "$correctAnswerPreview"}, $self->nbsp($correctAnswer)) : ""; $row .= $showAttemptResults ? CGI::td($self->nbsp($resultString)) : ""; $row .= $showMessages ? CGI::td({-class=>"Message"},$self->nbsp($answerMessage)) : ""; push @tableRows, $row; @@ -370,6 +371,70 @@ return '<SPAN CLASS="math">\\displaystyle{'.$tex.'}</SPAN>'; } } +sub previewCorrectAnswer { + my ($self, $answerResult, $imgGen, $tthPreambleCache) = @_; + my $ce = $self->r->ce; + my $effectiveUser = $self->{effectiveUser}; + my $set = $self->{set}; + my $problem = $self->{problem}; + my $displayMode = $self->{displayMode}; + + # note: right now, we have to do things completely differently when we are + # rendering math from INSIDE the translator and from OUTSIDE the translator. + # so we'll just deal with each case explicitly here. there's some code + # duplication that can be dealt with later by abstracting out tth/dvipng/etc. + + my $tex = $answerResult->{correct_value}->TeX; + + return "" unless defined $tex and $tex ne ""; + + if ($displayMode eq "plainText") { + return $tex; + } elsif ($displayMode eq "formattedText") { + + # read the TTH preamble, or use the cached copy passed in from the caller + my $tthPreamble=''; + if (defined $$tthPreambleCache) { + $tthPreamble = $$tthPreambleCache; + } else { + my $tthPreambleFile = $ce->{courseDirs}->{templates} . "/tthPreamble.tex"; + if (-r $tthPreambleFile) { + $tthPreamble = readFile($tthPreambleFile); + # thanks to Jim Martino. each line in the definition file should end with + #a % to prevent adding supurious paragraphs to output: + $tthPreamble =~ s/(.)\n/$1%\n/g; + # solves the problem if the file doesn't end with a return: + $tthPreamble .="%\n"; + # store preamble in cache: + $$tthPreambleCache = $tthPreamble; + } else { + } + } + + # construct TTH command line + my $tthCommand = $ce->{externalPrograms}->{tth} + . " -L -f5 -u -r 2> /dev/null <<END_OF_INPUT; echo > /dev/null\n" + . $tthPreamble . "\\[" . $tex . "\\]\n" + . "END_OF_INPUT\n"; + + # call tth + my $result = `$tthCommand`; + if ($?) { + return "<b>[tth failed: $? $@]</b>"; + } else { + # avoid border problems in tables and remove unneeded initial <br> + $result =~ s/(<table [^>]*)>/$1 CLASS="ArrayLayout">/gi; + $result =~ s!\s*<br clear="all" />!!; + return $result; + } + + } elsif ($displayMode eq "images") { + $imgGen->add($tex); + } elsif ($displayMode eq "jsMath") { + $tex =~ s/</</g; $tex =~ s/>/>/g; + return '<SPAN CLASS="math">\\displaystyle{'.$tex.'}</SPAN>'; + } +} ################################################################################ # Template escape implementations |