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

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

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