Annotation of loncom/homework/structuretags.pm, revision 1.21

1.6       tsai        1: # The LearningOnline Network with CAPA # <script> definiton
1.1       albertel    2: 
                      3: package Apache::structuretags; 
                      4: 
                      5: use strict;
                      6: use Apache::lonnet;
                      7: 
                      8: sub BEGIN {
1.11      albertel    9:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part'));
1.10      albertel   10: }
                     11: 
                     12: sub start_web {
                     13:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     14:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
1.19      albertel   15:   if ($target eq 'web') {
                     16:     return $bodytext;
                     17:   } 
                     18:   return '';
1.10      albertel   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]);
1.19      albertel   27:   if ($target eq 'tex') {
                     28:     return $bodytext
                     29:   }
1.10      albertel   30:   return '';
                     31: }
                     32: 
                     33: sub end_tex {
1.9       albertel   34: }
                     35: 
                     36: sub start_problem {
                     37:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.19      albertel   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);
1.21    ! albertel   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:   }
1.19      albertel   55:   if ($target eq 'web') {
                     56:     # create a page header and exit
1.16      albertel   57:     my $args ='';
1.19      albertel   58:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
1.16      albertel   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:   } 
1.19      albertel   62:   return '';
1.9       albertel   63: }
                     64: 
                     65: sub end_problem {
1.16      albertel   66:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     67:   my $result='';
                     68:   unless ($target eq 'meta') {
1.19      albertel   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
1.20      albertel   75:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.19      albertel   76:     } 
                     77:     if ($target eq 'web') { $result.="</form></body>\n"; }
1.16      albertel   78:   } else {
1.18      albertel   79:     if ($Apache::inputtags::part eq '0') {
1.16      albertel   80:       $result=&Apache::response::mandatory_part_meta;
                     81:     }
                     82:   }
                     83:   return $result;
1.1       albertel   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;';
1.3       albertel   92: #  print "<br>$code<br>";
1.1       albertel   93:   my $result = &Apache::run::run($code,$safeeval);
                     94:   if ( ! $result ) { 
1.3       albertel   95: #    my $skip=$$parser[$#$parser]->get_text("/block");
1.9       albertel   96:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
1.3       albertel   97: #    print "skipped $skip";
1.1       albertel   98:   }
                     99:   return "";
                    100: }
                    101: 
                    102: sub end_block {
1.4       tsai      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: 
1.5       tsai      112:   push( @Apache::structuretags::whileconds, $code); 
1.4       tsai      113:   my $result = &Apache::run::run($code,$safeeval);
                    114:   my $bodytext=$$parser[$#$parser]->get_text("/while");
1.5       tsai      115:   push( @Apache::structuretags::whilebody, $bodytext);
                    116:   if ( $result ) { 
1.8       albertel  117:     &Apache::lonxml::newparser($parser,\$bodytext);
1.4       tsai      118:   }
                    119:   return "";
                    120: }
                    121: 
                    122: sub end_while {
1.5       tsai      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 ) { 
1.8       albertel  128:     &Apache::lonxml::newparser($parser,\$bodytext);
1.5       tsai      129:   } 
                    130:   return "";
1.1       albertel  131: }
1.6       tsai      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)=@_;
1.9       albertel  141:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
1.6       tsai      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
1.9       albertel  152:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
1.7       tsai      153:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
1.6       tsai      154:       push(@randomlist,$list_item);
1.7       tsai      155:    #  print "<BR><B>START-TAG $b_tok->[1], $b_tok->[4], $list_item</B>";
1.6       tsai      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:   }
1.7       tsai      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:   }
1.8       albertel  171: 
                    172:   &Apache::lonxml::newparser($parser,\$bodytext);
1.6       tsai      173:   return "";
1.7       tsai      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:     }
1.6       tsai      184: }
                    185: 
                    186: sub end_randomlist {
                    187: }
                    188: 
1.11      albertel  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);
1.14      albertel  194:   $Apache::inputtags::part=$id;
1.18      albertel  195:   @Apache::inputtags::responselist = ();
1.15      www       196:   if ($target eq 'meta') {
1.16      albertel  197:     return &Apache::response::mandatory_part_meta;
1.21    ! albertel  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:     }
1.15      www       206:   }
1.19      albertel  207:   return '';
1.11      albertel  208: }
                    209: 
                    210: sub end_part {
                    211:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.18      albertel  212:   &Apache::lonxml::debug("in end_part $target ");
1.19      albertel  213:   if ( $target eq 'meta' ) { return ''; }
                    214:   if ( $target eq 'grade' ) { return &Apaceh::inputtags::grade; }
1.20      albertel  215:   return &Apache::inputtags::gradestatus($Apache::inputtags::part);
1.11      albertel  216: }
1.1       albertel  217: 
                    218: 1;
                    219: __END__

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