File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.13: download - view: text, annotated - select for diffs
Tue Nov 28 19:10:32 2000 UTC (23 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- reindent code

    1: # The LearningOnline Network with CAPA # <script> definiton
    2: 
    3: package Apache::structuretags; 
    4: 
    5: use strict;
    6: use Apache::lonnet;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part'));
   10: }
   11: 
   12: sub start_web {
   13:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   14:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
   15:   return $bodytext;
   16: }
   17: 
   18: sub end_web {
   19: }
   20: 
   21: sub start_tex {
   22:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   23:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
   24:   return '';
   25: }
   26: 
   27: sub end_tex {
   28: }
   29: 
   30: sub start_problem {
   31:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   32:   my $args ='';
   33:   if ( $#$parstack > -1 ) {
   34:     $args=$$parstack[$#$parstack];
   35:   }
   36:   my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
   37:   push (@Apache::inputtags::part,'0');
   38: #  $safeeval->share_from('Apache::lonnet',['&EXT']);
   39:   $safeeval->share_from('Apache::inputtags',['@part']);
   40:   return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
   41: }
   42: 
   43: sub end_problem {
   44:   return "</form></body>\n";
   45: }
   46: 
   47: sub start_block {
   48:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   49: 
   50:   my $code = @$parstack[$#$parstack];
   51:   $code =~ s/\"//g;
   52:   $code .=';return $condition;';
   53: #  print "<br>$code<br>";
   54:   my $result = &Apache::run::run($code,$safeeval);
   55:   if ( ! $result ) { 
   56: #    my $skip=$$parser[$#$parser]->get_text("/block");
   57:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
   58: #    print "skipped $skip";
   59:   }
   60:   return "";
   61: }
   62: 
   63: sub end_block {
   64: }
   65: 
   66: sub start_while {
   67:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   68: 
   69:   my $code = @$parstack[$#$parstack];
   70:   $code =~ s/\"//g;
   71:   $code .=';return $condition;';
   72: 
   73:   push( @Apache::structuretags::whileconds, $code); 
   74:   my $result = &Apache::run::run($code,$safeeval);
   75:   my $bodytext=$$parser[$#$parser]->get_text("/while");
   76:   push( @Apache::structuretags::whilebody, $bodytext);
   77:   if ( $result ) { 
   78:     &Apache::lonxml::newparser($parser,\$bodytext);
   79:   }
   80:   return "";
   81: }
   82: 
   83: sub end_while {
   84:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   85:   my $code = pop @Apache::structuretags::whileconds;
   86:   my $bodytext = pop @Apache::structuretags::whilebody;
   87:   my $result = &Apache::run::run($code,$safeeval);
   88:   if ( $result ) { 
   89:     &Apache::lonxml::newparser($parser,\$bodytext);
   90:   } 
   91:   return "";
   92: }
   93: 
   94: # <randomlist> 
   95: #  <tag1>..</tag1>
   96: #  <tag2>..</tag2>
   97: #  <tag3>..</tag3>
   98: #  ... 
   99: # </randomlist>
  100: sub start_randomlist {
  101:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  102:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  103:   my $b_parser= HTML::TokeParser->new(\$body);
  104:   my $b_tok;
  105:   my @randomlist;
  106:   my $list_item;
  107: 
  108:   while($b_tok = $b_parser->get_token() ) {
  109:     if($b_tok->[0] eq 'S') { # start tag
  110:     # get content of the tag until matching end tag
  111:     # get all text upto the matching tag
  112:     # and push the content into @randomlist
  113:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  114:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  115:       push(@randomlist,$list_item);
  116:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
  117:     }
  118:     if($b_tok->[0] eq 'T') { # text
  119:     # what to do with text in between tags?
  120:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
  121:     }
  122:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
  123:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
  124:     # }
  125:   }
  126:   my @idx_arr = (0 .. $#randomlist);
  127:   &Apache::structuretags::shuffle(\@idx_arr);
  128:   my $bodytext = '';
  129:   for(0 .. $#randomlist) {
  130:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  131:   }
  132: 
  133:   &Apache::lonxml::newparser($parser,\$bodytext);
  134:   return "";
  135: }
  136: 
  137: sub shuffle {
  138:     my $a=shift;
  139:     my $i;
  140:     for($i=@$a;--$i;) {
  141:       my $j=int rand($i+1);
  142:       next if $i == $j;
  143:       @$a[$i,$j] = @$a[$j,$i];
  144:     }
  145: }
  146: 
  147: sub end_randomlist {
  148: }
  149: 
  150: sub start_part {
  151:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  152:   my $args ='';
  153:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  154:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
  155:   push (@Apache::inputtags::part,$id);
  156: }
  157: 
  158: sub end_part {
  159:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  160:   pop @Apache::inputtags::part;
  161: }
  162: 
  163: 1;
  164: __END__

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>