File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.17: download - view: text, annotated - select for diffs
Wed Nov 29 00:51:57 2000 UTC (23 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added sig figs to parameters that are passed along
- can now get coorrect responses
- fixed caparesponse to create the proper data bits

    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:   return $bodytext;
   16: }
   17: 
   18: sub end_web {
   19: }
   20: 
   21: sub start_tex {
   22:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   23:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
   24:   return '';
   25: }
   26: 
   27: sub end_tex {
   28: }
   29: 
   30: sub start_problem {
   31:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   32:   unless ($target eq 'meta') {
   33:     my $args ='';
   34:     if ( $#$parstack > -1 ) {
   35:       $args=$$parstack[$#$parstack];
   36:     }
   37:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
   38:     $Apache::inputtags::part='0';
   39:     my $expression='$external::part='.$Apache::inputtags::part.';';
   40:     $safeeval->share_from('Apache::inputtags',['@part']);
   41:     &Apache::run::run($expression,$safeeval);
   42:     return "<title>$name</title>\n<body bgcolor=#FFFFFF>\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">";
   43:   } 
   44: }
   45: 
   46: sub end_problem {
   47:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   48:   my $result='';
   49:   unless ($target eq 'meta') {
   50:     $result="</form></body>\n";
   51:   } else {
   52:     if ($Apache::inputtags::part ne '0') {
   53:       $result=&Apache::response::mandatory_part_meta;
   54:     }
   55:   }
   56:   return $result;
   57: }
   58: 
   59: sub start_block {
   60:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   61: 
   62:   my $code = @$parstack[$#$parstack];
   63:   $code =~ s/\"//g;
   64:   $code .=';return $condition;';
   65: #  print "<br>$code<br>";
   66:   my $result = &Apache::run::run($code,$safeeval);
   67:   if ( ! $result ) { 
   68: #    my $skip=$$parser[$#$parser]->get_text("/block");
   69:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
   70: #    print "skipped $skip";
   71:   }
   72:   return "";
   73: }
   74: 
   75: sub end_block {
   76: }
   77: 
   78: sub start_while {
   79:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   80: 
   81:   my $code = @$parstack[$#$parstack];
   82:   $code =~ s/\"//g;
   83:   $code .=';return $condition;';
   84: 
   85:   push( @Apache::structuretags::whileconds, $code); 
   86:   my $result = &Apache::run::run($code,$safeeval);
   87:   my $bodytext=$$parser[$#$parser]->get_text("/while");
   88:   push( @Apache::structuretags::whilebody, $bodytext);
   89:   if ( $result ) { 
   90:     &Apache::lonxml::newparser($parser,\$bodytext);
   91:   }
   92:   return "";
   93: }
   94: 
   95: sub end_while {
   96:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   97:   my $code = pop @Apache::structuretags::whileconds;
   98:   my $bodytext = pop @Apache::structuretags::whilebody;
   99:   my $result = &Apache::run::run($code,$safeeval);
  100:   if ( $result ) { 
  101:     &Apache::lonxml::newparser($parser,\$bodytext);
  102:   } 
  103:   return "";
  104: }
  105: 
  106: # <randomlist> 
  107: #  <tag1>..</tag1>
  108: #  <tag2>..</tag2>
  109: #  <tag3>..</tag3>
  110: #  ... 
  111: # </randomlist>
  112: sub start_randomlist {
  113:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  114:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  115:   my $b_parser= HTML::TokeParser->new(\$body);
  116:   my $b_tok;
  117:   my @randomlist;
  118:   my $list_item;
  119: 
  120:   while($b_tok = $b_parser->get_token() ) {
  121:     if($b_tok->[0] eq 'S') { # start tag
  122:     # get content of the tag until matching end tag
  123:     # get all text upto the matching tag
  124:     # and push the content into @randomlist
  125:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  126:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  127:       push(@randomlist,$list_item);
  128:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
  129:     }
  130:     if($b_tok->[0] eq 'T') { # text
  131:     # what to do with text in between tags?
  132:       #  print "<B>TEXT $b_tok->[1]</B><BR>";
  133:     }
  134:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
  135:       #  print "<B>END-TAG $b_tok->[1]</B><BR>";
  136:     # }
  137:   }
  138:   my @idx_arr = (0 .. $#randomlist);
  139:   &Apache::structuretags::shuffle(\@idx_arr);
  140:   my $bodytext = '';
  141:   for(0 .. $#randomlist) {
  142:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  143:   }
  144: 
  145:   &Apache::lonxml::newparser($parser,\$bodytext);
  146:   return "";
  147: }
  148: 
  149: sub shuffle {
  150:     my $a=shift;
  151:     my $i;
  152:     for($i=@$a;--$i;) {
  153:       my $j=int rand($i+1);
  154:       next if $i == $j;
  155:       @$a[$i,$j] = @$a[$j,$i];
  156:     }
  157: }
  158: 
  159: sub end_randomlist {
  160: }
  161: 
  162: sub start_part {
  163:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  164:   my $args ='';
  165:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  166:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
  167:   $Apache::inputtags::part=$id;
  168:   if ($target eq 'meta') {
  169:     return &Apache::response::mandatory_part_meta;
  170:   }
  171: }
  172: 
  173: sub end_part {
  174:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  175: }
  176: 
  177: 1;
  178: __END__

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