File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.37: download - view: text, annotated - select for diffs
Sun May 13 20:01:34 2001 UTC (22 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- able to insert tags now

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

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