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

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)=@_;
1.15    ! www        32:   unless ($target eq 'meta') {
1.9       albertel   33:   my $args ='';
                     34:   if ( $#$parstack > -1 ) {
                     35:     $args=$$parstack[$#$parstack];
                     36:   }
                     37:   my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
1.14      albertel   38:   $Apache::inputtags::part='0';
                     39:   my $expression='$external::part='.$Apache::inputtags::part.';';
1.12      albertel   40:   $safeeval->share_from('Apache::inputtags',['@part']);
1.15    ! www        41:   &Apache::run::run($expression);
1.11      albertel   42:   return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
1.15    ! www        43: } else {
        !            44:     return &Apache::response::mandatory_part_meta;
        !            45: }
1.9       albertel   46: }
                     47: 
                     48: sub end_problem {
1.11      albertel   49:   return "</form></body>\n";
1.1       albertel   50: }
                     51: 
                     52: sub start_block {
                     53:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     54: 
                     55:   my $code = @$parstack[$#$parstack];
                     56:   $code =~ s/\"//g;
                     57:   $code .=';return $condition;';
1.3       albertel   58: #  print "<br>$code<br>";
1.1       albertel   59:   my $result = &Apache::run::run($code,$safeeval);
                     60:   if ( ! $result ) { 
1.3       albertel   61: #    my $skip=$$parser[$#$parser]->get_text("/block");
1.9       albertel   62:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.3       albertel   63: #    print "skipped $skip";
1.1       albertel   64:   }
                     65:   return "";
                     66: }
                     67: 
                     68: sub end_block {
1.4       tsai       69: }
                     70: 
                     71: sub start_while {
                     72:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     73: 
                     74:   my $code = @$parstack[$#$parstack];
                     75:   $code =~ s/\"//g;
                     76:   $code .=';return $condition;';
                     77: 
1.5       tsai       78:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai       79:   my $result = &Apache::run::run($code,$safeeval);
                     80:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai       81:   push( @Apache::structuretags::whilebody, $bodytext);
                     82:   if ( $result ) { 
1.8       albertel   83:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai       84:   }
                     85:   return "";
                     86: }
                     87: 
                     88: sub end_while {
1.5       tsai       89:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     90:   my $code = pop @Apache::structuretags::whileconds;
                     91:   my $bodytext = pop @Apache::structuretags::whilebody;
                     92:   my $result = &Apache::run::run($code,$safeeval);
                     93:   if ( $result ) { 
1.8       albertel   94:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai       95:   } 
                     96:   return "";
1.1       albertel   97: }
1.6       tsai       98: 
                     99: # <randomlist> 
                    100: #  <tag1>..</tag1>
                    101: #  <tag2>..</tag2>
                    102: #  <tag3>..</tag3>
                    103: #  ... 
                    104: # </randomlist>
                    105: sub start_randomlist {
                    106:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.9       albertel  107:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai      108:   my $b_parser= HTML::TokeParser->new(\$body);
                    109:   my $b_tok;
                    110:   my @randomlist;
                    111:   my $list_item;
                    112: 
                    113:   while($b_tok = $b_parser->get_token() ) {
                    114:     if($b_tok->[0] eq 'S') { # start tag
                    115:     # get content of the tag until matching end tag
                    116:     # get all text upto the matching tag
                    117:     # and push the content into @randomlist
1.9       albertel  118:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai      119:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai      120:       push(@randomlist,$list_item);
1.7       tsai      121:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6       tsai      122:     }
                    123:     if($b_tok->[0] eq 'T') { # text
                    124:     # what to do with text in between tags?
                    125:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
                    126:     }
                    127:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
                    128:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
                    129:     # }
                    130:   }
1.7       tsai      131:   my @idx_arr = (0 .. $#randomlist);
                    132:   &Apache::structuretags::shuffle(\@idx_arr);
                    133:   my $bodytext = '';
                    134:   for(0 .. $#randomlist) {
                    135:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
                    136:   }
1.8       albertel  137: 
                    138:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      139:   return "";
1.7       tsai      140: }
                    141: 
                    142: sub shuffle {
                    143:     my $a=shift;
                    144:     my $i;
                    145:     for($i=@$a;--$i;) {
                    146:       my $j=int rand($i+1);
                    147:       next if $i == $j;
                    148:       @$a[$i,$j] = @$a[$j,$i];
                    149:     }
1.6       tsai      150: }
                    151: 
                    152: sub end_randomlist {
                    153: }
                    154: 
1.11      albertel  155: sub start_part {
                    156:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    157:   my $args ='';
                    158:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    159:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
1.14      albertel  160:   $Apache::inputtags::part=$id;
1.15    ! www       161:   if ($target eq 'meta') {
        !           162:       return &Apache::response::mandatory_part_meta;
        !           163:   }
1.11      albertel  164: }
                    165: 
                    166: sub end_part {
                    167:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.14      albertel  168:   $Apache::inputtags::part='0';
1.11      albertel  169: }
1.1       albertel  170: 
                    171: 1;
                    172: __END__

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