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

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

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