File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.102: download - view: text, annotated - select for diffs
Wed Jul 24 20:34:52 2002 UTC (21 years, 9 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Added one blank space after </tex> to correct a few LaTeX errors in Ray's
problema.

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.102 2002/07/24 20:34:52 sakharuk Exp $
    5: #
    6: # Copyright Michigan State University Board of Trustees
    7: #
    8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
    9: #
   10: # LON-CAPA is free software; you can redistribute it and/or modify
   11: # it under the terms of the GNU General Public License as published by
   12: # the Free Software Foundation; either version 2 of the License, or
   13: # (at your option) any later version.
   14: #
   15: # LON-CAPA is distributed in the hope that it will be useful,
   16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
   17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   18: # GNU General Public License for more details.
   19: #
   20: # You should have received a copy of the GNU General Public License
   21: # along with LON-CAPA; if not, write to the Free Software
   22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
   23: #
   24: # /home/httpd/html/adm/gpl.txt
   25: #
   26: # http://www.lon-capa.org/
   27: #
   28: # 2/19 Guy
   29: # 6/26/2001 fixed extra web display at end of <web></web> tags
   30: # 8/17,8/18,8/20 Gerd Kortemeyer
   31: 
   32: package Apache::structuretags; 
   33: 
   34: use strict;
   35: use Apache::lonnet;
   36: use Apache::File();
   37: 
   38: BEGIN {
   39:   &Apache::lonxml::register('Apache::structuretags',('block','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
   40: #  &Apache::lonxml::register_insert('problem','',('part','postanswerdate','preduedate'))
   41: }
   42: 
   43: sub start_web {
   44:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   45:   my $bodytext=&Apache::lonxml::get_all_text("/web",$$parser[$#$parser]);
   46:   if ($target eq 'web') {
   47:     return $bodytext;
   48:   } 
   49:   return '';
   50: }
   51: 
   52: sub end_web {
   53:     return '';
   54: }
   55: 
   56: sub start_tex {
   57:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   58:   my $bodytext=&Apache::lonxml::get_all_text("/tex",$$parser[$#$parser]);
   59:   if ($target eq 'tex') {
   60:       return $bodytext.' ';
   61:   }
   62:   return '';
   63: }
   64: 
   65: sub end_tex {
   66:     return '';
   67: }
   68: 
   69: sub page_start {
   70:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   71:   my %found;
   72:   foreach my $taginside ($tagstack) {
   73:     foreach my $taglookedfor ('html','body','form') {
   74:       if ($taginside =~ /^$taglookedfor$/i) { $found{$taglookedfor} = 1; }
   75:     }
   76:   }
   77: 
   78:   my $result;
   79:   my $head_tag_start;
   80:   if (!defined($found{'html'})) {
   81:     $result=&Apache::londefdef::start_html($target,$token,$tagstack,$parstack,
   82: 					   $parser,$safeeval);
   83:     $head_tag_start='<head>'.&Apache::lonxml::registerurl(undef,$target);
   84:   }
   85:   my $body_tag_start;
   86:   if (!defined($found{'body'})) {
   87:     $body_tag_start='<body onLoad="'.&Apache::lonxml::loadevents().'" '.
   88:       'onUnload="'.&Apache::lonxml::unloadevents().'" ';
   89:     my $background=&Apache::lonxml::get_param('background',$parstack,$safeeval);
   90:     if ($background) {
   91:       $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
   92: 	$background;
   93:       $body_tag_start.='background="'.$background.'" ';
   94:     } else {
   95:       my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,$safeeval);
   96:       if ($bgcolor) {
   97: 	$body_tag_start.='bgcolor="'.$bgcolor.'" ';
   98:       } else {
   99: 	$body_tag_start.='bgcolor="#ffffff"';
  100:       }
  101:     }
  102:     $body_tag_start.='>';
  103:   }
  104:   return ($result,$head_tag_start,$body_tag_start);
  105: }
  106: 
  107: sub initialize_storage {
  108:   %Apache::lonhomework::results=();
  109:   my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  110:   if ($ENV{'request.state'} eq 'construct') {
  111:     %Apache::lonhomework::history=
  112:       &Apache::lonnet::tmprestore($ENV{'request.uri'},'',$domain,$name);
  113:     my ($temp)=keys %Apache::lonhomework::history ;
  114:     &Apache::lonxml::debug("Return message of $temp");
  115:   } else {
  116:     %Apache::lonhomework::history=
  117:       &Apache::lonnet::restore($symb,$courseid,$domain,$name);
  118:   }
  119:   #ignore error conditions
  120:   my ($temp)=keys %Apache::lonhomework::history ;
  121:   if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
  122: }
  123: 
  124: # -------------------------------------------------------------finalize_storage
  125: # Stores away the result has to a student's environment
  126: # checks form.grade_ for specific values, other wises stores
  127: # to the running users environment
  128: sub finalize_storage {
  129:   my $result;
  130:   my ($temp) = keys %Apache::lonhomework::results;
  131:   if ( $temp ne '' ) {
  132:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  133:     if ($ENV{'request.state'} eq 'construct') {
  134:       $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
  135: 				      $ENV{'request.uri'},'',$domain,$name);
  136:       &Apache::lonxml::debug('Construct Store return message:'.$result);
  137:     } else {
  138:       $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
  139: 				      $symb,$courseid,$domain,$name);
  140:       &Apache::lonxml::debug('Store return message:'.$result);
  141:     }
  142:   }
  143:   return $result;
  144: }
  145: 
  146: sub checkout_msg {
  147: return (<<ENDCHECKOUT);
  148: <h2>The resource needs to be checked out</h2>
  149: As a resource gets checked out, a unique timestamped ID is given to it, and a
  150: permanent record is left in the system.<p />
  151: <font color=red>
  152: Checking out resources is subject to course policies, and may exclude future
  153: credit even if done erroneously.<p />
  154: </font>
  155: <form name="checkout" method="POST" action="$ENV{'request.uri'}">
  156: <input type="hidden" name="doescheckout" value="yes" />
  157: <input type="button" name="checkoutbutton" value="Check out Exam for Viewing" onClick="javascript:if (confirm('Check out Exam?')) { document.checkout.submit(); }" />
  158: </form>
  159: ENDCHECKOUT
  160: }
  161: 
  162: sub start_problem {
  163:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  164: 
  165:   # meta is called from lonpublisher, which doesn't uses the normal
  166:   # lonhomework method of parsing the file which means that inputtags 
  167:   # won't get reset
  168:   if ( $Apache::inputtags::part ne '' && $target != 'meta' ) {
  169:     &Apache::lonxml::error('Only one problem allowed in a .problem file');
  170:     my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[-1]);
  171:     return '';
  172:   }
  173: #intialize globals
  174:   $Apache::inputtags::part='0';
  175:   @Apache::inputtags::responselist = ();
  176:   @Apache::inputtags::previous=();
  177:   if ($target ne 'analyze') {
  178:     &initialize_storage();
  179:     if ($target eq 'web') {
  180:       &Apache::lonhomework::showhash(%Apache::lonhomework::history);
  181:     }
  182:     $Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
  183:     &Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
  184:   }
  185:   if ($Apache::lonhomework::type eq '') {
  186:     my $uri=$ENV{'request.uri'};
  187:     if ($uri=~/\.(\w+)$/) {
  188:       $Apache::lonhomework::type=$1;
  189:       &Apache::lonxml::debug("Using type of $1");
  190:     } else {
  191:       $Apache::lonhomework::type='problem';
  192:       &Apache::lonxml::debug("Using default type, problem, :$uri:");
  193:     }
  194:   }
  195: 
  196:   #added vars to the scripting enviroment
  197:   my $expression='$external::part='.$Apache::inputtags::part.';';
  198:   &Apache::run::run($expression,$safeeval);
  199:   my $status;
  200:   my $accessmsg;
  201: 
  202:   #should get back a <html> or the neccesary stuff to start XML/MathML
  203:   my ($result,$head_tag_start,$body_tag_start)=
  204:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  205:   if ($target eq 'tex' and $ENV{'request.symb'} =~ m/\.page_/) { $result = '';}
  206: 
  207:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  208:     #handle exam checkout
  209:     if ($Apache::lonhomework::type eq 'exam') {
  210:       my $token=$Apache::lonhomework::history{"resource.0.outtoken"};
  211:       if (($ENV{'form.doescheckout'}) && (!$token)) {
  212: 	$token=&Apache::lonxml::maketoken();
  213: 	$Apache::lonhomework::history{"resource.0.outtoken"}=$token;
  214:       }
  215:       $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
  216:     }
  217: 
  218:     #handle rand seed in construction space
  219:     my $rndseed;
  220:     if ($ENV{'request.state'} eq "construct") {
  221:       $rndseed=$ENV{'form.rndseed'};
  222:       if (!$rndseed) {
  223: 	$rndseed=time;
  224: 	$ENV{'form.rndseed'}=$rndseed;
  225:       }
  226:       &Apache::run::run('$external::randomseed='.$rndseed.';',$safeeval);
  227:     }
  228:     ($status,$accessmsg) = &Apache::lonhomework::check_access('0');
  229:     push (@Apache::inputtags::status,$status);
  230:     my $expression='$external::datestatus="'.$status.'";';
  231:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
  232:     &Apache::run::run($expression,$safeeval);
  233:     if (( $status eq 'CLOSED' ) ||
  234:         ( $status eq 'UNCHECKEDOUT') ||
  235:         ( $status eq 'BANNED')) {
  236:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$$parser[$#$parser]);
  237:       if ( $target eq "web" ) {
  238: 	$result.= $head_tag_start.'</head>';
  239:         my $msg=$body_tag_start.
  240: 	    '<h1>Not open to be viewed</h1>';
  241:         if ($status eq 'CLOSED') {
  242: 	    $msg.='The problem '.$accessmsg;
  243: 	} elsif ($status eq 'UNCHECKEDOUT') {
  244:             $msg.=&checkout_msg;
  245:         }
  246: 	$result.=$msg.'<br />';
  247:       }
  248:     } elsif ($target eq 'web') {
  249:       my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  250:       if ($name eq '') { 
  251: 	$name=&Apache::lonnet::EXT('resource.title');
  252: 	if ($name eq 'con_lost') { $name = ''; }
  253:       }
  254:       $Apache::lonhomework::name=$name;
  255:       if ($status eq 'CAN_ANSWER') {
  256: 	# create a page header and exit
  257: 	$result.="$head_tag_start<title>$name</title></head>\n
  258:               $body_tag_start\n
  259:               <form name=\"lonhomework\" method=\"POST\" action=\"".$ENV{'request.uri'}."\">".
  260: 		'<input type="hidden" name="submitted" value="yes" />';
  261: 	if ($ENV{'request.state'} eq "construct") {
  262: 	  $result.=
  263: 	    '<input type="hidden" name="problemmode" value="View" />
  264:              <input type="submit" name="problemmode" value="Edit" />
  265:              <input type="submit" name="problemmode" value="EditXML" />
  266:              Random Seed:<input type="text" name="rndseed" width="10" value="'.
  267: 	       $rndseed.'" />
  268:              <input type="submit" name="changerandseed" value="Change" />
  269:              <input type="submit" name="resetdata" value="Reset Submissions" />
  270:              <input type="checkbox" name="showallfoils" ';
  271: 	  if (defined($ENV{'form.showallfoils'})) { $result.='checked="on"'; }
  272: 	  $result.= ' /> Show All Foils
  273:              <hr />';
  274: 	}
  275: 	# if we are viewing someone else preserve that info
  276: 	if (defined $ENV{'form.grade_symb'}) {
  277: 	  foreach my $field ('symb','courseid','domain','username') {
  278: 	    $result .= '<input type="hidden" name="grade_'.$field.
  279: 	      '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
  280: 	  }
  281: 	}
  282:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
  283: 	       || $status eq 'CLOSED') {
  284: 	$result.=$head_tag_start.
  285: 	  "<title>$name</title></head>\n$body_tag_start\n";
  286:       }
  287:     } elsif ($target eq 'tex') {
  288: 	my $id = $Apache::inputtags::part;
  289: 	my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
  290: 	$duedate = POSIX::strftime("%c",localtime($duedate));
  291: 	my $temp_file;
  292: 	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
  293:         if (-e $filename) {
  294: 	    $temp_file = Apache::File->new($filename); 
  295: 	} else {
  296: 	    $temp_file = Apache::File->new('>>'.$filename); 
  297: 	}
  298: 	my @due_file_content = <$temp_file>;
  299: 	my $due_file_content = $due_file_content[$#due_file_content];
  300:         chomp $due_file_content;
  301:         if ($due_file_content ne $duedate) {	    
  302: 	$temp_file = Apache::File->new('>'.$filename); 
  303: 	    print $temp_file "$duedate\n";
  304: 	    if($duedate eq 'Wed 31 Dec 1969 07:00:00 PM EDT') {$duedate = '';}	    
  305: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
  306: 		$result .= '\begin{document} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm';	
  307: 	    } else {
  308: 		$result .= '\parbox{\minipagewidth}{\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
  309: 	    } 
  310: 	} else {
  311: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
  312: 		$result .= '\begin{document} \noindent \vskip 1 mm';	
  313: 	    } else {
  314: 		$result .= '\parbox{\minipagewidth}{\vskip 1mm \\\\\\\\';
  315: 	    } 
  316: 	}
  317:     }
  318:   } elsif ($target eq 'edit') {
  319:     $result.=$head_tag_start."</head>".$body_tag_start.
  320:       '<form name="lonhomework" method="POST" action="'.
  321: 	$ENV{'request.uri'}.'">
  322:        <input type="hidden" name="submitted" value="edit" />
  323:        <input type="hidden" name="problemmode" value="Edit" />
  324:        <input type="submit" name="problemmode" value="Discard Edits and View" />
  325:        <input type="submit" name="problemmode" value="EditXML" />
  326:        <input type="submit" name="Undo" value="undo" /> <hr />
  327:        <input type="submit" name="submit" value="Submit Changes" />
  328:        <input type="submit" name="submit" value="Submit Changes and View" /><br />
  329:       ';
  330:     my $temp=&Apache::edit::insertlist($target,$token);
  331:     $result.=$temp;
  332:   } elsif ($target eq 'modified') {
  333:     $result=$token->[4];
  334:     $result.=&Apache::edit::handle_insert();
  335:   } else {
  336:     # page_start returned a starting result, delete it if we don't need it
  337:     $result = '';
  338:   }
  339:   return $result;
  340: }
  341: 
  342: sub end_problem {
  343:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  344:   my $result='';
  345:   my $status=$Apache::inputtags::status['-1'];
  346:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ) {
  347:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
  348:       # if part is zero, no <part>s existed, so we need to the grading
  349:       &Apache::inputtags::grade;
  350:     } elsif ( $target eq 'web' && $Apache::inputtags::part eq '0' && 
  351: 	      $status ne 'UNCHECKEDOUT') {
  352:       # if part is zero, no <part>s existed, so we need show the current 
  353:       # grading status
  354:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
  355:     }
  356:     if (
  357: 	($target eq 'web' && ($ENV{'request.state'} ne 'construct')) ||
  358: 	($target eq 'answer')
  359:        ) {
  360:       if ($status eq 'CAN_ANSWER') {
  361: 	$result.="</form></body>\n"; 
  362:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
  363: 	       $status eq 'UNCHECKEDOUT' ) {
  364: 	$result.="</body>\n";
  365:       }
  366:       $result.=&Apache::lonxml::xmlend();
  367:     }
  368:     if ($target eq 'grade') { 
  369:       &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  370:       &finalize_storage();
  371:     }
  372:   } elsif ($target eq 'meta') {
  373:     if ($Apache::inputtags::part eq '0') {
  374:       $result=&Apache::response::mandatory_part_meta;
  375:     }
  376:   } elsif ($target eq 'edit') {
  377:     &Apache::lonxml::debug("in end_problem with $target, edit");
  378:     $result ='<br /><input type="submit" name="submit" value="Submit Changes and Edit" />';
  379:     $result.='<input type="submit" name="submit" value="Submit Changes and View" />';
  380:   } elsif ($target eq 'tex') {
  381:       $result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
  382:       if (not $ENV{'request.symb'} =~ m/\.page_/) {
  383: 	  $result .= '\end{document} ';
  384:       } else {
  385: 	  $result .= '} ';
  386:       }
  387:   }
  388:   return $result;
  389: }
  390: 
  391: sub start_library {
  392:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  393:   my ($result,$head_tag_start,$body_tag_start)=
  394:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  395:   if ($target eq 'edit') {
  396:     $result.=$head_tag_start."</head>".$body_tag_start.
  397:       '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
  398:        <input type="hidden" name="submitted" value="edit" />
  399:        <input type="hidden" name="problemmode" value="Edit" />
  400:        <input type="submit" name="problemmode" value="View" />
  401:        <input type="submit" name="Undo" value="undo" /> <hr />
  402:       ';
  403:     my $temp=&Apache::edit::insertlist($target,$token);
  404:     $result.=$temp;
  405:     return $result;
  406:   }
  407:   if ($target eq 'modified') {
  408:     $result=$token->[4];
  409:     $result.=&Apache::edit::handle_insert();
  410:     return $result;
  411:   }
  412:   return '';
  413: }
  414: 
  415: sub end_library {
  416:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  417:   my $result='';
  418:   if ($target eq 'edit') {
  419:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
  420:   }
  421:   return $result;
  422: }
  423: 
  424: sub start_block {
  425:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  426: 
  427:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  428:     my $code = @$parstack[$#$parstack];
  429:     $code =~ s/\"//g;
  430:     $code .=';return $condition;';
  431:     #  print "<br />$code<br />";
  432:     my $result = &Apache::run::run($code,$safeeval);
  433:     &Apache::lonxml::debug("block :$code: returned :$result:");
  434:     if ( ! $result ) { 
  435:       my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  436:       &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
  437:     }
  438:   }
  439:   return "";
  440: }
  441: 
  442: sub end_block {
  443:   return '';
  444: }
  445: 
  446: sub start_while {
  447:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  448: 
  449:   my $code = @$parstack[$#$parstack];
  450:   $code =~ s/\"//g;
  451:   $code .=';return $condition;';
  452: 
  453:   push( @Apache::structuretags::whileconds, $code); 
  454:   my $result = &Apache::run::run($code,$safeeval);
  455:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  456:   push( @Apache::structuretags::whilebody, $bodytext);
  457:   if ( $result ) { 
  458:     &Apache::lonxml::newparser($parser,\$bodytext);
  459:   }
  460:   return "";
  461: }
  462: 
  463: sub end_while {
  464:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  465:   my $code = pop @Apache::structuretags::whileconds;
  466:   my $bodytext = pop @Apache::structuretags::whilebody;
  467:   my $result = &Apache::run::run($code,$safeeval);
  468:   if ( $result ) { 
  469:     &Apache::lonxml::newparser($parser,\$bodytext);
  470:   } 
  471:   return "";
  472: }
  473: 
  474: # <randomlist> 
  475: #  <tag1>..</tag1>
  476: #  <tag2>..</tag2>
  477: #  <tag3>..</tag3>
  478: #  ... 
  479: # </randomlist>
  480: sub start_randomlist {
  481:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  482:   my $result;
  483:   if ($target eq 'answer' || $target eq 'grade' || $target eq 'web') {
  484:     my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  485:     my $b_parser= HTML::TokeParser->new(\$body);
  486:     my $b_tok;
  487:     my @randomlist;
  488:     my $list_item;
  489:     while($b_tok = $b_parser->get_token() ) {
  490:       if($b_tok->[0] eq 'S') { # start tag
  491: 	# get content of the tag until matching end tag
  492: 	# get all text upto the matching tag
  493: 	# and push the content into @randomlist
  494: 	$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  495: 	$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  496: 	push(@randomlist,$list_item);
  497: 	#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
  498:       }
  499:       if($b_tok->[0] eq 'T') { # text
  500: 	# what to do with text in between tags?
  501: 	#  print "<b>TEXT $b_tok->[1]</b><br />";
  502:       }
  503:       # if($b_tok->[0] eq 'E') { # end tag, should not happen
  504:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
  505:       # }
  506:     }
  507:     my @idx_arr = (0 .. $#randomlist);
  508:     &Apache::structuretags::shuffle(\@idx_arr);
  509:     my $bodytext = '';
  510:     my $show=$#randomlist;
  511:     my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  512:     $showarg--;
  513:     if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  514:     for(0 .. $show) {
  515:       $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  516:     }
  517:     &Apache::lonxml::newparser($parser,\$bodytext);
  518:   } elsif ($target eq 'edit' ) {
  519:     $result .= &Apache::edit::tag_start($target,$token);
  520:     $result .= &Apache::edit::text_arg('Maximum Tags to Show:','show',$token,5);
  521:     $result .= &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  522:   } elsif ($target eq 'modified' ) {
  523:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  524: 						 'show');
  525:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  526:   }
  527:   return $result;
  528: }
  529: 
  530: sub shuffle {
  531:     my $a=shift;
  532:     my $i;
  533:     if (defined(@$a)) {
  534:       &Apache::response::setrandomnumber();
  535:       for($i=@$a;--$i;) {
  536: 	my $j=int rand($i+1);
  537: 	next if $i == $j;
  538: 	@$a[$i,$j] = @$a[$j,$i];
  539:       }
  540:     }
  541: }
  542: 
  543: sub end_randomlist {
  544:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  545:   my $result;
  546:   if ($target eq 'edit' ) {
  547:     $result=&Apache::edit::tag_end($target,$token,'End Randomly Parsed Block');
  548:   }
  549:   return $result;
  550: }
  551: 
  552: sub start_part {
  553:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  554:   my $result='';
  555:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  556:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  557:   $Apache::inputtags::part=$id;
  558:   @Apache::inputtags::responselist = ();
  559:   @Apache::inputtags::previous=();
  560:   if ($target eq 'meta') {
  561:     return &Apache::response::mandatory_part_meta;
  562:   } elsif ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  563:     my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  564:     push (@Apache::inputtags::status,$status);
  565:     my $expression='$external::datestatus="'.$status.'";';
  566:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  567:     &Apache::run::run($expression,$safeeval);
  568:     if ( $status eq 'CLOSED' ) {
  569:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  570:       if ( $target eq "web" ) {
  571: 	$result="<br />Part is not open to be viewed. It $accessmsg<br />";
  572:       } elsif ( $target eq 'tex' ) {
  573: 	$result="\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\";
  574:       }
  575:     } else {
  576:       if ($target eq 'tex') {
  577: 	$result='\vskip 0 mm';
  578:       }
  579:     }
  580:   }
  581:   return $result;
  582: }
  583: 
  584: sub end_part {
  585:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  586:   &Apache::lonxml::debug("in end_part $target ");
  587:   my $status=$Apache::inputtags::status['-1'];
  588:   pop @Apache::inputtags::status;
  589:   if ( $target eq 'meta' ) { return ''; }
  590:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
  591:     return &Apache::inputtags::grade;
  592:   }
  593:   if ($target eq 'web') {
  594:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  595:   }
  596:   return '';
  597: }
  598: 
  599: sub start_preduedate {
  600:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  601:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  602:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  603: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  604:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  605:     }
  606:   }
  607:   return '';
  608: }
  609: 
  610: sub end_preduedate {
  611:   return '';
  612: }
  613: 
  614: sub start_postanswerdate {
  615:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  616:   if ($target eq 'web' || $target eq 'grade') {
  617:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  618:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  619:     }
  620:   }
  621:   return '';
  622: }
  623: 
  624: sub end_postanswerdate {
  625:   return '';
  626: }
  627: 
  628: sub start_notsolved {
  629:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  630:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  631:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  632:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  633:     if ($gradestatus =~ /^correct/) {
  634:       &Apache::lonxml::debug("skipping");
  635:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  636:     }
  637:   }
  638:   return '';
  639: }
  640: 
  641: sub end_notsolved {
  642:   return '';
  643: }
  644: 
  645: sub start_solved {
  646:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  647:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  648:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  649:     if ($gradestatus !~ /^correct/) {
  650:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  651:     }
  652:   }
  653:   return '';
  654: }
  655: 
  656: sub end_solved {
  657:   return '';
  658: }
  659: 
  660: sub start_startouttext {
  661:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  662:   my @result=(''.'');
  663:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  664:   return (@result);
  665: }
  666: sub end_startouttext {
  667:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  668:   my $result='';
  669:   my $text='';
  670: 
  671:   if ($target eq 'edit') {
  672:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[-1]);
  673:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  674: <td>Delete:".
  675:   &Apache::edit::deletelist($target,$token)
  676:     ."</td>
  677: <td>".
  678:   &Apache::edit::insertlist($target,$token).
  679:     &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n".
  680:       &Apache::edit::editfield($token->[1],$text,"",50,4);
  681:   }
  682:   if ($target eq 'modified') {
  683:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  684:     $result='<startouttext />'.&Apache::edit::modifiedfield();
  685:   }
  686:   if ($target eq 'tex') {
  687:       $result .= '\noindent ';
  688:   }
  689:   return $result;
  690: }
  691: sub start_endouttext {
  692:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  693:   my $result='';
  694:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  695:   if ($target eq "modified") { $result='<endouttext />'; }
  696:   return $result;
  697: }
  698: sub end_endouttext {
  699:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  700:   my @result=('','');
  701:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  702:   return (@result);
  703: }
  704: sub delete_startouttext {
  705:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  706: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  707:   my $text=$$parser['-1']->get_text("/endouttext");
  708:   my $ntoken=$$parser['-1']->get_token();
  709:   &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
  710:   &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
  711:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
  712:   # they did the last time round
  713:   &Apache::lonxml::increasedepth($ntoken);
  714:   &Apache::lonxml::decreasedepth($ntoken);
  715:   return 1;
  716: }
  717: 
  718: 1;
  719: __END__

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