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

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

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