File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.32: download - view: text, annotated - select for diffs
Mon Feb 19 20:39:00 2001 UTC (23 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <br> -> <br /> cleanup

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

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