| … | |
… | |
| 546 | ); |
546 | ); |
| 547 | } |
547 | } |
| 548 | |
548 | |
| 549 | sub previewAnswer($$) { |
549 | sub previewAnswer($$) { |
| 550 | my ($self, $answerResult) = @_; |
550 | my ($self, $answerResult) = @_; |
| 551 | my $ce = $self->{courseEnvironment}; |
551 | my $ce = $self->{courseEnvironment}; |
| 552 | my $user = $self->{user}; |
552 | my $user = $self->{user}; |
| 553 | my $set = $self->{set}; |
553 | my $set = $self->{set}; |
| 554 | my $problem = $self->{problem}; |
554 | my $problem = $self->{problem}; |
|
|
555 | my $displayMode = $self->{displayMode}; |
| 555 | |
556 | |
| 556 | # how are we going to name this? |
557 | # note: right now, we have to do things completely differently when we are |
| 557 | my $targetPathCommon = "/png/" |
558 | # rendering math from INSIDE the translator and from OUTSIDE the translator. |
| 558 | . $user->id . "." |
559 | # so we'll just deal with each case explicitly here. there's some code |
| 559 | . $set->id . "." |
560 | # duplication that can be dealt with later by abstracting out tth/dvipng/etc. |
| 560 | . $problem->id . "." |
|
|
| 561 | . $answerResult->{ans_name} . ".png"; |
|
|
| 562 | |
561 | |
| 563 | # figure out where to put things |
|
|
| 564 | my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp}); |
|
|
| 565 | my $latex = $ce->{externalPrograms}->{latex}; |
|
|
| 566 | my $dvipng = $ce->{externalPrograms}->{dvipng}; |
|
|
| 567 | my $tex = $answerResult->{preview_latex_string}; |
562 | my $tex = $answerResult->{preview_latex_string}; |
|
|
563 | |
|
|
564 | if ($displayMode eq "plainText") { |
|
|
565 | return $tex; |
|
|
566 | } elsif ($displayMode eq "formattedText") { |
|
|
567 | my $tthCommand = $ce->{externalPrograms}->{tth} |
|
|
568 | . " -L -f5 -r 2> /dev/null <<END_OF_INPUT; echo > /dev/null\n" |
|
|
569 | . "\\($tex\\)\n" |
|
|
570 | . "END_OF_INPUT\n"; |
|
|
571 | |
|
|
572 | |
|
|
573 | # call tth |
|
|
574 | my $result = `$tthCommand`; |
|
|
575 | if ($?) { |
|
|
576 | return "<b>[tth failed: $? $@]</b>"; |
|
|
577 | } |
|
|
578 | return $result; |
|
|
579 | } elsif ($displayMode eq "images") { |
|
|
580 | # how are we going to name this? |
|
|
581 | my $targetPathCommon = "/png/" |
|
|
582 | . $user->id . "." |
|
|
583 | . $set->id . "." |
|
|
584 | . $problem->id . "." |
|
|
585 | . $answerResult->{ans_name} . ".png"; |
|
|
586 | |
|
|
587 | # figure out where to put things |
|
|
588 | my $wd = tempdir("webwork-dvipng-XXXXXXXX", DIR => $ce->{courseDirs}->{html_temp}); |
|
|
589 | my $latex = $ce->{externalPrograms}->{latex}; |
|
|
590 | my $dvipng = $ce->{externalPrograms}->{dvipng}; |
| 568 | my $targetPath = $ce->{courseDirs}->{html_temp} . $targetPathCommon; |
591 | my $targetPath = $ce->{courseDirs}->{html_temp} . $targetPathCommon; |
| 569 | # should use surePathToTmpFile, but we have to |
592 | # should use surePathToTmpFile, but we have to |
| 570 | # isolate it from the problem enivronment first |
593 | # isolate it from the problem enivronment first |
| 571 | my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon; |
594 | my $targetURL = $ce->{courseURLs}->{html_temp} . $targetPathCommon; |
| 572 | |
595 | |
| 573 | # call dvipng to generate a preview |
596 | # call dvipng to generate a preview |
| 574 | dvipng($wd, $latex, $dvipng, $tex, $targetPath); |
597 | dvipng($wd, $latex, $dvipng, $tex, $targetPath); |
| 575 | if (-e $targetPath) { |
598 | if (-e $targetPath) { |
| 576 | return "<img src=\"$targetURL\" alt=\"$tex\" />"; |
599 | return "<img src=\"$targetURL\" alt=\"$tex\" />"; |
| 577 | } else { |
600 | } else { |
| 578 | return "<b>[math2img failed]</b>"; |
601 | return "<b>[math2img failed]</b>"; |
|
|
602 | } |
| 579 | } |
603 | } |
| 580 | } |
604 | } |
| 581 | |
605 | |
| 582 | ##### permission queries ##### |
606 | ##### permission queries ##### |
| 583 | |
607 | |