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

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

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