File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.8: download - view: text, annotated - select for diffs
Thu Oct 5 19:26:16 2000 UTC (23 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- uses the new lonxml newparser() method of adding a parser to the parser stack

    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'));
   10: }
   11: 
   12: sub start_block {
   13:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   14: 
   15:   my $code = @$parstack[$#$parstack];
   16:   $code =~ s/\"//g;
   17:   $code .=';return $condition;';
   18: #  print "<br>$code<br>";
   19:   my $result = &Apache::run::run($code,$safeeval);
   20:   if ( ! $result ) { 
   21: #    my $skip=$$parser[$#$parser]->get_text("/block");
   22:     my $skip=&Apache::lonxml::get_all_text("block",$$parser[$#$parser]);
   23: #    print "skipped $skip";
   24:   }
   25:   return "";
   26: }
   27: 
   28: sub end_block {
   29: }
   30: 
   31: sub start_while {
   32:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   33: 
   34:   my $code = @$parstack[$#$parstack];
   35:   $code =~ s/\"//g;
   36:   $code .=';return $condition;';
   37: 
   38:   push( @Apache::structuretags::whileconds, $code); 
   39:   my $result = &Apache::run::run($code,$safeeval);
   40:   my $bodytext=$$parser[$#$parser]->get_text("/while");
   41:   push( @Apache::structuretags::whilebody, $bodytext);
   42:   if ( $result ) { 
   43:     &Apache::lonxml::newparser($parser,\$bodytext);
   44:   }
   45:   return "";
   46: }
   47: 
   48: sub end_while {
   49:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   50:   my $code = pop @Apache::structuretags::whileconds;
   51:   my $bodytext = pop @Apache::structuretags::whilebody;
   52:   my $result = &Apache::run::run($code,$safeeval);
   53:   if ( $result ) { 
   54:     &Apache::lonxml::newparser($parser,\$bodytext);
   55:   } 
   56:   return "";
   57: }
   58: 
   59: # <randomlist> 
   60: #  <tag1>..</tag1>
   61: #  <tag2>..</tag2>
   62: #  <tag3>..</tag3>
   63: #  ... 
   64: # </randomlist>
   65: sub start_randomlist {
   66:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   67:   my $body= &Apache::lonxml::get_all_text("randomlist",$$parser[$#$parser]);
   68:   my $b_parser= HTML::TokeParser->new(\$body);
   69:   my $b_tok;
   70:   my @randomlist;
   71:   my $list_item;
   72: 
   73:   while($b_tok = $b_parser->get_token() ) {
   74:     if($b_tok->[0] eq 'S') { # start tag
   75:     # get content of the tag until matching end tag
   76:     # get all text upto the matching tag
   77:     # and push the content into @randomlist
   78:       $list_item = &Apache::lonxml::get_all_text($b_tok->[1],$b_parser);
   79:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
   80:       push(@randomlist,$list_item);
   81:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
   82:     }
   83:     if($b_tok->[0] eq 'T') { # text
   84:     # what to do with text in between tags?
   85:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
   86:     }
   87:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
   88:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
   89:     # }
   90:   }
   91:   my @idx_arr = (0 .. $#randomlist);
   92:   &Apache::structuretags::shuffle(\@idx_arr);
   93:   my $bodytext = '';
   94:   for(0 .. $#randomlist) {
   95:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
   96:   }
   97: 
   98:   &Apache::lonxml::newparser($parser,\$bodytext);
   99:   return "";
  100: }
  101: 
  102: sub shuffle {
  103:     my $a=shift;
  104:     my $i;
  105:     for($i=@$a;--$i;) {
  106:       my $j=int rand($i+1);
  107:       next if $i == $j;
  108:       @$a[$i,$j] = @$a[$j,$i];
  109:     }
  110: }
  111: 
  112: sub end_randomlist {
  113: }
  114: 
  115: 
  116: 1;
  117: __END__

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