Annotation of loncom/homework/structuretags.pm, revision 1.8

1.6       tsai        1: # The LearningOnline Network with CAPA # <script> definiton
1.1       albertel    2: 
                      3: package Apache::structuretags; 
                      4: 
                      5: use strict;
                      6: use Apache::lonnet;
                      7: 
                      8: sub BEGIN {
1.6       tsai        9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist'));
1.1       albertel   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;';
1.3       albertel   18: #  print "<br>$code<br>";
1.1       albertel   19:   my $result = &Apache::run::run($code,$safeeval);
                     20:   if ( ! $result ) { 
1.3       albertel   21: #    my $skip=$$parser[$#$parser]->get_text("/block");
                     22:     my $skip=&Apache::lonxml::get_all_text("block",$$parser[$#$parser]);
                     23: #    print "skipped $skip";
1.1       albertel   24:   }
                     25:   return "";
                     26: }
                     27: 
                     28: sub end_block {
1.4       tsai       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: 
1.5       tsai       38:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai       39:   my $result = &Apache::run::run($code,$safeeval);
                     40:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai       41:   push( @Apache::structuretags::whilebody, $bodytext);
                     42:   if ( $result ) { 
1.8     ! albertel   43:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai       44:   }
                     45:   return "";
                     46: }
                     47: 
                     48: sub end_while {
1.5       tsai       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 ) { 
1.8     ! albertel   54:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai       55:   } 
                     56:   return "";
1.1       albertel   57: }
1.6       tsai       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);
1.7       tsai       79:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai       80:       push(@randomlist,$list_item);
1.7       tsai       81:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6       tsai       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:   }
1.7       tsai       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:   }
1.8     ! albertel   97: 
        !            98:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai       99:   return "";
1.7       tsai      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:     }
1.6       tsai      110: }
                    111: 
                    112: sub end_randomlist {
                    113: }
                    114: 
1.1       albertel  115: 
                    116: 1;
                    117: __END__

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