[LON-CAPA-cvs] cvs: modules /gerd translate.pl

www lon-capa-cvs@mail.lon-capa.org
Sun, 11 Feb 2007 20:43:53 -0000


www		Sun Feb 11 15:43:53 2007 EDT

  Added files:                 
    /modules/gerd	translate.pl 
  Log:
  Program to translate ExamView problems.
  
  

Index: modules/gerd/translate.pl
+++ modules/gerd/translate.pl
use strict;
use HTML::TokeParser;
my $fn=shift;
my $parser = HTML::TokeParser->new($fn);
my %foils;
my $truth;
my $text;
my $output;
my $intermediate;
my $id;
my $type;
my $buffer;
my $narrative;
my $narrativename;
my %allnarratives=();
while (my $token=$parser->get_token) {
   if ($token->[0] eq 'S') {
      if ($token->[1] eq 'question') {
         $id=$token->[2]->{'question-id'};
         $type=$token->[2]->{'type'};
         $narrative=$token->[2]->{'narrative'};
      } elsif ($token->[1] eq 'narrative') {
         $narrativename=$token->[2]->{'name'};
      } elsif ($token->[1] eq 'text') {
	 $intermediate='';
      } elsif ($token->[1] eq 'answer') {
         $intermediate='';
      } elsif ($token->[1] eq 'row') {
         $intermediate.="\n<tr>";
      } elsif ($token->[1] eq 'cell') {
         $intermediate.='<td>';
      } elsif ($token->[1] eq 'equation') {
         $buffer=$intermediate;
      } elsif ($token->[1]=~/^choice\-/) {
         $intermediate='';
      } else {
         $intermediate.=$token->[4];
      }
   }
   if ($token->[0] eq 'T') {
      $intermediate.=$token->[1];
   }
   if ($token->[0] eq 'E') {
      if ($token->[1] eq 'text') {
         $text=$intermediate;
         $text=~s/[^\w\.\;\:\?\!\(\)\<\>\=\"\'\+\-\*\/\n ]//gsi;
      } elsif ($token->[1]=~/^choice\-(\w+)$/) {
         $foils{$1}=$intermediate;
      } elsif ($token->[1] eq 'answer') {
         $truth=$intermediate;
      } elsif ($token->[1] eq 'row') {
         $intermediate.='</tr>';
      } elsif ($token->[1] eq 'cell') {
         $intermediate.='</td>';
      } elsif ($token->[1] eq 'equation') {
         $intermediate=$buffer.'<m>$\Sigma$</m>';
      } elsif ($token->[1] eq 'question') {
         if ($narrative) {
            $text=$allnarratives{$narrative}.'<br />'.$text;
         }
         &make_question($id,$type,$text,$truth,$fn,%foils);
      } elsif ($token->[1] eq 'narrative') {
         $allnarratives{$narrativename}=$text;
      } else {
         $intermediate.=$token->[2];
      }
   }
}

sub make_question {
    my ($id,$type,$text,$truth,$fn,%foils)=@_; 
    if ($type eq 'sa') { return; }
    my $question='';
    my $output="<problem>\n";
    my $max=2;
    if ($type eq 'tf') { $max=1; }
    $output.='<parameter name="maxtries" default="'.$max.'" type="int_pos" description="Maximum Number of Tries" />';
    $output.="\n<startouttext />$text<endouttext />";
    $output.="\n".'<radiobuttonresponse direction="vertical" max="10" randomize="yes">'."\n<foilgroup>";
    if ($type eq 'tf') {
       $output.="\n".'<foil location="random" value="'.((lc($truth) eq 't')?'true':'false').'" name="foil1">';
       $output.="\n<startouttext />True<endouttext />";
       $output.="\n</foil>";
       $output.="\n".'<foil location="bottom" value="'.((lc($truth) ne 't')?'true':'false').'" name="foil2">';
       $output.="\n<startouttext />False<endouttext />";
       $output.="\n</foil>";
    } else {
       foreach my $index ('a'..'z') {
          if ($foils{$index}) {
             $output.="\n".'<foil location="'.(($foils{$index}=~/^\s*(none|cannot)/i)?'bottom':'random').'" value="'.((lc($truth) eq lc($index))?'true':'false').'" name="foil'.$index.'">';
             $output.="\n<startouttext />$foils{$index}<endouttext />";
             $output.="\n</foil>";
          }
       }
    }
    $output.="\n</foilgroup>\n</radiobuttonresponse>\n</problem>\n";
    if ($output) {
       unless (-e 'data/'.$fn) { system("mkdir data/$fn"); }
       if ($id<10) { $id='0'.$id; }
       open(OUT,">data/$fn/problem$id.problem");
       print OUT $output;
       close(OUT);
    }
}