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

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.465.2.15! raeburn     4: # $Id: structuretags.pm,v 1.465.2.14 2011/10/06 03:23:44 raeburn 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.435     jms        30: =pod
                     31: 
                     32: =head1 NAME
                     33: 
                     34: Apache::structuretags
                     35: 
                     36: =head1 SYNOPSIS
                     37: 
                     38: 
                     39: This is part of the LearningOnline Network with CAPA project
                     40: described at http://www.lon-capa.org.
                     41: 
                     42: 
                     43: =head1 NOTABLE SUBROUTINES
                     44: 
                     45: =over
                     46: 
                     47: =item 
                     48: 
                     49: =back
                     50: 
                     51: =cut
                     52: 
1.133     sakharuk   53: 
1.1       albertel   54: package Apache::structuretags; 
                     55: 
                     56: use strict;
                     57: use Apache::lonnet;
1.101     sakharuk   58: use Apache::File();
1.147     www        59: use Apache::lonmenu;
1.210     albertel   60: use Apache::lonlocal;
1.231     sakharuk   61: use Apache::lonxml;
1.434     foxr       62: use Apache::londefdef;
1.338     albertel   63: use Apache::lonenc();
1.267     albertel   64: use Time::HiRes qw( gettimeofday tv_interval );
1.356     www        65: use lib '/home/httpd/lib/perl/';
                     66: use LONCAPA;
                     67:  
1.78      harris41   68: BEGIN {
1.465.2.2  raeburn    69:     &Apache::lonxml::register('Apache::structuretags',('block','languageblock','translated','instructorcomment','while','randomlist','problem','library','web','tex','part','preduedate','postanswerdate','solved','notsolved','problemtype','startpartmarker','startouttext','endpartmarker','endouttext','simpleeditbutton','definetag'));
1.10      albertel   70: }
                     71: 
                     72: sub start_web {
1.326     albertel   73:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.383     albertel   74:     if ($target ne 'edit' && $target ne 'modified') {
                     75: 	my $bodytext=&Apache::lonxml::get_all_text("/web",$parser,$style);
                     76: 	if ($target eq 'web' || $target eq 'webgrade') {
                     77: 	    return $bodytext;
                     78: 	}
                     79:     } elsif ($target eq "edit" ) {
                     80: 	my $bodytext = 
                     81: 	    &Apache::lonxml::get_all_text_unbalanced("/web",$parser);
                     82: 	my $result = &Apache::edit::tag_start($target,$token);
                     83: 	$result .= &Apache::edit::editfield($token->[1],$bodytext,'',80,1);
                     84: 	return $result;
                     85:     } elsif ( $target eq "modified" ) {
                     86: 	return $token->[4].&Apache::edit::modifiedfield("/web",$parser);
1.159     albertel   87:     }
                     88:     return '';
1.10      albertel   89: }
                     90: 
                     91: sub end_web {
1.44      ng         92:     return '';
1.10      albertel   93: }
                     94: 
                     95: sub start_tex {
1.326     albertel   96:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.198     sakharuk   97:     my $result='';
1.383     albertel   98:     if ($target ne 'edit' && $target ne 'modified') {
                     99: 	my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser,$style);
                    100: 	if ($target eq 'tex') {
1.434     foxr      101: 	    
                    102: 	    # If inside a table, occurrences of \\ must be removed;
                    103: 	    # else the table blows up.
                    104: 
                    105: 	    if (&Apache::londefdef::is_inside_of($tagstack, "table")) {
                    106: 		$bodytext =~ s/\\\\//g;
                    107: 	    }
1.432     foxr      108: 	    return $bodytext.'{}';
1.383     albertel  109: 	}
                    110:     } elsif ($target eq "edit" ) {
                    111: 	my $bodytext = 
                    112: 	    &Apache::lonxml::get_all_text_unbalanced("/tex",$parser);
                    113: 	my $result = &Apache::edit::tag_start($target,$token);
                    114: 	$result .= &Apache::edit::editfield($token->[1],$bodytext,'',80,1);
                    115: 	return $result;
                    116:     } elsif ( $target eq "modified" ) {
                    117: 	return $token->[4].&Apache::edit::modifiedfield("/tex",$parser);
1.159     albertel  118:     }
1.198     sakharuk  119:     return $result;;
1.10      albertel  120: }
                    121: 
                    122: sub end_tex {
1.44      ng        123:     return '';
1.9       albertel  124: }
                    125: 
1.400     albertel  126: sub homework_js {
                    127:     return &Apache::loncommon::resize_textarea_js().
1.416     raeburn   128:            &setmode_javascript().
1.400     albertel  129: 	<<'JS';
                    130: <script type="text/javascript">
1.465.2.9  raeburn   131: // <![CDATA[
1.465.2.14  raeburn   132: function setSubmittedPart (part,prefix) {
                    133:     if (typeof(prefix) == 'undefined') {
                    134:         this.document.lonhomework.submitted.value="part_"+part;
                    135:     } else {
                    136:         for (var i=0;i<this.document.lonhomework.elements.length;i++) {
                    137:             if (this.document.lonhomework.elements[i].name == prefix+'submitted') {
                    138:                 this.document.lonhomework.elements[i].value="part_"+part;
                    139:             }
                    140:         }
                    141:     }
1.400     albertel  142: }
                    143: 
                    144: function image_response_click (which, e) {
                    145:     init_geometry();
                    146:     if (!e) { e = window.event; } //IE
                    147:     var input_element = document.lonhomework.elements[which];
1.401     albertel  148:     var token_element = document.lonhomework.elements[which+'_token'];
1.400     albertel  149:     var token = token_element.value;
1.401     albertel  150:     var img_element   = document.getElementById(which+'_imageresponse');
1.400     albertel  151:     var x= e.clientX-getX(img_element)+Geometry.getHorizontalScroll();
                    152:     var y= e.clientY-getY(img_element)+Geometry.getVerticalScroll();
                    153:     var click = x+':'+y;
                    154:     input_element.value = click;
1.465.2.13  raeburn   155:     img_element.src = '/adm/randomlabel.png?token='+token+'&clickdata='+click;
1.400     albertel  156: }
1.465.2.9  raeburn   157: // ]]>
1.400     albertel  158: </script>
                    159: JS
                    160: }
                    161: 
1.416     raeburn   162: sub setmode_javascript {
                    163:     return <<"ENDSCRIPT";
                    164: <script type="text/javascript">
1.465.2.13  raeburn   165: // <![CDATA[
1.416     raeburn   166: function setmode(form,probmode) {
                    167:     form.problemmode.value = probmode;
                    168:     form.submit();
                    169: }
1.465.2.13  raeburn   170: // ]]>
1.416     raeburn   171: </script>
                    172: ENDSCRIPT
                    173: }
                    174: 
1.465.2.6  raeburn   175: sub file_delchk_js {
1.465.2.12  raeburn   176:     my $delfilewarn = &mt('You have indicated you wish to remove some files previously included in your submission.').'\\n'.
                    177:                       &mt('Continue submission with these files removed?');
1.465.2.6  raeburn   178:     return <<"ENDSCRIPT";
                    179: <script type="text/javascript">
1.465.2.9  raeburn   180: // <![CDATA[
1.465.2.6  raeburn   181: function file_deletion_check(formname) {
                    182:     var elemnum = formname.elements.length;
                    183:     if (elemnum == 0) {
                    184:         return true;
                    185:     }
1.465.2.9  raeburn   186:     var str = new RegExp("^HWFILE.+_delete\$");
                    187:     var delboxes = new Array();
1.465.2.6  raeburn   188:     for (i=0; i<formname.elements.length; i++) {
                    189:         var id = formname.elements[i].id;
                    190:         if (id != '') {
                    191:             if (str.test(id)) {
                    192:                 if (formname.elements[i].type == 'checkbox') {
                    193:                     if (formname.elements[i].checked) {
1.465.2.9  raeburn   194:                         delboxes.push(id);
1.465.2.6  raeburn   195:                     }
                    196:                 }
                    197:             }
                    198:         }
                    199:     }
1.465.2.9  raeburn   200:     if (delboxes.length > 0) {
1.465.2.6  raeburn   201:         if (confirm("$delfilewarn")) {
                    202:             return true;
                    203:         } else {
1.465.2.9  raeburn   204:             for (var j=0; j<delboxes.length; j++) {
                    205:                 formname.elements[delboxes[j]].checked = false;
                    206:             }
1.465.2.6  raeburn   207:             return false;
                    208:         }
                    209:     } else {
                    210:         return true;
                    211:     }
                    212: }
1.465.2.9  raeburn   213: // ]]>
1.465.2.6  raeburn   214: </script>
                    215: ENDSCRIPT
                    216: }
                    217: 
1.48      albertel  218: sub page_start {
1.345     albertel  219:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$name,
                    220: 	$extra_head)=@_;
1.159     albertel  221:     my %found;
1.207     albertel  222:     foreach my $taginside (@$tagstack) {
1.159     albertel  223: 	foreach my $taglookedfor ('html','body','form') {
                    224: 	    if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
                    225: 	}
                    226:     }
                    227: 
1.343     albertel  228:     if ($target eq 'tex') {
                    229: 	return
                    230: 	    &Apache::londefdef::start_html($target,$token,$tagstack,
                    231: 					   $parstack,$parser,$safeeval);
                    232:     }
                    233: 
1.465.2.4  raeburn   234:     $extra_head .= &homework_js().
                    235:                    &Apache::lonhtmlcommon::dragmath_js("EditMathPopup");
                    236:     if (&Apache::lonhtmlcommon::htmlareabrowser()) {
                    237:         my %textarea_args = (
                    238:                                 dragmath => 'math',
                    239:                               );
                    240:         $extra_head .= &Apache::lonhtmlcommon::htmlareaselectactive(\%textarea_args);
1.425     raeburn   241:     }
1.465.2.6  raeburn   242:     my $is_task = ($env{'request.uri'} =~ /\.task$/);
                    243:     if ($is_task) {
                    244:         $extra_head .= &file_delchk_js();
                    245:     }
1.425     raeburn   246: 
1.344     albertel  247:     my %body_args;
                    248:     if (defined($found{'html'})) {
                    249: 	$body_args{'skip_phases'}{'head'}=1;
                    250:     } else {
1.343     albertel  251: 	
1.345     albertel  252: 	$extra_head .= &Apache::lonhtmlcommon::spellheader();
1.343     albertel  253: 
1.379     albertel  254: 	$extra_head .= &Apache::londefdef::generate_css_links();
                    255: 
1.384     albertel  256: 	if ($env{'request.state'} eq 'construct') {
1.343     albertel  257: 	    $extra_head.=&Apache::edit::js_change_detection().
                    258: 		"<script type=\"text/javascript\">\n".
                    259: 		"if (typeof swmenu != 'undefined') {swmenu.currentURL=null;}\n".
                    260: 		&Apache::loncommon::browser_and_searcher_javascript().
                    261:                 "\n</script>\n";
                    262: 	}
1.159     albertel  263:     }
1.343     albertel  264: 
1.446     bisitz    265:     my $pageheader = '';
1.344     albertel  266:     if (defined($found{'body'})) {
                    267: 	$body_args{'skip_phases'}{'body'}=1;
                    268:     } elsif (!defined($found{'body'}) 
                    269: 	     && $env{'request.state'} eq 'construct') {
1.343     albertel  270: 	if ($target eq 'web' || $target eq 'edit') {
1.402     albertel  271: 	    if ($env{'environment.remote'} ne 'off') {
                    272: 		$body_args{'only_body'}  = 1;
                    273: 	    }
1.450     bisitz    274:         # Breadcrumbs for Construction Space
                    275:         &Apache::lonhtmlcommon::clear_breadcrumbs();
                    276:         &Apache::lonhtmlcommon::add_breadcrumb({
                    277:             'text'  => 'Construction Space',
                    278:             'href'  => &Apache::loncommon::authorspace(),
                    279:         });
1.460     droeschl  280:         # breadcrumbs (and tools) will be created 
                    281:         # in start_page->bodytag->innerregister
                    282: 
1.450     bisitz    283: # FIXME Where are we?
                    284: #        &Apache::lonhtmlcommon::add_breadcrumb({
                    285: #            'text'  => 'Problem Editing', # 'Problem Testing'
                    286: #            'href'  => '',
                    287: #        });
1.460     droeschl  288:         $pageheader =&Apache::loncommon::head_subbox(
1.446     bisitz    289:                 &Apache::loncommon::CSTR_pageheader());
1.297     albertel  290: 	}
1.272     albertel  291:     } elsif (!defined($found{'body'})) {
1.343     albertel  292: 	my %add_entries;
1.159     albertel  293: 	my $background=&Apache::lonxml::get_param('background',$parstack,
                    294: 						  $safeeval);
1.343     albertel  295: 	if ($background ne '' ) {
                    296: 	    $add_entries{'background'} = $background;
                    297: 	}
1.344     albertel  298: 
1.290     albertel  299: 	my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
                    300: 					       $safeeval);
1.446     bisitz    301:         if ($bgcolor eq '' ) { $bgcolor = '#FFFFFF'; }
1.344     albertel  302: 
1.446     bisitz    303:         $body_args{'bgcolor'}        = $bgcolor;
                    304:         # $body_args{'no_title'}       = 1;
                    305:         $body_args{'force_register'} = 1;
                    306:         $body_args{'add_entries'}    = \%add_entries;
                    307:         if ($env{'environment.remote'} eq 'off'
                    308:             && $env{'request.state'} eq   'construct') {
                    309:             $body_args{'only_body'}  = 1;
                    310:         }
1.344     albertel  311:     }
1.365     albertel  312:     $body_args{'no_auto_mt_title'} = 1;
1.344     albertel  313:     my $page_start = &Apache::loncommon::start_page($name,$extra_head,
                    314: 						    \%body_args);
1.446     bisitz    315:     $page_start .= $pageheader;
1.462     raeburn   316:     if (!defined($found{'body'}) 
                    317: 	&& $env{'request.state'} ne 'construct'
                    318: 	&& ($target eq 'web' || $target eq 'webgrade')) {
                    319: 
                    320: 	my ($symb,undef,undef,undef,$publicuser)= &Apache::lonnet::whichuser();
                    321:         if ($symb eq '' && !$publicuser) {
                    322:             $page_start .= '<p class="LC_info">'
                    323:                           .&mt('Browsing resource, all submissions are temporary.')
                    324:                           .'</p>';
1.457     bisitz    325:         }
1.344     albertel  326:     }
                    327: 
1.409     albertel  328:     if (!defined($found{'body'}) && $env{'request.state'} ne 'construct') {
1.343     albertel  329: 	$page_start .= &Apache::lonxml::message_location();
1.159     albertel  330:     }
1.344     albertel  331:     
1.159     albertel  332:     my $form_tag_start;
                    333:     if (!defined($found{'form'})) {
1.337     albertel  334: 	$form_tag_start='<form name="lonhomework" enctype="multipart/form-data" method="post" action="';
1.465     raeburn   335: 	my $uri = &Apache::loncommon::inhibit_menu_check(
1.455     droeschl  336:                 &Apache::lonenc::check_encrypt($env{'request.uri'}));
1.464     raeburn   337:         $uri = &HTML::Entities::encode($uri,'<>&"');
1.327     albertel  338: 	$form_tag_start.=$uri.'" ';
                    339: 	if ($target eq 'edit') {
                    340: 	    $form_tag_start.=&Apache::edit::form_change_detection();
                    341: 	}
1.465.2.6  raeburn   342:         if ($is_task) {
                    343:             $form_tag_start .= ' onsubmit="return file_deletion_check(this);"';
                    344:         }
1.368     albertel  345: 	$form_tag_start.='>'."\n";
1.355     albertel  346: 
                    347: 	my $symb=&Apache::lonnet::symbread();
                    348: 	if ($symb =~ /\S/) {
                    349: 	    $symb=
                    350: 		&HTML::Entities::encode(&Apache::lonenc::check_encrypt($symb));
                    351: 	    $form_tag_start.=
1.368     albertel  352: 		"\t".'<input type="hidden" name="symb" value="'.$symb.'" />'."\n";
1.355     albertel  353: 	}
1.159     albertel  354:     }
1.343     albertel  355:     return ($page_start,$form_tag_start);
1.105     albertel  356: }
                    357: 
1.141     matthew   358: #use Time::HiRes();
1.105     albertel  359: sub get_resource_name {
1.159     albertel  360:     my ($parstack,$safeeval)=@_;
1.388     foxr      361:     my $name;
1.204     albertel  362:     if (defined($Apache::lonhomework::name)) {
1.388     foxr      363: 	$name = $Apache::lonhomework::name;
                    364:     } else {
                    365: 	my ($symb)=&Apache::lonnet::whichuser();
1.392     albertel  366: 	$name=&Apache::lonnet::gettitle($symb);
1.388     foxr      367: 	if ($name eq '') {
                    368: 	    $name=&Apache::lonnet::EXT('resource.title');
                    369: 	    if ($name eq 'con_lost') { $name = ''; }
                    370: 	}
                    371: 	if ($name!~/\S+/) {
                    372: 	    $name=$env{'request.uri'};
                    373: 	    $name=~s-.*/([^/]+)$-$1-;
                    374: 	}
                    375: 	# The name has had html tags escaped:
                    376:        
                    377: 	$name=~s/&lt;/</gs;
                    378: 	$name=~s/&gt;/>/gs;
                    379: 
                    380: 	$Apache::lonhomework::name=$name;
1.204     albertel  381:     }
1.159     albertel  382:     return $name;
1.105     albertel  383: }
                    384: 
                    385: sub setup_rndseed {
1.465.2.7  raeburn   386:     my ($safeeval,$target)=@_;
1.367     albertel  387:     my ($symb)=&Apache::lonnet::whichuser();
1.465.2.7  raeburn   388:     my ($questiontype,$set_safespace,$rndseed);
                    389:     if ($target eq 'analyze') {
                    390:         $questiontype = $env{'form.grade_questiontype'};
                    391:     }
                    392:     unless (defined($questiontype)) {
                    393:         $questiontype = $Apache::lonhomework::type;
                    394:     }
1.333     albertel  395:     if ($env{'request.state'} eq "construct" 
                    396: 	|| $symb eq '' 
                    397: 	|| $Apache::lonhomework::type eq 'practice'
                    398: 	|| $Apache::lonhomework::history{'resource.CODE'}) {
1.316     www       399: 	&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.317     albertel  400: 						['rndseed']);
1.284     albertel  401: 	$rndseed=$env{'form.rndseed'};
1.159     albertel  402: 	if (!$rndseed) {
1.162     albertel  403: 	    $rndseed=$Apache::lonhomework::history{'rndseed'};
                    404: 	    if (!$rndseed) {
                    405: 		$rndseed=time;
                    406: 	    }
1.284     albertel  407: 	    $env{'form.rndseed'}=$rndseed;
1.162     albertel  408: 	}
1.465.2.7  raeburn   409:         if (($env{'request.state'} eq "construct") &&
                    410:             ($Apache::lonhomework::type eq 'randomizetry')) {
                    411:             my $tries = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.tries"};
                    412:             if ($tries) {
                    413:                 $rndseed += $tries;
                    414:             }
                    415:             $env{'form.'.$Apache::inputtags::part.'.rndseed'}=$rndseed;
                    416:         }
1.374     albertel  417: 	if ( ($env{'form.resetdata'} eq &mt('New Problem Variation')
                    418: 	      && $env{'form.submitted'} eq 'yes')  ||
1.284     albertel  419: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
1.190     albertel  420: 	    srand(time);
                    421: 	    $rndseed=int(rand(2100000000));
1.284     albertel  422: 	    $env{'form.rndseed'}=$rndseed;
                    423: 	    delete($env{'form.resetdata'});
                    424: 	    delete($env{'form.newrandomization'});
1.159     albertel  425: 	}
1.187     albertel  426: 	if (defined($rndseed) && $rndseed ne int($rndseed)) {
1.307     albertel  427: 	    $rndseed=join(':',&Apache::lonnet::digest($rndseed));
1.187     albertel  428:         }
1.247     albertel  429:         if ($Apache::lonhomework::history{'resource.CODE'}) {
                    430: 	   $rndseed=&Apache::lonnet::rndseed();
                    431: 	}
1.465.2.7  raeburn   432:         $set_safespace = 1;
                    433:     } elsif ($questiontype eq 'randomizetry') {
                    434:         if ($target eq 'analyze') {
                    435:             if (defined($env{'form.grade_rndseed'})) {
                    436:                 $rndseed = $env{'form.grade_rndseed'};
                    437:             }
                    438:         }
                    439:         unless (($target eq 'analyze') && (defined($rndseed))) {
                    440:             $rndseed=&Apache::lonnet::rndseed();
                    441:             my $curr_try = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.tries"};
                    442:             if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    443:                 $curr_try ++;
                    444:             }
                    445:             if ($rndseed =~/^(\d+)[,:](\d+)$/) {
                    446:                 $rndseed = $1;
                    447:             }
                    448:             if ($curr_try) {
                    449:                 my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                    450:                 if (($reqtries =~ /^\d+$/) && ($reqtries > 1)) {
                    451:                     my $inc = int(($curr_try-1)/$reqtries);
                    452:                     $rndseed += $inc;
                    453:                 } else {
                    454:                     $rndseed += $curr_try;
                    455:                 }
                    456:             }
                    457:         }
                    458:         $set_safespace = 1;
                    459:     }
                    460:     if ($set_safespace) {
                    461:         if ($safeeval) {
                    462:             &Apache::lonxml::debug("Setting rndseed to $rndseed");
                    463:             &Apache::run::run('$external::randomseed="'.$rndseed.'";',$safeeval);
                    464:         }
                    465:     }
                    466:     unless (($env{'request.state'} eq "construct") || ($symb eq '')) {
                    467:         $env{'form.'.$Apache::inputtags::part.'.rndseed'}=$rndseed;
1.159     albertel  468:     }
                    469:     return $rndseed;
1.105     albertel  470: }
                    471: 
1.268     albertel  472: sub remember_problem_state {
                    473:     return '
1.284     albertel  474:        <input type="hidden" name="problemstate" value="'.$env{'form.problemstate'}.'" />
                    475:        <input type="hidden" name="problemtype" value="'.$env{'form.problemtype'}.'" />
                    476:        <input type="hidden" name="problemstatus" value="'.$env{'form.problemstatus'}.'" />';
1.268     albertel  477: }
                    478: 
1.465.2.14  raeburn   479: sub problem_edit_action_button {
                    480:     my ($name,$action,$accesskey,$text,$flag)=@_;
                    481:     my $actionscript="setmode(this.form,'$action')";
                    482:     return "\n<input type='button' name='$name' accesskey='$accesskey' value='".&mt($text)."'".
                    483:            ($flag?&Apache::edit::submit_ask_anyway($actionscript):&Apache::edit::submit_dont_ask($actionscript))." />";
                    484: }
                    485: 
1.423     www       486: sub problem_edit_buttons {
1.465.2.14  raeburn   487:     my ($mode)=@_;
                    488: # Buttons that do not save
                    489:     my $result='<div class="LC_edit_problem_discards">'.
                    490:               &problem_edit_action_button('subdiscview','discard','d','Discard Edits and View',1);
                    491:     if ($mode eq 'editxml') {
                    492:         $result.=&problem_edit_action_button('subedit','edit','e','Edit',1);
                    493:         $result.=&problem_edit_action_button('subundo','undoxml','u','Undo',1);
                    494:         $result.=&Apache::lonhtmlcommon::dragmath_button("LC_editxmltext",1);
                    495:     } else {
                    496:         $result.=&problem_edit_action_button('subeditxml','editxml','x','EditXML',1);
                    497:         $result.=&problem_edit_action_button('subundo','undo','u','Undo',1);
                    498:     }
                    499:     $result.="\n</div>";
                    500: # Buttons that save
                    501:     $result.='<div class="LC_edit_problem_saves">';
                    502:     if ($mode eq 'editxml') {
                    503:         $result.=&problem_edit_action_button('subsaveedit','saveeditxml','s','Save and EditXML');
                    504:         $result.=&problem_edit_action_button('subsaveview','saveviewxml','v','Save and View');
                    505:     } else {
                    506:         $result.=&problem_edit_action_button('subsaveedit','saveedit','s','Save and Edit');
                    507:         $result.=&problem_edit_action_button('subsaveview','saveview','v','Save and View');
                    508:     }
                    509:     $result.="\n</div>\n";
                    510:     return $result;
1.423     www       511: }
                    512: 
                    513: sub problem_edit_header {
1.465.2.14  raeburn   514:     return '<input type="hidden" name="submitted" value="edit" />'.
                    515: 	&remember_problem_state('edit').'
1.423     www       516: <div class="LC_edit_problem_header">
                    517: <div class="LC_edit_problem_header_title">
                    518: '.&mt('Problem Editing').&Apache::loncommon::help_open_menu('Problem Editing','Problem_Editor_XML_Index',5,'Authoring').'
                    519: </div>'.
1.465.2.14  raeburn   520: '<input type="hidden" name="problemmode" value="saveedit" />'.
1.423     www       521: &problem_edit_buttons().'
1.459     bisitz    522: <hr style="clear:both;" />
1.409     albertel  523: '.&Apache::lonxml::message_location().'
1.408     albertel  524: </div>
                    525: '.
1.458     bisitz    526:        '<table border="0" width="100%"><tr><td bgcolor="#F8F8F8">';
1.105     albertel  527: }
                    528: 
                    529: sub problem_edit_footer {
1.412     albertel  530:     return '</td></tr></table><br />
                    531: <div class="LC_edit_problem_footer">
1.453     bisitz    532:   <hr />'.
1.423     www       533: &problem_edit_buttons().'
1.459     bisitz    534:   <hr style="clear:both;" />
1.412     albertel  535: </div>
                    536: '.
1.342     albertel  537:     "\n</form>\n".&Apache::loncommon::end_page();
1.105     albertel  538: }
                    539: 
1.235     albertel  540: sub option {
                    541:     my ($value,$name) = @_;
                    542:     my $result ="<option value='".$value."' ";
1.284     albertel  543:     if ($env{'form.'.$name} eq $value) {
1.235     albertel  544: 	$result.=" selected='on' ";
                    545:     }
                    546:     $result.='>';
                    547:     return $result;
                    548: }
                    549: 
1.105     albertel  550: sub problem_web_to_edit_header {
1.159     albertel  551:     my ($rndseed)=@_;
1.406     albertel  552:     my $result .= '<div class="LC_edit_problem_header">';
                    553: 
                    554:     if (!$Apache::lonhomework::parsing_a_task) {
                    555: 	$result .= 
                    556: 	    '<div class="LC_edit_problem_header_title">'.
                    557: 	    &mt('Problem Testing').
                    558: 	    &Apache::loncommon::help_open_topic('Problem_Editor_Testing_Area').
                    559: 	    '</div>';
                    560:     } else {
                    561: 	$result .= 
                    562: 	    '<div class="LC_edit_problem_header_title">'.
                    563: 	    &mt('Task Testing').
                    564: 	    '</div>';
                    565:     }
                    566:     
1.315     albertel  567:     my $show_all_foils_text = 
                    568: 	($Apache::lonhomework::parsing_a_task) ?
1.452     bisitz    569: 	&mt('Show All Instances')
                    570: 	: &mt('Show All Foils');
1.315     albertel  571: 
1.452     bisitz    572:     my $show_all= '<span class="LC_nobreak"><label for="showallfoils">'
                    573:                  .'<input type="checkbox" name="showallfoils"';
1.440     bisitz    574:     if (defined($env{'form.showallfoils'})) { $show_all.=' checked="checked"'; }
1.452     bisitz    575:     $show_all.= ' /> '.$show_all_foils_text
                    576:                .'</label></span>';
1.406     albertel  577: 
                    578: 
1.384     albertel  579: 
1.406     albertel  580:     $result .= '<div class="LC_edit_problem_header_status_row">';
1.313     albertel  581:     if (!$Apache::lonhomework::parsing_a_task) {
                    582: 	$result.="
1.406     albertel  583: <div class='LC_edit_problem_header_row1'>
                    584: <span class=\"LC_nobreak\">
1.405     albertel  585: ".&mt("Problem Status:")."
1.235     albertel  586: <select name='problemstate'>
1.270     albertel  587:   <option value=''></option>
1.235     albertel  588:   ".&option('CLOSED'               ,'problemstate').&mt("Closed")."</option>
                    589:   ".&option('CAN_ANSWER'           ,'problemstate').&mt("Answerable")."</option>
                    590:   ".&option('CANNOT_ANSWER_tries'  ,'problemstate').&mt("Open with full tries")."</option>
                    591:   ".&option('CANNOT_ANSWER_correct','problemstate').&mt("Open and correct")."</option>
                    592:   ".&option('SHOW_ANSWER'          ,'problemstate').&mt("Show Answer")."</option>
                    593: </select>
1.406     albertel  594: </span>
                    595: <span class=\"LC_nobreak\">
1.405     albertel  596: ".&mt("Problem Type:")."
1.235     albertel  597: <select name='problemtype'>
1.270     albertel  598:   <option value=''></option>
1.242     albertel  599:   ".&option('exam'   ,'problemtype').&mt("Exam Problem")."</option>
1.428     raeburn   600:   ".&option('problem','problemtype').&mt("Homework Problem")."</option>
1.242     albertel  601:   ".&option('survey' ,'problemtype').&mt("Survey Question")."</option>
1.465     raeburn   602:   ".&option('surveycred' ,'problemtype').&mt("Survey Question (with credit)")."</option>
                    603:   ".&option('anonsurvey' ,'problemtype').&mt("Anonymous Survey Question")."</option>
                    604:   ".&option('anonsurveycred' ,'problemtype').&mt("Anonymous Survey Question (with credit)")."</option>
1.428     raeburn   605:   ".&option('practice' ,'problemtype').&mt("Practice Problem")."</option>
1.465.2.7  raeburn   606:   ".&option('randomizetry' ,'problemtype').&mt("New Randomization Each Try")."</option>
1.235     albertel  607: </select>
1.406     albertel  608: </span>
                    609: $show_all
                    610: </div>
                    611: <div class='LC_edit_problem_header_row2'>
                    612: <span class=\"LC_nobreak\">
1.405     albertel  613: ".&mt("Feedback Mode:")."
1.235     albertel  614: <select name='problemstatus'>
                    615:   <option value=''></option>
1.242     albertel  616:   ".&option('yes','problemstatus').&mt("Show Feedback")."</option>
1.405     albertel  617:   ".&option('no', 'problemstatus').&mt("Don't Show Incorect/Correct Feedback")."</option>
                    618:   ".&option('no_feedback_ever', 'problemstatus').&mt("Don't Show Any Feedback")."</option>
1.235     albertel  619: </select>
1.406     albertel  620: </span>
                    621: ";
                    622: 
1.376     albertel  623:     } elsif ($Apache::lonhomework::parsing_a_task) {
                    624: 	$result.="
1.406     albertel  625: <div class='LC_edit_problem_header_row1'>
                    626: <span class=\"LC_nobreak\">
1.405     albertel  627: ".&mt("Problem Status:")."
1.376     albertel  628: <select name='problemstate'>
                    629:   <option value=''></option>
                    630:   ".&option('CLOSED'               ,'problemstate').&mt("Closed")."</option>
                    631:   ".&option('CAN_ANSWER'           ,'problemstate').&mt("Answerable")."</option>
                    632:   ".&option('WEB_GRADE'            ,'problemstate').&mt("Criteria Grading")."</option>
                    633:   ".&option('SHOW_ANSWER'          ,'problemstate').&mt("Show Feedback")."</option>
                    634: </select>
1.406     albertel  635: </span>
                    636: $show_all
                    637: ";
                    638:     }
                    639:     $result.='
                    640:        <span class="LC_nobreak">
                    641:        '.&mt('Apply style file: ').'
                    642:          <input type="text" name="style_file" value="'.&HTML::Entities::encode($env{'construct.style'},'"<>&').'" />
                    643:          <a href="javascript:openbrowser(\'lonhomework\',\'style_file\',\'sty\')">'.&mt('Select').'</a>
                    644:        </span>
1.422     www       645:      </div>
                    646:      <div class="LC_edit_problem_header_row1">'.
                    647:        &Apache::lonxml::renderingoptions().'
1.406     albertel  648:      </div>
                    649:      <input type="submit" name="changeproblemmode" value="'.&mt("Change View").'" />
                    650:      <input type="submit" name="clear_style_file" accesskey="d" value="'.&mt('Show Default View').'" />
                    651:      <input type="submit" name="resetdata" accesskey="r" value="'.&mt('Reset Submissions').'" />
                    652:    </div>
1.453     bisitz    653:    <hr />
1.406     albertel  654:    <div class="LC_edit_problem_header_randomize_row">
                    655:      <input type="submit" name="newrandomization" accesskey="a" value="'.&mt('New Randomization').'" />
                    656:      <input type="submit" name="changerandseed" value="'.&mt('Change Random Seed To:').'" />
                    657:      <input type="text" name="rndseed" size="10" value="'.
                    658: 	       $rndseed.'"
                    659:              onchange="javascript:document.lonhomework.changerandseed.click()" />';
                    660: 
                    661:     if (!$Apache::lonhomework::parsing_a_task) {
                    662: 	my $numtoanalyze=$env{'form.numtoanalyze'};
                    663: 	if (!$numtoanalyze) { $numtoanalyze=20; }
1.408     albertel  664: 	$result .= '<span class="LC_nobreak">'.
                    665: 	    &mt('[_1] for [_2] versions.',
1.416     raeburn   666: 		'<input type="button" name="submitmode" value="'.&mt('Calculate answers').'" '.
1.419     bisitz    667:                 'onclick="javascript:setmode(this.form,'."'calcanswers'".')" />'
                    668:                ,'<input type="text" name="numtoanalyze" value="'.
1.408     albertel  669: 		$numtoanalyze.'" size="5" />').
                    670: 		&Apache::loncommon::help_open_topic("Analyze_Problem",'',undef,undef,300).
                    671: 		'</span>';
                    672: 						    
1.313     albertel  673:     }
1.406     albertel  674: 
                    675:     $result.='
                    676:    </div>
1.453     bisitz    677:    <hr />
1.447     bisitz    678:    <div>';
1.416     raeburn   679:     $result.='<input type="hidden" name="problemmode" value="view" />';
                    680:     $result .= '<input type="button" name="submitmode" accesskey="e" value="'.&mt('Edit').'" '.
                    681:                'onclick="javascript:setmode(this.form,'."'edit'".')" />';
                    682:     $result .= '<input type="button" name="submitmode" accesskey="x" value="'.&mt('EditXML').'" '.
                    683:                'onclick="javascript:setmode(this.form,'."'editxml'".')" />';
1.408     albertel  684:     $result.='
                    685:    </div>
1.453     bisitz    686:    <hr />
1.409     albertel  687:    '.&Apache::lonxml::message_location().'
1.406     albertel  688: </div>';
1.159     albertel  689:     return $result;
1.48      albertel  690: }
                    691: 
1.65      albertel  692: sub initialize_storage {
1.357     albertel  693:     my ($given_symb) = @_;
1.353     albertel  694:     undef(%Apache::lonhomework::results);
                    695:     undef(%Apache::lonhomework::history);
1.357     albertel  696:     my ($symb,$courseid,$domain,$name) = 
1.367     albertel  697: 	&Apache::lonnet::whichuser($given_symb);
1.353     albertel  698:     
                    699:     # anonymous users (CODEd exams) have no data
                    700:     if ($name eq 'anonymous' 
                    701: 	&& !defined($domain)) {
                    702: 	return;
                    703:     }
                    704: 
1.333     albertel  705:     if ($env{'request.state'} eq 'construct' 
                    706: 	|| $symb eq ''
                    707: 	|| $Apache::lonhomework::type eq 'practice') {
                    708: 	
                    709: 	my $namespace = $symb || $env{'request.uri'};
                    710: 	if ($env{'form.resetdata'} eq &mt('Reset Submissions') ||
1.374     albertel  711: 	    ($env{'form.resetdata'} eq &mt('New Problem Variation')
                    712: 	     && $env{'form.submitted'} eq 'yes') ||
1.333     albertel  713: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
                    714: 	    &Apache::lonnet::tmpreset($namespace,'',$domain,$name);
                    715: 	    &Apache::lonxml::debug("Attempt reset");
                    716: 	}
1.159     albertel  717: 	%Apache::lonhomework::history=
1.333     albertel  718: 	    &Apache::lonnet::tmprestore($namespace,'',$domain,$name);
1.159     albertel  719: 	my ($temp)=keys %Apache::lonhomework::history ;
                    720: 	&Apache::lonxml::debug("Return message of $temp");
                    721:     } else {
                    722: 	%Apache::lonhomework::history=
                    723: 	    &Apache::lonnet::restore($symb,$courseid,$domain,$name);
                    724:     }
1.353     albertel  725: 
1.159     albertel  726:     #ignore error conditions
1.67      albertel  727:     my ($temp)=keys %Apache::lonhomework::history ;
1.159     albertel  728:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.65      albertel  729: }
                    730: 
1.435     jms       731: =pod
                    732: 
                    733: =item finalize_storage()
                    734: 
                    735: 	Stores away the result has to a student's environment
                    736: 	checks form.grade_ for specific values, other wises stores
                    737: 	to the running users environment
                    738: 	Will increment totals for attempts, students, and corrects
                    739: 	if running user has student role.
                    740: 	
                    741: =cut
                    742: 
                    743: 
1.65      albertel  744: sub finalize_storage {
1.357     albertel  745:     my ($given_symb) = @_;
1.159     albertel  746:     my $result;
1.289     albertel  747:     if (%Apache::lonhomework::results) {
1.323     albertel  748: 	my @remove = grep(/^INTERNAL_/,keys(%Apache::lonhomework::results));
                    749: 	delete(@Apache::lonhomework::results{@remove});
1.357     albertel  750: 	my ($symb,$courseid,$domain,$name) = 
1.367     albertel  751: 	    &Apache::lonnet::whichuser($given_symb);
1.333     albertel  752: 	if ($env{'request.state'} eq 'construct' 
                    753: 	    || $symb eq ''
                    754: 	    || $Apache::lonhomework::type eq 'practice') {
                    755: 	    my $namespace = $symb || $env{'request.uri'};
1.284     albertel  756: 	    $Apache::lonhomework::results{'rndseed'}=$env{'form.rndseed'};
1.159     albertel  757: 	    $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
1.333     albertel  758: 					      $namespace,'',$domain,$name);
1.159     albertel  759: 	    &Apache::lonxml::debug('Construct Store return message:'.$result);
                    760: 	} else {
                    761: 	    $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
                    762: 					    $symb,$courseid,$domain,$name);
                    763: 	    &Apache::lonxml::debug('Store return message:'.$result);
1.465.2.3  raeburn   764:             &store_aggregates($symb,$courseid);
1.159     albertel  765: 	}
1.323     albertel  766:     } else {
                    767: 	&Apache::lonxml::debug('Nothing to store');
1.67      albertel  768:     }
1.159     albertel  769:     return $result;
1.65      albertel  770: }
                    771: 
1.435     jms       772: =pod
                    773: 
                    774: item store_aggregates()
                    775: 
                    776: 	Sends hash of values to be incremented in nohist_resourcetracker.db
                    777: 	for the course. Increments total number of attempts, unique students 
                    778: 	and corrects for each part for an instance of a problem, as appropriate.
                    779: 	
                    780: =cut
                    781: 
1.285     raeburn   782: sub store_aggregates {
                    783:     my ($symb,$courseid) = @_;
1.465.2.7  raeburn   784:     my (%aggregate,%anoncounter,%randtrycounter);
1.286     albertel  785:     my @parts;
1.288     albertel  786:     my $cdomain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                    787:     my $cname = $env{'course.'.$env{'request.course.id'}.'.num'};
1.286     albertel  788:     foreach my $key (keys(%Apache::lonhomework::results)) {
1.287     albertel  789:         if ($key =~ /resource\.([^\.]+)\.tries/) {
1.286     albertel  790:             push(@parts, $1);
1.285     raeburn   791:         }
                    792:     }
1.286     albertel  793:     foreach my $part (@parts) {
1.465.2.3  raeburn   794:         if ($env{'request.role'} =~/^st/) {
                    795:             if ($Apache::lonhomework::results{'resource.'.$part.'.award'}
                    796:                 eq 'APPROX_ANS' ||
                    797:                 $Apache::lonhomework::results{'resource.'.$part.'.award'}
                    798:                 eq 'EXACT_ANS') {
                    799:                 $aggregate{$symb."\0".$part."\0correct"} = 1;
                    800:             }
                    801:             if ($Apache::lonhomework::results{'resource.'.$part.'.tries'} == 1) {
                    802:                 $aggregate{$symb."\0".$part."\0users"} = 1;
                    803:             } else {
                    804:                 my (undef,$last_reset) = &Apache::grades::get_last_resets($symb,$env{'request.course.id'},[$part]);
                    805:                 if ($last_reset) {
                    806:                     if (&Apache::grades::get_num_tries(\%Apache::lonhomework::history,$last_reset,$part) == 0) {
                    807:                         $aggregate{$symb."\0".$part."\0users"} = 1;
                    808:                     }
                    809:                 }
                    810:             }
                    811:             $aggregate{$symb."\0".$part."\0attempts"} = 1;
1.285     raeburn   812:         }
1.465.2.3  raeburn   813:         if (($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'anonsurvey') ||
1.465.2.7  raeburn   814:             ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'anonsurveycred') ||
                    815:             ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'randomizetry')) {
                    816:             if ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'randomizetry') {
                    817:                 $randtrycounter{$symb."\0".$part} = 1;
                    818:             } else {
                    819:                 $anoncounter{$symb."\0".$part} = 1;
                    820:             }
1.465.2.3  raeburn   821:             my $needsrelease = $Apache::lonnet::needsrelease{'parameter:type:'.$Apache::lonhomework::results{'resource.'.$part.'.type'}};
                    822:             if ($needsrelease) {
                    823:                 my $curr_required = $env{'course.'.$env{'request.course.id'}.'.internal.releaserequired'};
                    824:                 if ($curr_required eq '') {
                    825:                     &Apache::lonnet::update_released_required($needsrelease);
                    826:                 } else {
                    827:                     my ($currmajor,$currminor) = split(/\./,$curr_required);
                    828:                     my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                    829:                     if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
                    830:                         &Apache::lonnet::update_released_required($needsrelease);
                    831:                     }
1.292     raeburn   832:                 }
                    833:             }
1.285     raeburn   834:         }
                    835:     }
1.292     raeburn   836:     if (keys (%aggregate) > 0) {
1.289     albertel  837: 	&Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
1.292     raeburn   838:                             $cdomain,$cname);
                    839:     }
1.465.2.3  raeburn   840:     if (keys(%anoncounter) > 0) {
1.465.2.8  raeburn   841:         &Apache::lonnet::cput('nohist_anonsurveys',\%anoncounter,
                    842:                               $cdomain,$cname);
1.465.2.3  raeburn   843:     }
1.465.2.7  raeburn   844:     if (keys(%randtrycounter) > 0) {
1.465.2.8  raeburn   845:         &Apache::lonnet::cput('nohist_randomizetry',\%randtrycounter,
                    846:                               $cdomain,$cname);
1.465.2.7  raeburn   847:     }
1.292     raeburn   848: }
1.289     albertel  849: 
1.65      albertel  850: sub checkout_msg {
1.211     albertel  851:     my %lt=&Apache::lonlocal::texthash( 
                    852: 		'resource'=>'The resource needs to be checked out',
                    853: 		'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.',
                    854:                 'warning'=>'Checking out resources is subject to course policies, and may exclude future credit even if done erroneously.',
                    855:                 'checkout'=>'Check out Exam for Viewing',
                    856: 		'checkout?'=>'Check out Exam?');
1.352     albertel  857:     my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.159     albertel  858:     return (<<ENDCHECKOUT);
1.211     albertel  859: <h2>$lt{'resource'}</h2>
                    860:     <p>$lt{'id_expln'}</p>
1.449     bisitz    861: <p class="LC_warning">$lt{'warning'}</p>
1.444     bisitz    862: <form name="checkout" method="post" action="$uri">
1.91      albertel  863: <input type="hidden" name="doescheckout" value="yes" />
1.211     albertel  864: <input type="button" name="checkoutbutton" value="$lt{'checkout'}" onClick="javascript:if (confirm('$lt{'checkout?'}')) { document.checkout.submit(); }" />
1.65      albertel  865: </form>
                    866: ENDCHECKOUT
                    867: }
                    868: 
1.252     albertel  869: sub firstaccess_msg {
1.253     albertel  870:     my ($time,$symb)=@_;
1.414     albertel  871:     my $result;
                    872:     my @interval=&Apache::lonnet::EXT("resource.0.interval");
                    873:     if ($interval[1] eq 'map') {
                    874: 	my ($map)=&Apache::lonnet::decode_symb($symb);
                    875: 	my $foldertitle=&Apache::lonnet::gettitle($map);
                    876:     
                    877: 	&Apache::lonxml::debug("map is $map title is $foldertitle");
1.418     bisitz    878: 	$result .= "<h2>".&mt('The resources in "[_1]" are open for a limited time.'
                    879:                              .' Once you click the "Show Resource" button below you have [_2] to complete all resources "[_1]".'
                    880:                              ,$foldertitle,$time)."</h2>";
1.414     albertel  881:     } elsif ($interval[1] eq 'course') {
                    882: 	my $course = $env{'course.'.$env{'request.course.id'}.'.description'};
1.418     bisitz    883:         $result .= "<h2>".&mt('The resources in "[_1]" are open for a limited time.'
                    884:                              .' Once you click the "Show Resource" button below you have [_2] to complete all resources "[_1]".'
                    885:                              ,$course,$time)."</h2>";
1.414     albertel  886:     } else {
                    887: 	my $title=&Apache::lonnet::gettitle($symb);
1.418     bisitz    888:         $result .= "<h2>".&mt('This resource "[_1]" is open for a limited time.'
                    889:                              .' Once you click the "Show Resource" button below you have [_2] to complete this resource "[_1]".'
                    890:                              ,$title,$time)."</h2>";
1.414     albertel  891:     }
1.352     albertel  892:     my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.418     bisitz    893:     my $buttontext = &mt('Show Resource');
                    894:     my $timertext = &mt('Start Timer?');
1.414     albertel  895:     $result .= (<<ENDCHECKOUT);
1.444     bisitz    896: <form name="markaccess" method="post" action="$uri">
1.252     albertel  897: <input type="hidden" name="markaccess" value="yes" />
1.418     bisitz    898: <input type="button" name="accessbutton" value="$buttontext" onClick="javascript:if (confirm('$timertext')) { document.markaccess.submit(); }" />
1.252     albertel  899: </form>
                    900: ENDCHECKOUT
1.414     albertel  901:     return $result;
1.252     albertel  902: }
                    903: 
1.204     albertel  904: sub init_problem_globals {
                    905:     my ($type)=@_;
                    906:     #initialize globals
1.308     foxr      907:     #   For problems, we start out in part 0 (outside a <part> tag).
                    908:     #   and part 0 is used to describe the main body of the <problem>
                    909:     #
1.204     albertel  910:     if ($type eq 'problem') {
                    911: 	$Apache::inputtags::part='0';
                    912: 	@Apache::inputtags::partlist=('0');
1.405     albertel  913: 	&Apache::lonhomework::set_show_problem_status(&get_problem_status('0'));
1.266     albertel  914: 	$Apache::lonhomework::ignore_response_errors=0;
1.308     foxr      915: 
1.266     albertel  916:     } elsif ($type eq 'library') {
1.204     albertel  917: 	$Apache::inputtags::part='';
                    918: 	@Apache::inputtags::partlist=();
1.405     albertel  919: 	&Apache::lonhomework::reset_show_problem_status();
1.266     albertel  920: 	$Apache::lonhomework::ignore_response_errors=1;
1.308     foxr      921: 
1.304     albertel  922:     } elsif ($type eq 'Task') {
                    923: 	$Apache::inputtags::part='0';
                    924: 	@Apache::inputtags::partlist=('0');
1.405     albertel  925: 	&Apache::lonhomework::reset_show_problem_status();
1.304     albertel  926: 	$Apache::lonhomework::ignore_response_errors=1;
1.204     albertel  927:     }
1.465.2.11  raeburn   928:     @Apache::functionplotresponse::callscripts=();
1.204     albertel  929:     @Apache::inputtags::responselist = ();
                    930:     @Apache::inputtags::importlist = ();
                    931:     @Apache::inputtags::previous=();
                    932:     @Apache::inputtags::previous_version=();
                    933:     $Apache::structuretags::printanswer='No';
                    934:     @Apache::structuretags::whileconds=();
                    935:     @Apache::structuretags::whilebody=();
                    936:     @Apache::structuretags::whileline=();
                    937:     $Apache::lonhomework::scantronmode=0;
                    938:     undef($Apache::lonhomework::name);
1.358     albertel  939:     undef($Apache::lonhomework::default_type);
                    940:     undef($Apache::lonhomework::type);
1.204     albertel  941: }
                    942: 
                    943: sub reset_problem_globals {
                    944:     my ($type)=@_;
                    945:     undef(%Apache::lonhomework::history);
                    946:     undef(%Apache::lonhomework::results);
                    947:     undef($Apache::inputtags::part);
1.465.2.15! raeburn   948:     if ($type eq 'Task') {
        !           949:         undef($Apache::inputtags::slot_name);
        !           950:     }
1.208     albertel  951: #don't undef this, lonhomework.pm takes care of this, we use this to 
                    952: #detect if we try to do 2 problems in one file
                    953: #   undef($Apache::lonhomework::parsing_a_problem);
1.204     albertel  954:     undef($Apache::lonhomework::name);
1.358     albertel  955:     undef($Apache::lonhomework::default_type);
                    956:     undef($Apache::lonhomework::type);
                    957:     undef($Apache::lonhomework::scantronmode);
                    958:     undef($Apache::lonhomework::ignore_response_errors);
1.465.2.11  raeburn   959:     undef(@Apache::functionplotresponse::callscripts);
1.405     albertel  960:     &Apache::lonhomework::reset_show_problem_status();
1.204     albertel  961: }
                    962: 
1.241     albertel  963: sub set_problem_state {
1.240     albertel  964:     my ($part)=@_;
1.284     albertel  965:     if ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct') {
1.240     albertel  966: 	$Apache::lonhomework::history{"resource.$part.solved"}=
                    967: 	    'correct_by_student';
                    968:     }
                    969: }
                    970: 
1.241     albertel  971: sub get_problem_status {
                    972:     my ($part)=@_;
1.267     albertel  973:     my $problem_status;
1.284     albertel  974:     if ($env{'request.state'} eq 'construct' &&
                    975: 	defined($env{'form.problemstatus'})) {
                    976: 	$problem_status=$env{'form.problemstatus'};
1.267     albertel  977:     } else {
                    978: 	$problem_status=&Apache::lonnet::EXT("resource.$part.problemstatus");
                    979: 	&Apache::lonxml::debug("problem status for $part is $problem_status");
1.284     albertel  980: 	&Apache::lonxml::debug("env probstat is ".$env{'form.problemstatus'});
1.241     albertel  981:     }
                    982:     return $problem_status;
                    983: }
                    984: 
1.9       albertel  985: sub start_problem {
1.326     albertel  986:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.19      albertel  987: 
1.311     foxr      988:     # We'll use the redirection to fix up printing of duedates.
1.321     albertel  989:     if (!$Apache::lonxml::metamode) {
                    990: 	&Apache::lonxml::startredirection();
                    991:     }
1.311     foxr      992: 
1.308     foxr      993:     # Problems don't nest and we don't allow more than one <problem> in
                    994:     # a .problem file.
                    995:     #
1.184     albertel  996:     if ( $Apache::inputtags::part ne '' ||
                    997: 	 $Apache::lonhomework::parsing_a_problem) {
                    998: 	&Apache::lonxml::error('Only one &lt;problem&gt; allowed in a .problem file');
1.326     albertel  999: 	#my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,$style);
1.159     albertel 1000: 	return '';
                   1001:     }
1.184     albertel 1002: 
                   1003:     $Apache::lonhomework::parsing_a_problem=1;
1.204     albertel 1004:     &init_problem_globals('problem');
1.166     albertel 1005: 
1.284     albertel 1006:     if (defined($env{'scantron.maxquest'})) {
1.166     albertel 1007: 	$Apache::lonhomework::scantronmode=1;
                   1008:     }
1.161     albertel 1009: 
1.159     albertel 1010:     if ($target ne 'analyze') {
1.415     raeburn  1011:         my $type = &Apache::lonnet::EXT('resource.0.type');
                   1012: 	$Apache::lonhomework::type=$type;
1.284     albertel 1013: 	if (($env{'request.state'} eq 'construct') &&
1.410     albertel 1014: 	    $env{'form.problemtype'} =~ /\S/) {
1.284     albertel 1015: 	    $Apache::lonhomework::type=$env{'form.problemtype'};
1.237     albertel 1016: 	}
1.332     albertel 1017: 	&Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
1.159     albertel 1018:     }
1.164     albertel 1019:     if ($Apache::lonhomework::type eq '' ) {
1.284     albertel 1020: 	my $uri=$env{'request.uri'};
1.159     albertel 1021: 	if ($uri=~/\.(\w+)$/) {
                   1022: 	    $Apache::lonhomework::type=$1;
                   1023: 	    &Apache::lonxml::debug("Using type of $1");
                   1024: 	} else {
                   1025: 	    $Apache::lonhomework::type='problem';
                   1026: 	    &Apache::lonxml::debug("Using default type, problem, :$uri:");
                   1027: 	}
1.87      albertel 1028:     }
1.301     albertel 1029:     $Apache::lonhomework::default_type = $Apache::lonhomework::type;
1.58      www      1030: 
1.363     albertel 1031:     &initialize_storage();
1.389     albertel 1032:     if ($target ne 'analyze'
                   1033:        	&& $env{'request.state'} eq 'construct') {
                   1034: 	&set_problem_state('0');
                   1035:     }
                   1036: 
1.366     albertel 1037:     if ($target eq 'web') {
                   1038: 	&Apache::lonxml::debug(" grading history ");
                   1039: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
                   1040:     }
1.363     albertel 1041: 
1.159     albertel 1042:     #added vars to the scripting enviroment
1.213     albertel 1043:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.248     albertel 1044:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.24      albertel 1045:     &Apache::run::run($expression,$safeeval);
1.159     albertel 1046:     my $status;
                   1047:     my $accessmsg;
                   1048: 
1.343     albertel 1049:     my $name= &get_resource_name($parstack,$safeeval);
1.350     albertel 1050:     my ($result,$form_tag_start);
1.354     albertel 1051:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'tex'
                   1052: 	|| $target eq 'edit') {
1.350     albertel 1053: 	($result,$form_tag_start) =
                   1054: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1055: 			$name);
                   1056:     }
                   1057: 
1.284     albertel 1058:     if ($target eq 'tex' and $env{'request.symb'} =~ m/\.page_/) {$result='';}
1.159     albertel 1059: 
1.465.2.7  raeburn  1060:     if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval,$target); }
1.159     albertel 1061:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1062: 	$target eq 'tex') {
                   1063: 	#handle exam checkout
                   1064: 	if ($Apache::lonhomework::type eq 'exam') {
                   1065: 	    my $token=
                   1066: 		$Apache::lonhomework::history{"resource.0.outtoken"};
1.284     albertel 1067: 	    if (($env{'form.doescheckout'}) && (!$token)) {
1.159     albertel 1068: 		$token=&Apache::lonxml::maketoken();
                   1069: 		$Apache::lonhomework::history{"resource.0.outtoken"}=
                   1070: 		    $token;
                   1071: 	    }
1.343     albertel 1072: 	    $result.=&Apache::lonxml::printtokenheader($target,$token);
1.142     albertel 1073: 	}
1.284     albertel 1074: 	if ($env{'form.markaccess'}) {
1.414     albertel 1075: 	    my @interval=&Apache::lonnet::EXT("resource.0.interval");
                   1076: 	    &Apache::lonnet::set_first_access($interval[1]);
1.252     albertel 1077: 	}
1.465.2.7  raeburn  1078: 
                   1079:         ($status,$accessmsg,my $slot_name,my $slot) =
                   1080:             &Apache::lonhomework::check_slot_access('0','problem');
                   1081:         push (@Apache::inputtags::status,$status);
                   1082: 
1.159     albertel 1083: 	#handle rand seed in construction space
1.465.2.7  raeburn  1084: 	my $rndseed=&setup_rndseed($safeeval,$target);
1.367     albertel 1085: 	my ($symb)=&Apache::lonnet::whichuser();
1.465.2.7  raeburn  1086: 
1.333     albertel 1087: 	if ($env{'request.state'} ne "construct" && 
                   1088: 	    ($symb eq '' || $Apache::lonhomework::type eq 'practice')) {
1.162     albertel 1089: 	    $form_tag_start.='<input type="hidden" name="rndseed" value="'.
1.462     raeburn  1090: 		$rndseed.'" />'.
                   1091: 		    '<input type="submit" name="resetdata"
                   1092:                              value="'.&mt('New Problem Variation').'" />';
1.334     albertel 1093: 	    if (exists($env{'form.username'})) {
                   1094: 		$form_tag_start.=
1.164     albertel 1095: 		    '<input type="hidden" name="username"
1.284     albertel 1096:                              value="'.$env{'form.username'}.'" />';
1.334     albertel 1097: 	    }
1.462     raeburn  1098: 	    if ($env{'request.role.adv'}) {
                   1099: 		$form_tag_start.= ' <label class="LC_nobreak">'
                   1100:                          .'<input type="checkbox" name="showallfoils"';
                   1101: 		if (defined($env{'form.showallfoils'})) {
                   1102: 		    $form_tag_start.=' checked="checked"';
                   1103: 		}
                   1104:                 $form_tag_start.= ' /> '
                   1105:                                  .&mt('Show All Foils')
                   1106:                                  .'</label>';
                   1107: 	    }
1.417     www      1108:             if ($Apache::lonhomework::type eq 'practice') {
1.428     raeburn  1109:                 $form_tag_start.=&practice_problem_header();
1.417     www      1110:             }
1.462     raeburn  1111: 	    $form_tag_start.='<hr />';
1.465.2.7  raeburn  1112:         } elsif (($env{'request.state'} ne "construct") &&
                   1113:                  ($Apache::lonhomework::type eq 'randomizetry') &&
                   1114:                  ($status eq 'CAN_ANSWER')) {
                   1115:             my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                   1116:             my $problemstatus = &get_problem_status($Apache::inputtags::part);
                   1117:             $form_tag_start.=&randomizetry_problem_header($problemstatus,$reqtries);
                   1118:         }
1.324     albertel 1119: 
1.159     albertel 1120: 	my $expression='$external::datestatus="'.$status.'";';
                   1121: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                   1122: 	&Apache::run::run($expression,$safeeval);
                   1123: 	&Apache::lonxml::debug("Got $status");
1.324     albertel 1124: 
1.159     albertel 1125: 	if (( $status eq 'CLOSED' ) ||
                   1126: 	    ( $status eq 'UNCHECKEDOUT') ||
1.252     albertel 1127: 	    ( $status eq 'NOT_YET_VIEWED') ||
1.159     albertel 1128: 	    ( $status eq 'BANNED') ||
1.216     albertel 1129: 	    ( $status eq 'UNAVAILABLE') ||
1.324     albertel 1130: 	    ( $status eq 'NOT_IN_A_SLOT') ||
1.216     albertel 1131: 	    ( $status eq 'INVALID_ACCESS')) {
1.326     albertel 1132: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                   1133: 						       $style);
1.159     albertel 1134: 	    if ( $target eq "web" ) {
1.343     albertel 1135: 		my $msg;
1.159     albertel 1136: 		if ($status eq 'UNAVAILABLE') {
1.245     albertel 1137: 		    $msg.='<h1>'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</h1>';
1.441     raeburn  1138:                 } elsif ($status eq 'NOT_IN_A_SLOT') {
                   1139:                     $msg.='<h1>'.&mt('You are not currently signed up to work at this time and/or place.').'</h1>';
1.253     albertel 1140: 		} elsif ($status ne 'NOT_YET_VIEWED') {
1.245     albertel 1141: 		    $msg.='<h1>'.&mt('Not open to be viewed').'</h1>';
1.159     albertel 1142: 		}
1.216     albertel 1143: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
1.393     www      1144: 		    $msg.=&mt('The problem ').$accessmsg;
1.159     albertel 1145: 		} elsif ($status eq 'UNCHECKEDOUT') {
1.343     albertel 1146: 		    $msg.=&checkout_msg();
1.252     albertel 1147: 		} elsif ($status eq 'NOT_YET_VIEWED') {
1.253     albertel 1148: 		    $msg.=&firstaccess_msg($accessmsg,$symb);
1.325     albertel 1149: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
1.441     raeburn  1150: 		    $msg.=&Apache::bridgetask::add_request_another_attempt_button("Sign up for time to work");
1.159     albertel 1151: 		}
                   1152: 		$result.=$msg.'<br />';
                   1153: 	    } elsif ($target eq 'tex') {
1.332     albertel 1154: 		my $startminipage = ($env{'form.problem_split'}=~/yes/i)? ''
                   1155: 		                    : '\begin{minipage}{\textwidth}';
1.443     foxr     1156: 		$result.='\noindent \vskip 1 mm '.
1.332     albertel 1157: 		    $startminipage.'\vskip 0 mm';
1.159     albertel 1158: 		if ($status eq 'UNAVAILABLE') {
1.211     albertel 1159: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
1.159     albertel 1160: 		} else {
1.211     albertel 1161: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
1.159     albertel 1162: 		}
                   1163: 	    }
1.324     albertel 1164: 	} elsif ($status eq 'NEEDS_CHECKIN') {
1.326     albertel 1165: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                   1166: 						       $style);
1.324     albertel 1167: 	    if ($target eq 'web') {
1.375     albertel 1168: 		$result .= 
                   1169: 		    &Apache::bridgetask::proctor_validation_screen($slot);
1.324     albertel 1170: 	    } elsif ($target eq 'grade') {
                   1171: 		&Apache::bridgetask::proctor_check_auth($slot_name,$slot,
                   1172: 							'problem');
                   1173: 	    }
1.159     albertel 1174: 	} elsif ($target eq 'web') {
1.360     albertel 1175: 	    if ($status eq 'CAN_ANSWER' 
                   1176: 		&& $slot_name ne ''
                   1177: 		&& $Apache::lonhomework::history{'resource.0.checkedin'} eq '') {
                   1178: 		# unproctored slot access, self checkin
                   1179: 		&Apache::bridgetask::check_in('problem',undef,undef,
                   1180: 					      $slot_name);
                   1181: 	    }
1.368     albertel 1182: 	    $result.="\n $form_tag_start \t".	
1.227     albertel 1183: 	      '<input type="hidden" name="submitted" value="yes" />';
                   1184: 	    # create a page header and exit
1.284     albertel 1185: 	    if ($env{'request.state'} eq "construct") {
                   1186: 		$result.= &problem_web_to_edit_header($env{'form.rndseed'});
1.428     raeburn  1187:                 if ($Apache::lonhomework::type eq 'practice') {
                   1188:                     $result.= '<input type="submit" name="resetdata" '.
                   1189:                               'value="'.&mt('New Problem Variation').'" />'.
                   1190:                               &practice_problem_header().'<hr />';
                   1191:                 }
1.227     albertel 1192: 	    }
                   1193: 	    # if we are viewing someone else preserve that info
1.284     albertel 1194: 	    if (defined $env{'form.grade_symb'}) {
1.227     albertel 1195: 		foreach my $field ('symb','courseid','domain','username') {
                   1196: 		    $result .= '<input type="hidden" name="grade_'.$field.
1.284     albertel 1197: 			'" value="'.$env{"form.grade_$field"}.'" />'."\n";
1.159     albertel 1198: 		}
1.465.2.7  raeburn  1199:                 foreach my $field ('questiontype','rndseed') {
                   1200:                     if ($env{"form.grade_$field"} ne '') {
                   1201:                         $result .= '<input type="hidden" name="grade_'.$field.
                   1202:                             '" value="'.$env{"form.grade_$field"}.'" />'."\n";
                   1203:                     }
                   1204:                 }
                   1205: 
1.159     albertel 1206: 	    }
                   1207: 	} elsif ($target eq 'tex') {
1.319     foxr     1208: 	    $result .= 'INSERTTEXFRONTMATTERHERE';
                   1209: 
1.99      sakharuk 1210: 	}
1.159     albertel 1211:     } elsif ($target eq 'edit') {
1.343     albertel 1212: 	$result .= $form_tag_start.&problem_edit_header();
1.226     albertel 1213: 	$Apache::lonxml::warnings_error_header=
                   1214: 	    &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 1215: 	my $temp=&Apache::edit::insertlist($target,$token);
                   1216: 	$result.=$temp;
                   1217:     } elsif ($target eq 'modified') {
                   1218: 	$result=$token->[4];
                   1219:     } else {
                   1220: 	# page_start returned a starting result, delete it if we don't need it
                   1221: 	$result = '';
1.99      sakharuk 1222:     }
1.159     albertel 1223:     return $result;
1.9       albertel 1224: }
                   1225: 
                   1226: sub end_problem {
1.159     albertel 1227:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.321     albertel 1228:     my $result;
1.310     foxr     1229: 
1.321     albertel 1230:     if (!$Apache::lonxml::metamode) {
                   1231: 	$result = &Apache::lonxml::endredirection(); #started in &start_problem
1.329     albertel 1232: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel 1233:     }
1.319     foxr     1234: 
                   1235:     if ($target eq 'tex') {
1.321     albertel 1236: 	# Figure out the front matter and replace the
                   1237: 	# INSERTTEXFRONTMATTERHERE in result with it.  note that we do
                   1238: 	# this in end_problem because whether or not we display due
                   1239: 	# dates depends on whether due dates have already been
                   1240: 	# displayed in the problem parts.
                   1241: 
1.319     foxr     1242: 	my $frontmatter   = '';
                   1243: 	my $startminipage = '';
                   1244: 	if (not $env{'form.problem_split'}=~/yes/) {
                   1245: 	    $startminipage = '\begin{minipage}{\textwidth}';
                   1246: 	}
                   1247: 	my $id = $Apache::inputtags::part;
                   1248: 	my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                   1249: 	my $packages=&Apache::lonnet::metadata($env{'request.uri'},'packages');
                   1250: 	my @packages = split /,/,$packages;
                   1251: 	my $allow_print_points = 0;
                   1252: 	foreach my $partial_key (@packages) {
                   1253: 	    if ($partial_key=~m/^part_0$/) {
                   1254: 		$allow_print_points=1;
                   1255: 	    }
                   1256: 	}
                   1257: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1258: 	if (defined($maxtries) && $maxtries < 0) { $allow_print_points=0; }
                   1259: 	if (lc($env{'course.'.$env{'request.course.id'}.
                   1260: 			'.disableexampointprint'}) eq 'yes') {
                   1261: 	    $allow_print_points=0;
                   1262: 	}
                   1263: 	my $name_of_resourse= &Apache::lonxml::latex_special_symbols(&get_resource_name($parstack,$safeeval),'header');
1.443     foxr     1264: 	my $begin_doc=' \typeout{STAMPOFPASSEDRESOURCESTART Resource <h2>"'.$name_of_resourse.'"</h2> located in <br /><small><b>'.$env{'request.uri'}.'</b></small><br /> STAMPOFPASSEDRESOURCEEND} \noindent ';
1.319     foxr     1265: 	my $toc_line='\vskip 1 mm\noindent '.$startminipage.
                   1266: 	    '\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}';
                   1267: 	
                   1268: 	#  Figure out what the due date is and if we need to print
                   1269: 	#  it in the problem header.  We have been logging the
                   1270: 	#  last due date written to file. 
                   1271: 	
                   1272: 	my $duetime = &Apache::lonnet::EXT("resource.$id.duedate"); 
                   1273: 	my $duedate = POSIX::strftime("%c",localtime($duetime));
1.448     bisitz   1274:         my $duedate_text = &mt('Due date: [_1]'
                   1275:                               ,&Apache::lonlocal::locallocaltime($duetime));
1.319     foxr     1276: 	my $temp_file;
                   1277: 	my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.due";
                   1278: 	
                   1279: 	# Figure out what the last printed due date is or set it
                   1280: 	# to the epoch if no duedates have been printed.
                   1281: 	
                   1282: 	my $due_file_content = 0;      #   If the file does not yet exist, time is the epoch.
                   1283: 	if (-e $filename) {
                   1284: 	    $temp_file = Apache::File->new($filename);
                   1285: 	    my @due_file      = <$temp_file>;
                   1286: 	    $due_file_content = $due_file[$#due_file];
                   1287: 	    chomp $due_file_content;
                   1288: 	} 
                   1289: 	
                   1290: 	# We display the due date iff it is not the same as the last
                   1291: 	# duedate in problem header ($due_file_content), and
                   1292: 	# none of our parts displayed a duedate.
                   1293: 	#
                   1294: 	my $parts_with_displayduedate;
                   1295: 	if (defined $Apache::outputtags::showonce{'displayduedate'}) {
                   1296: 	    $parts_with_displayduedate = 
                   1297: 		scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
                   1298: 	} else {
                   1299: 	    $parts_with_displayduedate = 0;
                   1300: 	}
                   1301: 	if (($due_file_content != $duetime) && ($parts_with_displayduedate == 0) ) {
                   1302: 	    $temp_file = Apache::File->new('>'.$filename);
                   1303: 	    print $temp_file "$duetime\n";
                   1304: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                   1305: 		if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
                   1306: 		    $frontmatter .= $begin_doc.
1.448     bisitz   1307: 			'\textit{'.$duedate_text.'} '.$toc_line;
1.319     foxr     1308: 		} else {
                   1309: 		    $frontmatter.= $begin_doc.$toc_line;
1.463     foxr     1310: 		    if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { 
1.465.2.14  raeburn  1311: 			$frontmatter .= '\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}';
1.463     foxr     1312: 		    }
1.319     foxr     1313: 		}
                   1314: 	    } else {
1.448     bisitz   1315: 		$frontmatter .= '\vskip 1mm\textit{'.$duedate_text.'} \\\\\\\\'.$startminipage;
1.319     foxr     1316: 	    }
                   1317: 	} else {
                   1318: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                   1319: 		$frontmatter .= $begin_doc.$toc_line;
1.463     foxr     1320: 		if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { 
1.465.2.14  raeburn  1321: 		    $frontmatter .= '\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}';
1.463     foxr     1322: 		}
1.319     foxr     1323: 	    } else {
1.381     albertel 1324: 		$frontmatter .= '\vskip 1mm \\\\\\\\'.$startminipage;
1.319     foxr     1325: 	    }
                   1326: 	}
                   1327: 	$result =~ s/INSERTTEXFRONTMATTERHERE/$frontmatter/;
                   1328:     }
                   1329: 
1.159     albertel 1330:     my $status=$Apache::inputtags::status['-1'];
                   1331:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                   1332: 	$target eq 'tex') {
1.249     albertel 1333: 	if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
1.159     albertel 1334: 	    # if part is zero, no <part>s existed, so we need to the grading
1.249     albertel 1335: 	    if ($status eq 'CAN_ANSWER' ||$Apache::lonhomework::scantronmode) {
                   1336: 		&Apache::inputtags::grade;
1.324     albertel 1337: 	    } elsif ($status eq 'NEEDS_CHECKIN') {
                   1338: 		# no need to grade, and don't want to hide data
1.249     albertel 1339: 	    } else {
                   1340: 		# move any submission data to .hidden
                   1341: 		&Apache::inputtags::hidealldata($Apache::inputtags::part);
                   1342: 	    }
1.159     albertel 1343: 	} elsif ( ($target eq 'web' || $target eq 'tex') &&
                   1344: 		  $Apache::inputtags::part eq '0' &&
1.465.2.14  raeburn  1345: 		  $status ne 'UNCHECKEDOUT' && $status ne 'NOT_YET_VIEWED') { 
1.159     albertel 1346: 	    # if part is zero, no <part>s existed, so we need show the current
                   1347: 	    # grading status
                   1348: 	    my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
                   1349: 	    $result.= $gradestatus;
                   1350: 	}
                   1351: 	if (
1.284     albertel 1352: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.159     albertel 1353: 	    ($target eq 'answer') || ($target eq 'tex')
                   1354: 	   ) {
1.465.2.14  raeburn  1355: 	    if (($target ne 'tex') &&
                   1356: 		($env{'form.answer_output_mode'} ne 'tex')) {
1.254     www      1357: 		$result.="</form>";
1.159     albertel 1358: 	    }
                   1359: 	    if ($target eq 'web') {
1.346     albertel 1360: 		$result.= &Apache::loncommon::end_page({'discussion' => 1});
1.159     albertel 1361: 	    } elsif ($target eq 'tex') {
1.178     sakharuk 1362: 		my $endminipage = '';
1.284     albertel 1363: 		if (not $env{'form.problem_split'}=~/yes/) {
1.178     sakharuk 1364: 		    $endminipage = '\end{minipage}';
                   1365: 		}
1.284     albertel 1366:                 if ($env{'form.print_discussions'} eq 'yes') {
1.263     sakharuk 1367: 		    $result.=&Apache::lonxml::xmlend($target,$parser);
1.159     albertel 1368: 		} else {
1.262     sakharuk 1369: 		    $result .= '\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
1.284     albertel 1370: 		    if (not $env{'request.symb'} =~ m/\.page_/) {
1.262     sakharuk 1371: 			$result .= $endminipage.'\end{document} ';
                   1372: 		    } else {
1.382     albertel 1373: 			$result .= $endminipage;
1.262     sakharuk 1374: 		    }
1.159     albertel 1375: 		}
                   1376: 	    }
                   1377: 	}
1.465.2.11  raeburn  1378:         if ($target eq 'web') {
                   1379:            $result.=&Apache::functionplotresponse::init_script();
                   1380:         }
1.159     albertel 1381: 	if ($target eq 'grade') {
                   1382: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                   1383: 	    &finalize_storage();
                   1384: 	}
1.284     albertel 1385: 	if ($target eq 'answer' && ($env{'request.state'} eq 'construct')
                   1386: 	    && $env{'form.answer_output_mode'} ne 'tex') {
1.346     albertel 1387: 	    $result.=&Apache::loncommon::end_page({'discussion' => 1});
1.294     albertel 1388: 	                        # normally we get it from above, but in CSTR
1.172     albertel 1389: 	                        # we always show answer mode too.
1.159     albertel 1390: 	}
                   1391:     } elsif ($target eq 'meta') {
                   1392: 	if ($Apache::inputtags::part eq '0') {
1.179     albertel 1393: 	    @Apache::inputtags::response=();
1.159     albertel 1394: 	    $result=&Apache::response::mandatory_part_meta;
                   1395: 	}
1.215     albertel 1396: 	$result.=&Apache::response::meta_part_order();
1.258     albertel 1397: 	$result.=&Apache::response::meta_response_order();
1.159     albertel 1398:     } elsif ($target eq 'edit') {
                   1399: 	&Apache::lonxml::debug("in end_problem with $target, edit");
1.314     albertel 1400: 	$result .= &problem_edit_footer();
1.320     albertel 1401:     } elsif ($target eq 'modified') {
                   1402: 	 $result .= $token->[2];
1.159     albertel 1403:     }
1.155     albertel 1404: 
1.284     albertel 1405:     if ($env{'request.state'} eq 'construct' && $target eq 'web') {
1.177     albertel 1406: 	&Apache::inputtags::check_for_duplicate_ids();
                   1407:     }
1.204     albertel 1408: 
                   1409:     &reset_problem_globals('problem');
1.159     albertel 1410: 
                   1411:     return $result;
1.48      albertel 1412: }
                   1413: 
1.108     albertel 1414: 
1.48      albertel 1415: sub start_library {
1.159     albertel 1416:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.343     albertel 1417:     my ($result,$form_tag_start);
1.371     albertel 1418:     if ($#$tagstack eq 0 && $$tagstack[0] eq 'library') {
1.244     albertel 1419: 	&init_problem_globals('library');
                   1420: 	$Apache::lonhomework::type='problem';
                   1421:     }
1.159     albertel 1422:     if ($target eq 'edit') {
1.343     albertel 1423: 	($result,$form_tag_start)=
                   1424: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1425: 			'Edit');
                   1426: 	$result.=$form_tag_start.&problem_edit_header();
1.159     albertel 1427: 	my $temp=&Apache::edit::insertlist($target,$token);
                   1428: 	$result.=$temp;
                   1429:     } elsif ($target eq 'modified') {
                   1430: 	$result=$token->[4];
1.340     albertel 1431:     } elsif (($target eq 'web' || $target eq 'webgrade')
1.371     albertel 1432: 	     && ($#$tagstack eq 0 && $$tagstack[0] eq 'library')
1.340     albertel 1433: 	     && $env{'request.state'} eq "construct" ) {
1.159     albertel 1434: 	my $name=&get_resource_name($parstack,$safeeval);
1.343     albertel 1435: 	($result,$form_tag_start)=
                   1436: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1437: 			$name);
1.465.2.7  raeburn  1438: 	my $rndseed=&setup_rndseed($safeeval,$target);
1.343     albertel 1439: 	$result.=" \n $form_tag_start".	
1.159     albertel 1440: 		  '<input type="hidden" name="submitted" value="yes" />';
                   1441: 	$result.=&problem_web_to_edit_header($rndseed);
1.428     raeburn  1442:         if ($Apache::lonhomework::type eq 'practice') {
                   1443:             $result.= '<input type="submit" name="resetdata" '.
                   1444:                       'value="'.&mt('New Problem Variation').'" />'.
                   1445:                       &practice_problem_header().'<hr />';
                   1446:         }
1.159     albertel 1447:     }
                   1448:     return $result;
1.48      albertel 1449: }
                   1450: 
                   1451: sub end_library {
1.159     albertel 1452:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1453:     my $result='';
                   1454:     if ($target eq 'edit') {
                   1455: 	$result=&problem_edit_footer();
1.371     albertel 1456:     } elsif ($target eq 'web' 
                   1457: 	     && ($#$tagstack eq 0 && $$tagstack[0] eq 'library') 
                   1458: 	     && $env{'request.state'} eq "construct") {
1.349     albertel 1459: 	$result.='</form>'.&Apache::loncommon::end_page({'discussion' => 1});
1.159     albertel 1460:     }
1.371     albertel 1461:     if ( $#$tagstack eq 0 && $$tagstack[0] eq 'library') {
                   1462: 	&reset_problem_globals('library');
                   1463:     }
1.159     albertel 1464:     return $result;
1.197     www      1465: }
                   1466: 
                   1467: sub start_definetag {
1.326     albertel 1468:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.197     www      1469: 
                   1470:     my $result;
                   1471: 
                   1472:     my $name = $token->[2]->{'name'};
1.326     albertel 1473:     my $skip=&Apache::lonxml::get_all_text("/definetag",$parser,$style);
1.396     albertel 1474:     if ($target eq 'web') {
                   1475: 	if ($name=~/^\//) {
                   1476: 	    $result=
                   1477: 		'<br /><table class="LC_sty_end"><tr><th>'.
                   1478: 		&mt('END [_1]'.'<tt>'.$name.'</tt>').'</th></tr>';
                   1479: 	} else {
                   1480: 	    $result=
                   1481: 		'<br /><table class="LC_sty_begin"><tr><th>'.
                   1482: 		&mt('BEGIN [_1]'.'<tt>'.$name.'</tt>').'</th></tr>';
                   1483: 	}
                   1484: 	$skip = &HTML::Entities::encode($skip, '<>&"');
                   1485: 	$result.='<tr><td><pre>'.$skip.'</pre></td></tr></table>';
1.197     www      1486:     }
                   1487:     return $result;
                   1488: }
                   1489: 
                   1490: sub end_definetag {
                   1491:     return '';
1.1       albertel 1492: }
                   1493: 
                   1494: sub start_block {
1.201     albertel 1495:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.131     albertel 1496: 
                   1497:     my $result;
1.1       albertel 1498: 
1.339     albertel 1499:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer'  ||
                   1500: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.159     albertel 1501: 	my $code = $token->[2]->{'condition'};
1.385     albertel 1502: 	if (defined($code) && $code ne '') {
1.137     albertel 1503: 	    if (!$Apache::lonxml::default_homework_loaded) {
                   1504: 		&Apache::lonxml::default_homework_load($safeeval);
                   1505: 	    }
1.131     albertel 1506: 	    $result = &Apache::run::run($code,$safeeval);
                   1507: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
                   1508: 	} else {
                   1509: 	    $result='1';
                   1510: 	}
                   1511: 	if ( ! $result ) {
1.201     albertel 1512: 	    my $skip=&Apache::lonxml::get_all_text("/block",$parser,$style);
1.131     albertel 1513: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1514: 	}
                   1515: 	$result='';
                   1516:     } elsif ($target eq 'edit') {
                   1517: 	$result .=&Apache::edit::tag_start($target,$token);
                   1518: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
                   1519: 					  $token,40);
                   1520: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1521:     } elsif ($target eq 'modified') {
                   1522: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1523: 						     $safeeval,'condition');
                   1524: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38      albertel 1525:     }
1.131     albertel 1526:     return $result;
1.1       albertel 1527: }
                   1528: 
                   1529: sub end_block {
1.167     www      1530:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1531:     my $result;
                   1532:     if ($target eq "edit") {
                   1533: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1534:     }
                   1535:     return $result;
                   1536: }
                   1537: 
                   1538: sub start_languageblock {
1.201     albertel 1539:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.167     www      1540: 
                   1541:     my $result;
                   1542: 
1.339     albertel 1543:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1544: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.201     albertel 1545: 	my $include = $token->[2]->{'include'};
                   1546: 	my $exclude = $token->[2]->{'exclude'};
1.436     raeburn  1547:         my @preferred_languages=&Apache::lonlocal::preferred_languages();
1.394     www      1548: # This should not even happen, since we should at least have the server language
1.398     www      1549:         if (!$preferred_languages[0]) { $preferred_languages[0]='en'; }
                   1550: # Now loop over all languages in order of preference
                   1551:         foreach my $preferred_language (@preferred_languages) {
1.394     www      1552: # If the languageblock has no arguments, show the contents
1.399     www      1553:            $result=1;
                   1554:            my $found=0;
1.394     www      1555: # Do we have an include argument?
1.398     www      1556: 	   if ($include) {
1.394     www      1557: # If include is specified, by default, don't render the block
1.399     www      1558:               $result=0;
1.398     www      1559:               foreach my $included_language (split(/\,/,$include)) {
1.394     www      1560: # ... but if my preferred language is included, render it
1.398     www      1561:                  if ($included_language eq $preferred_language) {
1.399     www      1562:                     $result=1; 
                   1563:                     $found=1; 
1.398     www      1564:                  }
                   1565:               }
                   1566: 	   }
1.394     www      1567: # Do we have an exclude argument?
1.398     www      1568:            if ($exclude) {
1.399     www      1569:               $result=1;
1.398     www      1570:               foreach my $excluded_language (split(/\,/,$exclude)) {
                   1571:                  if ($excluded_language eq $preferred_language) {
1.399     www      1572:                     $result=0;
                   1573:                     $found=1;
1.398     www      1574:                  }
                   1575:               }
                   1576: 	   }
1.399     www      1577:            if ($found) { last; }
1.398     www      1578:         }
1.201     albertel 1579: 	if ( ! $result ) {
                   1580: 	    my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser,
                   1581: 						   $style);
                   1582: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1583: 	}
                   1584: 	$result='';
1.167     www      1585:     } elsif ($target eq 'edit') {
                   1586: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 1587: 	$result .=&Apache::edit::text_arg(&mt('Include Language:'),'include',
1.167     www      1588: 					  $token,40);
1.211     albertel 1589: 	$result .=&Apache::edit::text_arg(&mt('Exclude Language:'),'exclude',
1.167     www      1590: 					  $token,40);
                   1591: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1592:     } elsif ($target eq 'modified') {
                   1593: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.168     albertel 1594: 						     $safeeval,'include',
                   1595: 						     'exclude');
1.167     www      1596: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1597:     }
                   1598:     return $result;
                   1599: }
                   1600: 
                   1601: sub end_languageblock {
1.170     www      1602:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1603:     my $result;
1.201     albertel 1604:     if ($target eq "edit") {
1.170     www      1605: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1606:     }
                   1607:     return $result;
                   1608: }
                   1609: 
1.397     albertel 1610: {
                   1611:     my %available_texts;
                   1612:     sub start_translated {
                   1613: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1614: 	&Apache::lonxml::register('Apache::structuretags',('lang'));
                   1615: 	undef(%available_texts);
                   1616:     }
                   1617:     
                   1618:     sub end_translated {
                   1619: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1620: 	my $result;
                   1621: 	#show the translation on viewable targets
                   1622: 	if ($target eq 'web'     || $target eq 'tex' || $target eq 'webgrade'||
                   1623: 	    # or non-viewable targets, if it's embedded in something that
                   1624: 	    # wants the output
                   1625: 	    (($target eq 'answer' || $target eq 'analyze'|| $target eq 'grade')
                   1626: 	     && &Apache::lonxml::in_redirection() ) ) {
                   1627: 	    my @possibilities = keys(%available_texts);
                   1628: 	    my $which = 
                   1629: 		&Apache::loncommon::languages(\@possibilities) || 'default';
                   1630: 	    $result = $available_texts{$which};
                   1631: 	}
                   1632: 	undef(%available_texts);
                   1633: 	&Apache::lonxml::deregister('Apache::structuretags',('lang'));
                   1634: 	return $result;
                   1635:     }
                   1636: 
                   1637: 
                   1638:     sub start_lang {
                   1639: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1640: 	if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1641: 	    $target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
                   1642: 	    &Apache::lonxml::startredirection();
                   1643: 	}
                   1644: 	return '';
                   1645:     }
                   1646: 
                   1647:     sub end_lang {
                   1648: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   1649: 	if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1650: 	    $target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
                   1651: 	    my $result = &Apache::lonxml::endredirection();
                   1652: 	    my $which = &Apache::lonxml::get_param('which',$parstack,
                   1653: 						   $safeeval);
1.431     raeburn  1654:             if ($which=~/\w/) {
                   1655:                 $available_texts{$which} = $result;
                   1656:             }
                   1657:             my $otherlangs = &Apache::lonxml::get_param('other',$parstack,
                   1658:                                                         $safeeval);
                   1659:             foreach my $language (split(/\s*\,\s*/,$otherlangs)) {
                   1660:                 if ($language=~/\w/) {
                   1661:                     $available_texts{$language} = $result;
                   1662:                 }
1.427     bisitz   1663:             }
                   1664: 
1.397     albertel 1665: 	}
                   1666: 	return '';
                   1667:     }
                   1668: }
                   1669: 
1.170     www      1670: sub start_instructorcomment {
1.201     albertel 1671:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.170     www      1672: 
                   1673:     my $result;
                   1674: 
1.339     albertel 1675:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1676: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.465.2.1  raeburn  1677:         $result=($env{'request.role'}=~/^(in|cc|co|au|ca|li)/);
1.284     albertel 1678: 	if ( (! $result) or ($env{'form.instructor_comments'} eq 'hide')) {
1.201     albertel 1679: 	    my $skip=&Apache::lonxml::get_all_text("/instructorcomment",
                   1680: 						   $parser,$style);
1.170     www      1681: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1682: 	}
                   1683: 	$result='';
                   1684:     } elsif ($target eq 'edit') {
                   1685: 	$result .=&Apache::edit::tag_start($target,$token);
                   1686: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1687:     }
                   1688:     return $result;
                   1689: }
                   1690: 
                   1691: sub end_instructorcomment {
1.159     albertel 1692:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 1693:     my $result;
                   1694:     if ($target eq "edit") {
                   1695: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1696:     }
                   1697:     return $result;
1.4       tsai     1698: }
                   1699: 
                   1700: sub start_while {
1.326     albertel 1701:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1702: 
1.160     albertel 1703:     my $result;
1.339     albertel 1704:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1705: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.160     albertel 1706: 	my $code = $token->[2]->{'condition'};
1.4       tsai     1707: 
1.160     albertel 1708: 	push( @Apache::structuretags::whileconds, $code);
                   1709: 	if (!$Apache::lonxml::default_homework_loaded) {
                   1710: 	    &Apache::lonxml::default_homework_load($safeeval);
                   1711: 	}
                   1712: 	my $result = &Apache::run::run($code,$safeeval);
1.326     albertel 1713: 	my $bodytext=&Apache::lonxml::get_all_text("/while",$parser,$style);
1.160     albertel 1714: 	push( @Apache::structuretags::whilebody, $bodytext);
1.161     albertel 1715: 	push( @Apache::structuretags::whileline, $token->[5]);
                   1716: 	&Apache::lonxml::debug("s code $code got -$result-");
1.160     albertel 1717: 	if ( $result ) {
                   1718: 	    &Apache::lonxml::newparser($parser,\$bodytext);
                   1719: 	}
                   1720:     } elsif ($target eq 'edit') {
                   1721: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 1722: 	$result .=&Apache::edit::text_arg(&mt('Test Condition:'),'condition',
1.160     albertel 1723: 					  $token,40);
                   1724: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1725:     } elsif ($target eq 'modified') {
                   1726: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1727: 						     $safeeval,'condition');
                   1728: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.159     albertel 1729:     }
1.160     albertel 1730:     return $result;
1.4       tsai     1731: }
                   1732: 
                   1733: sub end_while {
1.159     albertel 1734:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 1735:     my $result;
                   1736: 
1.339     albertel 1737:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1738: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.160     albertel 1739: 	my $code = pop(@Apache::structuretags::whileconds);
                   1740: 	my $bodytext = pop(@Apache::structuretags::whilebody);
1.161     albertel 1741: 	my $line = pop(@Apache::structuretags::whileline);
                   1742: 	my $return = &Apache::run::run($code,$safeeval);
                   1743: 	my $starttime=time;
                   1744: 	my $error=0;
                   1745: 	while ($return) {
                   1746: 	    if (time-$starttime >
                   1747: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}) {
1.378     albertel 1748: 		$return = 0; $error=1; next;
1.161     albertel 1749: 	    }
                   1750: 	    $result.=&Apache::scripttag::xmlparse($bodytext);
1.380     albertel 1751: 	    if ($target eq 'grade' || $target eq 'answer' ||
                   1752: 		$target eq 'analyze') {
                   1753: 		# grade/answer/analyze should produce no output but if we
                   1754: 		# are redirecting, the redirecter should know what to do
                   1755: 		# with the output
                   1756: 		if (!$Apache::lonxml::redirection) { undef($result); }
                   1757: 	    }
1.161     albertel 1758: 	    $return = &Apache::run::run($code,$safeeval);
                   1759: 	}
                   1760: 	if ($error) {
1.430     bisitz   1761: 	    &Apache::lonxml::error('<pre>'.&mt('Code ran too long. It ran for more than').' '.$Apache::lonnet::perlvar{'lonScriptTimeout'}.' '.&mt('seconds occurred while running &lt;while&gt; on line').' '.$line.'</pre>');
1.160     albertel 1762: 	}
                   1763:     } elsif ($target eq "edit") {
                   1764: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.159     albertel 1765:     }
1.160     albertel 1766:     return $result;
1.1       albertel 1767: }
1.6       tsai     1768: 
1.160     albertel 1769: # <randomlist show="1">
1.6       tsai     1770: #  <tag1>..</tag1>
                   1771: #  <tag2>..</tag2>
                   1772: #  <tag3>..</tag3>
1.160     albertel 1773: #  ...
1.6       tsai     1774: # </randomlist>
                   1775: sub start_randomlist {
1.326     albertel 1776:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 1777:     my $result;
1.339     albertel 1778:     if ($target eq 'answer' || $target eq 'grade'   || $target eq 'web' ||
                   1779: 	$target eq 'tex'    || $target eq 'analyze' || $target eq 'webgrade') {
1.331     albertel 1780: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
1.305     albertel 1781: 	my $b_parser= HTML::LCParser->new(\$body);
                   1782: 	$b_parser->xml_mode(1);
                   1783: 	$b_parser->marked_sections(1);
1.159     albertel 1784: 	my $b_tok;
                   1785: 	my @randomlist;
                   1786: 	my $list_item;
                   1787: 	while($b_tok = $b_parser->get_token() ) {
                   1788: 	    if($b_tok->[0] eq 'S') { # start tag
                   1789: 		# get content of the tag until matching end tag
                   1790: 		# get all text upto the matching tag
                   1791: 		# and push the content into @randomlist
                   1792: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
                   1793: 							   $b_parser);
                   1794: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
                   1795: 		push(@randomlist,$list_item);
                   1796: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
                   1797:                 #         $list_item</b>";
                   1798: 	    }
                   1799: 	    if($b_tok->[0] eq 'T') { # text
                   1800: 		# what to do with text in between tags?
                   1801: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
                   1802: 	    }
                   1803: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
                   1804: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
                   1805: 	    # }
                   1806: 	}
1.303     albertel 1807: 	if (@randomlist) {
                   1808: 	    my @idx_arr = (0 .. $#randomlist);
                   1809: 	    &Apache::structuretags::shuffle(\@idx_arr);
                   1810: 	    my $bodytext = '';
                   1811: 	    my $show=$#randomlist;
                   1812: 	    my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
                   1813: 	    $showarg--;
                   1814: 	    if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
1.439     raeburn  1815:             if (($target eq 'analyze') && ($env{'form.check_parts_withrandomlist'})) {
                   1816:                 my @currlist;
                   1817:                 my $part = $Apache::inputtags::part;
                   1818:                 if ($part ne '') {
                   1819:                     if (ref($Apache::lonhomework::analyze{'parts_withrandomlist'}) eq 'ARRAY') {
                   1820:                         my @currlist = @{$Apache::lonhomework::analyze{'parts_withrandomlist'}};
                   1821:                         if (!(grep(/^\Q$part\E$/,@currlist))) {
                   1822:                             push(@{$Apache::lonhomework::analyze{'parts_withrandomlist'}},$part);
                   1823:                         }
                   1824:                     } else {
                   1825:                         push(@{$Apache::lonhomework::analyze{'parts_withrandomlist'}},$part);
                   1826:                     }
                   1827:                 }
                   1828:             }
1.303     albertel 1829: 	    for(0 .. $show) {
                   1830: 		$bodytext .= "$randomlist[ $idx_arr[$_] ]";
                   1831: 	    }
                   1832: 	    &Apache::lonxml::newparser($parser,\$bodytext);
1.159     albertel 1833: 	}
                   1834:     } elsif ($target eq 'edit' ) {
                   1835: 	$result .=&Apache::edit::tag_start($target,$token);
                   1836: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
                   1837: 					   $token,5);
                   1838: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1839:     } elsif ($target eq 'modified' ) {
                   1840: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1841: 						     $safeeval,'show');
                   1842: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   1843:     }
                   1844:     return $result;
1.7       tsai     1845: }
                   1846: 
                   1847: sub shuffle {
                   1848:     my $a=shift;
                   1849:     my $i;
1.303     albertel 1850:     if (ref($a) eq 'ARRAY' && @$a) {
1.251     albertel 1851: 	&Apache::response::pushrandomnumber();
1.159     albertel 1852: 	for($i=@$a;--$i;) {
                   1853: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
                   1854: 	    next if $i == $j;
                   1855: 	    @$a[$i,$j] = @$a[$j,$i];
                   1856: 	}
1.251     albertel 1857: 	&Apache::response::poprandomnumber();
1.7       tsai     1858:     }
1.6       tsai     1859: }
                   1860: 
                   1861: sub end_randomlist {
1.159     albertel 1862:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1863:     my $result;
                   1864:     if ($target eq 'edit' ) {
                   1865: 	$result=&Apache::edit::tag_end($target,$token,
                   1866: 				       'End Randomly Parsed Block');
                   1867:     }
                   1868:     return $result;
1.6       tsai     1869: }
                   1870: 
1.283     albertel 1871: sub ordered_show_check {
                   1872:     my $last_part=$Apache::inputtags::partlist[-2];
                   1873:     my $in_order=
                   1874: 	&Apache::lonnet::EXT('resource.'.$Apache::inputtags::part.'.ordered');
                   1875:     my $in_order_show=1;
                   1876:     if ($last_part ne '0' && lc($in_order) eq 'yes') {
                   1877: 	$in_order_show=&Apache::response::check_status($last_part);
                   1878:     }
                   1879:     return $in_order_show;
                   1880: }
                   1881: 
1.465.2.2  raeburn  1882: sub start_startpartmarker {
                   1883:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1884:     my $result='';
                   1885:     if ($target eq 'edit') {
                   1886:         $result=&Apache::edit::tag_start($target,$token);
                   1887:         $result.=&mt('Marker for the start of a part. Place end marker below to wrap in-between tags into a new part.').'</td></tr>';
                   1888:         $result.=&Apache::edit::end_table();
                   1889: 
                   1890:     }
                   1891:     return $result;
                   1892: }
                   1893: 
                   1894: sub end_startpartmarker {
                   1895:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1896:     my @result;
                   1897:     if ($target eq 'edit') { $result[1]='no'; }
                   1898:     return @result;
                   1899: }
                   1900: 
                   1901: sub start_endpartmarker {
                   1902:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1903:     my $result='';
                   1904:     if ($target eq 'edit') {
                   1905:         $result=&Apache::edit::tag_start($target,$token);
                   1906:         $result.=&mt('Marker for the end of a part. Place start marker above to wrap in-between tags into a new part.').'</td></tr>';
                   1907:         $result.=&Apache::edit::end_table();
                   1908: 
                   1909:     }
                   1910:     return $result;
                   1911: }
                   1912: 
                   1913: sub end_endpartmarker {
                   1914:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1915:     my @result;
                   1916:     if ($target eq 'edit') { $result[1]='no'; }
                   1917:     return @result;
                   1918: }
                   1919: 
1.11      albertel 1920: sub start_part {
1.326     albertel 1921:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.321     albertel 1922:     if (!$Apache::lonxml::metamode) {
                   1923: 	&Apache::lonxml::startredirection(); # we'll use redirection to fix up 
                   1924: 	                                     # duedates.
                   1925:     }
1.159     albertel 1926:     my $result='';
1.386     albertel 1927:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.159     albertel 1928:     $Apache::inputtags::part=$id;
1.177     albertel 1929:     push(@Apache::inputtags::partlist,$id);
                   1930:     @Apache::inputtags::response=();
1.159     albertel 1931:     @Apache::inputtags::previous=();
                   1932:     @Apache::inputtags::previous_version=();
1.405     albertel 1933:     &Apache::lonhomework::set_show_problem_status(&get_problem_status($id));
1.403     albertel 1934:     &Apache::response::reset_params();
                   1935: 
1.159     albertel 1936:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.259     albertel 1937:     my $newtype=&Apache::lonnet::EXT("resource.$id.type");
                   1938:     if ($newtype) { $Apache::lonhomework::type=$newtype; }
1.283     albertel 1939:     my $in_order_show=&ordered_show_check();
1.214     albertel 1940:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.259     albertel 1941:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.209     albertel 1942:     &Apache::run::run($expression,$safeeval);
1.159     albertel 1943: 
                   1944:     if ($target eq 'meta') {
1.224     www      1945: 	my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
                   1946: 	return &Apache::response::mandatory_part_meta.
                   1947: 	       &Apache::response::meta_parameter_write('display','string',$display,'Part Description');
1.159     albertel 1948:     } elsif ($target eq 'web' || $target eq 'grade' ||
                   1949: 	     $target eq 'answer' || $target eq 'tex') {
1.283     albertel 1950: 	if ($hidden || !$in_order_show) {
1.326     albertel 1951: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,$style);
1.159     albertel 1952: 	} else {
                   1953: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
                   1954: 	    push (@Apache::inputtags::status,$status);
                   1955: 	    my $expression='$external::datestatus="'.$status.'";';
                   1956: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                   1957: 	    &Apache::run::run($expression,$safeeval);
1.284     albertel 1958: 	    if ($env{'request.state'} eq 'construct') {
1.241     albertel 1959: 		&set_problem_state($Apache::inputtags::part); 
1.240     albertel 1960: 	    }
1.216     albertel 1961: 	    if (( $status eq 'CLOSED' ) ||
                   1962: 		( $status eq 'UNCHECKEDOUT') ||
1.252     albertel 1963: 		( $status eq 'NOT_YET_VIEWED') ||
1.216     albertel 1964: 		( $status eq 'BANNED') ||
                   1965: 		( $status eq 'UNAVAILABLE') ||
                   1966: 		( $status eq 'INVALID_ACCESS')) {
1.326     albertel 1967: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,
                   1968: 							   $style);
1.159     albertel 1969: 		if ( $target eq "web" ) {
1.211     albertel 1970: 		    $result="<br />".&mt('Part is not open to be viewed. It')." $accessmsg<br />";
1.159     albertel 1971: 		} elsif ( $target eq 'tex' ) {
1.284     albertel 1972: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.211     albertel 1973: 			$result="\\end{minipage}\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\\\begin{minipage}{\\textwidth}";
1.195     sakharuk 1974: 		    } else {
1.211     albertel 1975: 			$result="\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\";
1.195     sakharuk 1976: 		    }
1.159     albertel 1977: 		}
                   1978: 	    } else {
                   1979: 		if ($target eq 'tex') {
1.284     albertel 1980: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.264     sakharuk 1981: 			if ($$tagstack[-2] eq 'td') {
1.388     foxr     1982: 			    $result.='\noindent \begin{minipage}{\textwidth}\noindent';
1.264     sakharuk 1983: 			} else {
                   1984: 			    $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
                   1985: 			}
1.195     sakharuk 1986: 		    }
1.159     albertel 1987: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.284     albertel 1988: 		    my $allkeys=&Apache::lonnet::metadata($env{'request.uri'},'packages');
1.222     sakharuk 1989: 		    my @allkeys = split /,/,$allkeys;
                   1990: 		    my $allow_print_points = 0;
                   1991: 		    foreach my $partial_key (@allkeys) {
1.230     albertel 1992: 			if ($partial_key=~m/^part_(.*)$/) {
1.222     sakharuk 1993: 			    if ($1 ne '0') {$allow_print_points=1;}
                   1994: 			}
                   1995: 		    }
1.275     albertel 1996: 		    my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1997: 		    if (defined($maxtries) && $maxtries < 0) {
                   1998: 			$allow_print_points=0;
                   1999: 		    }
1.302     albertel 2000: 		    if (lc($env{'course.'.$env{'request.course.id'}.
                   2001: 				    '.disableexampointprint'}) eq 'yes') {
                   2002: 			$allow_print_points=0;
                   2003: 		    }
1.463     foxr     2004: 		    if (($Apache::lonhomework::type eq 'exam') && ($allow_print_points)) { 
1.465.2.14  raeburn  2005: 			$result .= '\vskip 10mm\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}';
1.463     foxr     2006: 
                   2007: 		    }
1.233     www      2008: 		} elsif ($target eq 'web') {
1.465.2.7  raeburn  2009:                     if ($status eq 'CAN_ANSWER') {
                   2010:                         my $problemstatus = &get_problem_status($Apache::inputtags::part);
                   2011:                         my $probrandomize = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].type");
                   2012:                         my $probrandtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].randomizeontries");
                   2013:                         my $num = scalar(@Apache::inputtags::partlist)-1;
                   2014:                         if ($probrandomize eq 'randomizetry') {
                   2015:                             if (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.type") ne 'randomizetry') {
                   2016:                                 $result .= &randomizetry_part_header($problemstatus,'none',$num);
                   2017:                             } else {
                   2018:                                 my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                   2019:                                 if ($probrandtries ne $reqtries) {
                   2020:                                     $result .= &randomizetry_part_header($problemstatus,$reqtries,$num);
                   2021:                                 }
                   2022:                             }
                   2023:                         } elsif (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.type") eq 'randomizetry') {
                   2024:                             my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                   2025:                             $result .= &randomizetry_part_header($problemstatus,$reqtries,$num);
                   2026:                         }
                   2027:                     }
1.465.2.5  raeburn  2028: 		    $result.='<a name="'.&escape($Apache::inputtags::part).'"></a>';
1.159     albertel 2029: 		}
                   2030: 	    }
                   2031: 	}
                   2032:     } elsif ($target eq 'edit') {
                   2033: 	$result.=&Apache::edit::tag_start($target,$token);
                   2034: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
                   2035: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
1.224     www      2036: 	    '&nbsp;&nbsp;'.
                   2037: &Apache::edit::text_arg('Displayed Part Description:','display',$token).
1.159     albertel 2038: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2039:     } elsif ($target eq 'modified') {
                   2040: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.225     albertel 2041: 						     $safeeval,'id','display');
1.159     albertel 2042: 	if ($constructtag) {
1.225     albertel 2043: 	    #limiting ids to only letters numbers, and space
1.224     www      2044: 	    $token->[2]->{'id'}=~s/[^A-Za-z0-9 ]//gs;
1.159     albertel 2045: 	    $result = &Apache::edit::rebuild_tag($token);
                   2046: 	}
                   2047:     }
                   2048:     return $result;
1.11      albertel 2049: }
                   2050: 
                   2051: sub end_part {
1.159     albertel 2052:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2053:     &Apache::lonxml::debug("in end_part $target ");
                   2054:     my $status=$Apache::inputtags::status['-1'];
                   2055:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.283     albertel 2056:     my $in_order_show=&ordered_show_check();
1.321     albertel 2057:     my $result;
                   2058:     if (!$Apache::lonxml::metamode) {
                   2059: 	$result = &Apache::lonxml::endredirection(); # started in &start_part
1.329     albertel 2060: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel 2061:     }
1.312     albertel 2062:     if ($target eq 'grade') {
1.249     albertel 2063: 	if (($status eq 'CAN_ANSWER' || $Apache::lonhomework::scantronmode) &&
1.283     albertel 2064: 	    !$hidden && $in_order_show) {
1.311     foxr     2065: 	    $result.=&Apache::inputtags::grade;
1.249     albertel 2066: 	} else {
                   2067: 	    # move any submission data to .hidden
                   2068: 	    &Apache::inputtags::hidealldata($Apache::inputtags::part);
                   2069: 	}
1.283     albertel 2070:     } elsif (($target eq 'web' || $target eq 'tex') &&
                   2071: 	     !$hidden && $in_order_show) {
1.159     albertel 2072: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
                   2073: 							$target);
1.212     albertel 2074: 	if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
                   2075: 	    $gradestatus='';
                   2076: 	}
1.311     foxr     2077: 	$result.=$gradestatus;
1.265     sakharuk 2078: 	if ($$tagstack[-2] eq 'td' and $target eq 'tex') {$result.='\end{minipage}';} 
1.181     albertel 2079:     } elsif ($target eq 'edit') {
1.311     foxr     2080: 	$result.=&Apache::edit::end_table();
1.322     albertel 2081:     } elsif ($target eq 'modified') {
                   2082: 	 $result .= $token->[2];
1.159     albertel 2083:     }
                   2084:     pop @Apache::inputtags::status;
                   2085:     $Apache::inputtags::part='';
1.295     albertel 2086:     $Apache::lonhomework::type = $Apache::lonhomework::default_type;
1.159     albertel 2087:     return $result;
1.11      albertel 2088: }
1.1       albertel 2089: 
1.25      albertel 2090: sub start_preduedate {
1.326     albertel 2091:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.339     albertel 2092:     if ($target eq 'web' || $target eq 'grade'    || $target eq 'answer' ||
                   2093: 	$target eq 'tex' || $target eq 'webgrade') {
1.236     albertel 2094: 	&Apache::lonxml::debug("State in preduedate is ". $Apache::inputtags::status['-1']);
1.300     albertel 2095: 	if (!$Apache::lonhomework::scantronmode &&
                   2096: 	    $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1.236     albertel 2097: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                   2098: 	    &Apache::lonxml::debug("Wha? ". ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER'));
1.326     albertel 2099: 	    &Apache::lonxml::get_all_text("/preduedate",$parser,$style);
1.159     albertel 2100: 	}
1.24      albertel 2101:     }
1.159     albertel 2102:     return '';
1.24      albertel 2103: }
                   2104: 
1.25      albertel 2105: sub end_preduedate {
1.159     albertel 2106:     return '';
1.24      albertel 2107: }
                   2108: 
1.369     foxr     2109: # In all the modes where <postanswerdate> text is 
                   2110: # displayable,  all we do is eat up the text between the start/stop
                   2111: # tags if the conditions are not right to display it.
1.25      albertel 2112: sub start_postanswerdate {
1.326     albertel 2113:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.370     foxr     2114:     my $pav = &Apache::lonnet::allowed('pav', $env{'request.course.id'}) ||
                   2115: 	&Apache::lonnet::allowed('pav',
                   2116: 			   $env{'request.course.id'}.'/'.$env{'request.course.sec'});
1.369     foxr     2117:     if ($target eq 'web' || $target eq 'grade' || $target eq 'webgrade' ||
1.370     foxr     2118: 	$target eq 'tex' ) {
1.300     albertel 2119: 	if ($Apache::lonhomework::scantronmode ||
1.370     foxr     2120: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER' ||
                   2121: 	    (($target eq 'tex') && !$pav)) {
1.326     albertel 2122: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser,$style);
1.159     albertel 2123: 	}
                   2124:     }
                   2125:     return '';
1.24      albertel 2126: }
                   2127: 
1.25      albertel 2128: sub end_postanswerdate {
1.159     albertel 2129:     return '';
1.24      albertel 2130: }
                   2131: 
1.25      albertel 2132: sub start_notsolved {
1.326     albertel 2133:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2134:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.339     albertel 2135: 	$target eq 'tex' || $target eq 'webgrade') {
1.159     albertel 2136: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                   2137: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
1.239     albertel 2138: 	if ($gradestatus =~ /^correct/ &&
                   2139: 	    &Apache::response::show_answer()) {
1.159     albertel 2140: 	    &Apache::lonxml::debug("skipping");
1.326     albertel 2141: 	    &Apache::lonxml::get_all_text("/notsolved",$parser,$style);
1.159     albertel 2142: 	}
1.24      albertel 2143:     }
1.159     albertel 2144:     return '';
1.24      albertel 2145: }
                   2146: 
1.25      albertel 2147: sub end_notsolved {
1.159     albertel 2148:     return '';
1.24      albertel 2149: }
                   2150: 
                   2151: sub start_solved {
1.326     albertel 2152:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2153:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   2154: 	$target eq 'tex') {
                   2155: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.239     albertel 2156: 	if ($gradestatus !~ /^correct/ ||
                   2157: 	    !&Apache::response::show_answer()) {
1.326     albertel 2158: 	    &Apache::lonxml::get_all_text("/solved",$parser,$style);
1.159     albertel 2159: 	}
1.24      albertel 2160:     }
1.159     albertel 2161:     return '';
1.24      albertel 2162: }
                   2163: 
                   2164: sub end_solved {
1.248     albertel 2165:     return '';
                   2166: }
                   2167: 
                   2168: sub start_problemtype {
1.326     albertel 2169:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.248     albertel 2170:     my $result;
1.339     albertel 2171:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   2172: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.248     albertel 2173: 	my $mode=lc(&Apache::lonxml::get_param('mode',$parstack,$safeeval));
                   2174: 	if (!defined($mode)) { $mode='show'; }
                   2175: 	my $for=&Apache::lonxml::get_param('for',$parstack,$safeeval);
                   2176: 	my $found=0;
                   2177: 	foreach my $type (split(',',$for)) {
                   2178: 	    if ($Apache::lonhomework::type eq lc($type)) { $found=1; }
                   2179: 	}
                   2180: 	if ($mode eq 'show' && !$found) {
1.326     albertel 2181: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 2182: 	}
                   2183: 	if ($mode eq 'hide' && $found) {
1.326     albertel 2184: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 2185: 	}
                   2186:     } elsif ($target eq 'edit') {
                   2187: 	$result .=&Apache::edit::tag_start($target,$token);
                   2188: 	$result.=&Apache::edit::select_arg('Mode:','mode',
                   2189: 					   [['show','Show'],
                   2190: 					    ['hide','Hide']]
                   2191: 					   ,$token);
                   2192: 	$result .=&Apache::edit::checked_arg('When used as type(s):','for',
                   2193: 					     [ ['exam','Exam/Quiz Problem'],
                   2194: 					       ['survey','Survey'],
1.465     raeburn  2195:                                                ['surveycred','Survey (with credit)'],
                   2196:                                                ['anonsurvey','Anonymous Survey'],
                   2197:                                                ['anonsurveycred','Anonymous Survey (with credit)'],
1.428     raeburn  2198: 					       ['problem','Homework Problem'],
1.465.2.7  raeburn  2199:                                                ['practice','Practice Problem'],
                   2200:                                                ['randomizetry','New Randomization Each Try'] ]
1.248     albertel 2201: 					     ,$token);
                   2202: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2203:     } elsif ($target eq 'modified') {
                   2204: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   2205: 						     $safeeval,'mode','for');
                   2206: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   2207:     }
                   2208:     return $result;
                   2209: }
                   2210: 
                   2211: sub end_problemtype {
1.159     albertel 2212:     return '';
1.24      albertel 2213: }
1.34      albertel 2214: 
                   2215: sub start_startouttext {
1.159     albertel 2216:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2217:     my @result=(''.'');
                   2218:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
1.404     albertel 2219:     
                   2220:     my $nesting = 
                   2221: 	&Apache::lonxml::set_state('outtext',
                   2222: 				   &Apache::lonxml::get_state('outtext')+1);
                   2223:     if ($nesting > 1 && $env{'request.state'} eq 'construct') {
                   2224: 	&Apache::lonxml::error("Nesting of &lt;startouttext /&gt; not allowed, on line ".$token->[5]);
                   2225:     }
1.159     albertel 2226:     return (@result);
1.34      albertel 2227: }
1.159     albertel 2228: 
1.34      albertel 2229: sub end_startouttext {
1.326     albertel 2230:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2231:     my $result='';
                   2232:     my $text='';
                   2233:     if ($target eq 'edit') {
1.424     foxr     2234: 	my $areaid = 'homework_edit_'.$Apache::lonxml::curdepth;
1.326     albertel 2235: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.437     raeburn  2236: 	$result.=&Apache::edit::start_table($token)."<tr><td>".&mt('Text Block')."</td>"
1.438     bisitz   2237:                  .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.437     raeburn  2238:                  .&Apache::edit::deletelist($target,$token)
1.465.2.4  raeburn  2239:                  .'</span></td>'
                   2240: 	         .'<td align="left"><span id="math_'.$areaid.'" />'
                   2241: 		 .&Apache::lonhtmlcommon::dragmath_button($areaid,1)
                   2242: 		 .'</td>'
                   2243: 		 .'<td>'
                   2244: 		 .&Apache::edit::insertlist($target,$token)
                   2245: 		 .'</td>'
                   2246: 	         .'<td align="right" valign="top">' .
                   2247: 	         &Apache::loncommon::helpLatexCheatsheet().
1.159     albertel 2248: 		 &Apache::edit::end_row().
1.362     albertel 2249:                  &Apache::edit::start_spanning_row()."\n".
1.255     www      2250: 		 &Apache::edit::editfield($token->[1],$text,"",80,8,1);
1.159     albertel 2251:     }
                   2252:     if ($target eq 'modified') {
1.219     albertel 2253: 	$result='<startouttext />'.&Apache::edit::modifiedfield("endouttext",$parser);
1.159     albertel 2254:     }
                   2255:     if ($target eq 'tex') {
                   2256: 	$result .= '\noindent ';
                   2257:     }
                   2258:     return $result;
1.34      albertel 2259: }
1.159     albertel 2260: 
1.34      albertel 2261: sub start_endouttext {
1.159     albertel 2262:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2263:     my $result='';
                   2264:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
                   2265:     if ($target eq "modified") {
                   2266: 	$result='<endouttext />'.
1.377     albertel 2267: 	    &Apache::edit::handle_insertafter('startouttext');
                   2268:     }
1.404     albertel 2269: 
                   2270:     my $nesting = 
                   2271: 	&Apache::lonxml::set_state('outtext',
                   2272: 				   &Apache::lonxml::get_state('outtext')-1);
                   2273:     if ($nesting < 0 && $env{'request.state'} eq 'construct') {
                   2274: 	&Apache::lonxml::error(" Extraneous &lt;endouttext /&gt; not allowed on line ".$token->[5]);
                   2275: 	&Apache::lonxml::set_state('outtext', 0);
                   2276:     }
1.159     albertel 2277:     return $result;
1.34      albertel 2278: }
1.159     albertel 2279: 
1.34      albertel 2280: sub end_endouttext {
1.159     albertel 2281:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2282:     my @result=('','');
                   2283:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                   2284:     return (@result);
1.34      albertel 2285: }
1.159     albertel 2286: 
1.45      albertel 2287: sub delete_startouttext {
1.326     albertel 2288:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2289:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.159     albertel 2290:     my $text=$$parser['-1']->get_text("/endouttext");
                   2291:     my $ntoken=$$parser['-1']->get_token();
                   2292:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
                   2293:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
                   2294:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
                   2295:     # they did the last time round
                   2296:     &Apache::lonxml::increasedepth($ntoken);
                   2297:     &Apache::lonxml::decreasedepth($ntoken);
                   2298:     return 1;
1.193     www      2299: }
                   2300: 
                   2301: sub start_simpleeditbutton {
                   2302:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2303:     my $result='';
1.284     albertel 2304:     if (($env{'form.simple_edit_button'} ne 'off') &&
1.273     albertel 2305: 	($target eq 'web') &&
1.330     albertel 2306:         (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
1.284     albertel 2307:         my $url=$env{'request.noversionuri'};
1.193     www      2308:         $url=~s/\?.*$//;
1.367     albertel 2309: 	my ($symb) = &Apache::lonnet::whichuser();
1.451     bisitz   2310: #       Warning makes more sense and is more important on edit screen
1.442     bisitz   2311: #       $result='<p class="LC_warning">'
                   2312: #              .&mt('Note: it can take up to 10 minutes for changes to take effect for all users.')
                   2313: #              .&Apache::loncommon::help_open_topic('Caching')
                   2314: #              .'</p>';
1.465.2.14  raeburn  2315:         $result.=&Apache::loncommon::head_subbox(
                   2316:                  &Apache::lonhtmlcommon::start_funclist()
1.451     bisitz   2317:                 .&Apache::lonhtmlcommon::add_item_funclist(
                   2318:                      '<a href="'.$url.'/smpedit?symb='.&escape($symb).'">'
                   2319:                     .&mt('Edit').'</a>')
1.465.2.14  raeburn  2320:                 .&Apache::lonhtmlcommon::end_funclist());
1.442     bisitz   2321: 
1.193     www      2322:     }
                   2323:     return $result;
                   2324: }
                   2325: 
                   2326: sub end_simpleeditbutton {
                   2327:     return '';
1.45      albertel 2328: }
1.34      albertel 2329: 
1.428     raeburn  2330: sub practice_problem_header {
                   2331:     return '<span class="LC_info"><h3>'.&mt('Practice Problem').'</h3></span>'.
                   2332:            '<span class="LC_info">'.&mt('Submissions are not permanently recorded').
                   2333:            '</span>';
                   2334: }
                   2335: 
1.465.2.7  raeburn  2336: sub randomizetry_problem_header {
                   2337:     my ($problemstatus,$reqtries) = @_;
                   2338:     my ($header,$text);
                   2339:     if ($reqtries > 1) {
                   2340:         $header = &mt('New Problem Variation After Every [quant,_1,Try,Tries]',$reqtries);
                   2341:         if (($problemstatus eq 'no') ||
                   2342:             ($problemstatus eq 'no_feedback_ever')) {
                   2343:             $text = &mt('A new variation will be generated after every [quant,_1,try,tries], until the tries limit is reached.',$reqtries);
                   2344:         } else {
                   2345:             $text = &mt('A new variation will be generated after every [quant,_1,try,tries], until correct or tries limit is reached.',$reqtries);
                   2346:         }
                   2347:     } else {
                   2348:         $header = &mt('New Problem Variation Each Try');
                   2349:         if (($problemstatus eq 'no') ||
                   2350:             ($problemstatus eq 'no_feedback_ever')) {
                   2351:             $text = &mt('A new variation will be generated after each try until the tries limit is reached.');
                   2352: 
                   2353:         } else {
                   2354:             $text = &mt('A new variation will be generated after each try until correct or tries limit is reached.');
                   2355:         }
                   2356:     }
                   2357:     return '<span class="LC_info"><h3>'.$header.'</h3></span>'.
                   2358:            '<span class="LC_info">'.$text.'</span><hr />';
                   2359: }
                   2360: 
                   2361: sub randomizetry_part_header {
                   2362:     my ($problemstatus,$reqtries,$num) = @_;
                   2363:     my ($header,$text);
                   2364:     if ($reqtries eq 'none') {
                   2365:         $header = &mt('No Question Variation');
                   2366:         $text = &mt('For this question there will no new variation after a try.');
                   2367:     } elsif ($reqtries > 1) {
                   2368:         $header = &mt('New Question Variation After Every [quant,_1,Try,Tries]',$reqtries);
                   2369:         if (($problemstatus eq 'no') ||
                   2370:             ($problemstatus eq 'no_feedback_ever')) {
                   2371:             $text = &mt('For this question a new variation will be generated after every [quant,_1,try,tries], until the tries limit is reached.',$reqtries);
                   2372:         } else {
                   2373:             $text = &mt('For this question a new variation will be generated after every [quant,_1,try,tries], until correct or tries limit is reached.',$reqtries);
                   2374:         }
                   2375:     } else {
                   2376:         $header = &mt('New Question Variation For Each Try');
                   2377:         if (($problemstatus eq 'no') ||
                   2378:             ($problemstatus eq 'no_feedback_ever')) {
                   2379:             $text =  &mt('For this question a new variation will be generated after each try until the tries limit is reached.');
                   2380:         } else {
                   2381:             $text = &mt('For this question a new variation will be generated after each try until correct or tries limit is reached.');
                   2382:         }
                   2383:     }
                   2384:     my $output;
                   2385:     if ($num > 1) {
                   2386:         $output .= '<hr />';
                   2387:     }
                   2388:     $output .=  '<span class="LC_info"><h4>'.$header.'</h4></span>'.
                   2389:                   '<span class="LC_info">'.$text.'</span><br /><br />';
                   2390:     return $output;
                   2391: }
                   2392: 
1.1       albertel 2393: 1;
                   2394: __END__
1.435     jms      2395: 
                   2396: =pod
                   2397: 
                   2398: =back
                   2399: 
                   2400: =cut

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