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

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

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