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

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

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