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

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.11      albertel    9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part'));
1.10      albertel   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 {
1.9       albertel   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);
1.11      albertel   37:   push (@Apache::inputtags::part,'0');
1.13    ! albertel   38: #  $safeeval->share_from('Apache::lonnet',['&EXT']);
1.12      albertel   39:   $safeeval->share_from('Apache::inputtags',['@part']);
1.11      albertel   40:   return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
1.9       albertel   41: }
                     42: 
                     43: sub end_problem {
1.11      albertel   44:   return "</form></body>\n";
1.1       albertel   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;';
1.3       albertel   53: #  print "<br>$code<br>";
1.1       albertel   54:   my $result = &Apache::run::run($code,$safeeval);
                     55:   if ( ! $result ) { 
1.3       albertel   56: #    my $skip=$$parser[$#$parser]->get_text("/block");
1.9       albertel   57:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.3       albertel   58: #    print "skipped $skip";
1.1       albertel   59:   }
                     60:   return "";
                     61: }
                     62: 
                     63: sub end_block {
1.4       tsai       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: 
1.5       tsai       73:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai       74:   my $result = &Apache::run::run($code,$safeeval);
                     75:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai       76:   push( @Apache::structuretags::whilebody, $bodytext);
                     77:   if ( $result ) { 
1.8       albertel   78:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai       79:   }
                     80:   return "";
                     81: }
                     82: 
                     83: sub end_while {
1.5       tsai       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 ) { 
1.8       albertel   89:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai       90:   } 
                     91:   return "";
1.1       albertel   92: }
1.6       tsai       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)=@_;
1.9       albertel  102:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai      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
1.9       albertel  113:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai      114:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai      115:       push(@randomlist,$list_item);
1.7       tsai      116:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6       tsai      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:   }
1.7       tsai      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:   }
1.8       albertel  132: 
                    133:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      134:   return "";
1.7       tsai      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:     }
1.6       tsai      145: }
                    146: 
                    147: sub end_randomlist {
                    148: }
                    149: 
1.11      albertel  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: }
1.1       albertel  162: 
                    163: 1;
                    164: __END__

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