File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.98: download - view: text, annotated - select for diffs
Tue Jun 25 14:50:53 2002 UTC (21 years, 10 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
corrected to take into account that resources
take care of themselves.

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.98 2002/06/25 14:50:53 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: 
   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' || $target eq 'tex') {
  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:   } elsif ($target eq 'tex') {
  287:       my $id = $Apache::inputtags::part;
  288:       my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
  289:       $duedate = POSIX::strftime("%c",localtime($duedate));
  290:       if (not $ENV{'request.symb'} =~ m/\.page_/) {
  291: 	  $result .= '\begin{document} \noindent\fbox{Due date: '.$duedate.'} \\vskip 1 mm';	  
  292:       } else {
  293: 	  $result .= '\begin{minipage}{\minipagewidth} ';
  294:       }
  295:   }
  296:   } elsif ($target eq 'edit') {
  297:     $result.=$head_tag_start."</head>".$body_tag_start.
  298:       '<form name="lonhomework" method="POST" action="'.
  299: 	$ENV{'request.uri'}.'">
  300:        <input type="hidden" name="submitted" value="edit" />
  301:        <input type="hidden" name="problemmode" value="Edit" />
  302:        <input type="submit" name="problemmode" value="Discard Edits and View" />
  303:        <input type="submit" name="problemmode" value="EditXML" />
  304:        <input type="submit" name="Undo" value="undo" /> <hr />
  305:        <input type="submit" name="submit" value="Submit Changes" />
  306:        <input type="submit" name="submit" value="Submit Changes and View" /><br />
  307:       ';
  308:     my $temp=&Apache::edit::insertlist($target,$token);
  309:     $result.=$temp;
  310:   } elsif ($target eq 'modified') {
  311:     $result=$token->[4];
  312:     $result.=&Apache::edit::handle_insert();
  313:   } else {
  314:     # page_start returned a starting result, delete it if we don't need it
  315:     $result = '';
  316:   }
  317:   return $result;
  318: }
  319: 
  320: sub end_problem {
  321:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  322:   my $result='';
  323:   my $status=$Apache::inputtags::status['-1'];
  324:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ) {
  325:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
  326:       # if part is zero, no <part>s existed, so we need to the grading
  327:       &Apache::inputtags::grade;
  328:     } elsif ( $target eq 'web' && $Apache::inputtags::part eq '0' && 
  329: 	      $status ne 'UNCHECKEDOUT') {
  330:       # if part is zero, no <part>s existed, so we need show the current 
  331:       # grading status
  332:       $result.= &Apache::inputtags::gradestatus($Apache::inputtags::part);
  333:     }
  334:     if (
  335: 	($target eq 'web' && ($ENV{'request.state'} ne 'construct')) ||
  336: 	($target eq 'answer')
  337:        ) {
  338:       if ($status eq 'CAN_ANSWER') {
  339: 	$result.="</form></body>\n"; 
  340:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
  341: 	       $status eq 'UNCHECKEDOUT' ) {
  342: 	$result.="</body>\n";
  343:       }
  344:       $result.=&Apache::lonxml::xmlend();
  345:     }
  346:     if ($target eq 'grade') { 
  347:       &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  348:       &finalize_storage();
  349:     }
  350:   } elsif ($target eq 'meta') {
  351:     if ($Apache::inputtags::part eq '0') {
  352:       $result=&Apache::response::mandatory_part_meta;
  353:     }
  354:   } elsif ($target eq 'edit') {
  355:     &Apache::lonxml::debug("in end_problem with $target, edit");
  356:     $result ='<br /><input type="submit" name="submit" value="Submit Changes and Edit" />';
  357:     $result.='<input type="submit" name="submit" value="Submit Changes and View" />';
  358:   } elsif ($target eq 'tex') {
  359:       $result .= '\vskip 0.5mm\noindent\makebox[9.0cm][b]{\hrulefill}';
  360:       if (not $ENV{'request.symb'} =~ m/\.page_/) {
  361: 	  $result .= '\end{document} ';
  362:       } else {
  363: 	  $result .= '\end{minipage} ';
  364:       }
  365:   }
  366:   return $result;
  367: }
  368: 
  369: sub start_library {
  370:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  371:   my ($result,$head_tag_start,$body_tag_start)=
  372:     &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  373:   if ($target eq 'edit') {
  374:     $result.=$head_tag_start."</head>".$body_tag_start.
  375:       '<form name="lonhomework" method="POST" action="'.$ENV{'request.uri'}.'">
  376:        <input type="hidden" name="submitted" value="edit" />
  377:        <input type="hidden" name="problemmode" value="Edit" />
  378:        <input type="submit" name="problemmode" value="View" />
  379:        <input type="submit" name="Undo" value="undo" /> <hr />
  380:       ';
  381:     my $temp=&Apache::edit::insertlist($target,$token);
  382:     $result.=$temp;
  383:     return $result;
  384:   }
  385:   if ($target eq 'modified') {
  386:     $result=$token->[4];
  387:     $result.=&Apache::edit::handle_insert();
  388:     return $result;
  389:   }
  390:   return '';
  391: }
  392: 
  393: sub end_library {
  394:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  395:   my $result='';
  396:   if ($target eq 'edit') {
  397:     $result='<br /><input type="submit" name="submit" value="Submit Changes" />';
  398:   }
  399:   return $result;
  400: }
  401: 
  402: sub start_block {
  403:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  404: 
  405:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  406:     my $code = @$parstack[$#$parstack];
  407:     $code =~ s/\"//g;
  408:     $code .=';return $condition;';
  409:     #  print "<br />$code<br />";
  410:     my $result = &Apache::run::run($code,$safeeval);
  411:     &Apache::lonxml::debug("block :$code: returned :$result:");
  412:     if ( ! $result ) { 
  413:       my $skip=&Apache::lonxml::get_all_text("/block",$$parser[$#$parser]);
  414:       &Apache::lonxml::debug("skipping ahead :$skip: $$parser[$#$parser]");
  415:     }
  416:   }
  417:   return "";
  418: }
  419: 
  420: sub end_block {
  421:   return '';
  422: }
  423: 
  424: sub start_while {
  425:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  426: 
  427:   my $code = @$parstack[$#$parstack];
  428:   $code =~ s/\"//g;
  429:   $code .=';return $condition;';
  430: 
  431:   push( @Apache::structuretags::whileconds, $code); 
  432:   my $result = &Apache::run::run($code,$safeeval);
  433:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  434:   push( @Apache::structuretags::whilebody, $bodytext);
  435:   if ( $result ) { 
  436:     &Apache::lonxml::newparser($parser,\$bodytext);
  437:   }
  438:   return "";
  439: }
  440: 
  441: sub end_while {
  442:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  443:   my $code = pop @Apache::structuretags::whileconds;
  444:   my $bodytext = pop @Apache::structuretags::whilebody;
  445:   my $result = &Apache::run::run($code,$safeeval);
  446:   if ( $result ) { 
  447:     &Apache::lonxml::newparser($parser,\$bodytext);
  448:   } 
  449:   return "";
  450: }
  451: 
  452: # <randomlist> 
  453: #  <tag1>..</tag1>
  454: #  <tag2>..</tag2>
  455: #  <tag3>..</tag3>
  456: #  ... 
  457: # </randomlist>
  458: sub start_randomlist {
  459:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  460:   my $result;
  461:   if ($target eq 'answer' || $target eq 'grade' || $target eq 'web') {
  462:     my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  463:     my $b_parser= HTML::TokeParser->new(\$body);
  464:     my $b_tok;
  465:     my @randomlist;
  466:     my $list_item;
  467:     while($b_tok = $b_parser->get_token() ) {
  468:       if($b_tok->[0] eq 'S') { # start tag
  469: 	# get content of the tag until matching end tag
  470: 	# get all text upto the matching tag
  471: 	# and push the content into @randomlist
  472: 	$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  473: 	$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  474: 	push(@randomlist,$list_item);
  475: 	#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
  476:       }
  477:       if($b_tok->[0] eq 'T') { # text
  478: 	# what to do with text in between tags?
  479: 	#  print "<b>TEXT $b_tok->[1]</b><br />";
  480:       }
  481:       # if($b_tok->[0] eq 'E') { # end tag, should not happen
  482:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
  483:       # }
  484:     }
  485:     my @idx_arr = (0 .. $#randomlist);
  486:     &Apache::structuretags::shuffle(\@idx_arr);
  487:     my $bodytext = '';
  488:     my $show=$#randomlist;
  489:     my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  490:     $showarg--;
  491:     if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  492:     for(0 .. $show) {
  493:       $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  494:     }
  495:     &Apache::lonxml::newparser($parser,\$bodytext);
  496:   } elsif ($target eq 'edit' ) {
  497:     $result .= &Apache::edit::tag_start($target,$token);
  498:     $result .= &Apache::edit::text_arg('Maximum Tags to Show:','show',$token,5);
  499:     $result .= &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  500:   } elsif ($target eq 'modified' ) {
  501:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  502: 						 'show');
  503:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  504:   }
  505:   return $result;
  506: }
  507: 
  508: sub shuffle {
  509:     my $a=shift;
  510:     my $i;
  511:     if (defined(@$a)) {
  512:       &Apache::response::setrandomnumber();
  513:       for($i=@$a;--$i;) {
  514: 	my $j=int rand($i+1);
  515: 	next if $i == $j;
  516: 	@$a[$i,$j] = @$a[$j,$i];
  517:       }
  518:     }
  519: }
  520: 
  521: sub end_randomlist {
  522:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  523:   my $result;
  524:   if ($target eq 'edit' ) {
  525:     $result=&Apache::edit::tag_end($target,$token,'End Randomly Parsed Block');
  526:   }
  527:   return $result;
  528: }
  529: 
  530: sub start_part {
  531:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  532:   my $result='';
  533:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  534:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  535:   $Apache::inputtags::part=$id;
  536:   @Apache::inputtags::responselist = ();
  537:   @Apache::inputtags::previous=();
  538:   if ($target eq 'meta') {
  539:     return &Apache::response::mandatory_part_meta;
  540:   } elsif ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  541:     my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  542:     push (@Apache::inputtags::status,$status);
  543:     my $expression='$external::datestatus="'.$status.'";';
  544:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  545:     &Apache::run::run($expression,$safeeval);
  546:     if ( $status eq 'CLOSED' ) {
  547:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  548:       if ( $target eq "web" ) {
  549: 	$result="<br />Part is not open to be viewed. It $accessmsg<br />";
  550:       } elsif ( $target eq 'tex' ) {
  551: 	$result="\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\";
  552:       }
  553:     } else {
  554:       if ($target eq 'tex') {
  555: 	$result='\vskip 0 mm';
  556:       }
  557:     }
  558:   }
  559:   return $result;
  560: }
  561: 
  562: sub end_part {
  563:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  564:   &Apache::lonxml::debug("in end_part $target ");
  565:   my $status=$Apache::inputtags::status['-1'];
  566:   pop @Apache::inputtags::status;
  567:   if ( $target eq 'meta' ) { return ''; }
  568:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
  569:     return &Apache::inputtags::grade;
  570:   }
  571:   if ($target eq 'web') {
  572:     return &Apache::inputtags::gradestatus($Apache::inputtags::part);
  573:   }
  574:   return '';
  575: }
  576: 
  577: sub start_preduedate {
  578:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  579:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  580:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  581: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  582:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  583:     }
  584:   }
  585:   return '';
  586: }
  587: 
  588: sub end_preduedate {
  589:   return '';
  590: }
  591: 
  592: sub start_postanswerdate {
  593:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  594:   if ($target eq 'web' || $target eq 'grade') {
  595:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  596:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  597:     }
  598:   }
  599:   return '';
  600: }
  601: 
  602: sub end_postanswerdate {
  603:   return '';
  604: }
  605: 
  606: sub start_notsolved {
  607:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  608:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  609:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  610:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  611:     if ($gradestatus =~ /^correct/) {
  612:       &Apache::lonxml::debug("skipping");
  613:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  614:     }
  615:   }
  616:   return '';
  617: }
  618: 
  619: sub end_notsolved {
  620:   return '';
  621: }
  622: 
  623: sub start_solved {
  624:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  625:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer') {
  626:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  627:     if ($gradestatus !~ /^correct/) {
  628:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  629:     }
  630:   }
  631:   return '';
  632: }
  633: 
  634: sub end_solved {
  635:   return '';
  636: }
  637: 
  638: sub start_startouttext {
  639:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  640:   my @result=(''.'');
  641:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  642:   return (@result);
  643: }
  644: sub end_startouttext {
  645:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  646:   my $result='';
  647:   my $text='';
  648: 
  649:   if ($target eq 'edit') {
  650:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[-1]);
  651:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  652: <td>Delete:".
  653:   &Apache::edit::deletelist($target,$token)
  654:     ."</td>
  655: <td>".
  656:   &Apache::edit::insertlist($target,$token).
  657:     &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n".
  658:       &Apache::edit::editfield($token->[1],$text,"",50,4);
  659:   }
  660:   if ($target eq 'modified') {
  661:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  662:     $result='<startouttext />'.&Apache::edit::modifiedfield();
  663:   }
  664:   if ($target eq 'tex') {
  665:       $result .= '\noindent ';
  666:   }
  667:   return $result;
  668: }
  669: sub start_endouttext {
  670:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  671:   my $result='';
  672:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  673:   if ($target eq "modified") { $result='<endouttext />'; }
  674:   return $result;
  675: }
  676: sub end_endouttext {
  677:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  678:   my @result=('','');
  679:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  680:   return (@result);
  681: }
  682: sub delete_startouttext {
  683:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  684: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  685:   my $text=$$parser['-1']->get_text("/endouttext");
  686:   my $ntoken=$$parser['-1']->get_token();
  687:   &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
  688:   &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
  689:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
  690:   # they did the last time round
  691:   &Apache::lonxml::increasedepth($ntoken);
  692:   &Apache::lonxml::decreasedepth($ntoken);
  693:   return 1;
  694: }
  695: 
  696: 1;
  697: __END__

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