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

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

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