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

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

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