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

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

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