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

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.9     ! albertel    9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem'));
        !            10: }
        !            11: 
        !            12: sub start_problem {
        !            13:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
        !            14:   my $args ='';
        !            15:   if ( $#$parstack > -1 ) {
        !            16:     $args=$$parstack[$#$parstack];
        !            17:   }
        !            18:   my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
        !            19:   return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n"
        !            20: }
        !            21: 
        !            22: sub end_problem {
        !            23:   return "</body>\n";
1.1       albertel   24: }
                     25: 
                     26: sub start_block {
                     27:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     28: 
                     29:   my $code = @$parstack[$#$parstack];
                     30:   $code =~ s/\"//g;
                     31:   $code .=';return $condition;';
1.3       albertel   32: #  print "<br>$code<br>";
1.1       albertel   33:   my $result = &Apache::run::run($code,$safeeval);
                     34:   if ( ! $result ) { 
1.3       albertel   35: #    my $skip=$$parser[$#$parser]->get_text("/block");
1.9     ! albertel   36:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.3       albertel   37: #    print "skipped $skip";
1.1       albertel   38:   }
                     39:   return "";
                     40: }
                     41: 
                     42: sub end_block {
1.4       tsai       43: }
                     44: 
                     45: sub start_while {
                     46:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     47: 
                     48:   my $code = @$parstack[$#$parstack];
                     49:   $code =~ s/\"//g;
                     50:   $code .=';return $condition;';
                     51: 
1.5       tsai       52:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai       53:   my $result = &Apache::run::run($code,$safeeval);
                     54:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai       55:   push( @Apache::structuretags::whilebody, $bodytext);
                     56:   if ( $result ) { 
1.8       albertel   57:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai       58:   }
                     59:   return "";
                     60: }
                     61: 
                     62: sub end_while {
1.5       tsai       63:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     64:   my $code = pop @Apache::structuretags::whileconds;
                     65:   my $bodytext = pop @Apache::structuretags::whilebody;
                     66:   my $result = &Apache::run::run($code,$safeeval);
                     67:   if ( $result ) { 
1.8       albertel   68:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai       69:   } 
                     70:   return "";
1.1       albertel   71: }
1.6       tsai       72: 
                     73: # <randomlist> 
                     74: #  <tag1>..</tag1>
                     75: #  <tag2>..</tag2>
                     76: #  <tag3>..</tag3>
                     77: #  ... 
                     78: # </randomlist>
                     79: sub start_randomlist {
                     80:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.9     ! albertel   81:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai       82:   my $b_parser= HTML::TokeParser->new(\$body);
                     83:   my $b_tok;
                     84:   my @randomlist;
                     85:   my $list_item;
                     86: 
                     87:   while($b_tok = $b_parser->get_token() ) {
                     88:     if($b_tok->[0] eq 'S') { # start tag
                     89:     # get content of the tag until matching end tag
                     90:     # get all text upto the matching tag
                     91:     # and push the content into @randomlist
1.9     ! albertel   92:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai       93:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai       94:       push(@randomlist,$list_item);
1.7       tsai       95:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6       tsai       96:     }
                     97:     if($b_tok->[0] eq 'T') { # text
                     98:     # what to do with text in between tags?
                     99:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
                    100:     }
                    101:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
                    102:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
                    103:     # }
                    104:   }
1.7       tsai      105:   my @idx_arr = (0 .. $#randomlist);
                    106:   &Apache::structuretags::shuffle(\@idx_arr);
                    107:   my $bodytext = '';
                    108:   for(0 .. $#randomlist) {
                    109:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
                    110:   }
1.8       albertel  111: 
                    112:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      113:   return "";
1.7       tsai      114: }
                    115: 
                    116: sub shuffle {
                    117:     my $a=shift;
                    118:     my $i;
                    119:     for($i=@$a;--$i;) {
                    120:       my $j=int rand($i+1);
                    121:       next if $i == $j;
                    122:       @$a[$i,$j] = @$a[$j,$i];
                    123:     }
1.6       tsai      124: }
                    125: 
                    126: sub end_randomlist {
                    127: }
                    128: 
1.1       albertel  129: 
                    130: 1;
                    131: __END__

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