File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.139: download - view: text, annotated - select for diffs
Mon Nov 25 21:19:45 2002 UTC (21 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- end_part was poping the status stack to soon

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

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