File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.126: download - view: text, annotated - select for diffs
Mon Oct 14 20:48:36 2002 UTC (21 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- start of analyzing a problem support

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

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