File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.63: download - view: text, annotated - select for diffs
Mon Aug 20 16:15:54 2001 UTC (22 years, 8 months ago) by www
Branches: MAIN
CVS tags: HEAD
TAs should use other screen for input

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

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