File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.131: download - view: text, annotated - select for diffs
Wed Nov 6 22:58:36 2002 UTC (21 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixing up the block edit code should be pretty

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

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