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

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

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