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

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

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