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

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

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