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

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.524   ! raeburn     4: # $Id: structuretags.pm,v 1.523 2014/11/21 18:04:57 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.500     foxr       64: use Apache::loncommon();
1.267     albertel   65: use Time::HiRes qw( gettimeofday tv_interval );
1.356     www        66: use lib '/home/httpd/lib/perl/';
                     67: use LONCAPA;
                     68:  
1.78      harris41   69: BEGIN {
1.469     www        70:     &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   71: }
                     72: 
1.500     foxr       73: 
                     74: #---------------------------------------------------------------------------------
                     75: # 
                     76: #  This section of code deals with hyphenation management.
                     77: #  We must do three things:
                     78: #  - keep track fo the desired languages to alter the header.
                     79: #  - provide hyphenation selection as needed by each language that appears in the
                     80: #    text.
                     81: #  - Provide the header text needed to make available the desired hyphenations.
                     82: #
                     83: #
                     84: 
                     85: # Hash whose keys are the languages encountered in the document/resource.
                     86: #
                     87: 
                     88: my %languages_required;
                     89: ##
                     90: #   Given a language selection as input returns a chunk of LaTeX that
                     91: #   selects the required hyphenator.
                     92: #
                     93: #  @param language - the language being selected.
                     94: #  @return string
                     95: #  @retval The LaTeX needed to select the hyphenation appropriate to the language. 
                     96: #   
                     97: sub select_hyphenation {
                     98:     my $language  = shift;
                     99: 
                    100:     $language = &Apache::loncommon::latexlanguage($language); # Translate -> latex language.
                    101: 
                    102:     # If there is no latex language there's not much we can do:
                    103: 
                    104:     if ($language) {
                    105: 	&require_language($language);
                    106: 	my $babel_hyphenation = "\\selectlanguage{$language}";
                    107: 	
                    108: 	return $babel_hyphenation;
                    109:     } else {
                    110: 	return '';
                    111:     }
                    112: }
                    113: ##
                    114: # Selects hyphenation based on the current problem metadata.
                    115: # This requires that
                    116: # - There is a language metadata item set for the problem.
                    117: # - The language has a latex/babel hyphenation.
                    118: #
                    119: # @note: Uses &Apache::lonxml::request to locate the Uri associated with
                    120: #        this problem.
                    121: # @return string (possibly empty).
                    122: # @retval If not empty an appropriate \selectlanguage{} directive.
                    123: #
                    124: sub select_metadata_hyphenation {
                    125:     my $uri      = $Apache::lonxml::request->uri;
                    126:     my $language = &Apache::lonnet::metadata($uri, 'language'); 
                    127:     my $latex_language = &Apache::loncommon::latexhyphenation($language);
                    128:     if ($latex_language) {
                    129: 	return '\selectlanguage{'.$latex_language."}\n";
                    130:     }
                    131:     return '';			# no latex hyphenation or no lang metadata.
                    132: }
                    133: 
                    134: 
                    135: ##
                    136: #  Clears the set of languages required by the document being rendered.
                    137: #
                    138: sub clear_required_languages {
                    139:     %languages_required = ();
                    140: }
                    141: ##
                    142: # Allows an external client of this module to register a need for a language:
                    143: #
                    144: # @param LaTeX language required:
                    145: #
                    146: sub require_language {
                    147:     my $language = shift;
                    148:     $languages_required{$language} = 1;
                    149: }
                    150: 
                    151: ##
                    152: # Provides the header for babel that indicates the languages
                    153: # the document requires.
                    154: # @return string
                    155: # @retval \usepackage[lang1,lang2...]{babel}
                    156: # @retval ''   if there are no languages_required.
                    157: sub languages_header {
                    158:     my $header    ='';
                    159:     my @languages = (keys(%languages_required));
                    160: 
                    161:     # Only generate the header if there are languages:
                    162: 
                    163:     if (scalar @languages) {
                    164: 	my $language_list = join(',', (@languages));
                    165: 	$header  = '\usepackage['.$language_list."]{babel}\n";
                    166:     }
                    167:     return $header;
                    168: }
                    169: 
                    170: #----------------------------------------------------------------------------------
                    171: 
1.10      albertel  172: sub start_web {
1.326     albertel  173:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.383     albertel  174:     if ($target ne 'edit' && $target ne 'modified') {
                    175: 	my $bodytext=&Apache::lonxml::get_all_text("/web",$parser,$style);
                    176: 	if ($target eq 'web' || $target eq 'webgrade') {
                    177: 	    return $bodytext;
                    178: 	}
                    179:     } elsif ($target eq "edit" ) {
                    180: 	my $bodytext = 
                    181: 	    &Apache::lonxml::get_all_text_unbalanced("/web",$parser);
                    182: 	my $result = &Apache::edit::tag_start($target,$token);
                    183: 	$result .= &Apache::edit::editfield($token->[1],$bodytext,'',80,1);
                    184: 	return $result;
                    185:     } elsif ( $target eq "modified" ) {
                    186: 	return $token->[4].&Apache::edit::modifiedfield("/web",$parser);
1.159     albertel  187:     }
                    188:     return '';
1.10      albertel  189: }
                    190: 
                    191: sub end_web {
1.44      ng        192:     return '';
1.10      albertel  193: }
                    194: 
                    195: sub start_tex {
1.326     albertel  196:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.198     sakharuk  197:     my $result='';
1.383     albertel  198:     if ($target ne 'edit' && $target ne 'modified') {
                    199: 	my $bodytext=&Apache::lonxml::get_all_text("/tex",$parser,$style);
                    200: 	if ($target eq 'tex') {
1.434     foxr      201: 	    
                    202: 	    # If inside a table, occurrences of \\ must be removed;
                    203: 	    # else the table blows up.
                    204: 
                    205: 	    if (&Apache::londefdef::is_inside_of($tagstack, "table")) {
                    206: 		$bodytext =~ s/\\\\//g;
                    207: 	    }
1.432     foxr      208: 	    return $bodytext.'{}';
1.383     albertel  209: 	}
                    210:     } elsif ($target eq "edit" ) {
                    211: 	my $bodytext = 
                    212: 	    &Apache::lonxml::get_all_text_unbalanced("/tex",$parser);
                    213: 	my $result = &Apache::edit::tag_start($target,$token);
                    214: 	$result .= &Apache::edit::editfield($token->[1],$bodytext,'',80,1);
                    215: 	return $result;
                    216:     } elsif ( $target eq "modified" ) {
                    217: 	return $token->[4].&Apache::edit::modifiedfield("/tex",$parser);
1.159     albertel  218:     }
1.198     sakharuk  219:     return $result;;
1.10      albertel  220: }
                    221: 
                    222: sub end_tex {
1.44      ng        223:     return '';
1.9       albertel  224: }
                    225: 
1.400     albertel  226: sub homework_js {
                    227:     return &Apache::loncommon::resize_textarea_js().
1.416     raeburn   228:            &setmode_javascript().
1.400     albertel  229: 	<<'JS';
                    230: <script type="text/javascript">
1.483     raeburn   231: // <![CDATA[
1.494     raeburn   232: function setSubmittedPart (part,prefix) {
                    233:     if (typeof(prefix) == 'undefined') {
                    234:         this.document.lonhomework.submitted.value="part_"+part;
                    235:     } else {
                    236:         for (var i=0;i<this.document.lonhomework.elements.length;i++) {
                    237:             if (this.document.lonhomework.elements[i].name == prefix+'submitted') {
                    238:                 this.document.lonhomework.elements[i].value="part_"+part;
                    239:             }
                    240:         }
                    241:     }
1.400     albertel  242: }
                    243: 
                    244: function image_response_click (which, e) {
                    245:     init_geometry();
                    246:     if (!e) { e = window.event; } //IE
                    247:     var input_element = document.lonhomework.elements[which];
1.401     albertel  248:     var token_element = document.lonhomework.elements[which+'_token'];
1.400     albertel  249:     var token = token_element.value;
1.401     albertel  250:     var img_element   = document.getElementById(which+'_imageresponse');
1.400     albertel  251:     var x= e.clientX-getX(img_element)+Geometry.getHorizontalScroll();
                    252:     var y= e.clientY-getY(img_element)+Geometry.getVerticalScroll();
                    253:     var click = x+':'+y;
                    254:     input_element.value = click;
1.485     raeburn   255:     img_element.src = '/adm/randomlabel.png?token='+token+'&clickdata='+click;
1.400     albertel  256: }
1.520     raeburn   257: 
                    258: var submithandled = 0;
1.521     raeburn   259: var keypresshandled = 0;
1.520     raeburn   260: 
                    261: $(document).ready(function(){
1.521     raeburn   262: 
                    263:     $(document).keypress(function(event){
                    264:         var keycode = (event.keyCode ? event.keyCode : event.which);
                    265:         if ((keycode == '13') && (keypresshandled == 0)) {
                    266:             if ( $( document.activeElement ).hasClass("LC_textline") ) {
                    267:                 keypresshandled = 1;
                    268:                 var idsArray = $( document.activeElement ).attr("id").split(/HWVAL_/);
                    269:                 if (idsArray.length) {
                    270:                     event.preventDefault();
                    271:                     var itemsArray = idsArray[1].split(/_/);
                    272:                     var buttonId = idsArray[0]+'submit_'+itemsArray[0];
                    273:                     $("#"+buttonId).trigger("click");
                    274:                 }
                    275:             }
                    276:         }
                    277:     });
                    278: 
1.520     raeburn   279:     $(document).delegate('form :submit', 'click', function( event ) {
                    280:         if ( $( this ).hasClass( "LC_hwk_submit" ) ) {
                    281:             var buttonId = this.id;
                    282:             if (submithandled == 0) {
                    283:                 submithandled = 1;
                    284:                 $( "#msg_"+buttonId ).css({"display": "inline","background-color": "#87cefa",
1.521     raeburn   285:                                            "color": "black","padding": "2px"}) ;
                    286:                 if (( $(this.form).id == "LC_page" ) && ($('input[name="all_submit"]').length )) {
1.522     raeburn   287:                     if (buttonId != "all_submit") {
                    288:                         $( ".LC_status_"+buttonId ).hide();
                    289:                         if (( "#"+buttonId+"_pressed" ).length) {
                    290:                             $( "#"+buttonId+"_pressed" ).val( "1" );
                    291:                         }
1.520     raeburn   292:                     }
1.522     raeburn   293:                 } else {
                    294:                     $( ".LC_status_"+buttonId ).hide();
1.520     raeburn   295:                 }
                    296:                 $(this.form).submit();
1.521     raeburn   297:                 $( ".LC_hwk_submit" ).prop( "disabled", true);
                    298:                 $( ".LC_textline" ).prop( "readonly", "readonly");
1.520     raeburn   299:                 event.preventDefault();
                    300:                 return true;
                    301:             }
                    302:         }
                    303:     });
                    304: });
                    305: 
1.483     raeburn   306: // ]]>
1.400     albertel  307: </script>
                    308: JS
                    309: }
                    310: 
1.416     raeburn   311: sub setmode_javascript {
                    312:     return <<"ENDSCRIPT";
                    313: <script type="text/javascript">
1.485     raeburn   314: // <![CDATA[
1.416     raeburn   315: function setmode(form,probmode) {
                    316:     form.problemmode.value = probmode;
                    317:     form.submit();
                    318: }
1.485     raeburn   319: // ]]>
1.416     raeburn   320: </script>
                    321: ENDSCRIPT
                    322: }
                    323: 
1.48      albertel  324: sub page_start {
1.345     albertel  325:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$name,
                    326: 	$extra_head)=@_;
1.159     albertel  327:     my %found;
1.207     albertel  328:     foreach my $taginside (@$tagstack) {
1.159     albertel  329: 	foreach my $taglookedfor ('html','body','form') {
                    330: 	    if ($taginside =~ /^$taglookedfor$/i) {$found{$taglookedfor} = 1;}
                    331: 	}
                    332:     }
                    333: 
1.343     albertel  334:     if ($target eq 'tex') {
                    335: 	return
                    336: 	    &Apache::londefdef::start_html($target,$token,$tagstack,
                    337: 					   $parstack,$parser,$safeeval);
                    338:     }
                    339: 
1.474     raeburn   340:     $extra_head .= &homework_js().
                    341:                    &Apache::lonhtmlcommon::dragmath_js("EditMathPopup");
                    342:     if (&Apache::lonhtmlcommon::htmlareabrowser()) {
                    343:         my %textarea_args = (
                    344:                                 dragmath => 'math',
                    345:                               );
                    346:         $extra_head .= &Apache::lonhtmlcommon::htmlareaselectactive(\%textarea_args);
1.425     raeburn   347:     }
1.478     raeburn   348:     my $is_task = ($env{'request.uri'} =~ /\.task$/);
1.493     raeburn   349:     my $needs_upload;
1.495     raeburn   350:     my ($symb)= &Apache::lonnet::whichuser();
                    351:     my ($map,$resid,$resurl)=&Apache::lonnet::decode_symb($symb);
1.478     raeburn   352:     if ($is_task) {
1.495     raeburn   353:         $extra_head .= &Apache::lonhtmlcommon::file_submissionchk_js();
1.493     raeburn   354:     } else {
                    355:         if (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.uploadedfiletypes") ne '') {
1.495     raeburn   356:             unless ($env{'request.state'} eq 'construct') {
                    357:                 my $navmap = Apache::lonnavmaps::navmap->new();
                    358:                 if (ref($navmap)) {
                    359:                     my $mapres = $navmap->getResourceByUrl($map);
1.496     raeburn   360:                     my $is_page;
                    361:                     if (ref($mapres)) {
                    362:                         $is_page = $mapres->is_page();
                    363:                     }
                    364:                     unless ($is_page) {
1.495     raeburn   365:                         $needs_upload = 1;
                    366:                     }
                    367:                 }
                    368:             }
1.493     raeburn   369:         } else {
                    370:             unless ($env{'request.state'} eq 'construct') {
                    371:                 my $navmap = Apache::lonnavmaps::navmap->new();
                    372:                 if (ref($navmap)) {
1.495     raeburn   373:                     my $mapres = $navmap->getResourceByUrl($map);
1.496     raeburn   374:                     my $is_page;
                    375:                     if (ref($mapres)) {
                    376:                         $is_page = $mapres->is_page();
                    377:                     }
                    378:                     unless ($is_page) {
1.495     raeburn   379:                         my $res = $navmap->getBySymb($symb);
                    380:                         if (ref($res)) {
                    381:                             my $partlist = $res->parts();
                    382:                             if (ref($partlist) eq 'ARRAY') {
                    383:                                 foreach my $part (@{$partlist}) {
                    384:                                     my @types = $res->responseType($part);
                    385:                                     my @ids = $res->responseIds($part);
                    386:                                     for (my $i=0; $i < scalar(@ids); $i++) {
                    387:                                         if ($types[$i] eq 'essay') {
                    388:                                             my $partid = $part.'_'.$ids[$i];
                    389:                                             if (&Apache::lonnet::EXT("resource.$partid.uploadedfiletypes") ne '') {
                    390:                                                 $needs_upload = 1;
                    391:                                                 last;
                    392:                                             }
1.493     raeburn   393:                                         }
                    394:                                     }
                    395:                                 }
1.495     raeburn   396:                             } 
                    397:                         }
1.493     raeburn   398:                     }
                    399:                 }
                    400:             }
                    401:         }
1.495     raeburn   402:         if ($needs_upload) {
                    403:             $extra_head .= &Apache::lonhtmlcommon::file_submissionchk_js();
                    404:         }
1.478     raeburn   405:     }
1.425     raeburn   406: 
1.344     albertel  407:     my %body_args;
                    408:     if (defined($found{'html'})) {
                    409: 	$body_args{'skip_phases'}{'head'}=1;
                    410:     } else {
1.343     albertel  411: 	
1.345     albertel  412: 	$extra_head .= &Apache::lonhtmlcommon::spellheader();
1.343     albertel  413: 
1.379     albertel  414: 	$extra_head .= &Apache::londefdef::generate_css_links();
                    415: 
1.384     albertel  416: 	if ($env{'request.state'} eq 'construct') {
1.343     albertel  417: 	    $extra_head.=&Apache::edit::js_change_detection().
                    418: 		"<script type=\"text/javascript\">\n".
                    419: 		"if (typeof swmenu != 'undefined') {swmenu.currentURL=null;}\n".
                    420: 		&Apache::loncommon::browser_and_searcher_javascript().
                    421:                 "\n</script>\n";
                    422: 	}
1.159     albertel  423:     }
1.343     albertel  424: 
1.446     bisitz    425:     my $pageheader = '';
1.344     albertel  426:     if (defined($found{'body'})) {
                    427: 	$body_args{'skip_phases'}{'body'}=1;
                    428:     } elsif (!defined($found{'body'}) 
                    429: 	     && $env{'request.state'} eq 'construct') {
1.343     albertel  430: 	if ($target eq 'web' || $target eq 'edit') {
1.513     raeburn   431:         # Breadcrumbs for Authoring Space
1.450     bisitz    432:         &Apache::lonhtmlcommon::clear_breadcrumbs();
                    433:         &Apache::lonhtmlcommon::add_breadcrumb({
1.513     raeburn   434:             'text'  => 'Authoring Space',
1.497     raeburn   435:             'href'  => &Apache::loncommon::authorspace($env{'request.uri'}),
1.450     bisitz    436:         });
1.460     droeschl  437:         # breadcrumbs (and tools) will be created 
                    438:         # in start_page->bodytag->innerregister
                    439: 
1.450     bisitz    440: # FIXME Where are we?
                    441: #        &Apache::lonhtmlcommon::add_breadcrumb({
                    442: #            'text'  => 'Problem Editing', # 'Problem Testing'
                    443: #            'href'  => '',
                    444: #        });
1.460     droeschl  445:         $pageheader =&Apache::loncommon::head_subbox(
1.446     bisitz    446:                 &Apache::loncommon::CSTR_pageheader());
1.297     albertel  447: 	}
1.272     albertel  448:     } elsif (!defined($found{'body'})) {
1.343     albertel  449: 	my %add_entries;
1.159     albertel  450: 	my $background=&Apache::lonxml::get_param('background',$parstack,
                    451: 						  $safeeval);
1.343     albertel  452: 	if ($background ne '' ) {
                    453: 	    $add_entries{'background'} = $background;
                    454: 	}
1.344     albertel  455: 
1.290     albertel  456: 	my $bgcolor=&Apache::lonxml::get_param('bgcolor',$parstack,
                    457: 					       $safeeval);
1.446     bisitz    458:         if ($bgcolor eq '' ) { $bgcolor = '#FFFFFF'; }
1.344     albertel  459: 
1.446     bisitz    460:         $body_args{'bgcolor'}        = $bgcolor;
                    461:         # $body_args{'no_title'}       = 1;
                    462:         $body_args{'force_register'} = 1;
                    463:         $body_args{'add_entries'}    = \%add_entries;
1.466     droeschl  464:         if ( $env{'request.state'} eq   'construct') {
1.446     bisitz    465:             $body_args{'only_body'}  = 1;
1.518     raeburn   466:         } elsif ($target eq 'web') {
                    467:             $body_args{'print_suppress'} = 1;
1.446     bisitz    468:         }
1.344     albertel  469:     }
1.365     albertel  470:     $body_args{'no_auto_mt_title'} = 1;
1.344     albertel  471:     my $page_start = &Apache::loncommon::start_page($name,$extra_head,
                    472: 						    \%body_args);
1.446     bisitz    473:     $page_start .= $pageheader;
1.462     raeburn   474:     if (!defined($found{'body'}) 
                    475: 	&& $env{'request.state'} ne 'construct'
                    476: 	&& ($target eq 'web' || $target eq 'webgrade')) {
                    477: 
                    478: 	my ($symb,undef,undef,undef,$publicuser)= &Apache::lonnet::whichuser();
                    479:         if ($symb eq '' && !$publicuser) {
                    480:             $page_start .= '<p class="LC_info">'
                    481:                           .&mt('Browsing resource, all submissions are temporary.')
                    482:                           .'</p>';
1.457     bisitz    483:         }
1.344     albertel  484:     }
                    485: 
1.409     albertel  486:     if (!defined($found{'body'}) && $env{'request.state'} ne 'construct') {
1.343     albertel  487: 	$page_start .= &Apache::lonxml::message_location();
1.159     albertel  488:     }
                    489:     my $form_tag_start;
                    490:     if (!defined($found{'form'})) {
1.337     albertel  491: 	$form_tag_start='<form name="lonhomework" enctype="multipart/form-data" method="post" action="';
1.465     raeburn   492: 	my $uri = &Apache::loncommon::inhibit_menu_check(
1.455     droeschl  493:                 &Apache::lonenc::check_encrypt($env{'request.uri'}));
1.464     raeburn   494:         $uri = &HTML::Entities::encode($uri,'<>&"');
1.327     albertel  495: 	$form_tag_start.=$uri.'" ';
                    496: 	if ($target eq 'edit') {
                    497: 	    $form_tag_start.=&Apache::edit::form_change_detection();
                    498: 	}
1.493     raeburn   499:         my ($symb,$courseid,$udom,$uname)=&Apache::lonnet::whichuser();
                    500:         my ($path,$multiresp) = 
                    501:             &Apache::loncommon::get_turnedin_filepath($symb,$uname,$udom);
1.495     raeburn   502:         if (($is_task) || ($needs_upload)) {
                    503:             $form_tag_start .= ' onsubmit="return file_submission_check(this,'."'$path','$multiresp'".');"';
1.478     raeburn   504:         }
1.368     albertel  505: 	$form_tag_start.='>'."\n";
1.355     albertel  506: 
                    507: 	if ($symb =~ /\S/) {
                    508: 	    $symb=
                    509: 		&HTML::Entities::encode(&Apache::lonenc::check_encrypt($symb));
                    510: 	    $form_tag_start.=
1.368     albertel  511: 		"\t".'<input type="hidden" name="symb" value="'.$symb.'" />'."\n";
1.355     albertel  512: 	}
1.159     albertel  513:     }
1.343     albertel  514:     return ($page_start,$form_tag_start);
1.105     albertel  515: }
                    516: 
1.141     matthew   517: #use Time::HiRes();
1.105     albertel  518: sub get_resource_name {
1.159     albertel  519:     my ($parstack,$safeeval)=@_;
1.388     foxr      520:     my $name;
1.204     albertel  521:     if (defined($Apache::lonhomework::name)) {
1.388     foxr      522: 	$name = $Apache::lonhomework::name;
                    523:     } else {
                    524: 	my ($symb)=&Apache::lonnet::whichuser();
1.392     albertel  525: 	$name=&Apache::lonnet::gettitle($symb);
1.388     foxr      526: 	if ($name eq '') {
                    527: 	    $name=&Apache::lonnet::EXT('resource.title');
                    528: 	    if ($name eq 'con_lost') { $name = ''; }
                    529: 	}
                    530: 	if ($name!~/\S+/) {
                    531: 	    $name=$env{'request.uri'};
                    532: 	    $name=~s-.*/([^/]+)$-$1-;
                    533: 	}
                    534: 	# The name has had html tags escaped:
                    535:        
                    536: 	$name=~s/&lt;/</gs;
                    537: 	$name=~s/&gt;/>/gs;
                    538: 
                    539: 	$Apache::lonhomework::name=$name;
1.204     albertel  540:     }
1.159     albertel  541:     return $name;
1.105     albertel  542: }
                    543: 
                    544: sub setup_rndseed {
1.479     raeburn   545:     my ($safeeval,$target)=@_;
1.367     albertel  546:     my ($symb)=&Apache::lonnet::whichuser();
1.479     raeburn   547:     my ($questiontype,$set_safespace,$rndseed);
                    548:     if ($target eq 'analyze') {
                    549:         $questiontype = $env{'form.grade_questiontype'};
                    550:     }
                    551:     unless (defined($questiontype)) {
                    552:         $questiontype = $Apache::lonhomework::type;
                    553:     }
1.333     albertel  554:     if ($env{'request.state'} eq "construct" 
                    555: 	|| $symb eq '' 
                    556: 	|| $Apache::lonhomework::type eq 'practice'
                    557: 	|| $Apache::lonhomework::history{'resource.CODE'}) {
1.316     www       558: 	&Apache::loncommon::get_unprocessed_cgi($ENV{'QUERY_STRING'},
1.317     albertel  559: 						['rndseed']);
1.284     albertel  560: 	$rndseed=$env{'form.rndseed'};
1.159     albertel  561: 	if (!$rndseed) {
1.162     albertel  562: 	    $rndseed=$Apache::lonhomework::history{'rndseed'};
                    563: 	    if (!$rndseed) {
                    564: 		$rndseed=time;
                    565: 	    }
1.284     albertel  566: 	    $env{'form.rndseed'}=$rndseed;
1.162     albertel  567: 	}
1.479     raeburn   568:         if (($env{'request.state'} eq "construct") && 
                    569:             ($Apache::lonhomework::type eq 'randomizetry')) {
                    570:             my $tries = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.tries"};
                    571:             if ($tries) {
                    572:                 $rndseed += $tries;
                    573:             }
1.480     raeburn   574:             $env{'form.'.$Apache::inputtags::part.'.rndseed'}=$rndseed;
1.479     raeburn   575:         }
1.374     albertel  576: 	if ( ($env{'form.resetdata'} eq &mt('New Problem Variation')
                    577: 	      && $env{'form.submitted'} eq 'yes')  ||
1.284     albertel  578: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
1.190     albertel  579: 	    srand(time);
                    580: 	    $rndseed=int(rand(2100000000));
1.284     albertel  581: 	    $env{'form.rndseed'}=$rndseed;
                    582: 	    delete($env{'form.resetdata'});
                    583: 	    delete($env{'form.newrandomization'});
1.159     albertel  584: 	}
1.488     www       585:         $rndseed=~s/\,/\:/g;
                    586:         $rndseed=~s/[^\w\d\:\-]//g;
1.489     www       587: 	if (defined($rndseed)) {
                    588:             my ($c1,$c2)=split(/\:/,$rndseed);
                    589:             unless ($c2) { $c2=0; }
                    590:             unless (($c1==int($c1)) && ($c2==int($c2))) {
                    591: 	       $rndseed=join(':',&Apache::lonnet::digest($rndseed));
                    592:             }
1.187     albertel  593:         }
1.247     albertel  594:         if ($Apache::lonhomework::history{'resource.CODE'}) {
                    595: 	   $rndseed=&Apache::lonnet::rndseed();
                    596: 	}
1.479     raeburn   597:         $set_safespace = 1;
                    598:     } elsif ($questiontype eq 'randomizetry') {
                    599:         if ($target eq 'analyze') {
                    600:             if (defined($env{'form.grade_rndseed'})) {
                    601:                 $rndseed = $env{'form.grade_rndseed'};
                    602:             }
                    603:         }
                    604:         unless (($target eq 'analyze') && (defined($rndseed))) {
                    605:             $rndseed=&Apache::lonnet::rndseed();
                    606:             my $curr_try = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.tries"};
                    607:             if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    608:                 $curr_try ++;
                    609:             }
                    610:             if ($rndseed =~/^(\d+)[,:](\d+)$/) {
                    611:                 $rndseed = $1;
                    612:             }
                    613:             if ($curr_try) {
                    614:                 my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                    615:                 if (($reqtries =~ /^\d+$/) && ($reqtries > 1)) {
                    616:                     my $inc = int(($curr_try-1)/$reqtries);
                    617:                     $rndseed += $inc;
                    618:                 } else {
                    619:                     $rndseed += $curr_try;
                    620:                 }
                    621:             }
                    622:         }
                    623:         $set_safespace = 1;
                    624:     }
                    625:     if ($set_safespace) {
                    626:         if ($safeeval) {
                    627:             &Apache::lonxml::debug("Setting rndseed to $rndseed");
                    628:             &Apache::run::run('$external::randomseed="'.$rndseed.'";',$safeeval);
                    629:         }
                    630:     }
                    631:     unless (($env{'request.state'} eq "construct") || ($symb eq '')) {
                    632:         $env{'form.'.$Apache::inputtags::part.'.rndseed'}=$rndseed;
1.159     albertel  633:     }
                    634:     return $rndseed;
1.105     albertel  635: }
                    636: 
1.268     albertel  637: sub remember_problem_state {
                    638:     return '
1.284     albertel  639:        <input type="hidden" name="problemstate" value="'.$env{'form.problemstate'}.'" />
                    640:        <input type="hidden" name="problemtype" value="'.$env{'form.problemtype'}.'" />
                    641:        <input type="hidden" name="problemstatus" value="'.$env{'form.problemstatus'}.'" />';
1.268     albertel  642: }
                    643: 
1.487     www       644: sub problem_edit_action_button {
                    645:     my ($name,$action,$accesskey,$text,$flag)=@_;
                    646:     my $actionscript="setmode(this.form,'$action')";
                    647:     return "\n<input type='button' name='$name' accesskey='$accesskey' value='".&mt($text)."'".
                    648:            ($flag?&Apache::edit::submit_ask_anyway($actionscript):&Apache::edit::submit_dont_ask($actionscript))." />";
                    649: }
                    650: 
1.423     www       651: sub problem_edit_buttons {
1.487     www       652:    my ($mode)=@_;
                    653: # Buttons that do not save
                    654:    my $result='<div class="LC_edit_problem_discards">'.
                    655:               &problem_edit_action_button('subdiscview','discard','d','Discard Edits and View',1);
                    656:    if ($mode eq 'editxml') {
                    657:        $result.=&problem_edit_action_button('subedit','edit','e','Edit',1);
                    658:        $result.=&problem_edit_action_button('subundo','undoxml','u','Undo',1);
                    659:        $result.=&Apache::lonhtmlcommon::dragmath_button("LC_editxmltext",1);
                    660:    } else {
                    661:        $result.=&problem_edit_action_button('subeditxml','editxml','x','EditXML',1);
                    662:        $result.=&problem_edit_action_button('subundo','undo','u','Undo',1);
                    663:    }
                    664:    $result.="\n</div>";
                    665: # Buttons that save
                    666:    $result.='<div class="LC_edit_problem_saves">';
                    667:    if ($mode eq 'editxml') {
                    668:        $result.=&problem_edit_action_button('subsaveedit','saveeditxml','s','Save and EditXML');
                    669:        $result.=&problem_edit_action_button('subsaveview','saveviewxml','v','Save and View');
                    670:    } else {
                    671:        $result.=&problem_edit_action_button('subsaveedit','saveedit','s','Save and Edit');
                    672:        $result.=&problem_edit_action_button('subsaveview','saveview','v','Save and View');
                    673:    }
                    674:    $result.="\n</div>\n";
                    675:    return $result;
1.423     www       676: }
                    677: 
                    678: sub problem_edit_header {
1.487     www       679:     return '<input type="hidden" name="submitted" value="edit" />'.
                    680: 	&remember_problem_state('edit').'
1.423     www       681: <div class="LC_edit_problem_header">
                    682: <div class="LC_edit_problem_header_title">
                    683: '.&mt('Problem Editing').&Apache::loncommon::help_open_menu('Problem Editing','Problem_Editor_XML_Index',5,'Authoring').'
                    684: </div>'.
1.487     www       685: '<input type="hidden" name="problemmode" value="saveedit" />'.
1.423     www       686: &problem_edit_buttons().'
1.459     bisitz    687: <hr style="clear:both;" />
1.409     albertel  688: '.&Apache::lonxml::message_location().'
1.408     albertel  689: </div>
                    690: '.
1.515     golterma  691:        '<table id="LC_edit_problem_colorful" border="0" width="100%"><tr><td bgcolor="#F8F8F8">';
1.105     albertel  692: }
                    693: 
                    694: sub problem_edit_footer {
1.412     albertel  695:     return '</td></tr></table><br />
                    696: <div class="LC_edit_problem_footer">
1.453     bisitz    697:   <hr />'.
1.423     www       698: &problem_edit_buttons().'
1.459     bisitz    699:   <hr style="clear:both;" />
1.412     albertel  700: </div>
                    701: '.
1.342     albertel  702:     "\n</form>\n".&Apache::loncommon::end_page();
1.105     albertel  703: }
                    704: 
1.235     albertel  705: sub option {
                    706:     my ($value,$name) = @_;
                    707:     my $result ="<option value='".$value."' ";
1.284     albertel  708:     if ($env{'form.'.$name} eq $value) {
1.235     albertel  709: 	$result.=" selected='on' ";
                    710:     }
                    711:     $result.='>';
                    712:     return $result;
                    713: }
                    714: 
1.105     albertel  715: sub problem_web_to_edit_header {
1.159     albertel  716:     my ($rndseed)=@_;
1.406     albertel  717:     my $result .= '<div class="LC_edit_problem_header">';
                    718: 
                    719:     if (!$Apache::lonhomework::parsing_a_task) {
                    720: 	$result .= 
                    721: 	    '<div class="LC_edit_problem_header_title">'.
                    722: 	    &mt('Problem Testing').
                    723: 	    &Apache::loncommon::help_open_topic('Problem_Editor_Testing_Area').
                    724: 	    '</div>';
                    725:     } else {
                    726: 	$result .= 
                    727: 	    '<div class="LC_edit_problem_header_title">'.
                    728: 	    &mt('Task Testing').
                    729: 	    '</div>';
                    730:     }
                    731:     
1.315     albertel  732:     my $show_all_foils_text = 
                    733: 	($Apache::lonhomework::parsing_a_task) ?
1.452     bisitz    734: 	&mt('Show All Instances')
                    735: 	: &mt('Show All Foils');
1.315     albertel  736: 
1.452     bisitz    737:     my $show_all= '<span class="LC_nobreak"><label for="showallfoils">'
                    738:                  .'<input type="checkbox" name="showallfoils"';
1.440     bisitz    739:     if (defined($env{'form.showallfoils'})) { $show_all.=' checked="checked"'; }
1.452     bisitz    740:     $show_all.= ' /> '.$show_all_foils_text
                    741:                .'</label></span>';
1.406     albertel  742: 
                    743: 
1.384     albertel  744: 
1.406     albertel  745:     $result .= '<div class="LC_edit_problem_header_status_row">';
1.313     albertel  746:     if (!$Apache::lonhomework::parsing_a_task) {
                    747: 	$result.="
1.406     albertel  748: <div class='LC_edit_problem_header_row1'>
                    749: <span class=\"LC_nobreak\">
1.405     albertel  750: ".&mt("Problem Status:")."
1.235     albertel  751: <select name='problemstate'>
1.270     albertel  752:   <option value=''></option>
1.235     albertel  753:   ".&option('CLOSED'               ,'problemstate').&mt("Closed")."</option>
                    754:   ".&option('CAN_ANSWER'           ,'problemstate').&mt("Answerable")."</option>
                    755:   ".&option('CANNOT_ANSWER_tries'  ,'problemstate').&mt("Open with full tries")."</option>
                    756:   ".&option('CANNOT_ANSWER_correct','problemstate').&mt("Open and correct")."</option>
                    757:   ".&option('SHOW_ANSWER'          ,'problemstate').&mt("Show Answer")."</option>
                    758: </select>
1.406     albertel  759: </span>
                    760: <span class=\"LC_nobreak\">
1.405     albertel  761: ".&mt("Problem Type:")."
1.235     albertel  762: <select name='problemtype'>
1.270     albertel  763:   <option value=''></option>
1.509     bisitz    764:   ".&option('exam'   ,'problemtype').&mt("Bubblesheet Exam Problem")."</option>
1.428     raeburn   765:   ".&option('problem','problemtype').&mt("Homework Problem")."</option>
1.242     albertel  766:   ".&option('survey' ,'problemtype').&mt("Survey Question")."</option>
1.465     raeburn   767:   ".&option('surveycred' ,'problemtype').&mt("Survey Question (with credit)")."</option>
                    768:   ".&option('anonsurvey' ,'problemtype').&mt("Anonymous Survey Question")."</option>
                    769:   ".&option('anonsurveycred' ,'problemtype').&mt("Anonymous Survey Question (with credit)")."</option>
1.428     raeburn   770:   ".&option('practice' ,'problemtype').&mt("Practice Problem")."</option>
1.479     raeburn   771:   ".&option('randomizetry' ,'problemtype').&mt("New Randomization Each Try")."</option>
1.235     albertel  772: </select>
1.406     albertel  773: </span>
                    774: $show_all
                    775: </div>
                    776: <div class='LC_edit_problem_header_row2'>
                    777: <span class=\"LC_nobreak\">
1.405     albertel  778: ".&mt("Feedback Mode:")."
1.235     albertel  779: <select name='problemstatus'>
                    780:   <option value=''></option>
1.242     albertel  781:   ".&option('yes','problemstatus').&mt("Show Feedback")."</option>
1.517     bisitz    782:   ".&option('no', 'problemstatus').&mt("Don't Show Incorrect/Correct Feedback")."</option>
1.405     albertel  783:   ".&option('no_feedback_ever', 'problemstatus').&mt("Don't Show Any Feedback")."</option>
1.235     albertel  784: </select>
1.406     albertel  785: </span>
                    786: ";
                    787: 
1.376     albertel  788:     } elsif ($Apache::lonhomework::parsing_a_task) {
                    789: 	$result.="
1.406     albertel  790: <div class='LC_edit_problem_header_row1'>
                    791: <span class=\"LC_nobreak\">
1.405     albertel  792: ".&mt("Problem Status:")."
1.376     albertel  793: <select name='problemstate'>
                    794:   <option value=''></option>
                    795:   ".&option('CLOSED'               ,'problemstate').&mt("Closed")."</option>
                    796:   ".&option('CAN_ANSWER'           ,'problemstate').&mt("Answerable")."</option>
                    797:   ".&option('WEB_GRADE'            ,'problemstate').&mt("Criteria Grading")."</option>
                    798:   ".&option('SHOW_ANSWER'          ,'problemstate').&mt("Show Feedback")."</option>
                    799: </select>
1.406     albertel  800: </span>
                    801: $show_all
                    802: ";
                    803:     }
                    804:     $result.='
                    805:        <span class="LC_nobreak">
                    806:        '.&mt('Apply style file: ').'
                    807:          <input type="text" name="style_file" value="'.&HTML::Entities::encode($env{'construct.style'},'"<>&').'" />
                    808:          <a href="javascript:openbrowser(\'lonhomework\',\'style_file\',\'sty\')">'.&mt('Select').'</a>
                    809:        </span>
1.422     www       810:      </div>
                    811:      <div class="LC_edit_problem_header_row1">'.
                    812:        &Apache::lonxml::renderingoptions().'
1.406     albertel  813:      </div>
                    814:      <input type="submit" name="changeproblemmode" value="'.&mt("Change View").'" />
                    815:      <input type="submit" name="clear_style_file" accesskey="d" value="'.&mt('Show Default View').'" />
                    816:      <input type="submit" name="resetdata" accesskey="r" value="'.&mt('Reset Submissions').'" />
                    817:    </div>
1.453     bisitz    818:    <hr />
1.406     albertel  819:    <div class="LC_edit_problem_header_randomize_row">
                    820:      <input type="submit" name="newrandomization" accesskey="a" value="'.&mt('New Randomization').'" />
                    821:      <input type="submit" name="changerandseed" value="'.&mt('Change Random Seed To:').'" />
1.488     www       822:      <input type="text" name="rndseed" size="24" value="'.
1.406     albertel  823: 	       $rndseed.'"
                    824:              onchange="javascript:document.lonhomework.changerandseed.click()" />';
                    825: 
                    826:     if (!$Apache::lonhomework::parsing_a_task) {
                    827: 	my $numtoanalyze=$env{'form.numtoanalyze'};
                    828: 	if (!$numtoanalyze) { $numtoanalyze=20; }
1.408     albertel  829: 	$result .= '<span class="LC_nobreak">'.
                    830: 	    &mt('[_1] for [_2] versions.',
1.416     raeburn   831: 		'<input type="button" name="submitmode" value="'.&mt('Calculate answers').'" '.
1.419     bisitz    832:                 'onclick="javascript:setmode(this.form,'."'calcanswers'".')" />'
                    833:                ,'<input type="text" name="numtoanalyze" value="'.
1.408     albertel  834: 		$numtoanalyze.'" size="5" />').
                    835: 		&Apache::loncommon::help_open_topic("Analyze_Problem",'',undef,undef,300).
                    836: 		'</span>';
                    837: 						    
1.313     albertel  838:     }
1.406     albertel  839: 
                    840:     $result.='
                    841:    </div>
1.453     bisitz    842:    <hr />
1.447     bisitz    843:    <div>';
1.416     raeburn   844:     $result.='<input type="hidden" name="problemmode" value="view" />';
                    845:     $result .= '<input type="button" name="submitmode" accesskey="e" value="'.&mt('Edit').'" '.
                    846:                'onclick="javascript:setmode(this.form,'."'edit'".')" />';
                    847:     $result .= '<input type="button" name="submitmode" accesskey="x" value="'.&mt('EditXML').'" '.
                    848:                'onclick="javascript:setmode(this.form,'."'editxml'".')" />';
1.408     albertel  849:     $result.='
                    850:    </div>
1.453     bisitz    851:    <hr />
1.409     albertel  852:    '.&Apache::lonxml::message_location().'
1.406     albertel  853: </div>';
1.159     albertel  854:     return $result;
1.48      albertel  855: }
                    856: 
1.65      albertel  857: sub initialize_storage {
1.357     albertel  858:     my ($given_symb) = @_;
1.353     albertel  859:     undef(%Apache::lonhomework::results);
                    860:     undef(%Apache::lonhomework::history);
1.357     albertel  861:     my ($symb,$courseid,$domain,$name) = 
1.367     albertel  862: 	&Apache::lonnet::whichuser($given_symb);
1.353     albertel  863:     
                    864:     # anonymous users (CODEd exams) have no data
                    865:     if ($name eq 'anonymous' 
                    866: 	&& !defined($domain)) {
                    867: 	return;
                    868:     }
                    869: 
1.333     albertel  870:     if ($env{'request.state'} eq 'construct' 
                    871: 	|| $symb eq ''
                    872: 	|| $Apache::lonhomework::type eq 'practice') {
                    873: 	
                    874: 	my $namespace = $symb || $env{'request.uri'};
                    875: 	if ($env{'form.resetdata'} eq &mt('Reset Submissions') ||
1.374     albertel  876: 	    ($env{'form.resetdata'} eq &mt('New Problem Variation')
                    877: 	     && $env{'form.submitted'} eq 'yes') ||
1.333     albertel  878: 	    $env{'form.newrandomization'} eq &mt('New Randomization')) {
                    879: 	    &Apache::lonnet::tmpreset($namespace,'',$domain,$name);
                    880: 	    &Apache::lonxml::debug("Attempt reset");
                    881: 	}
1.159     albertel  882: 	%Apache::lonhomework::history=
1.333     albertel  883: 	    &Apache::lonnet::tmprestore($namespace,'',$domain,$name);
1.159     albertel  884: 	my ($temp)=keys %Apache::lonhomework::history ;
                    885: 	&Apache::lonxml::debug("Return message of $temp");
                    886:     } else {
                    887: 	%Apache::lonhomework::history=
                    888: 	    &Apache::lonnet::restore($symb,$courseid,$domain,$name);
                    889:     }
1.353     albertel  890: 
1.159     albertel  891:     #ignore error conditions
1.67      albertel  892:     my ($temp)=keys %Apache::lonhomework::history ;
1.159     albertel  893:     if ($temp =~ m/^error:.*/) { %Apache::lonhomework::history=(); }
1.65      albertel  894: }
                    895: 
1.435     jms       896: =pod
                    897: 
                    898: =item finalize_storage()
                    899: 
1.524   ! raeburn   900: 	Stores away the result hash to a student's environment;
1.523     raeburn   901: 	checks form.grade_ for specific values, otherwise stores
                    902: 	to the running user's environment.
1.524   ! raeburn   903: 
        !           904:         &check_correctness_changes() is called in two circumstances
        !           905:         in which the results hash is to be stored permanently, for
        !           906:         grading triggered by a student's submission, where feedback on
        !           907:         correctness is to be provided to the student. 
        !           908: 
        !           909:         1. Immediately prior to storing the results hash
        !           910: 
        !           911:         To handle the case where a student's submission (and award) were 
        !           912:         stored after history was retrieved in &initialize_storage(), e.g.,
        !           913:         if a student submitted answers in quick succession (e.g., from 
        !           914:         multiple tabs).  &Apache::inputtags::hidealldata() is called for
        !           915:         any parts with out-of-order storage (i.e., correct then incorrect,
        !           916:         where awarded >= 1 when correct).
        !           917: 
        !           918:         2. Immediately after storing the results hash
        !           919: 
        !           920:         To handle the case where lond on the student's homeserver returns
        !           921:         delay:N -- where N is the number of transactions between the last
        !           922:         retrieved in &initialize_storage() and the last stored immediately
        !           923:         before permanent storage of the current transaction via 
        !           924:         lond::store_handler().  &Apache::grades::makehidden() is called  
        !           925:         for any parts with out-of-order storage (i.e., correct then incorrect,
        !           926:         where awarded >= 1 when correct).
        !           927: 
        !           928: 	Will call &store_aggregates() to increment totals for attempts, 
        !           929:         students, and corrects, if running user has student role.
        !           930: 
1.435     jms       931: =cut
                    932: 
                    933: 
1.65      albertel  934: sub finalize_storage {
1.357     albertel  935:     my ($given_symb) = @_;
1.159     albertel  936:     my $result;
1.289     albertel  937:     if (%Apache::lonhomework::results) {
1.323     albertel  938: 	my @remove = grep(/^INTERNAL_/,keys(%Apache::lonhomework::results));
                    939: 	delete(@Apache::lonhomework::results{@remove});
1.357     albertel  940: 	my ($symb,$courseid,$domain,$name) = 
1.367     albertel  941: 	    &Apache::lonnet::whichuser($given_symb);
1.333     albertel  942: 	if ($env{'request.state'} eq 'construct' 
                    943: 	    || $symb eq ''
                    944: 	    || $Apache::lonhomework::type eq 'practice') {
                    945: 	    my $namespace = $symb || $env{'request.uri'};
1.284     albertel  946: 	    $Apache::lonhomework::results{'rndseed'}=$env{'form.rndseed'};
1.159     albertel  947: 	    $result=&Apache::lonnet::tmpstore(\%Apache::lonhomework::results,
1.333     albertel  948: 					      $namespace,'',$domain,$name);
1.159     albertel  949: 	    &Apache::lonxml::debug('Construct Store return message:'.$result);
                    950: 	} else {
1.524   ! raeburn   951:             my ($laststore,$checkedparts,@parts,%postcorrect);
        !           952:             if (($env{'user.name'} eq $name) && ($env{'user.domain'} eq $domain) &&
        !           953:                 (!$Apache::lonhomework::scantronmode) && (!defined($env{'form.grade_symb'})) &&
        !           954:                 (!defined($env{'form.grade_courseid'}))) {
        !           955:                 if ($Apache::lonhomework::history{'version'}) {
        !           956:                     $laststore = $Apache::lonhomework::history{'version'}.'='.
        !           957:                                  $Apache::lonhomework::history{'timestamp'};
        !           958:                 } else {
        !           959:                     $laststore = '0=0';
        !           960:                 }
        !           961:                 my %record = &Apache::lonnet::restore($symb,$courseid,$domain,$name);
        !           962:                 if ($record{'version'}) {
        !           963:                     my ($newversion,$oldversion,$oldtimestamp);
        !           964:                     if ($Apache::lonhomework::history{'version'}) {
        !           965:                         $oldversion = $Apache::lonhomework::history{'version'};
        !           966:                         $oldtimestamp = $Apache::lonhomework::history{'timestamp'};
        !           967:                     } else {
        !           968:                         $oldversion = 0;
        !           969:                         $oldtimestamp = 0;
        !           970:                     }
        !           971:                     if ($record{'version'} > $oldversion) {
        !           972:                         if ($record{'timestamp'} >= $oldtimestamp) {
        !           973:                             $laststore = $record{'version'}.'='.$record{'timestamp'};
        !           974:                             $newversion = $record{'version'} + 1;
        !           975:                             $checkedparts = 1;
        !           976:                             foreach my $key (keys(%Apache::lonhomework::results)) {
        !           977:                                 if ($key =~ /^resource\.([^\.]+)\.solved$/) {
        !           978:                                     my $part = $1;
        !           979:                                     if ($Apache::lonhomework::results{$key} eq 'incorrect_attempted') {
        !           980:                                         push(@parts,$part);
        !           981:                                     }
        !           982:                                 }
        !           983:                             }
        !           984:                             if (@parts) {
        !           985:                                 my @parts_to_hide = &check_correctness_changes($symb,$courseid,$domain,$name,
        !           986:                                                                                \%record,\@parts,$newversion,
        !           987:                                                                                $oldversion);
        !           988:                                 if (@parts_to_hide) {
        !           989:                                     foreach my $part (@parts_to_hide) {
        !           990:                                         $postcorrect{$part} = 1;
        !           991:                                         &Apache::inputtags::hidealldata($part);
        !           992:                                     }
        !           993:                                 }
        !           994:                             }
        !           995:                         }
        !           996:                     }
        !           997:                 }
        !           998:             }
1.159     albertel  999: 	    $result=&Apache::lonnet::cstore(\%Apache::lonhomework::results,
1.524   ! raeburn  1000: 					    $symb,$courseid,$domain,$name,$laststore);
        !          1001:             if ($result =~ /^delay\:(\d+)$/) {
        !          1002:                 my $numtrans = $1;
        !          1003:                 my ($oldversion) = split(/=/,$laststore);
        !          1004:                 if ($numtrans) {
        !          1005:                     my $newversion = $oldversion + 1 + $numtrans;
        !          1006:                     my @possparts;
        !          1007:                     if ($checkedparts) {
        !          1008:                         foreach my $part (@parts) {
        !          1009:                             unless ($postcorrect{$part}) {
        !          1010:                                 push(@possparts,$part);
        !          1011:                             }
        !          1012:                         }
        !          1013:                     } else {
        !          1014:                         foreach my $key (keys(%Apache::lonhomework::results)) {
        !          1015:                             if ($key =~ /^resource\.([^\.]+)\.solved$/) {
        !          1016:                                 my $part = $1;
        !          1017:                                 unless ($postcorrect{$part}) {
        !          1018:                                     if ($Apache::lonhomework::results{$key} eq 'incorrect_attempted') {
        !          1019:                                         push(@possparts,$part);
        !          1020:                                     }
        !          1021:                                 }
        !          1022:                             }
        !          1023:                         }
        !          1024:                     }
        !          1025:                     if (@possparts) {
        !          1026:                         my %newrecord = &Apache::lonnet::restore($symb,$courseid,$domain,$name);
        !          1027:                         my @parts_to_hide = &check_correctness_changes($symb,$courseid,$domain,$name,
        !          1028:                                                                        \%newrecord,\@possparts,$newversion,
        !          1029:                                                                        $oldversion);
        !          1030:                         if (@parts_to_hide) {
        !          1031:                             my $partslist = join(',',@parts_to_hide);
        !          1032:                             &Apache::grades::makehidden($newversion,$partslist,\%newrecord,$symb,$domain,$name,1);
        !          1033:                         }
        !          1034:                     }
        !          1035:                 }
        !          1036:             }
1.159     albertel 1037: 	    &Apache::lonxml::debug('Store return message:'.$result);
1.470     raeburn  1038:             &store_aggregates($symb,$courseid);
1.159     albertel 1039: 	}
1.323     albertel 1040:     } else {
                   1041: 	&Apache::lonxml::debug('Nothing to store');
1.67      albertel 1042:     }
1.159     albertel 1043:     return $result;
1.65      albertel 1044: }
                   1045: 
1.435     jms      1046: =pod
                   1047: 
1.524   ! raeburn  1048: =item check_correctness_changes()
        !          1049: 
        !          1050:         For all parts for which current results contain a solved status
        !          1051:         of "incorrect_attempted", check if there was a transaction in which  
        !          1052:         solved was set to "correct_by_student" in the time since the last 
        !          1053:         transaction (retrieved when &initialize_storage() was called i.e., 
        !          1054:         when &start_problem() was called), unless:
        !          1055:         (a) questiontype parameter is set to survey or anonymous survey (+/- credit)
        !          1056:         (b) problemstatus is set to no or no_feedback_ever
        !          1057:         If such a transaction exists, and did not occur after "reset status" 
        !          1058:         by a user with grading privileges, then the current transaction is an
        !          1059:         example of an out-of-order transaction (i.e., incorrect occurring after
        !          1060:         correct).  Accordingly, the current transaction should be hidden.
        !          1061: 
        !          1062: =cut
        !          1063: 
        !          1064: 
        !          1065: sub check_correctness_changes {
        !          1066:     my ($symb,$courseid,$domain,$name,$record,$parts,$newversion,$oldversion) = @_;
        !          1067:     my @parts_to_hide;
        !          1068:     unless ((ref($record) eq 'HASH') && (ref($parts) eq 'ARRAY')) {
        !          1069:         return @parts_to_hide;
        !          1070:     }
        !          1071:     if (@{$parts}) {
        !          1072:         my $usec;
        !          1073:         if (($env{'user.name'} eq $name) && ($env{'user.domain'} eq $domain) &&
        !          1074:             ($env{'request.course.id'} eq $courseid)) {
        !          1075:             $usec = $env{'request.course.sec'};
        !          1076:         } else {
        !          1077:             $usec = &Apache::lonnet::getsection($domain,$name,$courseid);
        !          1078:         }
        !          1079:         foreach my $id (@{$parts}) {
        !          1080:             next if (($Apache::lonhomework::results{'resource.'.$id.'.type'} =~ /survey/) ||
        !          1081:                      (&Apache::lonnet::EXT("resource.$id.problemstatus",$symb,
        !          1082:                                            $domain,$name,$usec,undef,$courseid) =~ /^no/));
        !          1083:             my $reset;
        !          1084:             for (my $i=$newversion-1; $i>=$oldversion; $i--) {
        !          1085:                 if (($record->{$i.':resource.'.$id.'.regrader'}) &&
        !          1086:                     ($record->{$i.':resource.'.$id.'.tries'} eq '') &&
        !          1087:                     ($record->{$i.':resource.'.$id.'.award'} eq '')) {
        !          1088:                     $reset = 1;
        !          1089:                 } elsif (($record->{$i.":resource.$id.solved"} eq 'correct_by_student') &&
        !          1090:                          ($record->{$i.":resource.$id.awarded"} >= 1)) {
        !          1091:                     unless ($reset) {
        !          1092:                         push(@parts_to_hide,$id);
        !          1093:                         last;
        !          1094:                     }
        !          1095:                 }
        !          1096:             }
        !          1097:         }
        !          1098:     }
        !          1099:     return @parts_to_hide;
        !          1100: }
        !          1101: 
        !          1102: =pod
        !          1103: 
1.435     jms      1104: item store_aggregates()
                   1105: 
                   1106: 	Sends hash of values to be incremented in nohist_resourcetracker.db
                   1107: 	for the course. Increments total number of attempts, unique students 
                   1108: 	and corrects for each part for an instance of a problem, as appropriate.
                   1109: 	
                   1110: =cut
                   1111: 
1.285     raeburn  1112: sub store_aggregates {
                   1113:     my ($symb,$courseid) = @_;
1.479     raeburn  1114:     my (%aggregate,%anoncounter,%randtrycounter);
1.286     albertel 1115:     my @parts;
1.288     albertel 1116:     my $cdomain = $env{'course.'.$env{'request.course.id'}.'.domain'};
                   1117:     my $cname = $env{'course.'.$env{'request.course.id'}.'.num'};
1.286     albertel 1118:     foreach my $key (keys(%Apache::lonhomework::results)) {
1.287     albertel 1119:         if ($key =~ /resource\.([^\.]+)\.tries/) {
1.286     albertel 1120:             push(@parts, $1);
1.285     raeburn  1121:         }
                   1122:     }
1.286     albertel 1123:     foreach my $part (@parts) {
1.470     raeburn  1124:         if ($env{'request.role'} =~/^st/) {
                   1125:             if ($Apache::lonhomework::results{'resource.'.$part.'.award'}
                   1126: 	        eq 'APPROX_ANS' ||
                   1127: 	        $Apache::lonhomework::results{'resource.'.$part.'.award'}
                   1128: 	        eq 'EXACT_ANS') {
                   1129:                 $aggregate{$symb."\0".$part."\0correct"} = 1;
                   1130:             }
                   1131:             if ($Apache::lonhomework::results{'resource.'.$part.'.tries'} == 1) {
                   1132:                 $aggregate{$symb."\0".$part."\0users"} = 1;
                   1133:             } else {
                   1134:                 my (undef,$last_reset) = &Apache::grades::get_last_resets($symb,$env{'request.course.id'},[$part]); 
                   1135:                 if ($last_reset) {
                   1136:                     if (&Apache::grades::get_num_tries(\%Apache::lonhomework::history,$last_reset,$part) == 0) {
                   1137:                         $aggregate{$symb."\0".$part."\0users"} = 1;
                   1138:                     }
                   1139:                 }
                   1140:             }
                   1141:             $aggregate{$symb."\0".$part."\0attempts"} = 1;
1.285     raeburn  1142:         }
1.470     raeburn  1143:         if (($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'anonsurvey') || 
1.479     raeburn  1144:             ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'anonsurveycred') ||
                   1145:             ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'randomizetry')) {
                   1146:             if ($Apache::lonhomework::results{'resource.'.$part.'.type'} eq 'randomizetry') {
                   1147:                 $randtrycounter{$symb."\0".$part} = 1;
                   1148:             } else {
                   1149:                 $anoncounter{$symb."\0".$part} = 1;
                   1150:             }
1.470     raeburn  1151:             my $needsrelease = $Apache::lonnet::needsrelease{'parameter:type:'.$Apache::lonhomework::results{'resource.'.$part.'.type'}};
                   1152:             if ($needsrelease) {   
                   1153:                 my $curr_required = $env{'course.'.$env{'request.course.id'}.'.internal.releaserequired'};
                   1154:                 if ($curr_required eq '') {
1.471     raeburn  1155:                     &Apache::lonnet::update_released_required($needsrelease);
1.470     raeburn  1156:                 } else {
                   1157:                     my ($currmajor,$currminor) = split(/\./,$curr_required);
                   1158:                     my ($needsmajor,$needsminor) = split(/\./,$needsrelease);
                   1159:                     if (($currmajor < $needsmajor) || ($currmajor == $needsmajor && $currminor < $needsminor)) {
1.471     raeburn  1160:                         &Apache::lonnet::update_released_required($needsrelease);
1.470     raeburn  1161:                     }
1.292     raeburn  1162:                 }
                   1163:             }
1.285     raeburn  1164:         }
                   1165:     }
1.292     raeburn  1166:     if (keys (%aggregate) > 0) {
1.289     albertel 1167: 	&Apache::lonnet::cinc('nohist_resourcetracker',\%aggregate,
1.292     raeburn  1168:                             $cdomain,$cname);
                   1169:     }
1.472     raeburn  1170:     if (keys(%anoncounter) > 0) {
1.481     raeburn  1171:         &Apache::lonnet::cput('nohist_anonsurveys',\%anoncounter,
                   1172:                               $cdomain,$cname);
1.472     raeburn  1173:     }
1.479     raeburn  1174:     if (keys(%randtrycounter) > 0) {
1.481     raeburn  1175:         &Apache::lonnet::cput('nohist_randomizetry',\%randtrycounter,
                   1176:                               $cdomain,$cname);
1.479     raeburn  1177:     }
1.292     raeburn  1178: }
1.289     albertel 1179: 
1.65      albertel 1180: sub checkout_msg {
1.211     albertel 1181:     my %lt=&Apache::lonlocal::texthash( 
                   1182: 		'resource'=>'The resource needs to be checked out',
                   1183: 		'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.',
                   1184:                 'warning'=>'Checking out resources is subject to course policies, and may exclude future credit even if done erroneously.',
1.509     bisitz   1185:                 'checkout'=>'Check out Bubblesheet Exam for Viewing',
                   1186: 		'checkout?'=>'Check out Bubblesheet Exam?');
1.352     albertel 1187:     my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.159     albertel 1188:     return (<<ENDCHECKOUT);
1.211     albertel 1189: <h2>$lt{'resource'}</h2>
                   1190:     <p>$lt{'id_expln'}</p>
1.449     bisitz   1191: <p class="LC_warning">$lt{'warning'}</p>
1.444     bisitz   1192: <form name="checkout" method="post" action="$uri">
1.91      albertel 1193: <input type="hidden" name="doescheckout" value="yes" />
1.514     bisitz   1194: <input type="button" name="checkoutbutton" value="$lt{'checkout'}" onclick="javascript:if (confirm('$lt{'checkout?'}')) { document.checkout.submit(); }" />
1.65      albertel 1195: </form>
                   1196: ENDCHECKOUT
                   1197: }
                   1198: 
1.252     albertel 1199: sub firstaccess_msg {
1.253     albertel 1200:     my ($time,$symb)=@_;
1.414     albertel 1201:     my $result;
                   1202:     my @interval=&Apache::lonnet::EXT("resource.0.interval");
                   1203:     if ($interval[1] eq 'map') {
                   1204: 	my ($map)=&Apache::lonnet::decode_symb($symb);
                   1205: 	my $foldertitle=&Apache::lonnet::gettitle($map);
                   1206:     
                   1207: 	&Apache::lonxml::debug("map is $map title is $foldertitle");
1.504     golterma 1208: 	$result .= "<h2>".&mt('The resources in "[_1]" are open for a limited time.',$foldertitle)."</h2>"
                   1209:                              .'<p>'.&mt('Once you click the "Show Resource" button below you have [_2] to complete all resources "[_1]".'
                   1210:                              ,$foldertitle,$time)."</p>";
1.414     albertel 1211:     } elsif ($interval[1] eq 'course') {
                   1212: 	my $course = $env{'course.'.$env{'request.course.id'}.'.description'};
1.504     golterma 1213:         $result .= "<h2>".&mt('The resources in "[_1]" are open for a limited time.',$course)."</h2>"
1.505     golterma 1214:                              .'<p>'.&mt('Once you click the "Show Resource" button below you have [_2] to complete all resources "[_1]".'
1.504     golterma 1215:                              ,$course,$time)."</p>";
1.414     albertel 1216:     } else {
                   1217: 	my $title=&Apache::lonnet::gettitle($symb);
1.504     golterma 1218:         $result .= "<h2>".&mt('This resource "[_1]" is open for a limited time.',$title)."</h2>"
                   1219:                              .'<p>'.&mt('Once you click the "Show Resource" button below you have [_2] to complete this resource "[_1]".'
                   1220:                              ,$title,$time)."</p>";
1.414     albertel 1221:     }
1.352     albertel 1222:     my $uri = &Apache::lonenc::check_encrypt($env{'request.uri'});
1.418     bisitz   1223:     my $buttontext = &mt('Show Resource');
                   1224:     my $timertext = &mt('Start Timer?');
1.414     albertel 1225:     $result .= (<<ENDCHECKOUT);
1.444     bisitz   1226: <form name="markaccess" method="post" action="$uri">
1.252     albertel 1227: <input type="hidden" name="markaccess" value="yes" />
1.514     bisitz   1228: <input type="button" name="accessbutton" value="$buttontext" onclick="javascript:if (confirm('$timertext')) { document.markaccess.submit(); }" />
1.252     albertel 1229: </form>
                   1230: ENDCHECKOUT
1.414     albertel 1231:     return $result;
1.252     albertel 1232: }
                   1233: 
1.204     albertel 1234: sub init_problem_globals {
                   1235:     my ($type)=@_;
                   1236:     #initialize globals
1.308     foxr     1237:     #   For problems, we start out in part 0 (outside a <part> tag).
                   1238:     #   and part 0 is used to describe the main body of the <problem>
                   1239:     #
1.204     albertel 1240:     if ($type eq 'problem') {
                   1241: 	$Apache::inputtags::part='0';
                   1242: 	@Apache::inputtags::partlist=('0');
1.405     albertel 1243: 	&Apache::lonhomework::set_show_problem_status(&get_problem_status('0'));
1.266     albertel 1244: 	$Apache::lonhomework::ignore_response_errors=0;
1.308     foxr     1245: 
1.266     albertel 1246:     } elsif ($type eq 'library') {
1.204     albertel 1247: 	$Apache::inputtags::part='';
                   1248: 	@Apache::inputtags::partlist=();
1.405     albertel 1249: 	&Apache::lonhomework::reset_show_problem_status();
1.266     albertel 1250: 	$Apache::lonhomework::ignore_response_errors=1;
1.308     foxr     1251: 
1.304     albertel 1252:     } elsif ($type eq 'Task') {
                   1253: 	$Apache::inputtags::part='0';
                   1254: 	@Apache::inputtags::partlist=('0');
1.405     albertel 1255: 	&Apache::lonhomework::reset_show_problem_status();
1.304     albertel 1256: 	$Apache::lonhomework::ignore_response_errors=1;
1.204     albertel 1257:     }
1.477     www      1258:     @Apache::functionplotresponse::callscripts=();
1.204     albertel 1259:     @Apache::inputtags::responselist = ();
                   1260:     @Apache::inputtags::importlist = ();
                   1261:     @Apache::inputtags::previous=();
                   1262:     @Apache::inputtags::previous_version=();
                   1263:     $Apache::structuretags::printanswer='No';
                   1264:     @Apache::structuretags::whileconds=();
                   1265:     @Apache::structuretags::whilebody=();
                   1266:     @Apache::structuretags::whileline=();
                   1267:     $Apache::lonhomework::scantronmode=0;
                   1268:     undef($Apache::lonhomework::name);
1.358     albertel 1269:     undef($Apache::lonhomework::default_type);
                   1270:     undef($Apache::lonhomework::type);
1.204     albertel 1271: }
                   1272: 
                   1273: sub reset_problem_globals {
                   1274:     my ($type)=@_;
                   1275:     undef(%Apache::lonhomework::history);
                   1276:     undef(%Apache::lonhomework::results);
                   1277:     undef($Apache::inputtags::part);
1.498     raeburn  1278:     if ($type eq 'Task') {
                   1279:         undef($Apache::inputtags::slot_name);
                   1280:     }
1.208     albertel 1281: #don't undef this, lonhomework.pm takes care of this, we use this to 
                   1282: #detect if we try to do 2 problems in one file
                   1283: #   undef($Apache::lonhomework::parsing_a_problem);
1.204     albertel 1284:     undef($Apache::lonhomework::name);
1.358     albertel 1285:     undef($Apache::lonhomework::default_type);
                   1286:     undef($Apache::lonhomework::type);
                   1287:     undef($Apache::lonhomework::scantronmode);
                   1288:     undef($Apache::lonhomework::ignore_response_errors);
1.477     www      1289:     undef(@Apache::functionplotresponse::callscripts);
1.405     albertel 1290:     &Apache::lonhomework::reset_show_problem_status();
1.204     albertel 1291: }
                   1292: 
1.241     albertel 1293: sub set_problem_state {
1.240     albertel 1294:     my ($part)=@_;
1.284     albertel 1295:     if ($env{'form.problemstate'} eq 'CANNOT_ANSWER_correct') {
1.240     albertel 1296: 	$Apache::lonhomework::history{"resource.$part.solved"}=
                   1297: 	    'correct_by_student';
                   1298:     }
                   1299: }
                   1300: 
1.241     albertel 1301: sub get_problem_status {
                   1302:     my ($part)=@_;
1.267     albertel 1303:     my $problem_status;
1.284     albertel 1304:     if ($env{'request.state'} eq 'construct' &&
                   1305: 	defined($env{'form.problemstatus'})) {
                   1306: 	$problem_status=$env{'form.problemstatus'};
1.267     albertel 1307:     } else {
                   1308: 	$problem_status=&Apache::lonnet::EXT("resource.$part.problemstatus");
                   1309: 	&Apache::lonxml::debug("problem status for $part is $problem_status");
1.284     albertel 1310: 	&Apache::lonxml::debug("env probstat is ".$env{'form.problemstatus'});
1.241     albertel 1311:     }
                   1312:     return $problem_status;
                   1313: }
                   1314: 
1.9       albertel 1315: sub start_problem {
1.326     albertel 1316:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.19      albertel 1317: 
1.311     foxr     1318:     # We'll use the redirection to fix up printing of duedates.
1.321     albertel 1319:     if (!$Apache::lonxml::metamode) {
                   1320: 	&Apache::lonxml::startredirection();
                   1321:     }
1.311     foxr     1322: 
1.308     foxr     1323:     # Problems don't nest and we don't allow more than one <problem> in
                   1324:     # a .problem file.
                   1325:     #
1.184     albertel 1326:     if ( $Apache::inputtags::part ne '' ||
                   1327: 	 $Apache::lonhomework::parsing_a_problem) {
                   1328: 	&Apache::lonxml::error('Only one &lt;problem&gt; allowed in a .problem file');
1.326     albertel 1329: 	#my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,$style);
1.159     albertel 1330: 	return '';
                   1331:     }
1.184     albertel 1332: 
                   1333:     $Apache::lonhomework::parsing_a_problem=1;
1.204     albertel 1334:     &init_problem_globals('problem');
1.166     albertel 1335: 
1.284     albertel 1336:     if (defined($env{'scantron.maxquest'})) {
1.166     albertel 1337: 	$Apache::lonhomework::scantronmode=1;
                   1338:     }
1.161     albertel 1339: 
1.159     albertel 1340:     if ($target ne 'analyze') {
1.415     raeburn  1341:         my $type = &Apache::lonnet::EXT('resource.0.type');
                   1342: 	$Apache::lonhomework::type=$type;
1.284     albertel 1343: 	if (($env{'request.state'} eq 'construct') &&
1.410     albertel 1344: 	    $env{'form.problemtype'} =~ /\S/) {
1.284     albertel 1345: 	    $Apache::lonhomework::type=$env{'form.problemtype'};
1.237     albertel 1346: 	}
1.332     albertel 1347: 	&Apache::lonxml::debug("Found this to be of type :$Apache::lonhomework::type:");
1.159     albertel 1348:     }
1.164     albertel 1349:     if ($Apache::lonhomework::type eq '' ) {
1.284     albertel 1350: 	my $uri=$env{'request.uri'};
1.159     albertel 1351: 	if ($uri=~/\.(\w+)$/) {
                   1352: 	    $Apache::lonhomework::type=$1;
                   1353: 	    &Apache::lonxml::debug("Using type of $1");
                   1354: 	} else {
                   1355: 	    $Apache::lonhomework::type='problem';
                   1356: 	    &Apache::lonxml::debug("Using default type, problem, :$uri:");
                   1357: 	}
1.87      albertel 1358:     }
1.301     albertel 1359:     $Apache::lonhomework::default_type = $Apache::lonhomework::type;
1.58      www      1360: 
1.363     albertel 1361:     &initialize_storage();
1.389     albertel 1362:     if ($target ne 'analyze'
                   1363:        	&& $env{'request.state'} eq 'construct') {
                   1364: 	&set_problem_state('0');
                   1365:     }
                   1366: 
1.366     albertel 1367:     if ($target eq 'web') {
                   1368: 	&Apache::lonxml::debug(" grading history ");
                   1369: 	&Apache::lonhomework::showhash(%Apache::lonhomework::history);
                   1370:     }
1.363     albertel 1371: 
1.159     albertel 1372:     #added vars to the scripting enviroment
1.213     albertel 1373:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.248     albertel 1374:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.24      albertel 1375:     &Apache::run::run($expression,$safeeval);
1.159     albertel 1376:     my $status;
                   1377:     my $accessmsg;
1.508     raeburn  1378:     my $resource_due;
1.159     albertel 1379: 
1.343     albertel 1380:     my $name= &get_resource_name($parstack,$safeeval);
1.506     raeburn  1381:     my ($result,$form_tag_start,$slot_name,$slot);
                   1382: 
                   1383:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1384:         $target eq 'tex') {
                   1385:         if ($env{'form.markaccess'}) {
                   1386:             my @interval=&Apache::lonnet::EXT("resource.0.interval");
1.519     raeburn  1387:             &Apache::lonnet::set_first_access($interval[1],$interval[0]);
1.506     raeburn  1388:         }
                   1389: 
                   1390:         ($status,$accessmsg,$slot_name,$slot) =
                   1391:             &Apache::lonhomework::check_slot_access('0','problem');
                   1392:         push (@Apache::inputtags::status,$status);
                   1393:     }
                   1394: 
1.354     albertel 1395:     if ($target eq 'web' || $target eq 'webgrade' || $target eq 'tex'
                   1396: 	|| $target eq 'edit') {
1.350     albertel 1397: 	($result,$form_tag_start) =
                   1398: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1399: 			$name);
                   1400:     }
                   1401: 
1.284     albertel 1402:     if ($target eq 'tex' and $env{'request.symb'} =~ m/\.page_/) {$result='';}
1.159     albertel 1403: 
1.479     raeburn  1404:     if ($target eq 'analyze') { my $rndseed=&setup_rndseed($safeeval,$target); }
1.159     albertel 1405:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   1406: 	$target eq 'tex') {
1.479     raeburn  1407: 
1.159     albertel 1408: 	#handle rand seed in construction space
1.479     raeburn  1409: 	my $rndseed=&setup_rndseed($safeeval,$target);
1.367     albertel 1410: 	my ($symb)=&Apache::lonnet::whichuser();
1.479     raeburn  1411: 
1.333     albertel 1412: 	if ($env{'request.state'} ne "construct" && 
                   1413: 	    ($symb eq '' || $Apache::lonhomework::type eq 'practice')) {
1.162     albertel 1414: 	    $form_tag_start.='<input type="hidden" name="rndseed" value="'.
1.462     raeburn  1415: 		$rndseed.'" />'.
                   1416: 		    '<input type="submit" name="resetdata"
                   1417:                              value="'.&mt('New Problem Variation').'" />';
1.334     albertel 1418: 	    if (exists($env{'form.username'})) {
                   1419: 		$form_tag_start.=
1.164     albertel 1420: 		    '<input type="hidden" name="username"
1.284     albertel 1421:                              value="'.$env{'form.username'}.'" />';
1.334     albertel 1422: 	    }
1.462     raeburn  1423: 	    if ($env{'request.role.adv'}) {
                   1424: 		$form_tag_start.= ' <label class="LC_nobreak">'
                   1425:                          .'<input type="checkbox" name="showallfoils"';
                   1426: 		if (defined($env{'form.showallfoils'})) {
                   1427: 		    $form_tag_start.=' checked="checked"';
                   1428: 		}
                   1429:                 $form_tag_start.= ' /> '
                   1430:                                  .&mt('Show All Foils')
                   1431:                                  .'</label>';
                   1432: 	    }
1.417     www      1433:             if ($Apache::lonhomework::type eq 'practice') {
1.428     raeburn  1434:                 $form_tag_start.=&practice_problem_header();
1.417     www      1435:             }
1.462     raeburn  1436: 	    $form_tag_start.='<hr />';
1.479     raeburn  1437:         } elsif (($env{'request.state'} ne "construct") &&
                   1438:                  ($Apache::lonhomework::type eq 'randomizetry') &&
                   1439:                  ($status eq 'CAN_ANSWER')) {
                   1440:             my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                   1441:             my $problemstatus = &get_problem_status($Apache::inputtags::part);
                   1442:             $form_tag_start.=&randomizetry_problem_header($problemstatus,$reqtries);
                   1443:         }
1.324     albertel 1444: 
1.159     albertel 1445: 	my $expression='$external::datestatus="'.$status.'";';
                   1446: 	$expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.0.solved"}.'";';
                   1447: 	&Apache::run::run($expression,$safeeval);
                   1448: 	&Apache::lonxml::debug("Got $status");
1.324     albertel 1449: 
1.159     albertel 1450: 	if (( $status eq 'CLOSED' ) ||
                   1451: 	    ( $status eq 'UNCHECKEDOUT') ||
1.252     albertel 1452: 	    ( $status eq 'NOT_YET_VIEWED') ||
1.159     albertel 1453: 	    ( $status eq 'BANNED') ||
1.216     albertel 1454: 	    ( $status eq 'UNAVAILABLE') ||
1.324     albertel 1455: 	    ( $status eq 'NOT_IN_A_SLOT') ||
1.499     raeburn  1456:             ( $status eq 'NOTRESERVABLE') ||
                   1457:             ( $status eq 'RESERVABLE') ||
                   1458:             ( $status eq 'RESERVABLE_LATER') ||
1.216     albertel 1459: 	    ( $status eq 'INVALID_ACCESS')) {
1.326     albertel 1460: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                   1461: 						       $style);
1.159     albertel 1462: 	    if ( $target eq "web" ) {
1.343     albertel 1463: 		my $msg;
1.159     albertel 1464: 		if ($status eq 'UNAVAILABLE') {
1.504     golterma 1465: 		    $msg.='<p class="LC_error">'.&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'</p>';
1.441     raeburn  1466:                 } elsif ($status eq 'NOT_IN_A_SLOT') {
1.504     golterma 1467:                     $msg.='<p class="LC_warning">'.&mt('You are not currently signed up to work at this time and/or place.').'</p>';
1.499     raeburn  1468:                 } elsif (($status eq 'RESERVABLE') || ($status eq 'RESERVABLE_LATER') ||
                   1469:                          ($status eq 'NOTRESERVABLE')) {
1.504     golterma 1470:                     $msg.='<p class="LC_warning">'.&mt('Access requires reservation to work at specific time/place.').'</p>';
1.253     albertel 1471: 		} elsif ($status ne 'NOT_YET_VIEWED') {
1.504     golterma 1472: 		    $msg.='<p class="LC_warning">'.&mt('Not open to be viewed').'</p>';
1.499     raeburn  1473:                 }
1.216     albertel 1474: 		if ($status eq 'CLOSED' || $status eq 'INVALID_ACCESS') {
1.393     www      1475: 		    $msg.=&mt('The problem ').$accessmsg;
1.159     albertel 1476: 		} elsif ($status eq 'UNCHECKEDOUT') {
1.343     albertel 1477: 		    $msg.=&checkout_msg();
1.252     albertel 1478: 		} elsif ($status eq 'NOT_YET_VIEWED') {
1.253     albertel 1479: 		    $msg.=&firstaccess_msg($accessmsg,$symb);
1.325     albertel 1480: 		} elsif ($status eq 'NOT_IN_A_SLOT') {
1.441     raeburn  1481: 		    $msg.=&Apache::bridgetask::add_request_another_attempt_button("Sign up for time to work");
1.499     raeburn  1482:                 } elsif ($status eq 'RESERVABLE') {
                   1483:                     $msg.=&mt('Available to make a reservation.').' '.&mt('Reservation window closes [_1].',
                   1484:                               &Apache::lonnavmaps::timeToHumanString($accessmsg,'end')).
                   1485:                           '<br />'.
                   1486:                           &Apache::bridgetask::add_request_another_attempt_button("Sign up for time to work");
                   1487:                 } elsif ($status eq 'RESERVABLE_LATER') {
                   1488:                     $msg.=&mt('Window to make a reservation will open [_1].',
                   1489:                               &Apache::lonnavmaps::timeToHumanString($accessmsg,'start'));
                   1490:                 } elsif ($status eq 'NOTRESERVABLE') {
                   1491:                     $msg.=&mt('Not available to make a reservation.');  
1.159     albertel 1492: 		}
                   1493: 		$result.=$msg.'<br />';
                   1494: 	    } elsif ($target eq 'tex') {
1.332     albertel 1495: 		my $startminipage = ($env{'form.problem_split'}=~/yes/i)? ''
                   1496: 		                    : '\begin{minipage}{\textwidth}';
1.443     foxr     1497: 		$result.='\noindent \vskip 1 mm '.
1.332     albertel 1498: 		    $startminipage.'\vskip 0 mm';
1.159     albertel 1499: 		if ($status eq 'UNAVAILABLE') {
1.211     albertel 1500: 		    $result.=&mt('Unable to determine if this resource is open due to network problems. Please try again later.').'\vskip 0 mm ';
1.159     albertel 1501: 		} else {
1.211     albertel 1502: 		    $result.=&mt('Problem is not open to be viewed. It')." $accessmsg \\vskip 0 mm ";
1.159     albertel 1503: 		}
                   1504: 	    }
1.324     albertel 1505: 	} elsif ($status eq 'NEEDS_CHECKIN') {
1.326     albertel 1506: 	    my $bodytext=&Apache::lonxml::get_all_text("/problem",$parser,
                   1507: 						       $style);
1.324     albertel 1508: 	    if ($target eq 'web') {
1.375     albertel 1509: 		$result .= 
                   1510: 		    &Apache::bridgetask::proctor_validation_screen($slot);
1.324     albertel 1511: 	    } elsif ($target eq 'grade') {
                   1512: 		&Apache::bridgetask::proctor_check_auth($slot_name,$slot,
                   1513: 							'problem');
                   1514: 	    }
1.159     albertel 1515: 	} elsif ($target eq 'web') {
1.508     raeburn  1516: 	    if ($status eq 'CAN_ANSWER') {
                   1517:                 $resource_due = &Apache::lonhomework::due_date(0, $env{'request.symb'});
                   1518:                 if ($slot_name ne '') {
                   1519:                     my $checked_in =
                   1520:                         $Apache::lonhomework::history{'resource.0.checkedin'};
                   1521:                     if ($checked_in eq '') {
                   1522:                         # unproctored slot access, self checkin
                   1523:                         &Apache::bridgetask::check_in('problem',undef,undef,
                   1524:                                                       $slot_name);
                   1525:                         $checked_in =
                   1526:                             $Apache::lonhomework::results{"resource.0.checkedin"};
                   1527:                     }
                   1528:                     if ((ref($slot) eq 'HASH') && ($checked_in ne '')) {
                   1529:                         if ($slot->{'starttime'} < time()) {
                   1530:                             if (!$resource_due) {
                   1531:                                 $resource_due = $slot->{'endtime'};
                   1532:                             } elsif ($slot->{'endtime'} < $resource_due) {
                   1533:                                 $resource_due = $slot->{'endtime'};
                   1534:                             }
                   1535:                         }
                   1536:                     }
                   1537:                 }
                   1538:                 if ($resource_due) {
                   1539:                     my $time_left = $resource_due - time();
                   1540:                     if ($resource_due && ($time_left > 0) && ($target eq 'web')) {
                   1541:                         $result .= &Apache::lonhtmlcommon::set_due_date($resource_due);
                   1542:                     }
                   1543:                 }
                   1544:             }
1.368     albertel 1545: 	    $result.="\n $form_tag_start \t".	
1.227     albertel 1546: 	      '<input type="hidden" name="submitted" value="yes" />';
                   1547: 	    # create a page header and exit
1.284     albertel 1548: 	    if ($env{'request.state'} eq "construct") {
                   1549: 		$result.= &problem_web_to_edit_header($env{'form.rndseed'});
1.428     raeburn  1550:                 if ($Apache::lonhomework::type eq 'practice') {
                   1551:                     $result.= '<input type="submit" name="resetdata" '.
                   1552:                               'value="'.&mt('New Problem Variation').'" />'.
                   1553:                               &practice_problem_header().'<hr />';
                   1554:                 }
1.227     albertel 1555: 	    }
                   1556: 	    # if we are viewing someone else preserve that info
1.284     albertel 1557: 	    if (defined $env{'form.grade_symb'}) {
1.227     albertel 1558: 		foreach my $field ('symb','courseid','domain','username') {
                   1559: 		    $result .= '<input type="hidden" name="grade_'.$field.
1.284     albertel 1560: 			'" value="'.$env{"form.grade_$field"}.'" />'."\n";
1.159     albertel 1561: 		}
1.479     raeburn  1562:                 foreach my $field ('trial','questiontype') {
                   1563:                     if ($env{"form.grade_$field"} ne '') {
                   1564:                         $result .= '<input type="hidden" name="grade_'.$field.
                   1565:                             '" value="'.$env{"form.grade_$field"}.'" />'."\n";
                   1566:                     }
                   1567:                 }
1.159     albertel 1568: 	    }
1.490     raeburn  1569:             if ($env{'form.grade_imsexport'}) {
                   1570:                 $result = '';
                   1571:             }
1.159     albertel 1572: 	} elsif ($target eq 'tex') {
1.319     foxr     1573: 	    $result .= 'INSERTTEXFRONTMATTERHERE';
1.500     foxr     1574: 	    $result .= &select_metadata_hyphenation();
                   1575: 	    
1.319     foxr     1576: 
1.99      sakharuk 1577: 	}
1.159     albertel 1578:     } elsif ($target eq 'edit') {
1.343     albertel 1579: 	$result .= $form_tag_start.&problem_edit_header();
1.226     albertel 1580: 	$Apache::lonxml::warnings_error_header=
                   1581: 	    &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 1582: 	my $temp=&Apache::edit::insertlist($target,$token);
                   1583: 	$result.=$temp;
                   1584:     } elsif ($target eq 'modified') {
                   1585: 	$result=$token->[4];
                   1586:     } else {
                   1587: 	# page_start returned a starting result, delete it if we don't need it
                   1588: 	$result = '';
1.99      sakharuk 1589:     }
1.159     albertel 1590:     return $result;
1.9       albertel 1591: }
                   1592: 
                   1593: sub end_problem {
1.159     albertel 1594:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.321     albertel 1595:     my $result;
1.310     foxr     1596: 
1.321     albertel 1597:     if (!$Apache::lonxml::metamode) {
                   1598: 	$result = &Apache::lonxml::endredirection(); #started in &start_problem
1.329     albertel 1599: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel 1600:     }
1.319     foxr     1601: 
                   1602:     if ($target eq 'tex') {
1.321     albertel 1603: 	# Figure out the front matter and replace the
                   1604: 	# INSERTTEXFRONTMATTERHERE in result with it.  note that we do
                   1605: 	# this in end_problem because whether or not we display due
                   1606: 	# dates depends on whether due dates have already been
                   1607: 	# displayed in the problem parts.
                   1608: 
1.319     foxr     1609: 	my $frontmatter   = '';
                   1610: 	my $startminipage = '';
                   1611: 	if (not $env{'form.problem_split'}=~/yes/) {
                   1612: 	    $startminipage = '\begin{minipage}{\textwidth}';
                   1613: 	}
                   1614: 	my $id = $Apache::inputtags::part;
                   1615: 	my $weight = &Apache::lonnet::EXT("resource.$id.weight");
                   1616: 	my $packages=&Apache::lonnet::metadata($env{'request.uri'},'packages');
                   1617: 	my @packages = split /,/,$packages;
                   1618: 	my $allow_print_points = 0;
                   1619: 	foreach my $partial_key (@packages) {
                   1620: 	    if ($partial_key=~m/^part_0$/) {
                   1621: 		$allow_print_points=1;
                   1622: 	    }
                   1623: 	}
                   1624: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1625: 	if (defined($maxtries) && $maxtries < 0) { $allow_print_points=0; }
                   1626: 	if (lc($env{'course.'.$env{'request.course.id'}.
                   1627: 			'.disableexampointprint'}) eq 'yes') {
                   1628: 	    $allow_print_points=0;
                   1629: 	}
                   1630: 	my $name_of_resourse= &Apache::lonxml::latex_special_symbols(&get_resource_name($parstack,$safeeval),'header');
1.443     foxr     1631: 	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.500     foxr     1632: 	&clear_required_languages();
1.319     foxr     1633: 	my $toc_line='\vskip 1 mm\noindent '.$startminipage.
                   1634: 	    '\addcontentsline{toc}{subsection}{'.$name_of_resourse.'}';
                   1635: 	
                   1636: 	#  Figure out what the due date is and if we need to print
                   1637: 	#  it in the problem header.  We have been logging the
                   1638: 	#  last due date written to file. 
                   1639: 	
                   1640: 	my $duetime = &Apache::lonnet::EXT("resource.$id.duedate"); 
                   1641: 	my $duedate = POSIX::strftime("%c",localtime($duetime));
1.448     bisitz   1642:         my $duedate_text = &mt('Due date: [_1]'
                   1643:                               ,&Apache::lonlocal::locallocaltime($duetime));
1.319     foxr     1644: 	my $temp_file;
                   1645: 	my $filename = "/home/httpd/prtspool/$env{'user.name'}_$env{'user.domain'}_printout.due";
                   1646: 	
                   1647: 	# Figure out what the last printed due date is or set it
                   1648: 	# to the epoch if no duedates have been printed.
                   1649: 	
                   1650: 	my $due_file_content = 0;      #   If the file does not yet exist, time is the epoch.
                   1651: 	if (-e $filename) {
                   1652: 	    $temp_file = Apache::File->new($filename);
                   1653: 	    my @due_file      = <$temp_file>;
                   1654: 	    $due_file_content = $due_file[$#due_file];
                   1655: 	    chomp $due_file_content;
                   1656: 	} 
                   1657: 	
                   1658: 	# We display the due date iff it is not the same as the last
                   1659: 	# duedate in problem header ($due_file_content), and
                   1660: 	# none of our parts displayed a duedate.
                   1661: 	#
                   1662: 	my $parts_with_displayduedate;
                   1663: 	if (defined $Apache::outputtags::showonce{'displayduedate'}) {
                   1664: 	    $parts_with_displayduedate = 
                   1665: 		scalar(@{$Apache::outputtags::showonce{'displayduedate'}});
                   1666: 	} else {
                   1667: 	    $parts_with_displayduedate = 0;
                   1668: 	}
                   1669: 	if (($due_file_content != $duetime) && ($parts_with_displayduedate == 0) ) {
                   1670: 	    $temp_file = Apache::File->new('>'.$filename);
                   1671: 	    print $temp_file "$duetime\n";
                   1672: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                   1673: 		if(not $duedate=~m/1969/ and $Apache::lonhomework::type ne 'exam') {
                   1674: 		    $frontmatter .= $begin_doc.
1.448     bisitz   1675: 			'\textit{'.$duedate_text.'} '.$toc_line;
1.319     foxr     1676: 		} else {
                   1677: 		    $frontmatter.= $begin_doc.$toc_line;
1.463     foxr     1678: 		    if ($Apache::lonhomework::type eq 'exam' and $allow_print_points==1) { 
1.492     christia 1679: 			$frontmatter .= '\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}';
1.463     foxr     1680: 		    }
1.319     foxr     1681: 		}
                   1682: 	    } else {
1.448     bisitz   1683: 		$frontmatter .= '\vskip 1mm\textit{'.$duedate_text.'} \\\\\\\\'.$startminipage;
1.319     foxr     1684: 	    }
                   1685: 	} else {
                   1686: 	    if (not $env{'request.symb'} =~ m/\.page_/) {
                   1687: 		$frontmatter .= $begin_doc.$toc_line;
1.463     foxr     1688: 		if (($Apache::lonhomework::type eq 'exam') and ($allow_print_points==1)) { 
1.492     christia 1689: 		    $frontmatter .= '\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}';
1.463     foxr     1690: 		}
1.319     foxr     1691: 	    } else {
1.381     albertel 1692: 		$frontmatter .= '\vskip 1mm \\\\\\\\'.$startminipage;
1.319     foxr     1693: 	    }
                   1694: 	}
                   1695: 	$result =~ s/INSERTTEXFRONTMATTERHERE/$frontmatter/;
                   1696:     }
                   1697: 
1.159     albertel 1698:     my $status=$Apache::inputtags::status['-1'];
                   1699:     if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
                   1700: 	$target eq 'tex') {
1.249     albertel 1701: 	if ( $target eq 'grade' && $Apache::inputtags::part eq '0') {
1.159     albertel 1702: 	    # if part is zero, no <part>s existed, so we need to the grading
1.249     albertel 1703: 	    if ($status eq 'CAN_ANSWER' ||$Apache::lonhomework::scantronmode) {
                   1704: 		&Apache::inputtags::grade;
1.324     albertel 1705: 	    } elsif ($status eq 'NEEDS_CHECKIN') {
                   1706: 		# no need to grade, and don't want to hide data
1.249     albertel 1707: 	    } else {
                   1708: 		# move any submission data to .hidden
                   1709: 		&Apache::inputtags::hidealldata($Apache::inputtags::part);
                   1710: 	    }
1.159     albertel 1711: 	} elsif ( ($target eq 'web' || $target eq 'tex') &&
                   1712: 		  $Apache::inputtags::part eq '0' &&
1.490     raeburn  1713: 		  $status ne 'UNCHECKEDOUT' && $status ne 'NOT_YET_VIEWED'
                   1714:                   && !$env{'form.grade_imsexport'}) {
1.159     albertel 1715: 	    # if part is zero, no <part>s existed, so we need show the current
                   1716: 	    # grading status
                   1717: 	    my $gradestatus = &Apache::inputtags::gradestatus($Apache::inputtags::part,$target);
                   1718: 	    $result.= $gradestatus;
                   1719: 	}
                   1720: 	if (
1.284     albertel 1721: 	    (($target eq 'web') && ($env{'request.state'} ne 'construct')) ||
1.159     albertel 1722: 	    ($target eq 'answer') || ($target eq 'tex')
                   1723: 	   ) {
1.490     raeburn  1724: 	    if (($target ne 'tex') &&
                   1725: 		($env{'form.answer_output_mode'} ne 'tex') && 
                   1726:                 (!$env{'form.grade_imsexport'})) {
1.254     www      1727: 		$result.="</form>";
1.159     albertel 1728: 	    }
                   1729: 	    if ($target eq 'web') {
1.507     raeburn  1730:                 #
                   1731:                 # Closing </body></html> not added by end_page().
                   1732:                 # Added separately at end of this routine, after added
                   1733:                 # <script></script> so document will be valid xhtml.
                   1734:                 #
                   1735: 		$result.= &Apache::loncommon::end_page({'discussion' => 1,
                   1736: 							'notbody'    => 1});
1.159     albertel 1737: 	    } elsif ($target eq 'tex') {
1.178     sakharuk 1738: 		my $endminipage = '';
1.284     albertel 1739: 		if (not $env{'form.problem_split'}=~/yes/) {
1.178     sakharuk 1740: 		    $endminipage = '\end{minipage}';
                   1741: 		}
1.284     albertel 1742:                 if ($env{'form.print_discussions'} eq 'yes') {
1.263     sakharuk 1743: 		    $result.=&Apache::lonxml::xmlend($target,$parser);
1.159     albertel 1744: 		} else {
1.262     sakharuk 1745: 		    $result .= '\keephidden{ENDOFPROBLEM}\vskip 0.5mm\noindent\makebox[\textwidth/$number_of_columns][b]{\hrulefill}';
1.284     albertel 1746: 		    if (not $env{'request.symb'} =~ m/\.page_/) {
1.262     sakharuk 1747: 			$result .= $endminipage.'\end{document} ';
                   1748: 		    } else {
1.382     albertel 1749: 			$result .= $endminipage;
1.262     sakharuk 1750: 		    }
1.159     albertel 1751: 		}
                   1752: 	    }
                   1753: 	}
1.476     www      1754:         if ($target eq 'web') {
                   1755:            $result.=&Apache::functionplotresponse::init_script();
                   1756:         }
1.159     albertel 1757: 	if ($target eq 'grade') {
                   1758: 	    &Apache::lonhomework::showhash(%Apache::lonhomework::results);
                   1759: 	    &finalize_storage();
                   1760: 	}
1.284     albertel 1761: 	if ($target eq 'answer' && ($env{'request.state'} eq 'construct')
                   1762: 	    && $env{'form.answer_output_mode'} ne 'tex') {
1.346     albertel 1763: 	    $result.=&Apache::loncommon::end_page({'discussion' => 1});
1.294     albertel 1764: 	                        # normally we get it from above, but in CSTR
1.172     albertel 1765: 	                        # we always show answer mode too.
1.159     albertel 1766: 	}
                   1767:     } elsif ($target eq 'meta') {
                   1768: 	if ($Apache::inputtags::part eq '0') {
1.179     albertel 1769: 	    @Apache::inputtags::response=();
1.159     albertel 1770: 	    $result=&Apache::response::mandatory_part_meta;
                   1771: 	}
1.215     albertel 1772: 	$result.=&Apache::response::meta_part_order();
1.258     albertel 1773: 	$result.=&Apache::response::meta_response_order();
1.159     albertel 1774:     } elsif ($target eq 'edit') {
                   1775: 	&Apache::lonxml::debug("in end_problem with $target, edit");
1.314     albertel 1776: 	$result .= &problem_edit_footer();
1.320     albertel 1777:     } elsif ($target eq 'modified') {
                   1778: 	 $result .= $token->[2];
1.159     albertel 1779:     }
1.155     albertel 1780: 
1.284     albertel 1781:     if ($env{'request.state'} eq 'construct' && $target eq 'web') {
1.177     albertel 1782: 	&Apache::inputtags::check_for_duplicate_ids();
                   1783:     }
1.204     albertel 1784: 
                   1785:     &reset_problem_globals('problem');
1.159     albertel 1786: 
1.502     foxr     1787:     #
                   1788:     # This shouild be just above the return so that the
                   1789:     # time put in the javascript is as late as possible in the
                   1790:     # computation:
                   1791:     #
                   1792:     if ($target eq 'web') {
                   1793:         $result .= &Apache::lonhtmlcommon::set_compute_end_time();
1.507     raeburn  1794:         #
                   1795:         # Closing tags delayed so any <script></script> tags 
                   1796:         # not in head can appear inside body, for valid xhtml.
                   1797:         # 
                   1798:         $result .= "</body>\n</html>";
1.502     foxr     1799:     }
1.159     albertel 1800:     return $result;
1.48      albertel 1801: }
                   1802: 
1.108     albertel 1803: 
1.48      albertel 1804: sub start_library {
1.159     albertel 1805:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.343     albertel 1806:     my ($result,$form_tag_start);
1.371     albertel 1807:     if ($#$tagstack eq 0 && $$tagstack[0] eq 'library') {
1.244     albertel 1808: 	&init_problem_globals('library');
                   1809: 	$Apache::lonhomework::type='problem';
                   1810:     }
1.159     albertel 1811:     if ($target eq 'edit') {
1.343     albertel 1812: 	($result,$form_tag_start)=
                   1813: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1814: 			'Edit');
                   1815: 	$result.=$form_tag_start.&problem_edit_header();
1.159     albertel 1816: 	my $temp=&Apache::edit::insertlist($target,$token);
                   1817: 	$result.=$temp;
                   1818:     } elsif ($target eq 'modified') {
                   1819: 	$result=$token->[4];
1.340     albertel 1820:     } elsif (($target eq 'web' || $target eq 'webgrade')
1.371     albertel 1821: 	     && ($#$tagstack eq 0 && $$tagstack[0] eq 'library')
1.340     albertel 1822: 	     && $env{'request.state'} eq "construct" ) {
1.159     albertel 1823: 	my $name=&get_resource_name($parstack,$safeeval);
1.343     albertel 1824: 	($result,$form_tag_start)=
                   1825: 	    &page_start($target,$token,$tagstack,$parstack,$parser,$safeeval,
                   1826: 			$name);
1.479     raeburn  1827: 	my $rndseed=&setup_rndseed($safeeval,$target);
1.343     albertel 1828: 	$result.=" \n $form_tag_start".	
1.159     albertel 1829: 		  '<input type="hidden" name="submitted" value="yes" />';
                   1830: 	$result.=&problem_web_to_edit_header($rndseed);
1.428     raeburn  1831:         if ($Apache::lonhomework::type eq 'practice') {
                   1832:             $result.= '<input type="submit" name="resetdata" '.
                   1833:                       'value="'.&mt('New Problem Variation').'" />'.
                   1834:                       &practice_problem_header().'<hr />';
                   1835:         }
1.159     albertel 1836:     }
                   1837:     return $result;
1.48      albertel 1838: }
                   1839: 
                   1840: sub end_library {
1.159     albertel 1841:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1842:     my $result='';
                   1843:     if ($target eq 'edit') {
                   1844: 	$result=&problem_edit_footer();
1.371     albertel 1845:     } elsif ($target eq 'web' 
                   1846: 	     && ($#$tagstack eq 0 && $$tagstack[0] eq 'library') 
                   1847: 	     && $env{'request.state'} eq "construct") {
1.349     albertel 1848: 	$result.='</form>'.&Apache::loncommon::end_page({'discussion' => 1});
1.159     albertel 1849:     }
1.371     albertel 1850:     if ( $#$tagstack eq 0 && $$tagstack[0] eq 'library') {
                   1851: 	&reset_problem_globals('library');
                   1852:     }
1.159     albertel 1853:     return $result;
1.197     www      1854: }
                   1855: 
                   1856: sub start_definetag {
1.326     albertel 1857:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.197     www      1858: 
                   1859:     my $result;
                   1860: 
                   1861:     my $name = $token->[2]->{'name'};
1.326     albertel 1862:     my $skip=&Apache::lonxml::get_all_text("/definetag",$parser,$style);
1.396     albertel 1863:     if ($target eq 'web') {
                   1864: 	if ($name=~/^\//) {
                   1865: 	    $result=
                   1866: 		'<br /><table class="LC_sty_end"><tr><th>'.
                   1867: 		&mt('END [_1]'.'<tt>'.$name.'</tt>').'</th></tr>';
                   1868: 	} else {
                   1869: 	    $result=
                   1870: 		'<br /><table class="LC_sty_begin"><tr><th>'.
                   1871: 		&mt('BEGIN [_1]'.'<tt>'.$name.'</tt>').'</th></tr>';
                   1872: 	}
                   1873: 	$skip = &HTML::Entities::encode($skip, '<>&"');
                   1874: 	$result.='<tr><td><pre>'.$skip.'</pre></td></tr></table>';
1.197     www      1875:     }
                   1876:     return $result;
                   1877: }
                   1878: 
                   1879: sub end_definetag {
                   1880:     return '';
1.1       albertel 1881: }
                   1882: 
                   1883: sub start_block {
1.201     albertel 1884:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.131     albertel 1885: 
                   1886:     my $result;
1.1       albertel 1887: 
1.339     albertel 1888:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer'  ||
                   1889: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.159     albertel 1890: 	my $code = $token->[2]->{'condition'};
1.385     albertel 1891: 	if (defined($code) && $code ne '') {
1.137     albertel 1892: 	    if (!$Apache::lonxml::default_homework_loaded) {
                   1893: 		&Apache::lonxml::default_homework_load($safeeval);
                   1894: 	    }
1.131     albertel 1895: 	    $result = &Apache::run::run($code,$safeeval);
                   1896: 	    &Apache::lonxml::debug("block :$code: returned :$result:");
                   1897: 	} else {
                   1898: 	    $result='1';
                   1899: 	}
                   1900: 	if ( ! $result ) {
1.201     albertel 1901: 	    my $skip=&Apache::lonxml::get_all_text("/block",$parser,$style);
1.131     albertel 1902: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   1903: 	}
                   1904: 	$result='';
                   1905:     } elsif ($target eq 'edit') {
                   1906: 	$result .=&Apache::edit::tag_start($target,$token);
                   1907: 	$result .=&Apache::edit::text_arg('Test Condition:','condition',
                   1908: 					  $token,40);
                   1909: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   1910:     } elsif ($target eq 'modified') {
                   1911: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   1912: 						     $safeeval,'condition');
                   1913: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.38      albertel 1914:     }
1.131     albertel 1915:     return $result;
1.1       albertel 1916: }
                   1917: 
                   1918: sub end_block {
1.167     www      1919:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   1920:     my $result;
                   1921:     if ($target eq "edit") {
                   1922: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   1923:     }
                   1924:     return $result;
                   1925: }
1.500     foxr     1926: #
                   1927: #  <languageblock [include='lang1,lang2...'] [exclude='lang1,lang2...']>
                   1928: #  ...
                   1929: #  </languageblock>
                   1930: #
                   1931: #   This declares the intent to provide content that can be rendered in the
                   1932: #   set of languages in the include specificatino but not in the exclude
                   1933: #   specification.  If a currently preferred language is in the include list
                   1934: #   the content in the <languageblock>...</languageblock> is rendered
                   1935: #   If the currently preferred language is in the exclude list,
                   1936: #   the content in the <languageblock>..></languageblock is not rendered.
                   1937: #
                   1938: #   Pathalogical case handling:
                   1939: #     - Include specified, without the preferred language but exclude  specified
                   1940: #       also without the preferred langauge results in rendering the block.
                   1941: #     - Exclude specified without include and excluden not containing a 
                   1942: #       preferred language renders the block.
                   1943: #     - Include and exclude both specifying the preferred language does not
                   1944: #       render the block.
                   1945: #     - If neither include/exclude is specified, the block gets rendered.
                   1946: #
                   1947: #  This tag has no effect when target is in {edit, modified}
                   1948: #
1.167     www      1949: sub start_languageblock {
1.201     albertel 1950:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.167     www      1951: 
1.500     foxr     1952:     my $result = '';
1.167     www      1953: 
1.339     albertel 1954:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   1955: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.201     albertel 1956: 	my $include = $token->[2]->{'include'};
                   1957: 	my $exclude = $token->[2]->{'exclude'};
1.436     raeburn  1958:         my @preferred_languages=&Apache::lonlocal::preferred_languages();
1.500     foxr     1959: 
                   1960:         # This should not even happen, since we should at least have the server language
                   1961: 
                   1962:         if (!$preferred_languages[0]) { 
                   1963: 	    $preferred_languages[0]='en'; 
                   1964: 	}
                   1965: 
                   1966:         # Now loop over all languages in order of preference
                   1967: 
                   1968: 	my $render;
1.398     www      1969:         foreach my $preferred_language (@preferred_languages) {
1.500     foxr     1970: 
                   1971: 	    # If neither include/nor exlude is present the block is going
                   1972: 	    # to get rendered.
                   1973: 
1.399     www      1974:            my $found=0;
1.500     foxr     1975:            $render=1;
                   1976: 
                   1977: 	   #  If include is specified,  don't render the block
                   1978: 	   #  unless the preferred language is included in the set.
                   1979: 
1.398     www      1980: 	   if ($include) {
1.500     foxr     1981:               $render=0;
1.398     www      1982:               foreach my $included_language (split(/\,/,$include)) {
                   1983:                  if ($included_language eq $preferred_language) {
1.500     foxr     1984:                     $render=1; 
1.399     www      1985:                     $found=1; 
1.500     foxr     1986: 		    last;	# Only need to find the first.
1.398     www      1987:                  }
                   1988:               }
                   1989: 	   }
1.500     foxr     1990:            # Do we have an exclude argument?
                   1991: 	   # If so, and one of the languages matches a preferred language
                   1992: 	   # inhibit rendering the block.  Note that in the pathalogical case the
                   1993: 	   # author has specified a preferred language in both the include and exclude
                   1994: 	   # attribte exclude is preferred.  
                   1995: 
1.398     www      1996:            if ($exclude) {
1.500     foxr     1997:               $render=1;
1.398     www      1998:               foreach my $excluded_language (split(/\,/,$exclude)) {
                   1999:                  if ($excluded_language eq $preferred_language) {
1.500     foxr     2000:                     $render=0;
1.399     www      2001:                     $found=1;
1.500     foxr     2002: 		    last;	# Only need to find the first.
1.398     www      2003:                  }
                   2004:               }
                   2005: 	   }
1.500     foxr     2006:            if ($found) { 
                   2007: 	       last; 		# Done on any match of include or exclude.
                   2008: 	   }
1.398     www      2009:         }
1.500     foxr     2010: 	# If $render not true skip the entire block until </languageblock>
                   2011: 	#
                   2012: 
                   2013: 	if ( ! $render ) {
1.201     albertel 2014: 	    my $skip=&Apache::lonxml::get_all_text("/languageblock",$parser,
                   2015: 						   $style);
                   2016: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   2017: 	}
1.500     foxr     2018: 	# If $render is true, we've not skipped the contents of the <languageglock>
                   2019: 	# and the normal loncapa processing flow will render it as a matter of course.
                   2020: 
1.167     www      2021:     } elsif ($target eq 'edit') {
                   2022: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 2023: 	$result .=&Apache::edit::text_arg(&mt('Include Language:'),'include',
1.167     www      2024: 					  $token,40);
1.211     albertel 2025: 	$result .=&Apache::edit::text_arg(&mt('Exclude Language:'),'exclude',
1.167     www      2026: 					  $token,40);
                   2027: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2028:     } elsif ($target eq 'modified') {
                   2029: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.168     albertel 2030: 						     $safeeval,'include',
                   2031: 						     'exclude');
1.167     www      2032: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   2033:     }
                   2034:     return $result;
                   2035: }
                   2036: 
                   2037: sub end_languageblock {
1.170     www      2038:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2039:     my $result;
1.201     albertel 2040:     if ($target eq "edit") {
1.170     www      2041: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   2042:     }
                   2043:     return $result;
                   2044: }
1.500     foxr     2045: #  languagblock specific tags:
                   2046: {
                   2047:     # For chunks of a resource that has translations, this hash contains
                   2048:     # the translations available indexed by language name.
                   2049:     #
                   2050: 
                   2051:     my %available_texts;       
1.170     www      2052: 
1.500     foxr     2053:     # <translated> starts a block of a resource that has multiple translations.
                   2054:     # See the <lang> tag as well.
                   2055:     # When </translated> is encountered if there is a translation for the 
                   2056:     # currently preferred language, that is rendered inthe web/tex/webgrade
                   2057:     # targets.  Otherwise, the default text is rendered.
                   2058:     #
                   2059:     # Note that <lang> is only registered for the duration of the 
                   2060:     #  <translated>...</translated> block 
                   2061:     #
                   2062:     # Pathalogical case handling:
                   2063:     #   - If there is no <lang> that specifies a 'default' and there is no
                   2064:     #     translation that matches a preferred language, nothing is rendered.
                   2065:     #   - Nested <translated>...</translated> might be linguistically supported by
                   2066:     #     XML due to the stack nature of tag registration(?) however the rendered
                   2067:     #     output will be incorrect because there is only one %available_texts
                   2068:     #     has and end_translated clears it.
                   2069:     #   - Material outside of a <lang>...</lang> block within the
                   2070:     #     <translated>...<translated> block won't render either e.g.:
                   2071:     #    <translated>
                   2072:     #      The following will be in your preferred langauge:
                   2073:     #      <lang which='en'>
                   2074:     #         This section in english
                   2075:     #      </lang>
                   2076:     #      <lang which='sgeiso'>
                   2077:     #         Hier es ist auf Deutsch.
                   2078:     #      </lang>
                   2079:     #      <lang which='sfriso'>
                   2080:     #         En Francais
                   2081:     #      </lang>
                   2082:     #    </translated>
                   2083:     #
                   2084:     #    The introductory text prior to the first <lang> tag is not rendered.
                   2085:     #
1.397     albertel 2086:     sub start_translated {
                   2087: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2088: 	&Apache::lonxml::register('Apache::structuretags',('lang'));
                   2089: 	undef(%available_texts);
                   2090:     }
                   2091:     
                   2092:     sub end_translated {
                   2093: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2094: 	my $result;
                   2095: 	#show the translation on viewable targets
                   2096: 	if ($target eq 'web'     || $target eq 'tex' || $target eq 'webgrade'||
                   2097: 	    # or non-viewable targets, if it's embedded in something that
                   2098: 	    # wants the output
                   2099: 	    (($target eq 'answer' || $target eq 'analyze'|| $target eq 'grade')
                   2100: 	     && &Apache::lonxml::in_redirection() ) ) {
                   2101: 	    my @possibilities = keys(%available_texts);
                   2102: 	    my $which = 
                   2103: 		&Apache::loncommon::languages(\@possibilities) || 'default';
1.500     foxr     2104: 	    if ($target eq 'tex') {
                   2105: 		$result = &select_hyphenation($which);
                   2106: 	    }
                   2107: 	    $result .= $available_texts{$which};
                   2108: 	    if ($target eq 'tex') {
                   2109: 		$result .= &select_metadata_hyphenation(); # Restore original language.
                   2110: 	    }
1.397     albertel 2111: 	}
                   2112: 	undef(%available_texts);
                   2113: 	&Apache::lonxml::deregister('Apache::structuretags',('lang'));
                   2114: 	return $result;
                   2115:     }
                   2116: 
1.500     foxr     2117:     # <lang [which='language-name'] [other='lang1,lang2...']>  
                   2118:     #  Specifies that the block contained within it is a translation 
                   2119:     #  for a specific language specified by the 'which' attribute. The
                   2120:     #   'other' attribute can be used by itself or in conjunction with
                   2121:     #   which to specify this tag _may_ be used as a translation for some
                   2122:     #   list of languages. e.g.:  <lang which='senisoUS' other='senisoCA,senisoAU,seniso'>
                   2123:     #   specifying that the block provides a translation for US (primary)
                   2124:     #   Canadian, Australian and UK Englush.
                   2125:     #   
                   2126:     # Comment: this seems a bit silly why not just support a list of languages
                   2127:     #           e.g. <lang which='l1,l2...'> and ditch the other attribute?
                   2128:     #
                   2129:     #  Effect:
                   2130:     #    The material within the <lang>..</lang> block is stored in the
                   2131:     #    specified set of $available_texts hash entries, the appropriate one
                   2132:     #    is selected at </translated> time.
                   2133:     #
                   2134:     #  Pathalogical case handling:
                   2135:     #    If a language occurs multiple times within a <translated> block,
                   2136:     #    only the last one is rendered e.g.:
                   2137:     #
                   2138:     #    <translated>
                   2139:     #       <lang which='senisoUS', other='senisoCA,senisoAU,seniso'>
                   2140:     #          Red green color blindness is quite common affecting about 7.8% of 
                   2141:     #          the male population, but onloy about .65% of the female population.
                   2142:     #       </lang>
                   2143:     #          Red green colour blindness is quite common affecting about 7.8% of 
                   2144:     #          the male population, but onloy about .65% of the female population.
                   2145:     #       <lang which='seniso', other='senisoCA,senisoAU'>
                   2146:     #     </translated>
                   2147:     #
                   2148:     #    renders the correct spelling of color (colour) for people who have specified
                   2149:     #    a preferred language that is one of the British Commonwealth languages
                   2150:     #    even though those are also listed as valid selections for the US english
                   2151:     #    <lang> block.
                   2152:     #
1.397     albertel 2153:     sub start_lang {
                   2154: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2155: 	if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   2156: 	    $target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
                   2157: 	    &Apache::lonxml::startredirection();
                   2158: 	}
                   2159: 	return '';
                   2160:     }
                   2161: 
                   2162:     sub end_lang {
                   2163: 	my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2164: 	if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   2165: 	    $target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
                   2166: 	    my $result = &Apache::lonxml::endredirection();
                   2167: 	    my $which = &Apache::lonxml::get_param('which',$parstack,
                   2168: 						   $safeeval);
1.431     raeburn  2169:             if ($which=~/\w/) {
                   2170:                 $available_texts{$which} = $result;
                   2171:             }
                   2172:             my $otherlangs = &Apache::lonxml::get_param('other',$parstack,
                   2173:                                                         $safeeval);
                   2174:             foreach my $language (split(/\s*\,\s*/,$otherlangs)) {
                   2175:                 if ($language=~/\w/) {
                   2176:                     $available_texts{$language} = $result;
                   2177:                 }
1.427     bisitz   2178:             }
                   2179: 
1.397     albertel 2180: 	}
                   2181: 	return '';
                   2182:     }
1.500     foxr     2183: }				# end langauge block specific tags.
                   2184: 
1.397     albertel 2185: 
1.170     www      2186: sub start_instructorcomment {
1.201     albertel 2187:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.170     www      2188: 
                   2189:     my $result;
                   2190: 
1.339     albertel 2191:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   2192: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.468     raeburn  2193:         $result=($env{'request.role'}=~/^(in|cc|co|au|ca|li)/);
1.284     albertel 2194: 	if ( (! $result) or ($env{'form.instructor_comments'} eq 'hide')) {
1.201     albertel 2195: 	    my $skip=&Apache::lonxml::get_all_text("/instructorcomment",
                   2196: 						   $parser,$style);
1.170     www      2197: 	    &Apache::lonxml::debug("skipping ahead :$skip: $$parser[-1]");
                   2198: 	}
                   2199: 	$result='';
                   2200:     } elsif ($target eq 'edit') {
                   2201: 	$result .=&Apache::edit::tag_start($target,$token);
                   2202: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2203:     }
                   2204:     return $result;
                   2205: }
                   2206: 
                   2207: sub end_instructorcomment {
1.159     albertel 2208:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 2209:     my $result;
                   2210:     if ($target eq "edit") {
                   2211: 	$result.= &Apache::edit::tag_end($target,$token,'');
                   2212:     }
                   2213:     return $result;
1.4       tsai     2214: }
                   2215: 
                   2216: sub start_while {
1.326     albertel 2217:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2218: 
1.160     albertel 2219:     my $result;
1.339     albertel 2220:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   2221: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.160     albertel 2222: 	my $code = $token->[2]->{'condition'};
1.4       tsai     2223: 
1.160     albertel 2224: 	push( @Apache::structuretags::whileconds, $code);
                   2225: 	if (!$Apache::lonxml::default_homework_loaded) {
                   2226: 	    &Apache::lonxml::default_homework_load($safeeval);
                   2227: 	}
                   2228: 	my $result = &Apache::run::run($code,$safeeval);
1.326     albertel 2229: 	my $bodytext=&Apache::lonxml::get_all_text("/while",$parser,$style);
1.160     albertel 2230: 	push( @Apache::structuretags::whilebody, $bodytext);
1.161     albertel 2231: 	push( @Apache::structuretags::whileline, $token->[5]);
                   2232: 	&Apache::lonxml::debug("s code $code got -$result-");
1.160     albertel 2233: 	if ( $result ) {
                   2234: 	    &Apache::lonxml::newparser($parser,\$bodytext);
                   2235: 	}
                   2236:     } elsif ($target eq 'edit') {
                   2237: 	$result .=&Apache::edit::tag_start($target,$token);
1.211     albertel 2238: 	$result .=&Apache::edit::text_arg(&mt('Test Condition:'),'condition',
1.160     albertel 2239: 					  $token,40);
                   2240: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2241:     } elsif ($target eq 'modified') {
                   2242: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   2243: 						     $safeeval,'condition');
                   2244: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.159     albertel 2245:     }
1.160     albertel 2246:     return $result;
1.4       tsai     2247: }
                   2248: 
                   2249: sub end_while {
1.159     albertel 2250:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.160     albertel 2251:     my $result;
                   2252: 
1.339     albertel 2253:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   2254: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.160     albertel 2255: 	my $code = pop(@Apache::structuretags::whileconds);
                   2256: 	my $bodytext = pop(@Apache::structuretags::whilebody);
1.161     albertel 2257: 	my $line = pop(@Apache::structuretags::whileline);
                   2258: 	my $return = &Apache::run::run($code,$safeeval);
                   2259: 	my $starttime=time;
                   2260: 	my $error=0;
                   2261: 	while ($return) {
                   2262: 	    if (time-$starttime >
                   2263: 		$Apache::lonnet::perlvar{'lonScriptTimeout'}) {
1.378     albertel 2264: 		$return = 0; $error=1; next;
1.161     albertel 2265: 	    }
                   2266: 	    $result.=&Apache::scripttag::xmlparse($bodytext);
1.380     albertel 2267: 	    if ($target eq 'grade' || $target eq 'answer' ||
                   2268: 		$target eq 'analyze') {
                   2269: 		# grade/answer/analyze should produce no output but if we
                   2270: 		# are redirecting, the redirecter should know what to do
                   2271: 		# with the output
                   2272: 		if (!$Apache::lonxml::redirection) { undef($result); }
                   2273: 	    }
1.161     albertel 2274: 	    $return = &Apache::run::run($code,$safeeval);
                   2275: 	}
1.516     bisitz   2276:         if ($error) {
                   2277:             &Apache::lonxml::error(
                   2278:                 '<pre>'
                   2279:                .&mt('Code ran too long. It ran for more than [_1] seconds.',
                   2280:                         $Apache::lonnet::perlvar{'lonScriptTimeout'})
                   2281:                .&mt('This occurred while running &lt;while&gt; on line [_1].',
                   2282:                         $line)
                   2283:                .'</pre>');
                   2284:         }
1.160     albertel 2285:     } elsif ($target eq "edit") {
                   2286: 	$result.= &Apache::edit::tag_end($target,$token,'');
1.159     albertel 2287:     }
1.160     albertel 2288:     return $result;
1.1       albertel 2289: }
1.6       tsai     2290: 
1.160     albertel 2291: # <randomlist show="1">
1.6       tsai     2292: #  <tag1>..</tag1>
                   2293: #  <tag2>..</tag2>
                   2294: #  <tag3>..</tag3>
1.160     albertel 2295: #  ...
1.6       tsai     2296: # </randomlist>
                   2297: sub start_randomlist {
1.326     albertel 2298:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2299:     my $result;
1.339     albertel 2300:     if ($target eq 'answer' || $target eq 'grade'   || $target eq 'web' ||
                   2301: 	$target eq 'tex'    || $target eq 'analyze' || $target eq 'webgrade') {
1.331     albertel 2302: 	my $body= &Apache::lonxml::get_all_text("/randomlist",$parser);
1.305     albertel 2303: 	my $b_parser= HTML::LCParser->new(\$body);
                   2304: 	$b_parser->xml_mode(1);
                   2305: 	$b_parser->marked_sections(1);
1.159     albertel 2306: 	my $b_tok;
                   2307: 	my @randomlist;
                   2308: 	my $list_item;
                   2309: 	while($b_tok = $b_parser->get_token() ) {
                   2310: 	    if($b_tok->[0] eq 'S') { # start tag
                   2311: 		# get content of the tag until matching end tag
                   2312: 		# get all text upto the matching tag
                   2313: 		# and push the content into @randomlist
                   2314: 		$list_item = &Apache::lonxml::get_all_text('/'.$b_tok->[1],
                   2315: 							   $b_parser);
                   2316: 		$list_item = "$b_tok->[4]"."$list_item"."</$b_tok->[1]>";
                   2317: 		push(@randomlist,$list_item);
                   2318: 		#  print "<br /><b>START-TAG $b_tok->[1], $b_tok->[4],
                   2319:                 #         $list_item</b>";
                   2320: 	    }
                   2321: 	    if($b_tok->[0] eq 'T') { # text
                   2322: 		# what to do with text in between tags?
                   2323: 		#  print "<b>TEXT $b_tok->[1]</b><br />";
                   2324: 	    }
                   2325: 	    # if($b_tok->[0] eq 'E') { # end tag, should not happen
                   2326: 	    #  print "<b>END-TAG $b_tok->[1]</b><br />";
                   2327: 	    # }
                   2328: 	}
1.303     albertel 2329: 	if (@randomlist) {
                   2330: 	    my @idx_arr = (0 .. $#randomlist);
                   2331: 	    &Apache::structuretags::shuffle(\@idx_arr);
                   2332: 	    my $bodytext = '';
                   2333: 	    my $show=$#randomlist;
                   2334: 	    my $showarg=&Apache::lonxml::get_param('show',$parstack,$safeeval);
                   2335: 	    $showarg--;
                   2336: 	    if ( ($showarg >= 0) && ($showarg < $show) ) { $show = $showarg; }
1.439     raeburn  2337:             if (($target eq 'analyze') && ($env{'form.check_parts_withrandomlist'})) {
                   2338:                 my @currlist;
                   2339:                 my $part = $Apache::inputtags::part;
                   2340:                 if ($part ne '') {
                   2341:                     if (ref($Apache::lonhomework::analyze{'parts_withrandomlist'}) eq 'ARRAY') {
                   2342:                         my @currlist = @{$Apache::lonhomework::analyze{'parts_withrandomlist'}};
                   2343:                         if (!(grep(/^\Q$part\E$/,@currlist))) {
                   2344:                             push(@{$Apache::lonhomework::analyze{'parts_withrandomlist'}},$part);
                   2345:                         }
                   2346:                     } else {
                   2347:                         push(@{$Apache::lonhomework::analyze{'parts_withrandomlist'}},$part);
                   2348:                     }
                   2349:                 }
                   2350:             }
1.303     albertel 2351: 	    for(0 .. $show) {
                   2352: 		$bodytext .= "$randomlist[ $idx_arr[$_] ]";
                   2353: 	    }
                   2354: 	    &Apache::lonxml::newparser($parser,\$bodytext);
1.159     albertel 2355: 	}
                   2356:     } elsif ($target eq 'edit' ) {
                   2357: 	$result .=&Apache::edit::tag_start($target,$token);
                   2358: 	$result .=&Apache::edit::text_arg('Maximum Tags to Show:','show',
                   2359: 					   $token,5);
                   2360: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2361:     } elsif ($target eq 'modified' ) {
                   2362: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   2363: 						     $safeeval,'show');
                   2364: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   2365:     }
                   2366:     return $result;
1.7       tsai     2367: }
                   2368: 
                   2369: sub shuffle {
                   2370:     my $a=shift;
                   2371:     my $i;
1.303     albertel 2372:     if (ref($a) eq 'ARRAY' && @$a) {
1.251     albertel 2373: 	&Apache::response::pushrandomnumber();
1.159     albertel 2374: 	for($i=@$a;--$i;) {
                   2375: 	    my $j=int(&Math::Random::random_uniform() * ($i+1));
                   2376: 	    next if $i == $j;
                   2377: 	    @$a[$i,$j] = @$a[$j,$i];
                   2378: 	}
1.251     albertel 2379: 	&Apache::response::poprandomnumber();
1.7       tsai     2380:     }
1.6       tsai     2381: }
                   2382: 
                   2383: sub end_randomlist {
1.159     albertel 2384:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2385:     my $result;
                   2386:     if ($target eq 'edit' ) {
                   2387: 	$result=&Apache::edit::tag_end($target,$token,
                   2388: 				       'End Randomly Parsed Block');
                   2389:     }
                   2390:     return $result;
1.6       tsai     2391: }
                   2392: 
1.283     albertel 2393: sub ordered_show_check {
                   2394:     my $last_part=$Apache::inputtags::partlist[-2];
                   2395:     my $in_order=
                   2396: 	&Apache::lonnet::EXT('resource.'.$Apache::inputtags::part.'.ordered');
                   2397:     my $in_order_show=1;
                   2398:     if ($last_part ne '0' && lc($in_order) eq 'yes') {
                   2399: 	$in_order_show=&Apache::response::check_status($last_part);
                   2400:     }
                   2401:     return $in_order_show;
                   2402: }
                   2403: 
1.469     www      2404: 
                   2405: sub start_startpartmarker {
                   2406:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2407:     my $result='';
                   2408:     if ($target eq 'edit') {
                   2409:         $result=&Apache::edit::tag_start($target,$token);
                   2410:         $result.=&mt('Marker for the start of a part. Place end marker below to wrap in-between tags into a new part.').'</td></tr>';
                   2411:         $result.=&Apache::edit::end_table();
                   2412: 
                   2413:     } 
                   2414:     return $result;
                   2415: }
                   2416: 
                   2417: sub end_startpartmarker {
                   2418:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2419:     my @result;
                   2420:     if ($target eq 'edit') { $result[1]='no'; }
                   2421:     return @result;
                   2422: }
                   2423: 
                   2424: sub start_endpartmarker {
                   2425:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2426:     my $result='';
                   2427:     if ($target eq 'edit') {
                   2428:         $result=&Apache::edit::tag_start($target,$token);
                   2429:         $result.=&mt('Marker for the end of a part. Place start marker above to wrap in-between tags into a new part.').'</td></tr>';
                   2430:         $result.=&Apache::edit::end_table();
                   2431: 
                   2432:     }
                   2433:     return $result;
                   2434: }
                   2435: 
                   2436: sub end_endpartmarker {
                   2437:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2438:     my @result;
                   2439:     if ($target eq 'edit') { $result[1]='no'; }
                   2440:     return @result;
                   2441: }
                   2442: 
                   2443: 
                   2444: 
                   2445: 
                   2446: 
1.11      albertel 2447: sub start_part {
1.326     albertel 2448:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.321     albertel 2449:     if (!$Apache::lonxml::metamode) {
                   2450: 	&Apache::lonxml::startredirection(); # we'll use redirection to fix up 
                   2451: 	                                     # duedates.
                   2452:     }
1.159     albertel 2453:     my $result='';
1.386     albertel 2454:     my $id= &Apache::lonxml::get_id($parstack,$safeeval);
1.159     albertel 2455:     $Apache::inputtags::part=$id;
1.177     albertel 2456:     push(@Apache::inputtags::partlist,$id);
                   2457:     @Apache::inputtags::response=();
1.159     albertel 2458:     @Apache::inputtags::previous=();
                   2459:     @Apache::inputtags::previous_version=();
1.405     albertel 2460:     &Apache::lonhomework::set_show_problem_status(&get_problem_status($id));
1.403     albertel 2461:     &Apache::response::reset_params();
                   2462: 
1.159     albertel 2463:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.259     albertel 2464:     my $newtype=&Apache::lonnet::EXT("resource.$id.type");
                   2465:     if ($newtype) { $Apache::lonhomework::type=$newtype; }
1.283     albertel 2466:     my $in_order_show=&ordered_show_check();
1.214     albertel 2467:     my $expression='$external::part=\''.$Apache::inputtags::part.'\';';
1.259     albertel 2468:     $expression.='$external::type=\''.$Apache::lonhomework::type.'\';';
1.209     albertel 2469:     &Apache::run::run($expression,$safeeval);
1.159     albertel 2470: 
                   2471:     if ($target eq 'meta') {
1.224     www      2472: 	my $display=&Apache::lonxml::get_param('display',$parstack,$safeeval);
                   2473: 	return &Apache::response::mandatory_part_meta.
                   2474: 	       &Apache::response::meta_parameter_write('display','string',$display,'Part Description');
1.159     albertel 2475:     } elsif ($target eq 'web' || $target eq 'grade' ||
                   2476: 	     $target eq 'answer' || $target eq 'tex') {
1.283     albertel 2477: 	if ($hidden || !$in_order_show) {
1.326     albertel 2478: 	    my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,$style);
1.159     albertel 2479: 	} else {
                   2480: 	    my ($status,$accessmsg) = &Apache::lonhomework::check_access($id);
                   2481: 	    push (@Apache::inputtags::status,$status);
                   2482: 	    my $expression='$external::datestatus="'.$status.'";';
                   2483: 	    $expression.='$external::gradestatus="'.$Apache::lonhomework::history{"resource.$id.solved"}.'";';
                   2484: 	    &Apache::run::run($expression,$safeeval);
1.284     albertel 2485: 	    if ($env{'request.state'} eq 'construct') {
1.241     albertel 2486: 		&set_problem_state($Apache::inputtags::part); 
1.240     albertel 2487: 	    }
1.216     albertel 2488: 	    if (( $status eq 'CLOSED' ) ||
                   2489: 		( $status eq 'UNCHECKEDOUT') ||
1.252     albertel 2490: 		( $status eq 'NOT_YET_VIEWED') ||
1.216     albertel 2491: 		( $status eq 'BANNED') ||
                   2492: 		( $status eq 'UNAVAILABLE') ||
                   2493: 		( $status eq 'INVALID_ACCESS')) {
1.326     albertel 2494: 		my $bodytext=&Apache::lonxml::get_all_text("/part",$parser,
                   2495: 							   $style);
1.159     albertel 2496: 		if ( $target eq "web" ) {
1.211     albertel 2497: 		    $result="<br />".&mt('Part is not open to be viewed. It')." $accessmsg<br />";
1.159     albertel 2498: 		} elsif ( $target eq 'tex' ) {
1.284     albertel 2499: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.211     albertel 2500: 			$result="\\end{minipage}\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\\\begin{minipage}{\\textwidth}";
1.195     sakharuk 2501: 		    } else {
1.211     albertel 2502: 			$result="\\vskip 0 mm ".&mt('Part is not open to be viewed. It')." $accessmsg \\\\";
1.195     sakharuk 2503: 		    }
1.159     albertel 2504: 		}
                   2505: 	    } else {
                   2506: 		if ($target eq 'tex') {
1.284     albertel 2507: 		    if (not $env{'form.problem_split'}=~/yes/) {
1.264     sakharuk 2508: 			if ($$tagstack[-2] eq 'td') {
1.388     foxr     2509: 			    $result.='\noindent \begin{minipage}{\textwidth}\noindent';
1.264     sakharuk 2510: 			} else {
                   2511: 			    $result.='\noindent \end{minipage}\vskip 0 mm \noindent \begin{minipage}{\textwidth}\noindent';
                   2512: 			}
1.195     sakharuk 2513: 		    }
1.159     albertel 2514: 		    my $weight = &Apache::lonnet::EXT("resource.$id.weight");
1.284     albertel 2515: 		    my $allkeys=&Apache::lonnet::metadata($env{'request.uri'},'packages');
1.222     sakharuk 2516: 		    my @allkeys = split /,/,$allkeys;
                   2517: 		    my $allow_print_points = 0;
                   2518: 		    foreach my $partial_key (@allkeys) {
1.230     albertel 2519: 			if ($partial_key=~m/^part_(.*)$/) {
1.222     sakharuk 2520: 			    if ($1 ne '0') {$allow_print_points=1;}
                   2521: 			}
                   2522: 		    }
1.275     albertel 2523: 		    my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   2524: 		    if (defined($maxtries) && $maxtries < 0) {
                   2525: 			$allow_print_points=0;
                   2526: 		    }
1.302     albertel 2527: 		    if (lc($env{'course.'.$env{'request.course.id'}.
                   2528: 				    '.disableexampointprint'}) eq 'yes') {
                   2529: 			$allow_print_points=0;
                   2530: 		    }
1.463     foxr     2531: 		    if (($Apache::lonhomework::type eq 'exam') && ($allow_print_points)) { 
1.492     christia 2532: 			$result .= '\vskip 10mm\fbox{\textit{'.&mt('[quant,_1,pt,pt]',$weight ).'}}';
1.463     foxr     2533: 
                   2534: 		    }
1.233     www      2535: 		} elsif ($target eq 'web') {
1.479     raeburn  2536:                     if ($status eq 'CAN_ANSWER') {
                   2537:                         my $problemstatus = &get_problem_status($Apache::inputtags::part); 
                   2538:                         my $probrandomize = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].type");
                   2539:                         my $probrandtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::partlist[0].randomizeontries");
                   2540:                         my $num = scalar(@Apache::inputtags::partlist)-1;
                   2541:                         if ($probrandomize eq 'randomizetry') {
                   2542:                             if (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.type") ne 'randomizetry') {
                   2543:                                 $result .= &randomizetry_part_header($problemstatus,'none',$num);
                   2544:                             } else {
                   2545:                                 my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                   2546:                                 if ($probrandtries ne $reqtries) {
                   2547:                                     $result .= &randomizetry_part_header($problemstatus,$reqtries,$num);
                   2548:                                 }
                   2549:                             }
                   2550:                         } elsif (&Apache::lonnet::EXT("resource.$Apache::inputtags::part.type") eq 'randomizetry') {
                   2551:                             my $reqtries = &Apache::lonnet::EXT("resource.$Apache::inputtags::part.randomizeontries");
                   2552:                             $result .= &randomizetry_part_header($problemstatus,$reqtries,$num);
                   2553:                         }
                   2554:                     }
1.475     raeburn  2555: 		    $result.='<a name="'.&escape($Apache::inputtags::part).'" ></a>';
1.159     albertel 2556: 		}
                   2557: 	    }
                   2558: 	}
                   2559:     } elsif ($target eq 'edit') {
                   2560: 	$result.=&Apache::edit::tag_start($target,$token);
                   2561: 	$result.=&Apache::edit::text_arg('Part ID:','id',$token).
                   2562: 	    &Apache::loncommon::help_open_topic("Part_Tag_Edit_Help").
1.224     www      2563: 	    '&nbsp;&nbsp;'.
                   2564: &Apache::edit::text_arg('Displayed Part Description:','display',$token).
1.159     albertel 2565: 		&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2566:     } elsif ($target eq 'modified') {
                   2567: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
1.225     albertel 2568: 						     $safeeval,'id','display');
1.159     albertel 2569: 	if ($constructtag) {
1.225     albertel 2570: 	    #limiting ids to only letters numbers, and space
1.224     www      2571: 	    $token->[2]->{'id'}=~s/[^A-Za-z0-9 ]//gs;
1.159     albertel 2572: 	    $result = &Apache::edit::rebuild_tag($token);
                   2573: 	}
                   2574:     }
                   2575:     return $result;
1.11      albertel 2576: }
                   2577: 
                   2578: sub end_part {
1.159     albertel 2579:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2580:     &Apache::lonxml::debug("in end_part $target ");
                   2581:     my $status=$Apache::inputtags::status['-1'];
                   2582:     my $hidden=&Apache::loncommon::check_if_partid_hidden($Apache::inputtags::part);
1.283     albertel 2583:     my $in_order_show=&ordered_show_check();
1.321     albertel 2584:     my $result;
                   2585:     if (!$Apache::lonxml::metamode) {
                   2586: 	$result = &Apache::lonxml::endredirection(); # started in &start_part
1.329     albertel 2587: 	$Apache::lonxml::post_evaluate=0;
1.321     albertel 2588:     }
1.312     albertel 2589:     if ($target eq 'grade') {
1.249     albertel 2590: 	if (($status eq 'CAN_ANSWER' || $Apache::lonhomework::scantronmode) &&
1.283     albertel 2591: 	    !$hidden && $in_order_show) {
1.311     foxr     2592: 	    $result.=&Apache::inputtags::grade;
1.249     albertel 2593: 	} else {
                   2594: 	    # move any submission data to .hidden
                   2595: 	    &Apache::inputtags::hidealldata($Apache::inputtags::part);
                   2596: 	}
1.283     albertel 2597:     } elsif (($target eq 'web' || $target eq 'tex') &&
                   2598: 	     !$hidden && $in_order_show) {
1.159     albertel 2599: 	my $gradestatus=&Apache::inputtags::gradestatus($Apache::inputtags::part,
                   2600: 							$target);
1.490     raeburn  2601: 	if (($Apache::lonhomework::type eq 'exam' && $target eq 'tex') ||
                   2602:              ($env{'form.grade_imsexport'})) {
1.212     albertel 2603: 	    $gradestatus='';
                   2604: 	}
1.311     foxr     2605: 	$result.=$gradestatus;
1.265     sakharuk 2606: 	if ($$tagstack[-2] eq 'td' and $target eq 'tex') {$result.='\end{minipage}';} 
1.181     albertel 2607:     } elsif ($target eq 'edit') {
1.311     foxr     2608: 	$result.=&Apache::edit::end_table();
1.322     albertel 2609:     } elsif ($target eq 'modified') {
                   2610: 	 $result .= $token->[2];
1.159     albertel 2611:     }
                   2612:     pop @Apache::inputtags::status;
                   2613:     $Apache::inputtags::part='';
1.295     albertel 2614:     $Apache::lonhomework::type = $Apache::lonhomework::default_type;
1.159     albertel 2615:     return $result;
1.11      albertel 2616: }
1.1       albertel 2617: 
1.25      albertel 2618: sub start_preduedate {
1.326     albertel 2619:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.339     albertel 2620:     if ($target eq 'web' || $target eq 'grade'    || $target eq 'answer' ||
                   2621: 	$target eq 'tex' || $target eq 'webgrade') {
1.236     albertel 2622: 	&Apache::lonxml::debug("State in preduedate is ". $Apache::inputtags::status['-1']);
1.300     albertel 2623: 	if (!$Apache::lonhomework::scantronmode &&
                   2624: 	    $Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
1.236     albertel 2625: 	    $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                   2626: 	    &Apache::lonxml::debug("Wha? ". ($Apache::inputtags::status['-1'] ne 'SHOW_ANSWER'));
1.326     albertel 2627: 	    &Apache::lonxml::get_all_text("/preduedate",$parser,$style);
1.159     albertel 2628: 	}
1.24      albertel 2629:     }
1.159     albertel 2630:     return '';
1.24      albertel 2631: }
                   2632: 
1.25      albertel 2633: sub end_preduedate {
1.159     albertel 2634:     return '';
1.24      albertel 2635: }
                   2636: 
1.369     foxr     2637: # In all the modes where <postanswerdate> text is 
                   2638: # displayable,  all we do is eat up the text between the start/stop
                   2639: # tags if the conditions are not right to display it.
1.25      albertel 2640: sub start_postanswerdate {
1.326     albertel 2641:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.370     foxr     2642:     my $pav = &Apache::lonnet::allowed('pav', $env{'request.course.id'}) ||
                   2643: 	&Apache::lonnet::allowed('pav',
                   2644: 			   $env{'request.course.id'}.'/'.$env{'request.course.sec'});
1.369     foxr     2645:     if ($target eq 'web' || $target eq 'grade' || $target eq 'webgrade' ||
1.370     foxr     2646: 	$target eq 'tex' ) {
1.300     albertel 2647: 	if ($Apache::lonhomework::scantronmode ||
1.370     foxr     2648: 	    $Apache::inputtags::status['-1'] ne 'SHOW_ANSWER' ||
                   2649: 	    (($target eq 'tex') && !$pav)) {
1.326     albertel 2650: 	    &Apache::lonxml::get_all_text("/postanswerdate",$parser,$style);
1.159     albertel 2651: 	}
                   2652:     }
                   2653:     return '';
1.24      albertel 2654: }
                   2655: 
1.25      albertel 2656: sub end_postanswerdate {
1.159     albertel 2657:     return '';
1.24      albertel 2658: }
                   2659: 
1.25      albertel 2660: sub start_notsolved {
1.326     albertel 2661:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2662:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
1.339     albertel 2663: 	$target eq 'tex' || $target eq 'webgrade') {
1.159     albertel 2664: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
                   2665: 	&Apache::lonxml::debug("not solved has :$gradestatus:");
1.239     albertel 2666: 	if ($gradestatus =~ /^correct/ &&
                   2667: 	    &Apache::response::show_answer()) {
1.159     albertel 2668: 	    &Apache::lonxml::debug("skipping");
1.326     albertel 2669: 	    &Apache::lonxml::get_all_text("/notsolved",$parser,$style);
1.159     albertel 2670: 	}
1.24      albertel 2671:     }
1.159     albertel 2672:     return '';
1.24      albertel 2673: }
                   2674: 
1.25      albertel 2675: sub end_notsolved {
1.159     albertel 2676:     return '';
1.24      albertel 2677: }
                   2678: 
                   2679: sub start_solved {
1.326     albertel 2680:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2681:     if ($target eq 'web' || $target eq 'grade' || $target eq 'answer' ||
                   2682: 	$target eq 'tex') {
                   2683: 	my $gradestatus=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
1.239     albertel 2684: 	if ($gradestatus !~ /^correct/ ||
                   2685: 	    !&Apache::response::show_answer()) {
1.326     albertel 2686: 	    &Apache::lonxml::get_all_text("/solved",$parser,$style);
1.159     albertel 2687: 	}
1.24      albertel 2688:     }
1.159     albertel 2689:     return '';
1.24      albertel 2690: }
                   2691: 
                   2692: sub end_solved {
1.248     albertel 2693:     return '';
                   2694: }
                   2695: 
                   2696: sub start_problemtype {
1.326     albertel 2697:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.248     albertel 2698:     my $result;
1.339     albertel 2699:     if ($target eq 'web' || $target eq 'grade'   || $target eq 'answer' ||
                   2700: 	$target eq 'tex' || $target eq 'analyze' || $target eq 'webgrade') {
1.248     albertel 2701: 	my $mode=lc(&Apache::lonxml::get_param('mode',$parstack,$safeeval));
                   2702: 	if (!defined($mode)) { $mode='show'; }
                   2703: 	my $for=&Apache::lonxml::get_param('for',$parstack,$safeeval);
                   2704: 	my $found=0;
                   2705: 	foreach my $type (split(',',$for)) {
                   2706: 	    if ($Apache::lonhomework::type eq lc($type)) { $found=1; }
                   2707: 	}
                   2708: 	if ($mode eq 'show' && !$found) {
1.326     albertel 2709: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 2710: 	}
                   2711: 	if ($mode eq 'hide' && $found) {
1.326     albertel 2712: 	    &Apache::lonxml::get_all_text("/problemtype",$parser,$style);
1.248     albertel 2713: 	}
                   2714:     } elsif ($target eq 'edit') {
                   2715: 	$result .=&Apache::edit::tag_start($target,$token);
                   2716: 	$result.=&Apache::edit::select_arg('Mode:','mode',
                   2717: 					   [['show','Show'],
                   2718: 					    ['hide','Hide']]
                   2719: 					   ,$token);
                   2720: 	$result .=&Apache::edit::checked_arg('When used as type(s):','for',
1.511     bisitz   2721: 					     [ ['exam','Bubblesheet Exam/Quiz Problem'],
1.248     albertel 2722: 					       ['survey','Survey'],
1.465     raeburn  2723:                                                ['surveycred','Survey (with credit)'],
                   2724:                                                ['anonsurvey','Anonymous Survey'],
                   2725:                                                ['anonsurveycred','Anonymous Survey (with credit)'],
1.428     raeburn  2726: 					       ['problem','Homework Problem'],
1.479     raeburn  2727:                                                ['practice','Practice Problem'],
                   2728:                                                ['randomizetry','New Randomization Each Try'] ]
1.248     albertel 2729: 					     ,$token);
                   2730: 	$result .=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
                   2731:     } elsif ($target eq 'modified') {
                   2732: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                   2733: 						     $safeeval,'mode','for');
                   2734: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                   2735:     }
                   2736:     return $result;
                   2737: }
                   2738: 
                   2739: sub end_problemtype {
1.159     albertel 2740:     return '';
1.24      albertel 2741: }
1.34      albertel 2742: 
                   2743: sub start_startouttext {
1.159     albertel 2744:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2745:     my @result=(''.'');
                   2746:     if ($target eq 'edit' || $target eq 'modified' ) { @result=('','no'); }
1.404     albertel 2747:     
                   2748:     my $nesting = 
                   2749: 	&Apache::lonxml::set_state('outtext',
                   2750: 				   &Apache::lonxml::get_state('outtext')+1);
                   2751:     if ($nesting > 1 && $env{'request.state'} eq 'construct') {
                   2752: 	&Apache::lonxml::error("Nesting of &lt;startouttext /&gt; not allowed, on line ".$token->[5]);
                   2753:     }
1.159     albertel 2754:     return (@result);
1.34      albertel 2755: }
1.159     albertel 2756: 
1.34      albertel 2757: sub end_startouttext {
1.326     albertel 2758:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.159     albertel 2759:     my $result='';
                   2760:     my $text='';
                   2761:     if ($target eq 'edit') {
1.424     foxr     2762: 	my $areaid = 'homework_edit_'.$Apache::lonxml::curdepth;
1.326     albertel 2763: 	$text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.437     raeburn  2764: 	$result.=&Apache::edit::start_table($token)."<tr><td>".&mt('Text Block')."</td>"
1.438     bisitz   2765:                  .'<td><span class="LC_nobreak">'.&mt('Delete?').' '
1.437     raeburn  2766:                  .&Apache::edit::deletelist($target,$token)
1.474     raeburn  2767:                  .'</span></td>'
1.515     golterma 2768: 	         .'<td><span id="math_'.$areaid.'">'
1.474     raeburn  2769: 		 .&Apache::lonhtmlcommon::dragmath_button($areaid,1)
1.512     bisitz   2770: 		 .'</span></td>'
1.474     raeburn  2771: 		 .'<td>'
                   2772: 		 .&Apache::edit::insertlist($target,$token)
                   2773: 		 .'</td>'
1.515     golterma 2774: 	         .'<td class="LC_edit_problem_latexhelper">' .
1.474     raeburn  2775: 	         &Apache::loncommon::helpLatexCheatsheet().
1.159     albertel 2776: 		 &Apache::edit::end_row().
1.362     albertel 2777:                  &Apache::edit::start_spanning_row()."\n".
1.255     www      2778: 		 &Apache::edit::editfield($token->[1],$text,"",80,8,1);
1.159     albertel 2779:     }
                   2780:     if ($target eq 'modified') {
1.219     albertel 2781: 	$result='<startouttext />'.&Apache::edit::modifiedfield("endouttext",$parser);
1.159     albertel 2782:     }
                   2783:     if ($target eq 'tex') {
                   2784: 	$result .= '\noindent ';
                   2785:     }
                   2786:     return $result;
1.34      albertel 2787: }
1.159     albertel 2788: 
1.34      albertel 2789: sub start_endouttext {
1.159     albertel 2790:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2791:     my $result='';
                   2792:     if ($target eq "edit" ) { $result="</td></tr>".&Apache::edit::end_table()."\n"; }
                   2793:     if ($target eq "modified") {
                   2794: 	$result='<endouttext />'.
1.377     albertel 2795: 	    &Apache::edit::handle_insertafter('startouttext');
                   2796:     }
1.404     albertel 2797: 
                   2798:     my $nesting = 
                   2799: 	&Apache::lonxml::set_state('outtext',
                   2800: 				   &Apache::lonxml::get_state('outtext')-1);
                   2801:     if ($nesting < 0 && $env{'request.state'} eq 'construct') {
                   2802: 	&Apache::lonxml::error(" Extraneous &lt;endouttext /&gt; not allowed on line ".$token->[5]);
                   2803: 	&Apache::lonxml::set_state('outtext', 0);
                   2804:     }
1.159     albertel 2805:     return $result;
1.34      albertel 2806: }
1.159     albertel 2807: 
1.34      albertel 2808: sub end_endouttext {
1.159     albertel 2809:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2810:     my @result=('','');
                   2811:     if ($target eq "edit" || $target eq 'modified') { @result=('','no'); }
                   2812:     return (@result);
1.34      albertel 2813: }
1.159     albertel 2814: 
1.45      albertel 2815: sub delete_startouttext {
1.326     albertel 2816:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
                   2817:     #  my $text=&Apache::lonxml::get_all_text("endouttext",$parser,$style);
1.159     albertel 2818:     my $text=$$parser['-1']->get_text("/endouttext");
                   2819:     my $ntoken=$$parser['-1']->get_token();
                   2820:     &Apache::lonxml::debug("Deleting :$text: and :$ntoken->[0]:$ntoken->[1]:$ntoken->[2]: for startouttext");
                   2821:     &Apache::lonxml::end_tag($tagstack,$parstack,$ntoken);
                   2822:     # Deleting 2 parallel tag pairs, but we need the numbers later to look like
                   2823:     # they did the last time round
                   2824:     &Apache::lonxml::increasedepth($ntoken);
                   2825:     &Apache::lonxml::decreasedepth($ntoken);
                   2826:     return 1;
1.193     www      2827: }
                   2828: 
                   2829: sub start_simpleeditbutton {
                   2830:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                   2831:     my $result='';
1.284     albertel 2832:     if (($env{'form.simple_edit_button'} ne 'off') &&
1.273     albertel 2833: 	($target eq 'web') &&
1.330     albertel 2834:         (&Apache::lonnet::allowed('mdc',$env{'request.course.id'}))) {
1.284     albertel 2835:         my $url=$env{'request.noversionuri'};
1.193     www      2836:         $url=~s/\?.*$//;
1.367     albertel 2837: 	my ($symb) = &Apache::lonnet::whichuser();
1.451     bisitz   2838: #       Warning makes more sense and is more important on edit screen
1.442     bisitz   2839: #       $result='<p class="LC_warning">'
                   2840: #              .&mt('Note: it can take up to 10 minutes for changes to take effect for all users.')
                   2841: #              .&Apache::loncommon::help_open_topic('Caching')
                   2842: #              .'</p>';
1.486     www      2843:         $result.=&Apache::loncommon::head_subbox(
                   2844:                  &Apache::lonhtmlcommon::start_funclist()
1.451     bisitz   2845:                 .&Apache::lonhtmlcommon::add_item_funclist(
                   2846:                      '<a href="'.$url.'/smpedit?symb='.&escape($symb).'">'
                   2847:                     .&mt('Edit').'</a>')
1.486     www      2848:                 .&Apache::lonhtmlcommon::end_funclist());
1.442     bisitz   2849: 
1.193     www      2850:     }
                   2851:     return $result;
                   2852: }
                   2853: 
                   2854: sub end_simpleeditbutton {
                   2855:     return '';
1.45      albertel 2856: }
1.34      albertel 2857: 
1.428     raeburn  2858: sub practice_problem_header {
                   2859:     return '<span class="LC_info"><h3>'.&mt('Practice Problem').'</h3></span>'.
                   2860:            '<span class="LC_info">'.&mt('Submissions are not permanently recorded').
                   2861:            '</span>';
                   2862: }
                   2863: 
1.479     raeburn  2864: sub randomizetry_problem_header {
                   2865:     my ($problemstatus,$reqtries) = @_;
                   2866:     my ($header,$text);
                   2867:     if ($reqtries > 1) {
                   2868:         $header = &mt('New Problem Variation After Every [quant,_1,Try,Tries]',$reqtries);
                   2869:         if (($problemstatus eq 'no') ||
                   2870:             ($problemstatus eq 'no_feedback_ever')) {
                   2871:             $text = &mt('A new variation will be generated after every [quant,_1,try,tries], until the tries limit is reached.',$reqtries);
                   2872:         } else {
                   2873:             $text = &mt('A new variation will be generated after every [quant,_1,try,tries], until correct or tries limit is reached.',$reqtries);
                   2874:         }
                   2875:     } else {
                   2876:         $header = &mt('New Problem Variation Each Try');
                   2877:         if (($problemstatus eq 'no') ||
                   2878:             ($problemstatus eq 'no_feedback_ever')) {
                   2879:             $text = &mt('A new variation will be generated after each try until the tries limit is reached.');
                   2880: 
                   2881:         } else {
                   2882:             $text = &mt('A new variation will be generated after each try until correct or tries limit is reached.');
                   2883:         }
                   2884:     }
                   2885:     return '<span class="LC_info"><h3>'.$header.'</h3></span>'.
                   2886:            '<span class="LC_info">'.$text.'</span><hr />';
                   2887: }
                   2888: 
                   2889: sub randomizetry_part_header {
                   2890:     my ($problemstatus,$reqtries,$num) = @_;
                   2891:     my ($header,$text);
                   2892:     if ($reqtries eq 'none') {
                   2893:         $header = &mt('No Question Variation');
                   2894:         $text = &mt('For this question there will no new variation after a try.');
                   2895:     } elsif ($reqtries > 1) {
                   2896:         $header = &mt('New Question Variation After Every [quant,_1,Try,Tries]',$reqtries);
                   2897:         if (($problemstatus eq 'no') ||
                   2898:             ($problemstatus eq 'no_feedback_ever')) {
                   2899:             $text = &mt('For this question a new variation will be generated after every [quant,_1,try,tries], until the tries limit is reached.',$reqtries);
                   2900:         } else {
                   2901:             $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);
                   2902:         }
                   2903:     } else {
                   2904:         $header = &mt('New Question Variation For Each Try');
                   2905:         if (($problemstatus eq 'no') ||
                   2906:             ($problemstatus eq 'no_feedback_ever')) {
                   2907:             $text =  &mt('For this question a new variation will be generated after each try until the tries limit is reached.');
                   2908:         } else {
                   2909:             $text = &mt('For this question a new variation will be generated after each try until correct or tries limit is reached.');
                   2910:         }
                   2911:     }
                   2912:     my $output;
                   2913:     if ($num > 1) {
                   2914:         $output .= '<hr />';
                   2915:     }
                   2916:     $output .=  '<span class="LC_info"><h4>'.$header.'</h4></span>'.
                   2917:                   '<span class="LC_info">'.$text.'</span><br /><br />';
                   2918:     return $output;
                   2919: }
                   2920: 
1.1       albertel 2921: 1;
                   2922: __END__
1.435     jms      2923: 
                   2924: =pod
                   2925: 
                   2926: =back
                   2927: 
                   2928: =cut

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