File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.132: download - view: text, annotated - select for diffs
Wed Nov 6 23:05:02 2002 UTC (21 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fixes most of BUG#793
- added new global var that controls wheter syle files defs are used
- use new tag <LONCAPA_INTERNAL_TURN_STYLE_ON /> to turn style def use back on
- removed recurse sub completely
- get_all_text now understands the Parser stack

Bugs:
- get_all_unbalanced_text needs to use parser stack
- get_all_text uses need to pass entrie parser stack
- style defs that have a tag that ends in a different style def can't use get_all_text


Need To Test:
- pars stack pops in end tags looking for start tags in get_all_text

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.132 2002/11/06 23:05:02 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:     &Apache::lonxml::debug("Got $status");
  294:     if (( $status eq 'CLOSED' ) ||
  295:         ( $status eq 'UNCHECKEDOUT') ||
  296:         ( $status eq 'BANNED')) {
  297:       my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
  298:       if ( $target eq "web" ) {
  299: 	$result.= $head_tag_start.'</head>';
  300:         my $msg=$body_tag_start.
  301: 	    '<h1>Not open to be viewed</h1>';
  302:         if ($status eq 'CLOSED') {
  303: 	    $msg.='The problem '.$accessmsg;
  304: 	} elsif ($status eq 'UNCHECKEDOUT') {
  305:             $msg.=&checkout_msg;
  306:         }
  307: 	$result.=$msg.'<br />';
  308:       } elsif ($target eq 'tex') {
  309: 	  $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 ";
  310:       } 
  311:     } elsif ($target eq 'web') {
  312:       my $name= &get_resource_name($parstack,$safeeval);
  313:       if ($status eq 'CAN_ANSWER') {
  314: 	# create a page header and exit
  315: 	$result.="$head_tag_start<title>$name</title></head>
  316:               $body_tag_start \n $form_tag_start".	
  317: 		'<input type="hidden" name="submitted" value="yes" />';
  318: 	if ($ENV{'request.state'} eq "construct") {
  319: 	  $result.= &problem_web_to_edit_header($rndseed);
  320: 	}
  321: 	# if we are viewing someone else preserve that info
  322: 	if (defined $ENV{'form.grade_symb'}) {
  323: 	  foreach my $field ('symb','courseid','domain','username') {
  324: 	    $result .= '<input type="hidden" name="grade_'.$field.
  325: 	      '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
  326: 	  }
  327: 	}
  328:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
  329: 	       || $status eq 'CLOSED') {
  330: 	$result.=$head_tag_start.
  331: 	  "<title>$name</title></head>\n$body_tag_start\n";
  332:       }
  333:     } elsif ($target eq 'tex') {
  334: 	my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  335: 	if ($name eq '') { 
  336: 	    $name=&Apache::lonnet::EXT('resource.title');
  337: 	    if ($name eq 'con_lost') { $name = ''; }
  338: 	}
  339: 	$Apache::lonhomework::name=$name;
  340: 	my $id = $Apache::inputtags::part;
  341: 	my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
  342: 	$duedate = POSIX::strftime("%c",localtime($duedate));
  343: 	my $temp_file;
  344: 	my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
  345:         if (-e $filename) {
  346: 	    $temp_file = Apache::File->new($filename); 
  347: 	} else {
  348: 	    $temp_file = Apache::File->new('>>'.$filename); 
  349: 	}
  350: 	my @due_file_content = <$temp_file>;
  351: 	my $due_file_content = $due_file_content[$#due_file_content];
  352:         chomp $due_file_content;
  353:         if ($due_file_content ne $duedate) {	    
  354: 	$temp_file = Apache::File->new('>'.$filename); 
  355: 	    print $temp_file "$duedate\n";	    
  356: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
  357: 		if(not $duedate=~m/1969/) {
  358: 		    $result .= '\begin{document} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm\noindent \begin{minipage}{\textwidth}';	
  359: 		} else {
  360: 		    $result .= '\begin{document} \noindent \vskip 1 mm \noindent\begin{minipage}{\textwidth}';
  361: 		}
  362: 	    } else {
  363: 		$result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
  364: 	    } 
  365: 	} else {
  366: 	    if (not $ENV{'request.symb'} =~ m/\.page_/) {
  367: 		$result .= '\begin{document} \noindent \vskip 1 mm\noindent\begin{minipage}{\textwidth}';	
  368: 	    } else {
  369: 		$result .= '\vskip 1mm \\\\\\\\';
  370: 	    } 
  371: 	}
  372:     }
  373:   } elsif ($target eq 'edit') {
  374:     $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  375:       &problem_edit_header();
  376:     my $temp=&Apache::edit::insertlist($target,$token);
  377:     $result.=$temp;
  378:   } elsif ($target eq 'modified') {
  379:     $result=$token->[4];
  380:     $result.=&Apache::edit::handle_insert();
  381:   } else {
  382:     # page_start returned a starting result, delete it if we don't need it
  383:     $result = '';
  384:   }
  385:   return $result;
  386: }
  387: 
  388: sub end_problem {
  389:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  390:   my $result='';
  391:   my $status=$Apache::inputtags::status['-1'];
  392:   if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' || $target eq 'tex') {
  393:     if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
  394:       # if part is zero, no <part>s existed, so we need to the grading
  395:       &Apache::inputtags::grade;
  396:     } elsif ( ($target eq 'web' || $target eq 'tex') && $Apache::inputtags::part eq '0' && 
  397: 	      $status ne 'UNCHECKEDOUT') {
  398:       # if part is zero, no <part>s existed, so we need show the current 
  399:       # grading status
  400:       my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
  401:       if ($Apache::lonhomework::type ne 'exam') {$result.= $gradestatus;}
  402:     }
  403:     if (
  404: 	(($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
  405: 	($target eq 'answer') || ($target eq 'tex')
  406:        ) {
  407:       if ($status eq 'CAN_ANSWER') {
  408: 	  if ($target ne 'tex') {
  409: 	      $result.="</form></body>\n";
  410: 	  } 
  411:       } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
  412: 	       $status eq 'UNCHECKEDOUT' ) {
  413: 	  if ($target ne 'tex') {
  414: 	      $result.="</body>\n";
  415: 	  }
  416:       }
  417:       if ($target ne 'tex') {
  418: 	  $result.=&Apache::lonxml::xmlend();
  419:       } else {
  420: 	      $result .= '\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
  421: 	      if (not $ENV{'request.symb'} =~ m/\.page_/) {
  422: 		  $result .= '\end{minipage}\end{document} ';
  423: 	      } else {
  424: 		  $result .= '';
  425: 	      }
  426:       }
  427:     }
  428:     if ($target eq 'grade') { 
  429:       &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  430:       &finalize_storage();
  431:     }
  432:   } elsif ($target eq 'meta') {
  433:     if ($Apache::inputtags::part eq '0') {
  434:       $result=&Apache::response::mandatory_part_meta;
  435:     }
  436:   } elsif ($target eq 'edit') {
  437:     &Apache::lonxml::debug("in end_problem with $target, edit");
  438:     $result = &problem_edit_footer();
  439:   } 
  440:   return $result;
  441: }
  442: 
  443: 
  444: sub start_library {
  445:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  446:   my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
  447: 
  448:   if ($target eq 'edit') {
  449:     ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  450:       &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  451:     $result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  452:       &problem_edit_header();
  453:     my $temp=&Apache::edit::insertlist($target,$token);
  454:     $result.=$temp;
  455:   } elsif ($target eq 'modified') {
  456:     $result=$token->[4];
  457:     $result.=&Apache::edit::handle_insert();
  458:   } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  459: 	   $ENV{'request.state'} eq "construct" ) {
  460:     ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  461:       &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  462:     my $name=&get_resource_name($parstack,$safeeval);
  463:     my $rndseed=&setup_rndseed($safeeval);
  464:     $result.="$head_tag_start<title>$name</title></head>
  465:               $body_tag_start \n $form_tag_start".	
  466: 		'<input type="hidden" name="submitted" value="yes" />';
  467:     $result.=&problem_web_to_edit_header($rndseed);
  468:   }
  469:   return $result;
  470: }
  471: 
  472: sub end_library {
  473:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  474:   my $result='';
  475:   if ($target eq 'edit') {
  476:     $result=&problem_edit_footer();
  477:   } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  478: 	   $ENV{'request.state'} eq "construct") {
  479:     $result.='</form></body>'.&Apache::lonxml::xmlend();
  480:   }
  481:   return $result;
  482: }
  483: 
  484: sub start_block {
  485:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  486: 
  487:     my $result;
  488: 
  489:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || 
  490: 	$target eq 'tex' || $target eq 'analyze') {
  491: 	my $code = @$parstack[$#$parstack];
  492: 	if ($code) {
  493: 	    $code =~ s/\"//g;
  494: 	    $code .=';return $condition;';
  495: 	    $result = &Apache::run::run($code,$safeeval);
  496: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
  497: 	} else {
  498: 	    $result='1';
  499: 	}
  500: 	if ( ! $result ) {
  501: 	    my $skip=&Apache::lonxml::get_all_text("/block",$$parser[-1]);
  502: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  503: 	}
  504: 	$result='';
  505:     } elsif ($target eq 'edit') {
  506: 	$result .=&Apache::edit::tag_start($target,$token);
  507: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
  508: 					  $token,40);
  509: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  510:     } elsif ($target eq 'modified') {
  511: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  512: 						     $safeeval,'condition');
  513: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  514:     }
  515:     return $result;
  516: }
  517: 
  518: sub end_block {
  519:   return '';
  520: }
  521: 
  522: sub start_while {
  523:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  524: 
  525:   my $code = @$parstack[$#$parstack];
  526:   $code =~ s/\"//g;
  527:   $code .=';return $condition;';
  528: 
  529:   push( @Apache::structuretags::whileconds, $code); 
  530:   my $result = &Apache::run::run($code,$safeeval);
  531:   my $bodytext=$$parser[$#$parser]->get_text("/while");
  532:   push( @Apache::structuretags::whilebody, $bodytext);
  533:   if ( $result ) { 
  534:     &Apache::lonxml::newparser($parser,\$bodytext);
  535:   }
  536:   return "";
  537: }
  538: 
  539: sub end_while {
  540:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  541:   my $code = pop @Apache::structuretags::whileconds;
  542:   my $bodytext = pop @Apache::structuretags::whilebody;
  543:   my $result = &Apache::run::run($code,$safeeval);
  544:   if ( $result ) { 
  545:     &Apache::lonxml::newparser($parser,\$bodytext);
  546:   } 
  547:   return "";
  548: }
  549: 
  550: # <randomlist show="1"> 
  551: #  <tag1>..</tag1>
  552: #  <tag2>..</tag2>
  553: #  <tag3>..</tag3>
  554: #  ... 
  555: # </randomlist>
  556: sub start_randomlist {
  557:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  558:   my $result;
  559:   if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
  560:       $target eq 'tex' || $target eq 'analyze') {
  561:     my $body= &Apache::lonxml::get_all_text("/randomlist",$$parser[$#$parser]);
  562:     my $b_parser= HTML::TokeParser->new(\$body);
  563:     my $b_tok;
  564:     my @randomlist;
  565:     my $list_item;
  566:     while($b_tok = $b_parser->get_token() ) {
  567:       if($b_tok->[0] eq 'S') { # start tag
  568: 	# get content of the tag until matching end tag
  569: 	# get all text upto the matching tag
  570: 	# and push the content into @randomlist
  571: 	$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],$b_parser);
  572: 	$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  573: 	push(@randomlist,$list_item);
  574: 	#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4], $list_item</b>";
  575:       }
  576:       if($b_tok->[0] eq 'T') { # text
  577: 	# what to do with text in between tags?
  578: 	#  print "<b>TEXT $b_tok->[1]</b><br />";
  579:       }
  580:       # if($b_tok->[0] eq 'E') { # end tag, should not happen
  581:       #  print "<b>END-TAG $b_tok->[1]</b><br />";
  582:       # }
  583:     }
  584:     my @idx_arr = (0 .. $#randomlist);
  585:     &Apache::structuretags::shuffle(\@idx_arr);
  586:     my $bodytext = '';
  587:     my $show=$#randomlist;
  588:     my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  589:     $showarg--;
  590:     if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  591:     for(0 .. $show) {
  592:       $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  593:     }
  594:     &Apache::lonxml::newparser($parser,\$bodytext);
  595:   } elsif ($target eq 'edit' ) {
  596:     $result .= &Apache::edit::tag_start($target,$token);
  597:     $result .= &Apache::edit::text_arg('Maximum Tags to Show:','show',$token,5);
  598:     $result .= &Apache::edit::end_row().&Apache::edit::start_spanning_row();
  599:   } elsif ($target eq 'modified' ) {
  600:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  601: 						 'show');
  602:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  603:   }
  604:   return $result;
  605: }
  606: 
  607: sub shuffle {
  608:     my $a=shift;
  609:     my $i;
  610:     if (defined(@$a)) {
  611:       &Apache::response::setrandomnumber();
  612:       for($i=@$a;--$i;) {
  613: 	my $j=int(&Math::Random::random_uniform() * ($i+1));
  614: 	next if $i == $j;
  615: 	@$a[$i,$j] = @$a[$j,$i];
  616:       }
  617:     }
  618: }
  619: 
  620: sub end_randomlist {
  621:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  622:   my $result;
  623:   if ($target eq 'edit' ) {
  624:     $result=&Apache::edit::tag_end($target,$token,'End Randomly Parsed Block');
  625:   }
  626:   return $result;
  627: }
  628: 
  629: sub start_part {
  630:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  631:   my $result='';
  632:   my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  633:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  634:   $Apache::inputtags::part=$id;
  635:   @Apache::inputtags::responselist = ();
  636:   @Apache::inputtags::previous=();
  637:   if ($target eq 'meta') {
  638:     return &Apache::response::mandatory_part_meta;
  639:   } elsif ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  640:     my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  641:     push (@Apache::inputtags::status,$status);
  642:     my $expression='$external::datestatus="'.$status.'";';
  643:     $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  644:     &Apache::run::run($expression,$safeeval);
  645:     if ( $status eq 'CLOSED' ) {
  646:       my $bodytext=&Apache::lonxml::get_all_text("/part",$$parser[$#$parser]);
  647:       if ( $target eq "web" ) {
  648: 	$result="<br />Part is not open to be viewed. It $accessmsg<br />";
  649:       } elsif ( $target eq 'tex' ) {
  650: 	$result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
  651:       }
  652:     } else {
  653:       if ($target eq 'tex') {
  654: 	if ($$tagstack[-2] ne 'problem') {
  655: 	  $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
  656: 	}
  657:       }
  658:     }
  659:   }
  660:   return $result;
  661: }
  662: 
  663: sub end_part {
  664:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  665:   &Apache::lonxml::debug("in end_part $target ");
  666:   my $status=$Apache::inputtags::status['-1'];
  667:   pop @Apache::inputtags::status;
  668:   if ( $target eq 'meta' ) { return ''; }
  669:   if ( $target eq 'grade' && $status eq 'CAN_ANSWER') {
  670:     return &Apache::inputtags::grade;
  671:   }
  672:   if ($target eq 'web' || $target eq 'tex' ) {
  673:     my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
  674:     if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
  675:     return $gradestatus;
  676:   }
  677:   return '';
  678: }
  679: 
  680: sub start_preduedate {
  681:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  682:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  683:     if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  684: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' ) {
  685:       &Apache::lonxml::get_all_text("/preduedate",$$parser[$#$parser]);
  686:     }
  687:   }
  688:   return '';
  689: }
  690: 
  691: sub end_preduedate {
  692:   return '';
  693: }
  694: 
  695: sub start_postanswerdate {
  696:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  697:   if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  698:     if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  699:       &Apache::lonxml::get_all_text("/postanswerdate",$$parser[$#$parser]);
  700:     }
  701:   } elsif ($target eq 'tex') {
  702:       return '\vskip 0 mm \noindent';
  703:   }
  704:   return '';
  705: }
  706: 
  707: sub end_postanswerdate {
  708:   return '';
  709: }
  710: 
  711: sub start_notsolved {
  712:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  713:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  714:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  715:     &Apache::lonxml::debug("not solved has :$gradestatus:");
  716:     if ($gradestatus =~ /^correct/) {
  717:       &Apache::lonxml::debug("skipping");
  718:       &Apache::lonxml::get_all_text("/notsolved",$$parser[$#$parser]);
  719:     }
  720:   }
  721:   return '';
  722: }
  723: 
  724: sub end_notsolved {
  725:   return '';
  726: }
  727: 
  728: sub start_solved {
  729:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  730:   if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  731:     my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  732:     if ($gradestatus !~ /^correct/) {
  733:       &Apache::lonxml::get_all_text("/solved",$$parser[$#$parser]);
  734:     }
  735:   }
  736:   return '';
  737: }
  738: 
  739: sub end_solved {
  740:   return '';
  741: }
  742: 
  743: sub start_startouttext {
  744:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  745:   my @result=(''.'');
  746:   if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
  747:   return (@result);
  748: }
  749: sub end_startouttext {
  750:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  751:   my $result='';
  752:   my $text='';
  753: 
  754:   if ($target eq 'edit') {
  755:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser[-1]);
  756:     $result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
  757: <td>Delete:".
  758:   &Apache::edit::deletelist($target,$token)
  759:     ."</td>
  760: <td>".
  761:   &Apache::edit::insertlist($target,$token).
  762:     &Apache::edit::end_row().&Apache::edit::start_spanning_row()."\n".
  763:       &Apache::edit::editfield($token->[1],$text,"",80,4);
  764:   }
  765:   if ($target eq 'modified') {
  766:     $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  767:     $result='<startouttext />'.&Apache::edit::modifiedfield();
  768:   }
  769:   if ($target eq 'tex') {
  770:       $result .= '\noindent ';
  771:   }
  772:   return $result;
  773: }
  774: sub start_endouttext {
  775:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  776:   my $result='';
  777:   if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
  778:   if ($target eq "modified") { 
  779:      $result='<endouttext />'.
  780:      &Apache::edit::handle_insertafter('startouttext'); }
  781:   return $result;
  782: }
  783: sub end_endouttext {
  784:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  785:   my @result=('','');
  786:   if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
  787:   return (@result);
  788: }
  789: sub delete_startouttext {
  790:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  791: #  my $text=&Apache::lonxml::get_all_text("endouttext",$$parser['-1']);
  792:   my $text=$$parser['-1']->get_text("/endouttext");
  793:   my $ntoken=$$parser['-1']->get_token();
  794:   &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
  795:   &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
  796:   # Deleting 2 parallel tag pairs, but we need the numbers later to look like 
  797:   # they did the last time round
  798:   &Apache::lonxml::increasedepth($ntoken);
  799:   &Apache::lonxml::decreasedepth($ntoken);
  800:   return 1;
  801: }
  802: 
  803: 1;
  804: __END__

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