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

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