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

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

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