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

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

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