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

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

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