File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.97: download - view: text, annotated - select for diffs
Fri Jun 21 20:27:54 2002 UTC (21 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- CLOSES BUG#188, adds save and view button, and makes it clear that
  the view button will discard all changes.

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

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