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

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);
1.22      albertel   46:   my $status;
                     47:   my $datemsg;
1.21      albertel   48:   if ($target ne 'meta') {
1.22      albertel   49:     ($status,$datemsg) = &Apache::lonhomework::check_date('0');
                     50:     push (@Apache::inputtags::status,$status);
                     51:     if ( $status eq 'CLOSED' ) {
1.21      albertel   52:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
                     53:       if ( $target eq "web" ) {
1.23    ! albertel   54: 	return "<br></br>Problem is not open to be viewed. The problem $datemsg<br></br>";
1.21      albertel   55:       }
1.22      albertel   56:     } 
1.21      albertel   57:   }
1.19      albertel   58:   if ($target eq 'web') {
1.16      albertel   59:     my $args ='';
1.19      albertel   60:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
1.16      albertel   61:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
1.22      albertel   62:     if ($status eq 'CAN_ANSWER') {
                     63:       # create a page header and exit
                     64:       return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
                     65:     } elsif ($status eq 'SHOW_ANSWER') {
                     66:       return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n";
                     67:     }
                     68:   }
1.19      albertel   69:   return '';
1.9       albertel   70: }
                     71: 
                     72: sub end_problem {
1.16      albertel   73:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     74:   my $result='';
1.22      albertel   75:   my $status=@Apache::inputtags::status['-1'];
1.16      albertel   76:   unless ($target eq 'meta') {
1.19      albertel   77:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
                     78:       # if part is zero, no <part>s existed, so we need to the grading
                     79:       &Apache::inputtags::grade;
                     80:     } elsif ($Apache::inputtags::part eq '0') {
                     81:       # if part is zero, no <part>s existed, so we need show the current 
                     82:       # grading status
1.20      albertel   83:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.19      albertel   84:     } 
1.22      albertel   85:     if ($target eq 'web') { 
                     86:       if ($status eq 'CAN_ANSWER') {
                     87: 	$result.="</form></body>\n"; 
                     88:       } elsif ($status eq 'SHOW_ANSWER') {
                     89: 	$result.="</body>\n"; 
                     90:       } 
                     91:     }
1.16      albertel   92:   } else {
1.18      albertel   93:     if ($Apache::inputtags::part eq '0') {
1.16      albertel   94:       $result=&Apache::response::mandatory_part_meta;
                     95:     }
                     96:   }
                     97:   return $result;
1.1       albertel   98: }
                     99: 
                    100: sub start_block {
                    101:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    102: 
                    103:   my $code = @$parstack[$#$parstack];
                    104:   $code =~ s/\"//g;
                    105:   $code .=';return $condition;';
1.3       albertel  106: #  print "<br>$code<br>";
1.1       albertel  107:   my $result = &Apache::run::run($code,$safeeval);
                    108:   if ( ! $result ) { 
1.3       albertel  109: #    my $skip=$$parser[$#$parser]->get_text("/block");
1.9       albertel  110:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.3       albertel  111: #    print "skipped $skip";
1.1       albertel  112:   }
                    113:   return "";
                    114: }
                    115: 
                    116: sub end_block {
1.4       tsai      117: }
                    118: 
                    119: sub start_while {
                    120:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    121: 
                    122:   my $code = @$parstack[$#$parstack];
                    123:   $code =~ s/\"//g;
                    124:   $code .=';return $condition;';
                    125: 
1.5       tsai      126:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai      127:   my $result = &Apache::run::run($code,$safeeval);
                    128:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai      129:   push( @Apache::structuretags::whilebody, $bodytext);
                    130:   if ( $result ) { 
1.8       albertel  131:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai      132:   }
                    133:   return "";
                    134: }
                    135: 
                    136: sub end_while {
1.5       tsai      137:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    138:   my $code = pop @Apache::structuretags::whileconds;
                    139:   my $bodytext = pop @Apache::structuretags::whilebody;
                    140:   my $result = &Apache::run::run($code,$safeeval);
                    141:   if ( $result ) { 
1.8       albertel  142:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai      143:   } 
                    144:   return "";
1.1       albertel  145: }
1.6       tsai      146: 
                    147: # <randomlist> 
                    148: #  <tag1>..</tag1>
                    149: #  <tag2>..</tag2>
                    150: #  <tag3>..</tag3>
                    151: #  ... 
                    152: # </randomlist>
                    153: sub start_randomlist {
                    154:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.9       albertel  155:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai      156:   my $b_parser= HTML::TokeParser->new(\$body);
                    157:   my $b_tok;
                    158:   my @randomlist;
                    159:   my $list_item;
                    160: 
                    161:   while($b_tok = $b_parser->get_token() ) {
                    162:     if($b_tok->[0] eq 'S') { # start tag
                    163:     # get content of the tag until matching end tag
                    164:     # get all text upto the matching tag
                    165:     # and push the content into @randomlist
1.9       albertel  166:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai      167:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai      168:       push(@randomlist,$list_item);
1.7       tsai      169:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6       tsai      170:     }
                    171:     if($b_tok->[0] eq 'T') { # text
                    172:     # what to do with text in between tags?
                    173:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
                    174:     }
                    175:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
                    176:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
                    177:     # }
                    178:   }
1.7       tsai      179:   my @idx_arr = (0 .. $#randomlist);
                    180:   &Apache::structuretags::shuffle(\@idx_arr);
                    181:   my $bodytext = '';
                    182:   for(0 .. $#randomlist) {
                    183:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
                    184:   }
1.8       albertel  185: 
                    186:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      187:   return "";
1.7       tsai      188: }
                    189: 
                    190: sub shuffle {
                    191:     my $a=shift;
                    192:     my $i;
                    193:     for($i=@$a;--$i;) {
                    194:       my $j=int rand($i+1);
                    195:       next if $i == $j;
                    196:       @$a[$i,$j] = @$a[$j,$i];
                    197:     }
1.6       tsai      198: }
                    199: 
                    200: sub end_randomlist {
                    201: }
                    202: 
1.11      albertel  203: sub start_part {
                    204:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    205:   my $args ='';
                    206:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    207:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
1.14      albertel  208:   $Apache::inputtags::part=$id;
1.18      albertel  209:   @Apache::inputtags::responselist = ();
1.15      www       210:   if ($target eq 'meta') {
1.16      albertel  211:     return &Apache::response::mandatory_part_meta;
1.21      albertel  212:   } else {
1.23    ! albertel  213:     my ($status,$datemsg) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
        !           214:     push (@Apache::inputtags::status,$status);
        !           215:     my $expression='$external::datestatus="'.$status.'";';
        !           216:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
        !           217:     &Apache::run::run($expression,$safeeval);
        !           218:     if ( $status eq 'CLOSED' ) {
1.21      albertel  219:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
                    220:       if ( $target eq "web" ) {
1.23    ! albertel  221: 	return "<br></br>Part is not open to be viewed. It $datemsg<br></br>";
1.21      albertel  222:       }
                    223:     }
1.15      www       224:   }
1.19      albertel  225:   return '';
1.11      albertel  226: }
                    227: 
                    228: sub end_part {
                    229:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.18      albertel  230:   &Apache::lonxml::debug("in end_part $target ");
1.23    ! albertel  231:   pop @Apache::inputtags::status;
1.19      albertel  232:   if ( $target eq 'meta' ) { return ''; }
                    233:   if ( $target eq 'grade' ) { return &Apaceh::inputtags::grade; }
1.20      albertel  234:   return &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.11      albertel  235: }
1.1       albertel  236: 
                    237: 1;
                    238: __END__

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