File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.34: download - view: text, annotated - select for diffs
Thu Apr 5 00:10:23 2001 UTC (23 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- cleaned and simplified

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: # 2/19 Guy
    4: package Apache::structuretags; 
    5: 
    6: use strict;
    7: use Apache::lonnet;
    8: 
    9: sub BEGIN {
   10:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
   11: }
   12: 
   13: sub start_web {
   14:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   15:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
   16:   if ($target eq 'web') {
   17:     return $bodytext;
   18:   } 
   19:   return '';
   20: }
   21: 
   22: sub end_web {
   23: }
   24: 
   25: sub start_tex {
   26:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   27:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
   28:   if ($target eq 'tex') {
   29:     return $bodytext
   30:   }
   31:   return '';
   32: }
   33: 
   34: sub end_tex {
   35: }
   36: 
   37: sub start_problem {
   38:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   39: 
   40: #intialize globals
   41:   $Apache::inputtags::part='0';
   42:   @Apache::inputtags::responselist = ();
   43: 
   44: #adeed vars to the scripting enviroment
   45:   my $expression='$external::part='.$Apache::inputtags::part.';';
   46:   &Apache::run::run($expression,$safeeval);
   47:   my $status;
   48:   my $datemsg;
   49:   if ($target eq 'web' || $target eq 'grade') {
   50:     ($status,$datemsg) = &Apache::lonhomework::check_date('0');
   51:     push (@Apache::inputtags::status,$status);
   52:     my $expression='$external::datestatus="'.$status.'";';
   53:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
   54:     &Apache::run::run($expression,$safeeval);
   55:     if ( $status eq 'CLOSED' ) {
   56:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
   57:       if ( $target eq "web" ) {
   58: 	return "<body bgcolor=\"#FFFFFF\"> <br />Problem is not open to be viewed. The problem $datemsg<br />";
   59:       }
   60:     } 
   61:   }
   62:   if ($target eq 'web') {
   63:     my $args ='';
   64:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   65:     my $name = &Apache::run::run("{$args;".'return $name}',$safeeval);
   66:     if ($name eq '') { 
   67:       $name=&Apache::lonnet::EXT('resource.title');
   68:       if ($name eq 'con_lost') { $name = ''; }
   69:     }
   70:     $Apache::lonhomework::name=$name;
   71:     if ($status eq 'CAN_ANSWER') {
   72:       # create a page header and exit
   73:       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" />';
   74:     } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
   75:       return "<title>$name</title>\n<body bgcolor=\"#FFFFFF\">\n";
   76:     }
   77:   }
   78:   if ($target eq 'edit') {
   79:     return "<body bgcolor=\"#FFFFFF\">\n<form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".'<input type="hidden" name="submitted" value="edit" />';
   80:   }
   81:   return '';
   82: }
   83: 
   84: sub end_problem {
   85:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   86:   my $result='';
   87:   my $status=$Apache::inputtags::status['-1'];
   88:   if ($target eq 'grade' || $target eq'web' ) {
   89:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
   90:        $status eq 'CAN_ANSWER') {
   91:       # if part is zero, no <part>s existed, so we need to the grading
   92:       &Apache::inputtags::grade;
   93:     } elsif ($Apache::inputtags::part eq '0') {
   94:       # if part is zero, no <part>s existed, so we need show the current 
   95:       # grading status
   96:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
   97:     } 
   98:     if ($target eq 'web') { 
   99:       if ($status eq 'CAN_ANSWER') {
  100: 	$result.="</form></body>\n"; 
  101:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
  102: 	$result.="</body>\n"; 
  103:       } 
  104:     }
  105:   } 
  106:   if ($target eq 'meta') {
  107:     if ($Apache::inputtags::part eq '0') {
  108:       
  109:       $result=&Apache::response::mandatory_part_meta;
  110:     }
  111:   }
  112:   if ($target eq 'edit') {
  113:     &Apache::lonxml::debug("in end_problem with $target, edit");
  114:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
  115:   }
  116:   return $result;
  117: }
  118: 
  119: sub start_block {
  120:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  121: 
  122:   my $code = @$parstack[$#$parstack];
  123:   $code =~ s/\"//g;
  124:   $code .=';return $condition;';
  125: #  print "<br />$code<br />";
  126:   my $result = &Apache::run::run($code,$safeeval);
  127:   &Apache::lonxml::debug("block :$code: returned :$result:");
  128:   if ( ! $result ) { 
  129:     my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  130:     &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
  131:   }
  132:   return "";
  133: }
  134: 
  135: sub end_block {
  136: }
  137: 
  138: sub start_while {
  139:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  140: 
  141:   my $code = @$parstack[$#$parstack];
  142:   $code =~ s/\"//g;
  143:   $code .=';return $condition;';
  144: 
  145:   push( @Apache::structuretags::whileconds, $code); 
  146:   my $result = &Apache::run::run($code,$safeeval);
  147:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  148:   push( @Apache::structuretags::whilebody, $bodytext);
  149:   if ( $result ) { 
  150:     &Apache::lonxml::newparser($parser,\$bodytext);
  151:   }
  152:   return "";
  153: }
  154: 
  155: sub end_while {
  156:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  157:   my $code = pop @Apache::structuretags::whileconds;
  158:   my $bodytext = pop @Apache::structuretags::whilebody;
  159:   my $result = &Apache::run::run($code,$safeeval);
  160:   if ( $result ) { 
  161:     &Apache::lonxml::newparser($parser,\$bodytext);
  162:   } 
  163:   return "";
  164: }
  165: 
  166: # <randomlist> 
  167: #  <tag1>..</tag1>
  168: #  <tag2>..</tag2>
  169: #  <tag3>..</tag3>
  170: #  ... 
  171: # </randomlist>
  172: sub start_randomlist {
  173:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  174:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  175:   my $b_parser= HTML::TokeParser->new(\$body);
  176:   my $b_tok;
  177:   my @randomlist;
  178:   my $list_item;
  179: 
  180:   while($b_tok = $b_parser->get_token() ) {
  181:     if($b_tok->[0] eq 'S') { # start tag
  182:     # get content of the tag until matching end tag
  183:     # get all text upto the matching tag
  184:     # and push the content into @randomlist
  185:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  186:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  187:       push(@randomlist,$list_item);
  188:    #  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
  189:     }
  190:     if($b_tok->[0] eq 'T') { # text
  191:     # what to do with text in between tags?
  192:       #  print "<b>TEXT $b_tok->[1]</b><br />";
  193:     }
  194:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
  195:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
  196:     # }
  197:   }
  198:   my @idx_arr = (0 .. $#randomlist);
  199:   &Apache::structuretags::shuffle(\@idx_arr);
  200:   my $bodytext = '';
  201:   for(0 .. $#randomlist) {
  202:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  203:   }
  204: 
  205:   &Apache::lonxml::newparser($parser,\$bodytext);
  206:   return "";
  207: }
  208: 
  209: sub shuffle {
  210:     my $a=shift;
  211:     my $i;
  212:     for($i=@$a;--$i;) {
  213:       my $j=int rand($i+1);
  214:       next if $i == $j;
  215:       @$a[$i,$j] = @$a[$j,$i];
  216:     }
  217: }
  218: 
  219: sub end_randomlist {
  220: }
  221: 
  222: sub start_part {
  223:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  224:   my $args ='';
  225:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
  226:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
  227:   $Apache::inputtags::part=$id;
  228:   @Apache::inputtags::responselist = ();
  229:   if ($target eq 'meta') {
  230:     return &Apache::response::mandatory_part_meta;
  231:   } else {
  232:     my ($status,$datemsg) = &Apache::lonhomework::check_date("OPEN_DATE",$id);
  233:     push (@Apache::inputtags::status,$status);
  234:     my $expression='$external::datestatus="'.$status.'";';
  235:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  236:     &Apache::run::run($expression,$safeeval);
  237:     if ( $status eq 'CLOSED' ) {
  238:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  239:       if ( $target eq "web" ) {
  240: 	return "<br />Part is not open to be viewed. It $datemsg<br />";
  241:       }
  242:     }
  243:   }
  244:   return '';
  245: }
  246: 
  247: sub end_part {
  248:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  249:   &Apache::lonxml::debug("in end_part $target ");
  250:   my $status=$Apache::inputtags::status['-1'];
  251:   pop @Apache::inputtags::status;
  252:   if ( $target eq 'meta' ) { return ''; }
  253:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') { 
  254:     return &Apache::inputtags::grade; 
  255:   }
  256:   return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  257: }
  258: 
  259: sub start_preduedate {
  260:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  261:   if ($target eq 'web' || $target eq 'grade') {
  262:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  263: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  264:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  265:     }
  266:   }
  267:   return '';
  268: }
  269: 
  270: sub end_preduedate {
  271:   return '';
  272: }
  273: 
  274: sub start_postanswerdate {
  275:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  276:   if ($target eq 'web' || $target eq 'grade') {
  277:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  278:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  279:     }
  280:   }
  281:   return '';
  282: }
  283: 
  284: sub end_postanswerdate {
  285:   return '';
  286: }
  287: 
  288: sub start_notsolved {
  289:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  290:   if ($target eq 'web' || $target eq 'grade') {
  291:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  292:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  293:     if ($gradestatus =~ /^correct/) {
  294:       &Apache::lonxml::debug("skipping");
  295:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  296:     }
  297:   }
  298:   return '';
  299: }
  300: 
  301: sub end_notsolved {
  302:   return '';
  303: }
  304: 
  305: sub start_solved {
  306:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  307:   if ($target eq 'web' || $target eq 'grade') {
  308:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  309:     if ($gradestatus !~ /^correct/) {
  310:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  311:     }
  312:   }
  313:   return '';
  314: }
  315: 
  316: sub end_solved {
  317:   return '';
  318: }
  319: 
  320: sub start_startouttext {
  321:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  322:   my $result='';
  323:   if ($target eq "edit" ) { $result=" "; }
  324:   return $result;
  325: }
  326: sub end_startouttext {
  327:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  328:   my $result='';
  329:   if ($target eq 'edit') {
  330:     $result=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
  331:     $result=&Apache::edit::editfield($token->[1],$result);
  332:   }
  333:   return $result;
  334: }
  335: sub start_endouttext {
  336:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  337:   my $result='';
  338:   if ($target eq "edit" ) { $result=" "; }
  339:   return $result;
  340: }
  341: sub end_endouttext {
  342:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  343:   my $result='';
  344:   if ($target eq "edit" ) { $result=" "; }
  345:   return $result;
  346: }
  347: 
  348: 
  349: 1;
  350: __END__

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