File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.9: download - view: text, annotated - select for diffs
Wed Oct 11 21:03:27 2000 UTC (23 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added <problem> to generate the title/body tags
- modified other tags to pass the correct argument to the new get_all_text

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

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