File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.59: download - view: text, annotated - select for diffs
Sat Aug 18 16:38:00 2001 UTC (22 years, 9 months ago) by www
Branches: MAIN
CVS tags: HEAD
Needed </stores>

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: # 2/19 Guy
    4: # 6/26/2001 fixed extra web display at end of <web></web> tags
    5: # 8/17,8/18 Gerd Kortemeyer
    6: 
    7: package Apache::structuretags; 
    8: 
    9: use strict;
   10: use Apache::lonnet;
   11: 
   12: sub BEGIN {
   13:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
   14: #  &Apache::lonxml::register_insert('problem','',('part','postanswerdate','preduedate'))
   15: }
   16: 
   17: sub start_web {
   18:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   19:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
   20:   if ($target eq 'web') {
   21:     return $bodytext;
   22:   } 
   23:   return '';
   24: }
   25: 
   26: sub end_web {
   27:     return '';
   28: }
   29: 
   30: sub start_tex {
   31:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   32:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
   33:   if ($target eq 'tex') {
   34:     return $bodytext
   35:   }
   36:   return '';
   37: }
   38: 
   39: sub end_tex {
   40:     return '';
   41: }
   42: 
   43: sub page_start {
   44:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   45:   my $result=&Apache::londefdef::start_html($target,$token,$tagstack,$parstack,$parser,$safeeval);
   46:   my $head_tag_start='<head>'.&Apache::lonxml::registerurl();
   47:   my $body_tag_start='<body onLoad="'.&Apache::lonxml::loadevents().'" '.
   48: 	  'onUnload="'.&Apache::lonxml::unloadevents().'" ';
   49:   my $background=&Apache::lonxml::get_param('background',$parstack,$safeeval);
   50:   if ($background) {
   51:     $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
   52:                                         $background;
   53:     $body_tag_start.='background="'.$background.'" ';
   54:   } else {
   55:     my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,$safeeval);
   56:     if ($bgcolor) {
   57:       $body_tag_start.='bgcolor="'.$bgcolor.'" ';
   58:     } else {
   59:       $body_tag_start.='bgcolor="#ffffff"';
   60:     }
   61:   }
   62:   $body_tag_start.='>';
   63:   return ($result,$head_tag_start,$body_tag_start);
   64: }
   65: 
   66: sub start_problem {
   67:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   68: 
   69: #intialize globals
   70:   $Apache::inputtags::part='0';
   71:   @Apache::inputtags::responselist = ();
   72:   @Apache::inputtags::previous=();
   73:   $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
   74:   &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
   75:   if ($Apache::lonhomework::type eq '') {
   76:     my $uri=$ENV{'request.uri'};
   77:     if ($uri=~/\.(\w+)$/) {
   78:       $Apache::lonhomework::type=$1;
   79:       &Apache::lonxml::debug("Using type of $1");
   80:     } else {
   81:       $Apache::lonhomework::type='problem';
   82:       &Apache::lonxml::debug("Using default type, problem, :$uri:");
   83:     }
   84:   }
   85: 
   86: #adeed vars to the scripting enviroment
   87:   my $expression='$external::part='.$Apache::inputtags::part.';';
   88:   &Apache::run::run($expression,$safeeval);
   89:   my $status;
   90:   my $accessmsg;
   91: 
   92:   #should get back a <html> or the neccesary stuff to start XML/MathML
   93:   my ($result,$head_tag_start,$body_tag_start)=
   94:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
   95: 
   96:   if ($Apache::lonhomework::type eq 'exam') {
   97:       if ($target eq 'web') {
   98:           my $token=$Apache::lonhomework::history{"resource.0.outtoken"};
   99:           if (($ENV{'form.doescheckout'}) && (!$token)) {
  100: 	     $token=&Apache::lonxml::maketoken();
  101:           }
  102:           $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
  103:       }
  104:       if ($target eq 'meta') {
  105:      
  106: 	  return (<<ENDSTORES);
  107: <stores part="0" name="outtoken" type="string" 
  108:  display="Document ID checked out"></stores>
  109: <stores part="0" name="checkouttime" type="date"
  110:  display="Time checked out"></stores>
  111: <stores part="0" name="outremote" type="string" 
  112:  display="Client IP checked out from"></stores>
  113: 
  114: <stores part="0" name="intoken" type="string" 
  115:  display="Document ID checked in"></stores>
  116: <stores part="0" name="checkintime" type="date"
  117:  display="Time checked in"></stores>
  118: <stores part="0" name="inremote" type="string" 
  119:  display="Client IP checked in from"></stores>
  120: ENDSTORES
  121:       }
  122:   }
  123:   if ($target eq 'web' || $target eq 'grade') {
  124:     ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
  125:     push (@Apache::inputtags::status,$status);
  126:     my $expression='$external::datestatus="'.$status.'";';
  127:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
  128:     &Apache::run::run($expression,$safeeval);
  129:     if (( $status eq 'CLOSED' ) ||
  130:         ( $status eq 'UNCHECKEDOUT') ||
  131:         ( $status eq 'BANNED')) {
  132:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
  133:       if ( $target eq "web" ) {
  134: 	$result.= $head_tag_start.'</head>';
  135:         my $msg=$body_tag_start.
  136: 	    '<h1>Not open to be viewed</h1>';
  137:         if ($status eq 'CLOSED') {
  138: 	    $msg.='The problem '.$accessmsg;
  139: 	} elsif ($status eq 'UNCHECKEDOUT') {
  140:             $msg.=(<<ENDCHECKOUT);
  141: <h2>The resource needs to be checked out</h2>
  142: As a resource gets checked out, a unique timestamped ID is given to it, and a
  143: permanent record is left in the system.<p />
  144: <font color=red>
  145: Checking out resources is subject to course policies, and may exclude future
  146: credit even if done erroneously.<p />
  147: </font>  
  148: <form method=post>
  149: <input type=submit name="doescheckout" 
  150: value="Check out Exam for Viewing" />
  151: </form>
  152: ENDCHECKOUT
  153:         }
  154: 	return $result.$msg.'<br />';
  155:       }
  156:     }
  157:   }
  158:   if ($target eq 'web') {
  159:     my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  160:     if ($name eq '') { 
  161:       $name=&Apache::lonnet::EXT('resource.title');
  162:       if ($name eq 'con_lost') { $name = ''; }
  163:     }
  164:     $Apache::lonhomework::name=$name;
  165:     if ($status eq 'CAN_ANSWER') {
  166:       # create a page header and exit
  167:       $result.="$head_tag_start<title>$name</title></head>\n
  168:               $body_tag_start\n
  169:               <form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".
  170: 		'<input type="hidden" name="submitted" value="yes" />';
  171:       if ($ENV{'request.state'} eq "construct") {
  172: 	$result.='<input type="hidden" name="problemmode" value="View" />
  173:                  <input type="submit" name="problemmode" value="Edit" /><hr />';
  174:       }
  175:       return $result;
  176:     } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' || $status eq 'CLOSED') {
  177:       return $result.$head_tag_start."<title>$name</title></head>\n$body_tag_start\n";
  178:     }
  179:   }
  180:   if ($target eq 'edit') {
  181:     $result.=$head_tag_start."</head>".$body_tag_start.
  182:       '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
  183:        <input type="hidden" name="submitted" value="edit" />
  184:        <input type="hidden" name="problemmode" value="Edit" />
  185:        <input type="submit" name="problemmode" value="View" />
  186:        <input type="submit" name="Undo" value="undo" /> <hr />
  187:        <input type="submit" name="submit" value="Submit Changes" /><br />
  188:       ';
  189:     my $temp=&Apache::edit::insertlist($target,$token);
  190:     $result.=$temp;
  191:     return $result;
  192:   }
  193:   if ($target eq 'modified') {
  194:     $result=$token->[4];
  195:     $result.=&Apache::edit::handle_insert();
  196:     return $result;
  197:   }
  198:   return '';
  199: }
  200: 
  201: sub end_problem {
  202:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  203:   my $result='';
  204:   my $status=$Apache::inputtags::status['-1'];
  205:   if ($target eq 'grade' || $target eq 'web' ) {
  206:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
  207:        $status eq 'CAN_ANSWER') {
  208:       # if part is zero, no <part>s existed, so we need to the grading
  209:       &Apache::inputtags::grade;
  210:     } elsif ($Apache::inputtags::part eq '0') {
  211:       # if part is zero, no <part>s existed, so we need show the current 
  212:       # grading status
  213:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
  214:     }
  215:     if ($target eq 'web') { 
  216:       if ($status eq 'CAN_ANSWER') {
  217: 	$result.="</form></body>\n"; 
  218:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
  219: 	$result.="</body>\n";
  220:       }
  221:       $result.=&Apache::lonxml::xmlend();
  222:     }
  223:   }
  224:   if ($target eq 'meta') {
  225:     if ($Apache::inputtags::part eq '0') {
  226:       $result=&Apache::response::mandatory_part_meta;
  227:     }
  228:   }
  229:   if ($target eq 'edit') {
  230:     &Apache::lonxml::debug("in end_problem with $target, edit");
  231:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
  232:   }
  233:   return $result;
  234: }
  235: 
  236: sub start_library {
  237:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  238:   my ($result,$head_tag_start,$body_tag_start)=
  239:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  240:   if ($target eq 'edit') {
  241:     $result.=$head_tag_start."</head>".$body_tag_start.
  242:       '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
  243:        <input type="hidden" name="submitted" value="edit" />
  244:        <input type="hidden" name="problemmode" value="Edit" />
  245:        <input type="submit" name="problemmode" value="View" />
  246:        <input type="submit" name="Undo" value="undo" /> <hr />
  247:       ';
  248:     my $temp=&Apache::edit::insertlist($target,$token);
  249:     $result.=$temp;
  250:     return $result;
  251:   }
  252:   if ($target eq 'modified') {
  253:     $result=$token->[4];
  254:     $result.=&Apache::edit::handle_insert();
  255:     return $result;
  256:   }
  257:   return '';
  258: }
  259: 
  260: sub end_library {
  261:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  262:   my $result='';
  263:   if ($target eq 'edit') {
  264:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
  265:   }
  266:   return $result;
  267: }
  268: 
  269: sub start_block {
  270:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  271: 
  272:   if ($target eq 'web' || $target eq 'grade') {
  273:     my $code = @$parstack[$#$parstack];
  274:     $code =~ s/\"//g;
  275:     $code .=';return $condition;';
  276:     #  print "<br />$code<br />";
  277:     my $result = &Apache::run::run($code,$safeeval);
  278:     &Apache::lonxml::debug("block :$code: returned :$result:");
  279:     if ( ! $result ) { 
  280:       my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  281:       &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
  282:     }
  283:   }
  284:   return "";
  285: }
  286: 
  287: sub end_block {
  288:   return '';
  289: }
  290: 
  291: sub start_while {
  292:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  293: 
  294:   my $code = @$parstack[$#$parstack];
  295:   $code =~ s/\"//g;
  296:   $code .=';return $condition;';
  297: 
  298:   push( @Apache::structuretags::whileconds, $code); 
  299:   my $result = &Apache::run::run($code,$safeeval);
  300:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  301:   push( @Apache::structuretags::whilebody, $bodytext);
  302:   if ( $result ) { 
  303:     &Apache::lonxml::newparser($parser,\$bodytext);
  304:   }
  305:   return "";
  306: }
  307: 
  308: sub end_while {
  309:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  310:   my $code = pop @Apache::structuretags::whileconds;
  311:   my $bodytext = pop @Apache::structuretags::whilebody;
  312:   my $result = &Apache::run::run($code,$safeeval);
  313:   if ( $result ) { 
  314:     &Apache::lonxml::newparser($parser,\$bodytext);
  315:   } 
  316:   return "";
  317: }
  318: 
  319: # <randomlist> 
  320: #  <tag1>..</tag1>
  321: #  <tag2>..</tag2>
  322: #  <tag3>..</tag3>
  323: #  ... 
  324: # </randomlist>
  325: sub start_randomlist {
  326:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  327:   my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  328:   my $b_parser= HTML::TokeParser->new(\$body);
  329:   my $b_tok;
  330:   my @randomlist;
  331:   my $list_item;
  332: 
  333:   while($b_tok = $b_parser->get_token() ) {
  334:     if($b_tok->[0] eq 'S') { # start tag
  335:     # get content of the tag until matching end tag
  336:     # get all text upto the matching tag
  337:     # and push the content into @randomlist
  338:       $list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  339:       $list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  340:       push(@randomlist,$list_item);
  341:    #  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
  342:     }
  343:     if($b_tok->[0] eq 'T') { # text
  344:     # what to do with text in between tags?
  345:       #  print "<b>TEXT $b_tok->[1]</b><br />";
  346:     }
  347:     # if($b_tok->[0] eq 'E') { # end tag, should not happen
  348:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
  349:     # }
  350:   }
  351:   my @idx_arr = (0 .. $#randomlist);
  352:   &Apache::structuretags::shuffle(\@idx_arr);
  353:   my $bodytext = '';
  354:   for(0 .. $#randomlist) {
  355:     $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  356:   }
  357: 
  358:   &Apache::lonxml::newparser($parser,\$bodytext);
  359:   return "";
  360: }
  361: 
  362: sub shuffle {
  363:     my $a=shift;
  364:     my $i;
  365:     &Apache::response::setrandomnumber();
  366:     for($i=@$a;--$i;) {
  367:       my $j=int rand($i+1);
  368:       next if $i == $j;
  369:       @$a[$i,$j] = @$a[$j,$i];
  370:     }
  371: }
  372: 
  373: sub end_randomlist {
  374:   return '';
  375: }
  376: 
  377: sub start_part {
  378:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  379:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  380:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  381:   $Apache::inputtags::part=$id;
  382:   @Apache::inputtags::responselist = ();
  383:   @Apache::inputtags::previous=();
  384:   if ($target eq 'meta') {
  385:     return &Apache::response::mandatory_part_meta;
  386:   } elsif ($target eq 'web' || $target eq 'grade') {
  387:     my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  388:     push (@Apache::inputtags::status,$status);
  389:     my $expression='$external::datestatus="'.$status.'";';
  390:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  391:     &Apache::run::run($expression,$safeeval);
  392:     if ( $status eq 'CLOSED' ) {
  393:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  394:       if ( $target eq "web" ) {
  395: 	return "<br />Part is not open to be viewed. It $accessmsg<br />";
  396:       }
  397:     }
  398:   }
  399:   return '';
  400: }
  401: 
  402: sub end_part {
  403:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  404:   &Apache::lonxml::debug("in end_part $target ");
  405:   my $status=$Apache::inputtags::status['-1'];
  406:   pop @Apache::inputtags::status;
  407:   if ( $target eq 'meta' ) { return ''; }
  408:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
  409:     return &Apache::inputtags::grade;
  410:   }
  411:   if ($target eq 'web') {
  412:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  413:   }
  414:   return '';
  415: }
  416: 
  417: sub start_preduedate {
  418:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  419:   if ($target eq 'web' || $target eq 'grade') {
  420:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  421: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  422:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  423:     }
  424:   }
  425:   return '';
  426: }
  427: 
  428: sub end_preduedate {
  429:   return '';
  430: }
  431: 
  432: sub start_postanswerdate {
  433:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  434:   if ($target eq 'web' || $target eq 'grade') {
  435:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  436:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  437:     }
  438:   }
  439:   return '';
  440: }
  441: 
  442: sub end_postanswerdate {
  443:   return '';
  444: }
  445: 
  446: sub start_notsolved {
  447:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  448:   if ($target eq 'web' || $target eq 'grade') {
  449:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  450:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  451:     if ($gradestatus =~ /^correct/) {
  452:       &Apache::lonxml::debug("skipping");
  453:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  454:     }
  455:   }
  456:   return '';
  457: }
  458: 
  459: sub end_notsolved {
  460:   return '';
  461: }
  462: 
  463: sub start_solved {
  464:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  465:   if ($target eq 'web' || $target eq 'grade') {
  466:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  467:     if ($gradestatus !~ /^correct/) {
  468:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  469:     }
  470:   }
  471:   return '';
  472: }
  473: 
  474: sub end_solved {
  475:   return '';
  476: }
  477: 
  478: sub start_startouttext {
  479:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  480:   my @result=(''.'');
  481:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  482:   return (@result);
  483: }
  484: sub end_startouttext {
  485:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  486:   my $result='';
  487:   my $text='';
  488: 
  489:   if ($target eq 'edit') {
  490:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[$#$parser]);
  491:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  492: <td>Delete:".
  493:   &Apache::edit::deletelist($target,$token)
  494:   ."</td>
  495: <td>".
  496:   &Apache::edit::insertlist($target,$token).
  497:     "</td>
  498: </tr><tr><td colspan=\"3\">\n".
  499: 	&Apache::edit::editfield($token->[1],$text,"",50,4);
  500:   }
  501:   if ($target eq 'modified') {
  502:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  503:     $result='<startouttext />'.&Apache::edit::modifiedfield();
  504:   }
  505:   return $result;
  506: }
  507: sub start_endouttext {
  508:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  509:   my $result='';
  510:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  511:   if ($target eq "modified") { $result='<endouttext />'; }
  512:   return $result;
  513: }
  514: sub end_endouttext {
  515:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  516:   my @result=('','');
  517:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  518:   return (@result);
  519: }
  520: sub delete_startouttext {
  521:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  522: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  523:   my $text=$$parser['-1']->get_text("/endouttext");
  524:   my $token=$$parser['-1']->get_token();
  525:   &Apache::lonxml::debug("Deleting :$text: and :$token->[0]:$token->[1]:$token->[2]: for startouttext");
  526:   &Apache::lonxml::end_tag($tagstack,$parstack,$token);
  527:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
  528:   # they did the last time round
  529:   &Apache::lonxml::increasedepth($token);
  530:   &Apache::lonxml::decreasedepth($token);
  531:   return 1;
  532: }
  533: 
  534: 1;
  535: __END__

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