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

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