File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.29: download - view: text, annotated - select for diffs
Mon Jan 8 13:55:08 2001 UTC (23 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- logic wrong on start_preduedate

    1: # The LearningOnline Network with CAPA # <script> definiton
    2: 
    3: package Apache::structuretags; 
    4: 
    5: use strict;
    6: use Apache::lonnet;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part','preduedate','postanswerdate','solved','notsolved'));
   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:   if ($target eq 'web') {
   16:     return $bodytext;
   17:   } 
   18:   return '';
   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]);
   27:   if ($target eq 'tex') {
   28:     return $bodytext
   29:   }
   30:   return '';
   31: }
   32: 
   33: sub end_tex {
   34: }
   35: 
   36: sub start_problem {
   37:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   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:   my $status;
   47:   my $datemsg;
   48:   if ($target ne 'meta') {
   49:     ($status,$datemsg) = &Apache::lonhomework::check_date('0');
   50:     push (@Apache::inputtags::status,$status);
   51:     my $expression='$external::datestatus="'.$status.'";';
   52:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
   53:     &Apache::run::run($expression,$safeeval);
   54:     if ( $status eq 'CLOSED' ) {
   55:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
   56:       if ( $target eq "web" ) {
   57: 	return "<body bgcolor=#FFFFFF> <br></br>Problem is not open to be viewed. The problem $datemsg<br></br>";
   58:       }
   59:     } 
   60:   }
   61:   if ($target eq 'web') {
   62:     my $args ='';
   63:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   64:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
   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'}."\">";
   68:     } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
   69:       return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n";
   70:     }
   71:   }
   72:   return '';
   73: }
   74: 
   75: sub end_problem {
   76:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   77:   my $result='';
   78:   my $status=$Apache::inputtags::status['-1'];
   79:   unless ($target eq 'meta') {
   80:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
   81:        $status eq 'CAN_ANSWER') {
   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
   87:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
   88:     } 
   89:     if ($target eq 'web') { 
   90:       if ($status eq 'CAN_ANSWER') {
   91: 	$result.="</form></body>\n"; 
   92:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
   93: 	$result.="</body>\n"; 
   94:       } 
   95:     }
   96:   } else {
   97:     if ($Apache::inputtags::part eq '0') {
   98:       $result=&Apache::response::mandatory_part_meta;
   99:     }
  100:   }
  101:   return $result;
  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;';
  110: #  print "<br>$code<br>";
  111:   my $result = &Apache::run::run($code,$safeeval);
  112:   &Apache::lonxml::debug("block :$code: returned :$result:");
  113:   if ( ! $result ) { 
  114:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  115:     &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
  116:   }
  117:   return "";
  118: }
  119: 
  120: sub end_block {
  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: 
  130:   push( @Apache::structuretags::whileconds, $code); 
  131:   my $result = &Apache::run::run($code,$safeeval);
  132:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  133:   push( @Apache::structuretags::whilebody, $bodytext);
  134:   if ( $result ) { 
  135:     &Apache::lonxml::newparser($parser,\$bodytext);
  136:   }
  137:   return "";
  138: }
  139: 
  140: sub end_while {
  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 ) { 
  146:     &Apache::lonxml::newparser($parser,\$bodytext);
  147:   } 
  148:   return "";
  149: }
  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)=@_;
  159:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  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
  170:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  171:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  172:       push(@randomlist,$list_item);
  173:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
  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:   }
  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:   }
  189: 
  190:   &Apache::lonxml::newparser($parser,\$bodytext);
  191:   return "";
  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:     }
  202: }
  203: 
  204: sub end_randomlist {
  205: }
  206: 
  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);
  212:   $Apache::inputtags::part=$id;
  213:   @Apache::inputtags::responselist = ();
  214:   if ($target eq 'meta') {
  215:     return &Apache::response::mandatory_part_meta;
  216:   } else {
  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' ) {
  223:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  224:       if ( $target eq "web" ) {
  225: 	return "<br></br>Part is not open to be viewed. It $datemsg<br></br>";
  226:       }
  227:     }
  228:   }
  229:   return '';
  230: }
  231: 
  232: sub end_part {
  233:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  234:   &Apache::lonxml::debug("in end_part $target ");
  235:   my $status=$Apache::inputtags::status['-1'];
  236:   pop @Apache::inputtags::status;
  237:   if ( $target eq 'meta' ) { return ''; }
  238:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') { 
  239:     return &Apache::inputtags::grade; 
  240:   }
  241:   return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  242: }
  243: 
  244: sub start_preduedate {
  245:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  246:   if ($target ne 'meta') {
  247:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  248: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  249:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  250:     }
  251:   }
  252:   return '';
  253: }
  254: 
  255: sub end_preduedate {
  256:   return '';
  257: }
  258: 
  259: sub start_postanswerdate {
  260:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  261:   if ($target ne 'meta') {
  262:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  263:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  264:     }
  265:   }
  266:   return '';
  267: }
  268: 
  269: sub end_postanswerdate {
  270:   return '';
  271: }
  272: 
  273: sub start_notsolved {
  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");
  280:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  281:     }
  282:   }
  283:   return '';
  284: }
  285: 
  286: sub end_notsolved {
  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: }
  304: 1;
  305: __END__

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