File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.22: download - view: text, annotated - select for diffs
Fri Dec 15 20:31:30 2000 UTC (23 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- dates work cleanly now

    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'));
   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:     if ( $status eq 'CLOSED' ) {
   52:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
   53:       if ( $target eq "web" ) {
   54: 	return "Problem is not open to be viewed. The problem $datemsg";
   55:       }
   56:     } 
   57:   }
   58:   if ($target eq 'web') {
   59:     my $args ='';
   60:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   61:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
   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:   }
   69:   return '';
   70: }
   71: 
   72: sub end_problem {
   73:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   74:   my $result='';
   75:   my $status=@Apache::inputtags::status['-1'];
   76:   unless ($target eq 'meta') {
   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
   83:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
   84:     } 
   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:     }
   92:   } else {
   93:     if ($Apache::inputtags::part eq '0') {
   94:       $result=&Apache::response::mandatory_part_meta;
   95:     }
   96:   }
   97:   return $result;
   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;';
  106: #  print "<br>$code<br>";
  107:   my $result = &Apache::run::run($code,$safeeval);
  108:   if ( ! $result ) { 
  109: #    my $skip=$$parser[$#$parser]->get_text("/block");
  110:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  111: #    print "skipped $skip";
  112:   }
  113:   return "";
  114: }
  115: 
  116: sub end_block {
  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: 
  126:   push( @Apache::structuretags::whileconds, $code); 
  127:   my $result = &Apache::run::run($code,$safeeval);
  128:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  129:   push( @Apache::structuretags::whilebody, $bodytext);
  130:   if ( $result ) { 
  131:     &Apache::lonxml::newparser($parser,\$bodytext);
  132:   }
  133:   return "";
  134: }
  135: 
  136: sub end_while {
  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 ) { 
  142:     &Apache::lonxml::newparser($parser,\$bodytext);
  143:   } 
  144:   return "";
  145: }
  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)=@_;
  155:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  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
  166:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  167:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  168:       push(@randomlist,$list_item);
  169:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
  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:   }
  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:   }
  185: 
  186:   &Apache::lonxml::newparser($parser,\$bodytext);
  187:   return "";
  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:     }
  198: }
  199: 
  200: sub end_randomlist {
  201: }
  202: 
  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);
  208:   $Apache::inputtags::part=$id;
  209:   @Apache::inputtags::responselist = ();
  210:   if ($target eq 'meta') {
  211:     return &Apache::response::mandatory_part_meta;
  212:   } else {
  213:     my ($date,$passed) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
  214:     if ( !$passed ) {
  215:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  216:       if ( $target eq "web" ) {
  217: 	return "Part is not open to be viewed. It opens on $date";
  218:       }
  219:     }
  220:   }
  221:   return '';
  222: }
  223: 
  224: sub end_part {
  225:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  226:   &Apache::lonxml::debug("in end_part $target ");
  227:   if ( $target eq 'meta' ) { return ''; }
  228:   if ( $target eq 'grade' ) { return &Apaceh::inputtags::grade; }
  229:   return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  230: }
  231: 
  232: 1;
  233: __END__

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