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

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

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