Annotation of loncom/homework/structuretags.pm, revision 1.188

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

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