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

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.25      albertel    9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part','preduedate','postanswerdate','solved','notsolved'));
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);
1.24      albertel   51:     my $expression='$external::datestatus="'.$status.'";';
                     52:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                     53:     &Apache::run::run($expression,$safeeval);
1.22      albertel   54:     if ( $status eq 'CLOSED' ) {
1.21      albertel   55:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
                     56:       if ( $target eq "web" ) {
1.27      albertel   57: 	return "<body bgcolor=#FFFFFF> <br></br>Problem is not open to be viewed. The problem $datemsg<br></br>";
1.21      albertel   58:       }
1.22      albertel   59:     } 
1.21      albertel   60:   }
1.19      albertel   61:   if ($target eq 'web') {
1.16      albertel   62:     my $args ='';
1.19      albertel   63:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
1.16      albertel   64:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
1.22      albertel   65:     if ($status eq 'CAN_ANSWER') {
                     66:       # create a page header and exit
                     67:       return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
1.29    ! albertel   68:     } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
1.22      albertel   69:       return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n";
                     70:     }
                     71:   }
1.19      albertel   72:   return '';
1.9       albertel   73: }
                     74: 
                     75: sub end_problem {
1.16      albertel   76:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     77:   my $result='';
1.24      albertel   78:   my $status=$Apache::inputtags::status['-1'];
1.16      albertel   79:   unless ($target eq 'meta') {
1.28      albertel   80:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
                     81:        $status eq 'CAN_ANSWER') {
1.19      albertel   82:       # if part is zero, no <part>s existed, so we need to the grading
                     83:       &Apache::inputtags::grade;
                     84:     } elsif ($Apache::inputtags::part eq '0') {
                     85:       # if part is zero, no <part>s existed, so we need show the current 
                     86:       # grading status
1.20      albertel   87:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.19      albertel   88:     } 
1.22      albertel   89:     if ($target eq 'web') { 
                     90:       if ($status eq 'CAN_ANSWER') {
                     91: 	$result.="</form></body>\n"; 
1.28      albertel   92:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
1.22      albertel   93: 	$result.="</body>\n"; 
                     94:       } 
                     95:     }
1.16      albertel   96:   } else {
1.18      albertel   97:     if ($Apache::inputtags::part eq '0') {
1.16      albertel   98:       $result=&Apache::response::mandatory_part_meta;
                     99:     }
                    100:   }
                    101:   return $result;
1.1       albertel  102: }
                    103: 
                    104: sub start_block {
                    105:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    106: 
                    107:   my $code = @$parstack[$#$parstack];
                    108:   $code =~ s/\"//g;
                    109:   $code .=';return $condition;';
1.3       albertel  110: #  print "<br>$code<br>";
1.1       albertel  111:   my $result = &Apache::run::run($code,$safeeval);
1.24      albertel  112:   &Apache::lonxml::debug("block :$code: returned :$result:");
1.1       albertel  113:   if ( ! $result ) { 
1.9       albertel  114:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.24      albertel  115:     &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
1.1       albertel  116:   }
                    117:   return "";
                    118: }
                    119: 
                    120: sub end_block {
1.4       tsai      121: }
                    122: 
                    123: sub start_while {
                    124:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    125: 
                    126:   my $code = @$parstack[$#$parstack];
                    127:   $code =~ s/\"//g;
                    128:   $code .=';return $condition;';
                    129: 
1.5       tsai      130:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai      131:   my $result = &Apache::run::run($code,$safeeval);
                    132:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai      133:   push( @Apache::structuretags::whilebody, $bodytext);
                    134:   if ( $result ) { 
1.8       albertel  135:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai      136:   }
                    137:   return "";
                    138: }
                    139: 
                    140: sub end_while {
1.5       tsai      141:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    142:   my $code = pop @Apache::structuretags::whileconds;
                    143:   my $bodytext = pop @Apache::structuretags::whilebody;
                    144:   my $result = &Apache::run::run($code,$safeeval);
                    145:   if ( $result ) { 
1.8       albertel  146:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai      147:   } 
                    148:   return "";
1.1       albertel  149: }
1.6       tsai      150: 
                    151: # <randomlist> 
                    152: #  <tag1>..</tag1>
                    153: #  <tag2>..</tag2>
                    154: #  <tag3>..</tag3>
                    155: #  ... 
                    156: # </randomlist>
                    157: sub start_randomlist {
                    158:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.9       albertel  159:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai      160:   my $b_parser= HTML::TokeParser->new(\$body);
                    161:   my $b_tok;
                    162:   my @randomlist;
                    163:   my $list_item;
                    164: 
                    165:   while($b_tok = $b_parser->get_token() ) {
                    166:     if($b_tok->[0] eq 'S') { # start tag
                    167:     # get content of the tag until matching end tag
                    168:     # get all text upto the matching tag
                    169:     # and push the content into @randomlist
1.9       albertel  170:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai      171:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai      172:       push(@randomlist,$list_item);
1.7       tsai      173:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6       tsai      174:     }
                    175:     if($b_tok->[0] eq 'T') { # text
                    176:     # what to do with text in between tags?
                    177:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
                    178:     }
                    179:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
                    180:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
                    181:     # }
                    182:   }
1.7       tsai      183:   my @idx_arr = (0 .. $#randomlist);
                    184:   &Apache::structuretags::shuffle(\@idx_arr);
                    185:   my $bodytext = '';
                    186:   for(0 .. $#randomlist) {
                    187:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
                    188:   }
1.8       albertel  189: 
                    190:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      191:   return "";
1.7       tsai      192: }
                    193: 
                    194: sub shuffle {
                    195:     my $a=shift;
                    196:     my $i;
                    197:     for($i=@$a;--$i;) {
                    198:       my $j=int rand($i+1);
                    199:       next if $i == $j;
                    200:       @$a[$i,$j] = @$a[$j,$i];
                    201:     }
1.6       tsai      202: }
                    203: 
                    204: sub end_randomlist {
                    205: }
                    206: 
1.11      albertel  207: sub start_part {
                    208:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    209:   my $args ='';
                    210:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    211:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
1.14      albertel  212:   $Apache::inputtags::part=$id;
1.18      albertel  213:   @Apache::inputtags::responselist = ();
1.15      www       214:   if ($target eq 'meta') {
1.16      albertel  215:     return &Apache::response::mandatory_part_meta;
1.21      albertel  216:   } else {
1.23      albertel  217:     my ($status,$datemsg) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
                    218:     push (@Apache::inputtags::status,$status);
                    219:     my $expression='$external::datestatus="'.$status.'";';
                    220:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                    221:     &Apache::run::run($expression,$safeeval);
                    222:     if ( $status eq 'CLOSED' ) {
1.21      albertel  223:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
                    224:       if ( $target eq "web" ) {
1.23      albertel  225: 	return "<br></br>Part is not open to be viewed. It $datemsg<br></br>";
1.21      albertel  226:       }
                    227:     }
1.15      www       228:   }
1.19      albertel  229:   return '';
1.11      albertel  230: }
                    231: 
                    232: sub end_part {
                    233:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.18      albertel  234:   &Apache::lonxml::debug("in end_part $target ");
1.28      albertel  235:   my $status=$Apache::inputtags::status['-1'];
1.23      albertel  236:   pop @Apache::inputtags::status;
1.19      albertel  237:   if ( $target eq 'meta' ) { return ''; }
1.28      albertel  238:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') { 
                    239:     return &Apache::inputtags::grade; 
                    240:   }
1.20      albertel  241:   return &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.11      albertel  242: }
1.1       albertel  243: 
1.25      albertel  244: sub start_preduedate {
1.24      albertel  245:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    246:   if ($target ne 'meta') {
1.29    ! albertel  247:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
        !           248: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
1.26      albertel  249:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
1.24      albertel  250:     }
                    251:   }
                    252:   return '';
                    253: }
                    254: 
1.25      albertel  255: sub end_preduedate {
1.24      albertel  256:   return '';
                    257: }
                    258: 
1.25      albertel  259: sub start_postanswerdate {
1.24      albertel  260:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    261:   if ($target ne 'meta') {
                    262:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
1.26      albertel  263:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
1.24      albertel  264:     }
                    265:   }
                    266:   return '';
                    267: }
                    268: 
1.25      albertel  269: sub end_postanswerdate {
1.24      albertel  270:   return '';
                    271: }
                    272: 
1.25      albertel  273: sub start_notsolved {
1.24      albertel  274:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    275:   if ($target ne 'meta') {
                    276:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    277:     &Apache::lonxml::debug("not solved has :$gradestatus:");
                    278:     if ($gradestatus =~ /^correct/) {
                    279:       &Apache::lonxml::debug("skipping");
1.26      albertel  280:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
1.24      albertel  281:     }
                    282:   }
                    283:   return '';
                    284: }
                    285: 
1.25      albertel  286: sub end_notsolved {
1.24      albertel  287:   return '';
                    288: }
                    289: 
                    290: sub start_solved {
                    291:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    292:   if ($target ne 'meta') {
                    293:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                    294:     if ($gradestatus !~ /^correct/) {
                    295:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
                    296:     }
                    297:   }
                    298:   return '';
                    299: }
                    300: 
                    301: sub end_solved {
                    302:   return '';
                    303: }
1.1       albertel  304: 1;
                    305: __END__

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