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

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

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