File:  [LON-CAPA] / loncom / homework / structuretags.pm
Revision 1.181: download - view: text, annotated - select for diffs
Sat Jun 7 04:08:43 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- remove </part> from bottm of part tag

    1: # The LearningOnline Network with CAPA 
    2: # definition of tags that give a structure to a document
    3: #
    4: # $Id: structuretags.pm,v 1.181 2003/06/07 04:08:43 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: use Apache::lonmenu;
   39: 
   40: BEGIN {
   41:     &Apache::lonxml::register('Apache::structuretags',('block','languageblock','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','startouttext','endouttext'));
   42: }
   43: 
   44: sub start_web {
   45:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   46:     my $bodytext=&Apache::lonxml::get_all_text("/web",$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);
   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,
   83: 					       $parstack,$parser,$safeeval);
   84: 	$head_tag_start='<head>'.&Apache::lonmenu::registerurl(undef,$target);
   85:     }
   86:     my $body_tag_start;
   87:     if (!defined($found{'body'})) {
   88: 	$body_tag_start='<body onLoad="'.&Apache::lonmenu::loadevents().'" '.
   89: 	    'onUnload="'.&Apache::lonmenu::unloadevents().'" ';
   90: 	my $background=&Apache::lonxml::get_param('background',$parstack,
   91: 						  $safeeval);
   92: 	if ($background) {
   93: 	    $Apache::lonxml::extlinks[$#Apache::lonxml::extlinks+1]=
   94: 		$background;
   95: 	    $body_tag_start.='background="'.$background.'" ';
   96: 	} else {
   97: 	    my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
   98: 						   $safeeval);
   99: 	    if ($bgcolor) {
  100: 		$body_tag_start.='bgcolor="'.$bgcolor.'" ';
  101: 	    } else {
  102: 		$body_tag_start.='bgcolor="#ffffff"';
  103: 	    }
  104: 	}
  105: 	$body_tag_start.='>'.&Apache::lonmenu::menubuttons(undef,$target,1);
  106: 	if ($target eq 'web' && $ENV{'request.state'} ne 'construct') {
  107: 	    my ($symb,undef,undef,undef,$publicuser)=
  108: 		&Apache::lonxml::whichuser();
  109: 	    if ($symb eq '' && !$publicuser) {
  110: 		my $help = &Apache::loncommon::help_open_topic("Ambiguous_Reference");
  111: 		$help="Browsing resource, all submissions are temporary.<br />";
  112: 		$body_tag_start.=$help;
  113: 	    }
  114: 	}
  115:     }
  116:     my $form_tag_start;
  117:     if (!defined($found{'form'})) {
  118: 	$form_tag_start='<form name="lonhomework" method="POST" action="'.
  119: 	    $ENV{'request.uri'}.'">';
  120:     }
  121:     return ($result,$head_tag_start,$body_tag_start,$form_tag_start);
  122: }
  123: 
  124: #use Time::HiRes();
  125: sub get_resource_name {
  126:     my ($parstack,$safeeval)=@_;
  127:     my $name=&Apache::lonnet::gettitle();
  128:     if ($name eq '') {
  129: 	$name=&Apache::lonnet::EXT('resource.title');
  130: 	if ($name eq 'con_lost') { $name = ''; }
  131:     }
  132:     $Apache::lonhomework::name=$name;
  133:     return $name;
  134: }
  135: 
  136: sub setup_rndseed {
  137:     my ($safeeval)=@_;
  138:     my $rndseed;
  139:     my ($symb)=&Apache::lonxml::whichuser();
  140:     if ($ENV{'request.state'} eq "construct" || $symb eq '') {
  141: 	$rndseed=$ENV{'form.rndseed'};
  142: 	if (!$rndseed) {
  143: 	    $rndseed=$Apache::lonhomework::history{'rndseed'};
  144: 	    if (!$rndseed) {
  145: 		$rndseed=time;
  146: 		$ENV{'form.rndseed'}=$rndseed;
  147: 	    }
  148: 	}
  149: 	if ($ENV{'form.resetdata'} eq 'New Problem Variation' ||
  150: 	    $ENV{'form.newrandomization'} eq 'New Randomization') {
  151: 	    $rndseed=time;
  152: 	    if ($rndseed eq $ENV{'form.rndseed'}) {
  153: 		srand($rndseed);
  154: 		$rndseed=int(rand(1000000000));
  155: 	    }
  156: 	    $ENV{'form.rndseed'}=$rndseed;
  157: 	    delete($ENV{'form.resetdata'});
  158: 	    delete($ENV{'form.newrandomization'});
  159: 	}
  160: 	&Apache::lonxml::debug("Setting rndseed to $rndseed");
  161: 	&Apache::run::run('$external::randomseed='.$rndseed.';',$safeeval);
  162:     }
  163:     return $rndseed;
  164: }
  165: 
  166: sub problem_edit_header {
  167:     return '<input type="hidden" name="submitted" value="edit" />
  168:        <input type="hidden" name="problemmode" value="Edit" />
  169:        <input type="submit" name="problemmode" value="Discard Edits and View" />
  170:        <input type="submit" name="problemmode" value="EditXML" />
  171:        <input type="submit" name="Undo" value="undo" /> <hr />
  172:        <input type="submit" name="submit" value="Submit Changes and Edit" />
  173:        <input type="submit" name="submit" value="Submit Changes and View" /><br /><p>&nbsp;</p><table border="0"><tr><td bgcolor="#DDDDDD">
  174:       ';
  175: }
  176: 
  177: sub problem_edit_footer {
  178:     return '</td></tr></table><br /><input type="submit" name="submit" value="Submit Changes and Edit" />
  179:     <input type="submit" name="submit" value="Submit Changes and View" />';
  180: }
  181: 
  182: sub problem_web_to_edit_header {
  183:     my ($rndseed)=@_;
  184:     my $result.='<input type="hidden" name="problemmode" value="View" />
  185:              <input type="submit" name="problemmode" value="Edit" />
  186:              <input type="submit" name="problemmode" value="EditXML" />
  187:              <input type="submit" name="newrandomization" value="New Randomization" />
  188:              <input type="submit" name="resetdata" value="Reset Submissions" />
  189:              <nobr><input type="submit" name="changerandseed" value="Change Random Seed To:" />
  190:               <input type="text" name="rndseed" width="10" value="'.
  191: 	       $rndseed.'"
  192:            onChange="javascript:document.lonhomework.changerandseed.click()" /></nobr>
  193:              <input type="checkbox" name="showallfoils" ';
  194:     if (defined($ENV{'form.showallfoils'})) { $result.='checked="on"'; }
  195:     $result.= ' />&nbsp;Show&nbsp;All&nbsp;Foils
  196:              <hr />';
  197:     my $numtoanalyze=$ENV{'form.numtoanalyze'};
  198:     if (!$numtoanalyze) { $numtoanalyze=20; }
  199:     $result.= '<input type="submit" name="problemmode" value="Calculate answers" /> for
  200:              <input type="text" name="numtoanalyze" value="'.
  201: 		 $numtoanalyze.'" size="5" /> versions of this problem.'.
  202: 		     &Apache::loncommon::help_open_topic("Analyze_Problem",
  203: 						     '',undef,undef,300).
  204: 		     '<hr />';
  205:     return $result;
  206: }
  207: 
  208: sub initialize_storage {
  209:     %Apache::lonhomework::results=();
  210:     %Apache::lonhomework::history=();
  211:     my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  212:     if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
  213: 	%Apache::lonhomework::history=
  214: 	    &Apache::lonnet::tmprestore($ENV{'request.uri'},'',$domain,$name);
  215: 	my ($temp)=keys %Apache::lonhomework::history ;
  216: 	&Apache::lonxml::debug("Return message of $temp");
  217:     } else {
  218: 	%Apache::lonhomework::history=
  219: 	    &Apache::lonnet::restore($symb,$courseid,$domain,$name);
  220:     }
  221:     #ignore error conditions
  222:     my ($temp)=keys %Apache::lonhomework::history ;
  223:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
  224: }
  225: 
  226: # -------------------------------------------------------------finalize_storage
  227: # Stores away the result has to a student's environment
  228: # checks form.grade_ for specific values, other wises stores
  229: # to the running users environment
  230: sub finalize_storage {
  231:     my $result;
  232:     my ($temp) = keys %Apache::lonhomework::results;
  233:     if ( $temp ne '' ) {
  234: 	my ($symb,$courseid,$domain,$name) = &Apache::lonxml::whichuser();
  235: 	if ($ENV{'request.state'} eq 'construct' || $symb eq '') {
  236: 	    $Apache::lonhomework::results{'rndseed'}=$ENV{'form.rndseed'};
  237: 	    $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
  238: 					$ENV{'request.uri'},'',$domain,$name);
  239: 	    &Apache::lonxml::debug('Construct Store return message:'.$result);
  240: 	} else {
  241: 	    $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
  242: 					    $symb,$courseid,$domain,$name);
  243: 	    &Apache::lonxml::debug('Store return message:'.$result);
  244: 	}
  245:     }
  246:     return $result;
  247: }
  248: 
  249: sub checkout_msg {
  250:     return (<<ENDCHECKOUT);
  251: <h2>The resource needs to be checked out</h2>
  252: As a resource gets checked out, a unique timestamped ID is given to it, and a
  253: permanent record is left in the system.<p />
  254: <font color=red>
  255: Checking out resources is subject to course policies, and may exclude future
  256: credit even if done erroneously.<p />
  257: </font>
  258: <form name="checkout" method="POST" action="$ENV{'request.uri'}">
  259: <input type="hidden" name="doescheckout" value="yes" />
  260: <input type="button" name="checkoutbutton" value="Check out Exam for Viewing" onClick="javascript:if (confirm('Check out Exam?')) { document.checkout.submit(); }" />
  261: </form>
  262: ENDCHECKOUT
  263: }
  264: 
  265: sub start_problem {
  266:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  267: 
  268:     $Apache::lonhomework::parsing_a_problem=1;
  269:     # meta is called from lonpublisher, which doesn't uses the normal
  270:     # lonhomework method of parsing the file which means that inputtags 
  271:     # won't get reset
  272:     if ( $Apache::inputtags::part ne '' && $target != 'meta' ) {
  273: 	&Apache::lonxml::error('Only one problem allowed in a .problem file');
  274: 	my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
  275: 	return '';
  276:     }
  277: #initialize globals
  278:     $Apache::inputtags::part='0';
  279:     @Apache::inputtags::partlist=('0');
  280:     @Apache::inputtags::responselist = ();
  281:     @Apache::inputtags::importlist = ();
  282:     @Apache::inputtags::previous=();
  283:     @Apache::inputtags::previous_version=();
  284:     $Apache::structuretags::printanswer='No';
  285:     @Apache::structuretags::whileconds=();
  286:     @Apache::structuretags::whilebody=();
  287:     @Apache::structuretags::whileline=();
  288:     $Apache::lonhomework::scantronmode=0;
  289:     $Apache::lonhomework::problemstatus=
  290: 	&Apache::lonnet::EXT('resource.0.problemstatus');
  291: 
  292:     if (defined($ENV{'scantron.maxquest'})) {
  293: 	$Apache::lonhomework::scantronmode=1;
  294:     }
  295: 
  296:     if ($target ne 'analyze') {
  297: 	&initialize_storage();
  298: 	if ($target eq 'web') {
  299: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::history);
  300: 	}
  301: 	$Apache::lonhomework::type=&Apache::lonnet::EXT('resource.0.type');
  302: 	&Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
  303:     }
  304:     if ($Apache::lonhomework::type eq '' ) {
  305: 	my $uri=$ENV{'request.uri'};
  306: 	if ($uri=~/\.(\w+)$/) {
  307: 	    $Apache::lonhomework::type=$1;
  308: 	    &Apache::lonxml::debug("Using type of $1");
  309: 	} else {
  310: 	    $Apache::lonhomework::type='problem';
  311: 	    &Apache::lonxml::debug("Using default type, problem, :$uri:");
  312: 	}
  313:     }
  314: 
  315:     #added vars to the scripting enviroment
  316:     my $expression='$external::part='.$Apache::inputtags::part.';';
  317:     &Apache::run::run($expression,$safeeval);
  318:     my $status;
  319:     my $accessmsg;
  320: 
  321:     #should get back a <html> or the neccesary stuff to start XML/MathML
  322:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  323: 	&page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  324:     if ($target eq 'tex' and $ENV{'request.symb'} =~ m/\.page_/) {$result='';}
  325: 
  326:     if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval); }
  327:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  328: 	$target eq 'tex') {
  329: 	#handle exam checkout
  330: 	if ($Apache::lonhomework::type eq 'exam') {
  331: 	    my $token=
  332: 		$Apache::lonhomework::history{"resource.0.outtoken"};
  333: 	    if (($ENV{'form.doescheckout'}) && (!$token)) {
  334: 		$token=&Apache::lonxml::maketoken();
  335: 		$Apache::lonhomework::history{"resource.0.outtoken"}=
  336: 		    $token;
  337: 	    }
  338: 	    $body_tag_start.=&Apache::lonxml::printtokenheader($target,$token);
  339: 	}
  340: 
  341: 	#handle rand seed in construction space
  342: 	my $rndseed=&setup_rndseed($safeeval);
  343: 	my ($symb)=&Apache::lonxml::whichuser();
  344: 	if ($ENV{'request.state'} ne "construct" && $symb eq '') {
  345: 	    $form_tag_start.='<input type="hidden" name="rndseed" value="'.
  346: 		$rndseed.'" />'.
  347: 		    '<input type="submit" name="resetdata"
  348:                              value="New Problem Variation" />'.
  349: 		    '<input type="hidden" name="username"
  350:                              value="'.$ENV{'form.username'}.'" />';
  351: 	}
  352: 	($status,$accessmsg) = &Apache::lonhomework::check_access('0');
  353: 	push (@Apache::inputtags::status,$status);
  354: 	my $expression='$external::datestatus="'.$status.'";';
  355: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
  356: 	&Apache::run::run($expression,$safeeval);
  357: 	&Apache::lonxml::debug("Got $status");
  358: 	if (( $status eq 'CLOSED' ) ||
  359: 	    ( $status eq 'UNCHECKEDOUT') ||
  360: 	    ( $status eq 'BANNED') ||
  361: 	    ( $status eq 'UNAVAILABLE')) {
  362: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser);
  363: 	    if ( $target eq "web" ) {
  364: 		$result.= $head_tag_start.'</head>';
  365: 		my $msg=$body_tag_start;
  366: 		if ($status eq 'UNAVAILABLE') {
  367: 		    $result.='<h1>Unable to determine if this resource is open due to network problems. Please try again later.</h1>';
  368: 		} else {
  369: 		    $result.='<h1>Not open to be viewed</h1>';
  370: 		}
  371: 		if ($status eq 'CLOSED') {
  372: 		    $msg.='The problem '.$accessmsg;
  373: 		} elsif ($status eq 'UNCHECKEDOUT') {
  374: 		    $msg.=&checkout_msg;
  375: 		}
  376: 		$result.=$msg.'<br />';
  377: 	    } elsif ($target eq 'tex') {
  378: 		$result.='\begin{document}\noindent \vskip 1 mm  \begin{minipage}{\textwidth}\vskip 0 mm';
  379: 		if ($status eq 'UNAVAILABLE') {
  380: 		    $result.='Unable to determine if this resource is open due to network problems. Please try again later.\vskip 0 mm ';
  381: 		} else {
  382: 		    $result.="Problem is not open to be viewed. It $accessmsg \\vskip 0 mm ";
  383: 		}
  384: 	    }
  385: 	} elsif ($target eq 'web') {
  386: 	    my $name= &get_resource_name($parstack,$safeeval);
  387: 	    if ($status eq 'CAN_ANSWER') {
  388: 		# create a page header and exit
  389: 		$result.="$head_tag_start<title>$name</title></head>
  390:               $body_tag_start \n $form_tag_start".	
  391: 		  '<input type="hidden" name="submitted" value="yes" />';
  392: 		if ($ENV{'request.state'} eq "construct") {
  393: 		    $result.= &problem_web_to_edit_header($rndseed);
  394: 		}
  395: 		# if we are viewing someone else preserve that info
  396: 		if (defined $ENV{'form.grade_symb'}) {
  397: 		    foreach my $field ('symb','courseid','domain','username') {
  398: 			$result .= '<input type="hidden" name="grade_'.$field.
  399: 			    '" value="'.$ENV{"form.grade_$field"}.'" />'."\n";
  400: 		    }
  401: 		}
  402: 	    } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER'
  403: 		     || $status eq 'CLOSED' || $status eq 'UNAVALAILABLE') {
  404: 		$result.=$head_tag_start.
  405: 		    "<title>$name</title></head>\n$body_tag_start\n";
  406: 	    }
  407: 	} elsif ($target eq 'tex') {
  408: 	    my $startminipage = '';
  409: 	    if ($ENV{'form.problem_split'} ne 'yes') {
  410: 		$startminipage = '\begin{minipage}{\textwidth}';
  411: 	    }
  412: 	    my $name= &Apache::lonxml::get_param('name',$parstack,$safeeval);
  413: 	    if ($name eq '') {
  414: 		$name=&Apache::lonnet::EXT('resource.title');
  415: 		if ($name eq 'con_lost') { $name = ''; }
  416: 	    }
  417: 	    $Apache::lonhomework::name=$name;
  418: 	    my $id = $Apache::inputtags::part;
  419: 	    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  420: 	    my $allkeys=&Apache::lonnet::metadata($ENV{'request.uri'},'keys');
  421: 	    my @allkeys = split /,/,$allkeys;
  422: 	    my $allow_print_points = 0;
  423: 	    foreach my $partial_key (@allkeys) {
  424: 		if ($partial_key=~m/weight/) {
  425: 		    $allow_print_points++;
  426: 		}
  427: 	    }
  428: 	    my $duedate = &Apache::lonnet::EXT("resource.$id.duedate"); 
  429: 	    $duedate = POSIX::strftime("%c",localtime($duedate));
  430: 	    my $temp_file;
  431: 	    my $filename = "/home/httpd/prtspool/$ENV{'user.name'}_$ENV{'user.domain'}_printout.due";
  432: 	    if (-e $filename) {
  433: 		$temp_file = Apache::File->new($filename);
  434: 	    } else {
  435: 		$temp_file = Apache::File->new('>>'.$filename);
  436: 	    }
  437: 	    my @due_file_content = <$temp_file>;
  438: 	    my $due_file_content = $due_file_content[$#due_file_content];
  439: 	    chomp $due_file_content;
  440: 	    my $name_of_resourse= &get_resource_name($parstack,$safeeval);
  441: 	    if ($due_file_content ne $duedate) {
  442: 		$temp_file = Apache::File->new('>'.$filename);
  443: 		print $temp_file "$duedate\n";
  444: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  445: 		    if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
  446: 			$result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent\textit{Due date: '.$duedate.'} \vskip 1 mm\noindent '.$startminipage;	
  447: 		    } else {
  448: 			$result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm \noindent'.$startminipage;
  449: 			if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  450: 		    }
  451: 		} else {
  452: 		    $result .= '\vskip 1mm\textit{Due date: '.$duedate.'} \\\\\\\\';
  453: 		}
  454: 	    } else {
  455: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  456: 		    $result .= '\begin{document} \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$ENV{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent \vskip 1 mm\noindent'.$startminipage;	
  457: 		    if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  458: 		} else {
  459: 		    $result .= '\vskip 1mm \\\\\\\\';
  460: 		}
  461: 	    }
  462: 	}
  463:     } elsif ($target eq 'edit') {
  464: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  465: 	    &problem_edit_header();
  466: 	my $temp=&Apache::edit::insertlist($target,$token);
  467: 	$result.=$temp;
  468:     } elsif ($target eq 'modified') {
  469: 	$result=$token->[4];
  470: 	$result.=&Apache::edit::handle_insert();
  471:     } else {
  472: 	# page_start returned a starting result, delete it if we don't need it
  473: 	$result = '';
  474:     }
  475:     return $result;
  476: }
  477: 
  478: sub end_problem {
  479:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  480:     my $result='';
  481:     my $status=$Apache::inputtags::status['-1'];
  482:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
  483: 	$target eq 'tex') {
  484: 	if ( $target eq 'grade' && $Apache::inputtags::part eq '0' &&
  485: 	     $status eq 'CAN_ANSWER' ) {
  486: 	    # if part is zero, no <part>s existed, so we need to the grading
  487: 	    &Apache::inputtags::grade;
  488: 	} elsif ( ($target eq 'web' || $target eq 'tex') &&
  489: 		  $Apache::inputtags::part eq '0' &&
  490: 		  $status ne 'UNCHECKEDOUT') {
  491: 	    # if part is zero, no <part>s existed, so we need show the current
  492: 	    # grading status
  493: 	    my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
  494: 	    $result.= $gradestatus;
  495: 	}
  496: 	if (
  497: 	    (($target eq 'web') && ($ENV{'request.state'} ne 'construct')) ||
  498: 	    ($target eq 'answer') || ($target eq 'tex')
  499: 	   ) {
  500: 	    if ($status eq 'CAN_ANSWER') {
  501: 		if ($target ne 'tex' &&
  502: 		    $ENV{'form.answer_output_mode'} ne 'tex') {
  503: 		    $result.="</form></body>\n";
  504: 		}
  505: 	    } elsif ($status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER' ||
  506: 		     $status eq 'UNCHECKEDOUT' ) {
  507: 		if ($target ne 'tex') {
  508: 		    $result.="</body>\n";
  509: 		}
  510: 	    }
  511: 	    if ($target eq 'web') {
  512: 		$result.=&Apache::lonxml::xmlend();
  513: 	    } elsif ($target eq 'tex') {
  514: 		my $endminipage = '';
  515: 		if ($ENV{'form.problem_split'} ne 'yes') {
  516: 		    $endminipage = '\end{minipage}';
  517: 		}
  518: 		$result .= '\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
  519: 		if (not $ENV{'request.symb'} =~ m/\.page_/) {
  520: 		    $result .= $endminipage.'\end{document} ';
  521: 		} else {
  522: 		    $result .= '';
  523: 		}
  524: 	    }
  525: 	}
  526: 	if ($target eq 'grade') {
  527: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
  528: 	    &finalize_storage();
  529: 	}
  530: 	if ($target eq 'answer' && ($ENV{'request.state'} eq 'construct')
  531: 	    && $ENV{'form.answer_output_mode'} ne 'tex') {
  532: 	    $result.='</html>'; # normally we get it from xmlend, but in CSTR
  533: 	                        # we always show answer mode too.
  534: 	}
  535:     } elsif ($target eq 'meta') {
  536: 	if ($Apache::inputtags::part eq '0') {
  537: 	    @Apache::inputtags::response=();
  538: 	    $result=&Apache::response::mandatory_part_meta;
  539: 	}
  540:     } elsif ($target eq 'edit') {
  541: 	&Apache::lonxml::debug("in end_problem with $target, edit");
  542: 	$result = &problem_edit_footer();
  543:     }
  544: 
  545:     if ($ENV{'request.state'} eq 'construct' && $target eq 'web') {
  546: 	&Apache::inputtags::check_for_duplicate_ids();
  547:     }
  548:     undef(%Apache::lonhomework::history);
  549:     undef(%Apache::lonhomework::results);
  550:     undef($Apache::inputtags::part);
  551:     undef($Apache::lonhomework::parsing_a_problem);
  552: 
  553:     return $result;
  554: }
  555: 
  556: 
  557: sub start_library {
  558:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  559:     my ($result,$head_tag_start,$body_tag_start,$form_tag_start);
  560: 
  561:     if ($target eq 'edit') {
  562: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  563: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  564: 	$result.=$head_tag_start."</head>".$body_tag_start.$form_tag_start.
  565: 	    &problem_edit_header();
  566: 	my $temp=&Apache::edit::insertlist($target,$token);
  567: 	$result.=$temp;
  568:     } elsif ($target eq 'modified') {
  569: 	$result=$token->[4];
  570: 	$result.=&Apache::edit::handle_insert();
  571:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  572: 	     $ENV{'request.state'} eq "construct" ) {
  573: 	($result,$head_tag_start,$body_tag_start,$form_tag_start)=
  574: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval);
  575: 	my $name=&get_resource_name($parstack,$safeeval);
  576: 	my $rndseed=&setup_rndseed($safeeval);
  577: 	$result.="$head_tag_start<title>$name</title></head>
  578:               $body_tag_start \n $form_tag_start".	
  579: 		  '<input type="hidden" name="submitted" value="yes" />';
  580: 	$result.=&problem_web_to_edit_header($rndseed);
  581:     }
  582:     return $result;
  583: }
  584: 
  585: sub end_library {
  586:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  587:     my $result='';
  588:     if ($target eq 'edit') {
  589: 	$result=&problem_edit_footer();
  590:     } elsif ($target eq 'web' && $$tagstack[0] ne 'problem' &&
  591: 	     $ENV{'request.state'} eq "construct") {
  592: 	$result.='</form></body>'.&Apache::lonxml::xmlend();
  593:     }
  594:     return $result;
  595: }
  596: 
  597: sub start_block {
  598:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  599: 
  600:     my $result;
  601: 
  602:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  603: 	$target eq 'tex' || $target eq 'analyze') {
  604: 	my $code = $token->[2]->{'condition'};
  605: 	if ($code) {
  606: 	    if (!$Apache::lonxml::default_homework_loaded) {
  607: 		&Apache::lonxml::default_homework_load($safeeval);
  608: 	    }
  609: 	    $result = &Apache::run::run($code,$safeeval);
  610: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
  611: 	} else {
  612: 	    $result='1';
  613: 	}
  614: 	if ( ! $result ) {
  615: 	    my $skip=&Apache::lonxml::get_all_text("/block",$parser);
  616: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  617: 	}
  618: 	$result='';
  619:     } elsif ($target eq 'edit') {
  620: 	$result .=&Apache::edit::tag_start($target,$token);
  621: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
  622: 					  $token,40);
  623: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  624:     } elsif ($target eq 'modified') {
  625: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  626: 						     $safeeval,'condition');
  627: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  628:     }
  629:     return $result;
  630: }
  631: 
  632: sub end_block {
  633:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  634:     my $result;
  635:     if ($target eq "edit") {
  636: 	$result.= &Apache::edit::tag_end($target,$token,'');
  637:     }
  638:     return $result;
  639: }
  640: 
  641: sub start_languageblock {
  642:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  643: 
  644:     my $result;
  645: 
  646:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  647: 	$target eq 'tex' || $target eq 'analyze') {
  648: 	my $include = $token->[2]->{'include'};
  649: 	my $exclude = $token->[2]->{'exclude'};
  650:         my %languages=&Apache::loncommon::display_languages();
  651:         $result='1';
  652: 	if ($include) {
  653:             $result='';
  654:             foreach (split(/\,/,$include)) {
  655:                 if ($languages{$_}) { $result='1'; }
  656:             }
  657: 	}
  658:         if ($exclude) {
  659:             foreach (split(/\,/,$exclude)) {
  660:                 if ($languages{$_}) { $result='0'; }
  661:             }
  662: 	}
  663: 	if ( ! $result ) {
  664: 	    my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser);
  665: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  666: 	}
  667: 	$result='';
  668:     } elsif ($target eq 'edit') {
  669: 	$result .=&Apache::edit::tag_start($target,$token);
  670: 	$result .=&Apache::edit::text_arg('Include Language:','include',
  671: 					  $token,40);
  672: 	$result .=&Apache::edit::text_arg('Exclude Language:','exclude',
  673: 					  $token,40);
  674: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  675:     } elsif ($target eq 'modified') {
  676: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  677: 						     $safeeval,'include',
  678: 						     'exclude');
  679: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  680:     }
  681:     return $result;
  682: }
  683: 
  684: sub end_languageblock {
  685:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  686:     my $result;
  687:     if ($target eq "edit") {
  688: 	$result.= &Apache::edit::tag_end($target,$token,'');
  689:     }
  690:     return $result;
  691: }
  692: 
  693: sub start_instructorcomment {
  694:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  695: 
  696:     my $result;
  697: 
  698:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  699: 	$target eq 'tex' || $target eq 'analyze') {
  700:         $result=($ENV{'request.role'}=~/^(in|cc|au|ca|li)/);
  701: 	if ( ! $result ) {
  702: 	    my $skip=&Apache::lonxml::get_all_text("/instructorcomment",$parser);
  703: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
  704: 	}
  705: 	$result='';
  706:     } elsif ($target eq 'edit') {
  707: 	$result .=&Apache::edit::tag_start($target,$token);
  708: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  709:     }
  710:     return $result;
  711: }
  712: 
  713: sub end_instructorcomment {
  714:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  715:     my $result;
  716:     if ($target eq "edit") {
  717: 	$result.= &Apache::edit::tag_end($target,$token,'');
  718:     }
  719:     return $result;
  720: }
  721: 
  722: sub start_while {
  723:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  724: 
  725:     my $result;
  726:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  727: 	$target eq 'tex' || $target eq 'analyze') {
  728: 	my $code = $token->[2]->{'condition'};
  729: 
  730: 	push( @Apache::structuretags::whileconds, $code);
  731: 	if (!$Apache::lonxml::default_homework_loaded) {
  732: 	    &Apache::lonxml::default_homework_load($safeeval);
  733: 	}
  734: 	my $result = &Apache::run::run($code,$safeeval);
  735: 	my $bodytext=&Apache::lonxml::get_all_text("/while",$parser);
  736: 	push( @Apache::structuretags::whilebody, $bodytext);
  737: 	push( @Apache::structuretags::whileline, $token->[5]);
  738: 	&Apache::lonxml::debug("s code $code got -$result-");
  739: 	if ( $result ) {
  740: 	    &Apache::lonxml::newparser($parser,\$bodytext);
  741: 	}
  742:     } elsif ($target eq 'edit') {
  743: 	$result .=&Apache::edit::tag_start($target,$token);
  744: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
  745: 					  $token,40);
  746: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  747:     } elsif ($target eq 'modified') {
  748: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  749: 						     $safeeval,'condition');
  750: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  751:     }
  752:     return $result;
  753: }
  754: 
  755: sub end_while {
  756:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  757:     my $result;
  758: 
  759:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  760: 	$target eq 'tex' || $target eq 'analyze') {
  761: 	my $code = pop(@Apache::structuretags::whileconds);
  762: 	my $bodytext = pop(@Apache::structuretags::whilebody);
  763: 	my $line = pop(@Apache::structuretags::whileline);
  764: 	my $return = &Apache::run::run($code,$safeeval);
  765: 	my $starttime=time;
  766: 	my $error=0;
  767: 	while ($return) {
  768: 	    if (time-$starttime >
  769: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}) {
  770: 		$return = 0; $error=1; next;
  771: 	    }
  772: 	    $result.=&Apache::scripttag::xmlparse($bodytext);
  773: 	    $return = &Apache::run::run($code,$safeeval);
  774: 	}
  775: 	if ($error) {
  776: 	    &Apache::lonxml::error('<pre>Code ran too long. It ran for more than '.$Apache::lonnet::perlvar{'lonScriptTimeout'}.' seconds occured while running &lt;while$gt; on line '.$line.'</pre>');
  777: 	}
  778:     } elsif ($target eq "edit") {
  779: 	$result.= &Apache::edit::tag_end($target,$token,'');
  780:     }
  781:     return $result;
  782: }
  783: 
  784: # <randomlist show="1">
  785: #  <tag1>..</tag1>
  786: #  <tag2>..</tag2>
  787: #  <tag3>..</tag3>
  788: #  ...
  789: # </randomlist>
  790: sub start_randomlist {
  791:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  792:     my $result;
  793:     if ($target eq 'answer' || $target eq 'grade' || $target eq 'web' ||
  794: 	$target eq 'tex' || $target eq 'analyze') {
  795: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
  796: 	my $b_parser= HTML::TokeParser->new(\$body);
  797: 	my $b_tok;
  798: 	my @randomlist;
  799: 	my $list_item;
  800: 	while($b_tok = $b_parser->get_token() ) {
  801: 	    if($b_tok->[0] eq 'S') { # start tag
  802: 		# get content of the tag until matching end tag
  803: 		# get all text upto the matching tag
  804: 		# and push the content into @randomlist
  805: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
  806: 							   $b_parser);
  807: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
  808: 		push(@randomlist,$list_item);
  809: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
  810:                 #         $list_item</b>";
  811: 	    }
  812: 	    if($b_tok->[0] eq 'T') { # text
  813: 		# what to do with text in between tags?
  814: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
  815: 	    }
  816: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
  817: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
  818: 	    # }
  819: 	}
  820: 	my @idx_arr = (0 .. $#randomlist);
  821: 	&Apache::structuretags::shuffle(\@idx_arr);
  822: 	my $bodytext = '';
  823: 	my $show=$#randomlist;
  824: 	my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
  825: 	$showarg--;
  826: 	if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
  827: 	for(0 .. $show) {
  828: 	    $bodytext .= "$randomlist[ $idx_arr[$_] ]";
  829: 	}
  830: 	&Apache::lonxml::newparser($parser,\$bodytext);
  831:     } elsif ($target eq 'edit' ) {
  832: 	$result .=&Apache::edit::tag_start($target,$token);
  833: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
  834: 					   $token,5);
  835: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  836:     } elsif ($target eq 'modified' ) {
  837: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  838: 						     $safeeval,'show');
  839: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  840:     }
  841:     return $result;
  842: }
  843: 
  844: sub shuffle {
  845:     my $a=shift;
  846:     my $i;
  847:     if (defined(@$a)) {
  848: 	&Apache::response::setrandomnumber();
  849: 	for($i=@$a;--$i;) {
  850: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
  851: 	    next if $i == $j;
  852: 	    @$a[$i,$j] = @$a[$j,$i];
  853: 	}
  854:     }
  855: }
  856: 
  857: sub end_randomlist {
  858:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  859:     my $result;
  860:     if ($target eq 'edit' ) {
  861: 	$result=&Apache::edit::tag_end($target,$token,
  862: 				       'End Randomly Parsed Block');
  863:     }
  864:     return $result;
  865: }
  866: 
  867: sub start_part {
  868:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  869:     my $result='';
  870:     my $id= &Apache::lonxml::get_param('id',$parstack,$safeeval);
  871:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  872:     $Apache::inputtags::part=$id;
  873:     push(@Apache::inputtags::partlist,$id);
  874:     @Apache::inputtags::response=();
  875:     @Apache::inputtags::previous=();
  876:     @Apache::inputtags::previous_version=();
  877:     $Apache::lonhomework::problemstatus=
  878: 	&Apache::lonnet::EXT("resource.$id.problemstatus");
  879:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  880: 
  881:     if ($target eq 'meta') {
  882: 	return &Apache::response::mandatory_part_meta;
  883:     } elsif ($target eq 'web' || $target eq 'grade' ||
  884: 	     $target eq 'answer' || $target eq 'tex') {
  885: 	if ($hidden) {
  886: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  887: 	} else {
  888: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
  889: 	    push (@Apache::inputtags::status,$status);
  890: 	    my $expression='$external::datestatus="'.$status.'";';
  891: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
  892: 	    &Apache::run::run($expression,$safeeval);
  893: 	    if ( $status eq 'CLOSED' ) {
  894: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser);
  895: 		if ( $target eq "web" ) {
  896: 		    $result="<br />Part is not open to be viewed. It $accessmsg<br />";
  897: 		} elsif ( $target eq 'tex' ) {
  898: 		    $result="\\end{minipage}\\vskip 0 mm Part is not open to be viewed. It $accessmsg \\\\\\begin{minipage}{\\textwidth}";
  899: 		}
  900: 	    } else {
  901: 		if ($target eq 'tex') {
  902: 		    $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
  903: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
  904: 		    if ($Apache::lonhomework::type eq 'exam') { $result .= '\fbox{\textit{'.$weight.' pt}}';}
  905: 		}
  906: 	    }
  907: 	}
  908:     } elsif ($target eq 'edit') {
  909: 	$result.=&Apache::edit::tag_start($target,$token);
  910: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
  911: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
  912: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
  913:     } elsif ($target eq 'modified') {
  914: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  915: 						     $safeeval,'id');
  916: 	if ($constructtag) {
  917: 	    $result = &Apache::edit::rebuild_tag($token);
  918: 	    $result.=&Apache::edit::handle_insert();
  919: 	}
  920:     }
  921:     return $result;
  922: }
  923: 
  924: sub end_part {
  925:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  926:     &Apache::lonxml::debug("in end_part $target ");
  927:     my $status=$Apache::inputtags::status['-1'];
  928:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
  929:     my $result='';
  930:     if ( $target eq 'meta' ) {
  931: 	$result='';
  932:     } elsif ( $target eq 'grade' && $status eq 'CAN_ANSWER' && !$hidden) {
  933: 	$result=&Apache::inputtags::grade;
  934:     } elsif (($target eq 'web' || $target eq 'tex') && !$hidden ) {
  935: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
  936: 							$target);
  937: 	if ($Apache::lonhomework::type eq 'exam') {$gradestatus='';}
  938: 	$result=$gradestatus;
  939:     } elsif ($target eq 'edit') {
  940: 	$result=&Apache::edit::end_table();
  941:     }
  942:     pop @Apache::inputtags::status;
  943:     $Apache::inputtags::part='';
  944:     return $result;
  945: }
  946: 
  947: sub start_preduedate {
  948:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  949:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' || $target eq 'tex') {
  950: 	if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  951: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER' &&
  952: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  953: 	    &Apache::lonxml::get_all_text("/preduedate",$parser);
  954: 	}
  955:     }
  956:     return '';
  957: }
  958: 
  959: sub end_preduedate {
  960:     return '';
  961: }
  962: 
  963: sub start_postanswerdate {
  964:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  965:     if ($target eq 'web' || $target eq 'grade' || $target eq 'tex') {
  966: 	if ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER') {
  967: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser);
  968: 	}
  969:     } elsif ($target eq 'tex') {
  970: 	return '\vskip 0 mm \noindent';
  971:     }
  972:     return '';
  973: }
  974: 
  975: sub end_postanswerdate {
  976:     return '';
  977: }
  978: 
  979: sub start_notsolved {
  980:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  981:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
  982: 	$target eq 'tex') {
  983: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  984: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
  985: 	if ($gradestatus =~ /^correct/) {
  986: 	    &Apache::lonxml::debug("skipping");
  987: 	    &Apache::lonxml::get_all_text("/notsolved",$parser);
  988: 	}
  989:     }
  990:     return '';
  991: }
  992: 
  993: sub end_notsolved {
  994:     return '';
  995: }
  996: 
  997: sub start_solved {
  998:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  999:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
 1000: 	$target eq 'tex') {
 1001: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
 1002: 	if ($gradestatus !~ /^correct/) {
 1003: 	    &Apache::lonxml::get_all_text("/solved",$parser);
 1004: 	}
 1005:     }
 1006:     return '';
 1007: }
 1008: 
 1009: sub end_solved {
 1010:     return '';
 1011: }
 1012: 
 1013: sub start_startouttext {
 1014:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1015:     my @result=(''.'');
 1016:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
 1017:     return (@result);
 1018: }
 1019: 
 1020: sub end_startouttext {
 1021:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1022:     my $result='';
 1023:     my $text='';
 1024: 
 1025:     if ($target eq 'edit') {
 1026: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
 1027: 	$result.=&Apache::edit::start_table($token)."<tr><td>Text Block</td>
 1028: <td>Delete:".
 1029:                  &Apache::edit::deletelist($target,$token)
 1030: 		 ."</td>
 1031: <td>".
 1032:                  &Apache::edit::insertlist($target,$token).
 1033: 		 &Apache::edit::end_row().
 1034:                  &Apache::edit::start_spanning_row()."\n"
 1035: 		 .'<table><tr><td>'.
 1036: 		 &Apache::loncommon::help_open_topic("Greek_Symbols",
 1037: 						     'Greek Symbols',
 1038: 						     undef,undef,600)
 1039: 		 .'</td><td>'.
 1040: 		 &Apache::loncommon::help_open_topic("Other_Symbols",
 1041: 						     'Other Symbols',
 1042: 						     undef,undef,600)
 1043: 		 .'</td></tr></table>'.
 1044: 		 &Apache::edit::editfield($token->[1],$text,"",80,4);
 1045:     }
 1046:     if ($target eq 'modified') {
 1047: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser);
 1048: 	$result='<startouttext />'.&Apache::edit::modifiedfield();
 1049:     }
 1050:     if ($target eq 'tex') {
 1051: 	$result .= '\noindent ';
 1052:     }
 1053:     return $result;
 1054: }
 1055: 
 1056: sub start_endouttext {
 1057:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1058:     my $result='';
 1059:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
 1060:     if ($target eq "modified") {
 1061: 	$result='<endouttext />'.
 1062: 	    &Apache::edit::handle_insertafter('startouttext'); }
 1063:     return $result;
 1064: }
 1065: 
 1066: sub end_endouttext {
 1067:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1068:     my @result=('','');
 1069:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
 1070:     return (@result);
 1071: }
 1072: 
 1073: sub delete_startouttext {
 1074:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
 1075:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser);
 1076:     my $text=$$parser['-1']->get_text("/endouttext");
 1077:     my $ntoken=$$parser['-1']->get_token();
 1078:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
 1079:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
 1080:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
 1081:     # they did the last time round
 1082:     &Apache::lonxml::increasedepth($ntoken);
 1083:     &Apache::lonxml::decreasedepth($ntoken);
 1084:     return 1;
 1085: }
 1086: 
 1087: 1;
 1088: __END__

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