Annotation of loncom/homework/inputtags.pm, revision 1.324

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.324   ! raeburn     4: # $Id: inputtags.pm,v 1.323 2014/01/13 15:29:10 bisitz Exp $
1.47      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/
1.1       albertel   27: 
1.249     jms        28: =pod
                     29: 
                     30: =head1 NAME
                     31: 
                     32: Apache::inputtags
                     33: 
                     34: =head1 SYNOPSIS
                     35: 
                     36: 
                     37: 
                     38: This is part of the LearningOnline Network with CAPA project
                     39: described at http://www.lon-capa.org.
                     40: 
                     41: 
                     42: =head1 NOTABLE SUBROUTINES
                     43: 
                     44: =over
                     45: 
                     46: =item 
                     47: 
                     48: =back
                     49: 
                     50: =cut
                     51: 
1.1       albertel   52: package Apache::inputtags;
1.55      albertel   53: use HTML::Entities();
1.1       albertel   54: use strict;
1.82      www        55: use Apache::loncommon;
1.265     raeburn    56: use Apache::lonhtmlcommon;
1.115     www        57: use Apache::lonlocal;
1.165     albertel   58: use Apache::lonnet;
1.192     www        59: use LONCAPA;
                     60:  
1.1       albertel   61: 
1.50      harris41   62: BEGIN {
1.286     www        63:     &Apache::lonxml::register('Apache::inputtags',('hiddensubmission','hiddenline','textfield','textline'));
1.1       albertel   64: }
                     65: 
1.249     jms        66: =pod
                     67: 
                     68: =item initialize_inputtags()
                     69: 
                     70: Initializes a set of global variables used during the parse of the problem.
                     71: 
                     72: @Apache::inputtags::input        - List of current input ids.
                     73: @Apache::inputtags::inputlist    - List of all input ids seen this problem.
                     74: @Apache::inputtags::response     - List of all current resopnse ids.
                     75: @Apache::inputtags::responselist - List of all response ids seen this 
                     76:                                      problem.
                     77: @Apache::inputtags::hint         - List of all hint ids.
                     78: @Apache::inputtags::hintlist     - List of all hint ids seen this problem.
                     79: @Apache::inputtags::previous     - List describing if specific responseds
                     80:                                      have been used
                     81: @Apache::inputtags::previous_version - Submission responses were used in.
                     82: $Apache::inputtags::part         - Current part id (valid only in 
                     83:                                      <problem>)
                     84:                                    0 if not in a part.
                     85: @Apache::inputtags::partlist     - List of part ids seen in the current
                     86:                                      <problem>
                     87: @Apache::inputtags::status       - List of problem  statuses. First 
                     88:                                    element is the status of the <problem>
                     89:                                    the remainder are for individual <part>s.
                     90: %Apache::inputtags::params       - Hash of defined parameters for the
                     91:                                    current response.
                     92: @Apache::inputtags::import       - List of all ids for <import> thes get
                     93:                                    join()ed and prepended.
                     94: @Apache::inputtags::importlist   - List of all import ids seen.
                     95: $Apache::inputtags::response_with_no_part
                     96:                                  - Flag set true if we have seen a response
                     97:                                    that is not inside a <part>
                     98: %Apache::inputtags::answertxt    - <*response> tags store correct
                     99:                                    answer strings for display by <textline/>
                    100:                                    in this hash.
                    101: %Apache::inputtags::submission_display
                    102:                                  - <*response> tags store improved display
                    103:                                    of submission strings for display by part
                    104:                                    end.
                    105: 
                    106: =cut
1.178     albertel  107: 
1.1       albertel  108: sub initialize_inputtags {
1.135     albertel  109:     @Apache::inputtags::input=();
                    110:     @Apache::inputtags::inputlist=();
1.174     albertel  111:     @Apache::inputtags::response=();
1.135     albertel  112:     @Apache::inputtags::responselist=();
1.174     albertel  113:     @Apache::inputtags::hint=();
1.173     albertel  114:     @Apache::inputtags::hintlist=();
1.135     albertel  115:     @Apache::inputtags::previous=();
                    116:     @Apache::inputtags::previous_version=();
                    117:     $Apache::inputtags::part='';
                    118:     @Apache::inputtags::partlist=();
                    119:     @Apache::inputtags::status=();
                    120:     %Apache::inputtags::params=();
                    121:     @Apache::inputtags::import=();
                    122:     @Apache::inputtags::importlist=();
                    123:     $Apache::inputtags::response_with_no_part=0;
1.144     albertel  124:     %Apache::inputtags::answertxt=();
1.217     albertel  125:     %Apache::inputtags::submission_display=();
1.103     albertel  126: }
                    127: 
1.310     foxr      128: #
                    129: #  provides the onblur binding for spellchecking.  This could be an
                    130: #  empty string if spellchecking was not enabled.
                    131: #  Jquery selector binding is done rather than setting an onblur
                    132: #  attribute because we'll need to set the element's spellcheck language
                    133: #  option dynamically so we need $(this) to be defined.
                    134: #
                    135: # @param id   - The element id to bind.
                    136: # @param lang - Language in which spellchecking is desired.
                    137: #               if undef, nothing is generated.  
                    138: # @return string - onblur specification to do the requested spellchecking.
                    139: #
                    140: sub spellcheck_onblur {
                    141:     my ($id, $lang) = @_;
                    142:     my $result = '';
                    143:     if ($lang) {
                    144: 
                    145: 	$result = <<JAVASCRIPT;
                    146: <script type="text/javascript">
                    147: \$('\#$id').blur(function() {
                    148:     doSpellcheck('\#$id', '$lang');
                    149:  });
                    150: </script>
                    151: 
                    152: JAVASCRIPT
                    153: 
                    154: 
                    155:     }
                    156:     return $result;
                    157: }
                    158: 
1.103     albertel  159: sub check_for_duplicate_ids {
                    160:     my %check;
                    161:     foreach my $id (@Apache::inputtags::partlist,
                    162: 		    @Apache::inputtags::responselist,
1.173     albertel  163: 		    @Apache::inputtags::hintlist,
1.103     albertel  164: 		    @Apache::inputtags::importlist) {
                    165: 	$check{$id}++;
                    166:     }
                    167:     my @duplicates;
                    168:     foreach my $id (sort(keys(%check))) {
                    169: 	if ($check{$id} > 1) {
                    170: 	    push(@duplicates,$id);
                    171: 	}
                    172:     }
                    173:     if (@duplicates) {
                    174: 	&Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
                    175:     }
1.1       albertel  176: }
                    177: 
1.14      albertel  178: sub start_input {
1.135     albertel  179:     my ($parstack,$safeeval)=@_;
1.228     albertel  180:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.135     albertel  181:     push (@Apache::inputtags::input,$id);
                    182:     push (@Apache::inputtags::inputlist,$id);
                    183:     return $id;
1.14      albertel  184: }
                    185: 
                    186: sub end_input {
1.135     albertel  187:     pop @Apache::inputtags::input;
                    188:     return '';
1.14      albertel  189: }
                    190: 
1.124     www       191: sub addchars {
                    192:     my ($fieldid,$addchars)=@_;
                    193:     my $output='';
                    194:     foreach (split(/\,/,$addchars)) {
                    195: 	$output.='<a href="javascript:void(document.forms.lonhomework.'.
                    196: 	    $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
                    197:     }
                    198:     return $output;
                    199: }
                    200: 
1.48      albertel  201: sub start_textfield {
1.185     albertel  202:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.135     albertel  203:     my $result = "";
                    204:     my $id = &start_input($parstack,$safeeval);
                    205:     my $resid=$Apache::inputtags::response[-1];
                    206:     if ($target eq 'web') {
                    207: 	$Apache::lonxml::evaluate--;
1.205     albertel  208: 	my $partid=$Apache::inputtags::part;
1.274     raeburn   209:         my ($oldresponse,$newvariation);
                    210:         if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
                    211:              ($Apache::lonhomework::type eq 'randomizetry')) &&
                    212:              ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
                    213:             if ($env{'form.'.$partid.'.rndseed'} ne
                    214:                 $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
                    215:                 $newvariation = 1;
                    216:             }
                    217:         }
                    218:         unless ($newvariation) {
1.317     raeburn   219:             if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
                    220:                 (($env{'form.grade_username'} eq $env{'user.name'}) &&
                    221:                  ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
                    222:                 $oldresponse = $Apache::lonhomework::history{"resource.$partid.$resid.submission"};
                    223:             } elsif (($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurvey') ||
                    224:                     ($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurveycred')) {
1.322     raeburn   225:                 $oldresponse = '* '.&mt('(only shown to submitter)').' *';
1.317     raeburn   226:             } else {
                    227:                 $oldresponse = $Apache::lonhomework::history{"resource.$partid.$resid.submission"};
                    228:             }
1.274     raeburn   229:         }
1.135     albertel  230: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    231: 	    my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    232: 	    if ( $cols eq '') { $cols = 80; }
                    233: 	    my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.143     www       234: 	    if ( $rows eq '') { $rows = 16; }
1.135     albertel  235: 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    236: 	    $result='';
1.310     foxr      237: 	    my $tagident = 'HWVAL_' . $resid;
1.135     albertel  238: 	    if ($addchars) {
1.310     foxr      239: 		$result.=&addchars($tagident, $addchars);
1.135     albertel  240: 	    }
1.324   ! raeburn   241:             my $textareaclass;
        !           242:             unless (&Apache::londefdef::is_inside_of($tagstack,
        !           243:                                                     'externalresponse')) {
        !           244:                 $textareaclass = 'class="LC_richDetectHtml spellchecked"';
        !           245:             }
1.310     foxr      246: 	    $result.= '<textarea wrap="hard" name="'.$tagident.'" id="'.$tagident.'" ' .
1.308     foxr      247: 		      'rows="'.$rows.'" cols="'.$cols.'" '.$textareaclass
                    248: 		      .'>'.
1.302     raeburn   249:                       &HTML::Entities::encode($oldresponse,'<>&"');
1.135     albertel  250: 	    if ($oldresponse ne '') {
1.143     www       251: 
1.135     albertel  252: 		#get rid of any startup text if the user has already responded
1.185     albertel  253: 		&Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.135     albertel  254: 	    }
                    255: 	} else {
1.205     albertel  256: 	    #show past answer in the essayresponse case
                    257: 	    if ($oldresponse =~ /\S/
                    258: 		&& &Apache::londefdef::is_inside_of($tagstack,
                    259: 						    'essayresponse') ) {
                    260: 		$result='<table class="LC_pastsubmission"><tr><td>'.
1.323     bisitz    261: 		    &HTML::Entities::encode($oldresponse,'"<>&').
                    262:                     '</td></tr></table>';
1.205     albertel  263: 	    }
1.135     albertel  264: 	    #get rid of any startup text
1.185     albertel  265: 	    &Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.61      albertel  266: 	}
1.135     albertel  267:     } elsif ($target eq 'grade') {
1.185     albertel  268: 	my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    269: 						   $style);
1.165     albertel  270: 	if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
1.135     albertel  271: 	    # if the seed text is still there it wasn't a real submission
1.165     albertel  272: 	    $env{'form.HWVAL_'.$resid}='';
1.135     albertel  273: 	}
                    274:     } elsif ($target eq 'edit') {
                    275: 	$result.=&Apache::edit::tag_start($target,$token);
                    276: 	$result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
                    277: 	$result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
                    278: 	$result.=&Apache::edit::text_arg
                    279: 	    ('Click-On Texts (comma sep):','addchars',$token,10);
1.185     albertel  280: 	my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    281: 						   $style);
1.135     albertel  282: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
1.314     raeburn   283:         my $spell_langs = &spelling_languages();
1.312     raeburn   284: 	$result .= &Apache::edit::select_arg('Spellcheck for:', 'spellcheck',
1.314     raeburn   285: 					     $spell_langs, $token);
1.135     albertel  286:     } elsif ($target eq 'modified') {
                    287: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    288: 						     $safeeval,'rows','cols',
1.311     foxr      289: 						     'addchars', 'spellcheck');
1.135     albertel  290: 	if ($constructtag) {
                    291: 	    $result = &Apache::edit::rebuild_tag($token);
                    292: 	} else {
                    293: 	    $result=$token->[4];
                    294: 	}
                    295: 	$result.=&Apache::edit::modifiedfield("/textfield",$parser);
                    296:     } elsif ($target eq 'tex') {
                    297: 	my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
                    298: 	my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    299: 	if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
                    300: 	    $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
                    301: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    302: 	    $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
                    303: 	} else {
1.313     raeburn   304:             if ($env{'form.pdfFormFields'} eq 'yes') {
                    305:                 my $fieldname = $env{'request.symb'}.
                    306:                                 '&part_'. $Apache::inputtags::part.
                    307:                                 '&textresponse'.
                    308:                                 '&HWVAL_' . $Apache::inputtags::response['-1'];
                    309:                 $result.='\TextField[name='.$fieldname.',multiline=true,height=6\baselineskip,width=270,borderwidth=0,backgroundcolor={.85 .85 .85}]\\';
                    310:             } else {
                    311:                 my $TeXwidth=$width_of_box/80;
                    312:                 $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
                    313:                 for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    314:                 $result.='}}}\vskip 2 mm ';
                    315:             }
1.135     albertel  316: 	}
1.60      albertel  317:     }
1.135     albertel  318:     return $result;
1.6       albertel  319: }
                    320: 
1.48      albertel  321: sub end_textfield {
1.135     albertel  322:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    323:     my $result;
                    324:     if ($target eq 'web') {
1.310     foxr      325: 	my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
1.135     albertel  326: 	$Apache::lonxml::evaluate++;
                    327: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.310     foxr      328: 	    my $resid = $Apache::inputtags::response[-1];
                    329: 	    my $tagident = 'HWVAL_' . $resid;
                    330: 	    my $result =  "</textarea>";
                    331: 	    $result .= &spellcheck_onblur($tagident, $spellcheck);
                    332: 	    return $result;
1.135     albertel  333: 	}
                    334:     } elsif ($target eq 'edit') {
                    335: 	$result=&Apache::edit::end_table();
                    336:     }
                    337:     &end_input;
                    338:     return $result;
1.6       albertel  339: }
                    340: 
1.190     albertel  341: sub exam_score_line {
1.188     albertel  342:     my ($target) = @_;
1.190     albertel  343: 
1.188     albertel  344:     my $result;
                    345:     if ($target eq 'tex') {
                    346: 	my $repetition = &Apache::response::repetition();
                    347: 	$result.='\begin{enumerate}';
1.190     albertel  348: 	if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
1.188     albertel  349: 	foreach my $i (0..$repetition-1) {
                    350: 	    $result.='\item[\textbf{'.
                    351: 		($Apache::lonxml::counter+$i).
                    352: 		'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
                    353: 	}
                    354: 	$result.= '\end{enumerate}';
1.190     albertel  355:     }
                    356: 
                    357:     return $result;
                    358: }
                    359: 
                    360: sub exam_box {
                    361:     my ($target) = @_;
                    362:     my $result;
1.188     albertel  363: 
1.190     albertel  364:     if ($target eq 'tex') {
                    365: 	$result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
                    366: 	$result .= &exam_score_line($target);
1.188     albertel  367:     } elsif ($target eq 'web') {
                    368: 	my $id=$Apache::inputtags::response[-1];
                    369: 	$result.= '<br /><br />
                    370:                    <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
                    371:                    </textarea> <br /><br />';
                    372:     }
                    373:     return $result;
                    374: }
                    375: 
                    376: sub needs_exam_box {
                    377:     my ($tagstack) = @_;
                    378:     my @tags = ('formularesponse',
                    379: 		'stringresponse',
                    380: 		'reactionresponse',
                    381: 		'organicresponse',
                    382: 		);
                    383: 
                    384:     foreach my $tag (@tags) {
                    385: 	if (grep(/\Q$tag\E/,@$tagstack)) {
                    386: 	    return 1;
                    387: 	}
                    388:     }
                    389:     return 0;
                    390: }
                    391: 
1.1       albertel  392: sub start_textline {
1.135     albertel  393:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    394:     my $result = "";
1.210     albertel  395:     my $input_id = &start_input($parstack,$safeeval);
1.310     foxr      396: 
                    397:     # The spellcheck attribute 
                    398:     # 1. enables spellchecking.
                    399:     # 2. Provides the language code in which the spellchecking will be performed.
                    400: 
                    401:     my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
1.135     albertel  402:     if ($target eq 'web') {
                    403: 	$Apache::lonxml::evaluate--;
                    404: 	my $partid=$Apache::inputtags::part;
                    405: 	my $id=$Apache::inputtags::response[-1];
1.204     albertel  406: 	if (!&Apache::response::show_answer()) {
1.135     albertel  407: 	    my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    408: 	    my $maxlength;
                    409: 	    if ($size eq '') { $size=20; } else {
1.214     albertel  410: 		if ($size < 20) {
                    411: 		    $maxlength = ' maxlength="'.$size.'"';
                    412: 		}
1.135     albertel  413: 	    }
1.274     raeburn   414:             my ($oldresponse,$newvariation);
                    415:             if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
                    416:                  ($Apache::lonhomework::type eq 'randomizetry')) &&
                    417:                  ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
                    418:                 if ($env{'form.'.$partid.'.rndseed'} ne
                    419:                     $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
                    420:                     $newvariation = 1;
                    421:                 }
                    422:             }
                    423:             unless ($newvariation) {
1.317     raeburn   424:                 if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
                    425:                     (($env{'form.grade_username'} eq $env{'user.name'}) &&
                    426:                      ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
                    427:                     $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    428:                 } elsif (($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurvey') ||
                    429:                         ($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurveycred') ||
                    430:                         ($Apache::lonhomework::type eq 'anonsurvey') ||
                    431:                         ($Apache::lonhomework::type eq 'anonsurveycred')) {
1.322     raeburn   432:                         $oldresponse = '* '.&mt('(only shown to submitter)').' *';
1.317     raeburn   433:                 } else {
                    434:                     $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    435:                 }
1.274     raeburn   436: 	        &Apache::lonxml::debug("oldresponse $oldresponse is ".ref($oldresponse));
                    437: 	        if (ref($oldresponse) eq 'ARRAY') {
                    438: 		    $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
                    439: 	        }
                    440: 	        $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
                    441:                 $oldresponse =~ s/^\s+//;
                    442:                 $oldresponse =~ s/\s+$//;
                    443:                 $oldresponse =~ s/\s+/ /g;
                    444:             }
1.135     albertel  445: 	    if ($Apache::lonhomework::type ne 'exam') {
                    446: 		my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    447: 		$result='';
                    448: 		if ($addchars) {
                    449: 		    $result.=&addchars('HWVAL_'.$id,$addchars);
                    450: 		}
1.157     albertel  451: 		my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
                    452: 							$safeeval);
1.193     albertel  453: 		if (lc($readonly) eq 'yes' 
                    454: 		    || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
1.157     albertel  455: 		    $readonly=' readonly="readonly" ';
1.158     albertel  456: 		} else {
                    457: 		    $readonly='';
1.157     albertel  458: 		}
1.193     albertel  459: 		my $name = 'HWVAL_'.$id;
                    460: 		if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
                    461: 		    $name = "none";
                    462: 		}
1.310     foxr      463: 		$result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');"'
                    464: 		     . ' type="text" '
                    465: 		     . $readonly.' name="'. $name . '"'
                    466: 		     . ' id="' . $name . '"'
                    467: 		     . ' value="'.  $oldresponse.'"'
                    468: 		     . ' class="spellchecked"  size="'.$size.'"'.$maxlength
                    469: 		     . '/>';
                    470: 
                    471: 		$result .= &spellcheck_onblur($name, $spellcheck);
1.135     albertel  472: 	    }
1.188     albertel  473: 	    if ($Apache::lonhomework::type eq 'exam'
                    474: 		&& &needs_exam_box($tagstack)) {
                    475: 		$result.=&exam_box($target);
                    476: 	    }
1.135     albertel  477: 	} else {
                    478: 	    #right or wrong don't show what was last typed in.
1.208     albertel  479: 	    my $count = scalar(@Apache::inputtags::inputlist)-1;
                    480: 	    $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
1.144     albertel  481: 	    #$result='';
1.135     albertel  482: 	}
                    483:     } elsif ($target eq 'edit') {
                    484: 	$result=&Apache::edit::tag_start($target,$token);
                    485: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157     albertel  486: 	    &Apache::edit::text_arg('Click-On Texts (comma sep):',
                    487: 				    'addchars',$token,10);
                    488:         $result.=&Apache::edit::select_arg('Readonly:','readonly',
                    489: 					   ['no','yes'],$token);
1.314     raeburn   490:         my $spell_langs = &spelling_languages();
1.316     bisitz    491: 	$result.=&Apache::edit::select_arg('Spellcheck for:', 'spellcheck',
1.314     raeburn   492: 					   $spell_langs, $token);
1.157     albertel  493: 	$result.=&Apache::edit::end_row();
                    494: 	$result.=&Apache::edit::end_table();
1.135     albertel  495:     } elsif ($target eq 'modified') {
1.157     albertel  496: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    497: 						     $safeeval,'size',
1.311     foxr      498: 						     'addchars','readonly', 'spellcheck');
1.135     albertel  499: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.188     albertel  500:     } elsif ($target eq 'tex' 
                    501: 	     && $Apache::lonhomework::type ne 'exam') {
1.135     albertel  502: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    503: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
1.257     onken     504: 	if ($env{'form.pdfFormFields'} eq 'yes'
                    505:             && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.246     onken     506:             my $fieldname = $env{'request.symb'}.
                    507:                                  '&part_'. $Apache::inputtags::part.
                    508:                                  '&textresponse'.
                    509:                                  '&HWVAL_' . $Apache::inputtags::response['-1'];
1.259     onken     510:             $result='\textField{'.$fieldname.'}{'.$size.'}{12 bp}';
1.246     onken     511:         } else {
                    512:             $result='\framebox['.$size.'][s]{\tiny\strut}';
                    513:         }
1.188     albertel  514:     } elsif ($target eq 'tex' 
                    515: 	     && $Apache::lonhomework::type eq 'exam'
                    516: 	     && &needs_exam_box($tagstack)) {
                    517: 	$result.=&exam_box($target);
1.135     albertel  518:     }
                    519:     return $result;
1.1       albertel  520: }
                    521: 
                    522: sub end_textline {
1.135     albertel  523:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    524:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    525:     elsif ($target eq 'edit') { return ('','no'); }
1.208     albertel  526:     &end_input();
1.135     albertel  527:     return "";
1.9       albertel  528: }
                    529: 
1.98      albertel  530: sub start_hiddenline {
                    531:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    532:     my $result = "";
1.211     albertel  533:     my $input_id = &start_input($parstack,$safeeval);
1.98      albertel  534:     if ($target eq 'web') {
                    535: 	$Apache::lonxml::evaluate--;
                    536: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    537: 	    my $partid=$Apache::inputtags::part;
                    538: 	    my $id=$Apache::inputtags::response[-1];
1.211     albertel  539: 	    my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    540: 	    if (ref($oldresponse) eq 'ARRAY') {
                    541: 		$oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
                    542: 	    }
                    543: 	    $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
                    544: 
1.98      albertel  545: 	    if ($Apache::lonhomework::type ne 'exam') {
                    546: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
                    547: 		    $oldresponse.'" />';
                    548: 	    }
                    549: 	}
                    550:     } elsif ($target eq 'edit') {
                    551: 	$result=&Apache::edit::tag_start($target,$token);
                    552: 	$result.=&Apache::edit::end_table;
                    553:     }
1.189     albertel  554: 
                    555:     if ( ($target eq 'web' || $target eq 'tex')
                    556: 	 && $Apache::lonhomework::type eq 'exam'
                    557: 	 && &needs_exam_box($tagstack)) {
                    558: 	$result.=&exam_box($target);
                    559:     }
1.98      albertel  560:     return $result;
                    561: }
                    562: 
                    563: sub end_hiddenline {
1.135     albertel  564:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    565:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    566:     elsif ($target eq 'edit') { return ('','no'); }
1.211     albertel  567:     &end_input();
1.135     albertel  568:     return "";
1.98      albertel  569: }
                    570: 
1.286     www       571: 
                    572: sub start_hiddensubmission {
                    573:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    574:     my $result = "";
                    575:     my $input_id = &start_input($parstack,$safeeval);
                    576:     if ($target eq 'web') {
                    577:         $Apache::lonxml::evaluate--;
                    578:         if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    579:             my $partid=$Apache::inputtags::part;
                    580:             my $id=$Apache::inputtags::response[-1];
                    581:             if ($Apache::lonhomework::type ne 'exam') {
                    582:                 my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    583:                 $value = &HTML::Entities::encode($value,'<>&"');
                    584:                 $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.$value.'" />';
                    585:             }
                    586:         }
                    587:     } elsif ($target eq 'edit') {
                    588:         $result=&Apache::edit::tag_start($target,$token);
                    589:         $result.=&Apache::edit::text_arg('Value:','value',$token,'15');
                    590:         $result.=&Apache::edit::end_row();
                    591:         $result.=&Apache::edit::end_table();
                    592:     } elsif ($target eq 'modified') {
                    593:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    594:                                                      $safeeval,'value');
                    595:         if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    596:     }
                    597: 
                    598:     if ( ($target eq 'web' || $target eq 'tex')
                    599:          && $Apache::lonhomework::type eq 'exam'
                    600:          && &needs_exam_box($tagstack)) {
                    601:         $result.=&exam_box($target);
                    602:     }
                    603:     return $result;
                    604: }
                    605: 
                    606: sub end_hiddensubmission {
                    607:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    608:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    609:     elsif ($target eq 'edit') { return ('','no'); }
                    610:     &end_input();
                    611:     return "";
                    612: }
                    613: 
1.249     jms       614: =pod
                    615: 
                    616: =item file_selector()
                    617: 
                    618: $part -> partid
                    619: $id -> responseid
                    620: $uploadefiletypes -> comma seperated list of extensions allowed or * for any
1.252     raeburn   621: $which -> 'uploadonly'  -> only newly uploaded files
1.249     jms       622:           'portfolioonly' -> only allow files from portfolio
                    623:           'both' -> allow files from either location
                    624: $extratext -> additional text to go between the link and the input box
1.252     raeburn   625: $maxfilesize -> maximum cumulative filesize for submitted files (in MB).
1.249     jms       626: returns a table row <tr> 
                    627: 
                    628: =cut
                    629: 
1.160     albertel  630: sub file_selector {
1.252     raeburn   631:     my ($part,$id,$uploadedfiletypes,$which,$extratext,$maxfilesize)=@_;
1.160     albertel  632:     if (!$uploadedfiletypes) { return ''; }
1.167     albertel  633: 
                    634:     my $jspart=$part;
                    635:     $jspart=~s/\./_/g;
                    636: 
1.160     albertel  637:     my $result;
1.265     raeburn   638:     my $current_files_display = &current_file_submissions($part,$id);
                    639:     my $addfiles;
                    640:     if ($current_files_display) {
1.268     raeburn   641:         $result .= &Apache::lonhtmlcommon::row_title(&mt('Currently submitted files')).
1.265     raeburn   642:                    $current_files_display.
                    643:                    &Apache::lonhtmlcommon::row_closure();
                    644:         $addfiles = &mt('Submit other file(s)');
                    645:     } else {
                    646:         $addfiles = &mt('Choose file(s) to submit');
                    647:     }
                    648:     $result .= &Apache::lonhtmlcommon::row_title($addfiles);
                    649:     my $constraints;
                    650:     if ($uploadedfiletypes ne '*') {
                    651: 	$constraints =
                    652: 	    &mt('Allowed filetypes: [_1]','<b>'.$uploadedfiletypes.'</b>').'<br />';
                    653:     }
                    654:     if ($maxfilesize) {
1.319     bisitz    655:         $constraints .= &mt('Combined size of all files not to exceed: [_1] MB.',
1.265     raeburn   656:                         '<b>'.$maxfilesize.'</b>').'<br />';
                    657:     }
                    658:     if ($constraints) {
                    659:         $result .= $constraints.'<br />';
1.162     albertel  660:     }
1.160     albertel  661:     if ($which eq 'uploadonly' || $which eq 'both') { 
1.265     raeburn   662: 	$result.=&mt('Submit a file: (only one file per submission)').
1.160     albertel  663: 	    ' <br /><input type="file" size="50" name="HWFILE'.
1.293     raeburn   664: 	    $jspart.'_'.$id.'" id="HWFILE'.$jspart.'_'.$id.'" /><br />';
1.160     albertel  665:     }
1.265     raeburn   666:     if ( $which eq 'both') {
1.160     albertel  667: 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
                    668:     }
1.303     raeburn   669:     if ($which eq 'portfolioonly' || $which eq 'both') {
                    670:         my $symb = $env{'request.symb'};
                    671:         (undef,undef,my $res)=&Apache::lonnet::decode_symb($symb);
                    672:         my $showsymb;
                    673:         # If resource is a .task and URL is unencrypted, include symb in query string
                    674:         # for url opened in portfolio file selection window. Can be used to override
                    675:         # blocking of portfolio access resulting from an exam event in a different course. 
                    676:         if ($res =~ /\.task$/i) {
                    677:             my $encsymb = &Apache::lonenc::check_encrypt($symb);
                    678:             if ($symb eq $encsymb) {
                    679:                 $showsymb = $symb;
                    680:             }
                    681:         }
1.306     raeburn   682: 	$result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&amp;fieldname='.$env{'form.request.prefix'}.'HWPORT'.$jspart.'_'.$id.'&amp;symb='.$showsymb.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
1.265     raeburn   683: 	    &mt('Select Portfolio Files: (one or more files per submission)').'</a><br />'.
1.167     albertel  684: 	    '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
1.160     albertel  685: 	    '<br />';
1.265     raeburn   686: 
1.160     albertel  687:     }
1.265     raeburn   688:     $result.=&Apache::lonhtmlcommon::row_closure(1);
1.160     albertel  689:     return $result;
                    690: }
                    691: 
1.265     raeburn   692: sub current_file_submissions {
1.205     albertel  693:     my ($part,$id) = @_;
1.265     raeburn   694:     my $jspart=$part;
                    695:     $jspart=~s/\./_/g;
1.270     raeburn   696:     my $uploadedfile=$Apache::lonhomework::history{"resource.$part.$id.uploadedfile"};
1.265     raeburn   697:     my $portfiles=$Apache::lonhomework::history{"resource.$part.$id.portfiles"};
                    698:     return if (($uploadedfile eq '') && ($portfiles !~/[^\s]/));
1.320     raeburn   699:     my $header = &portpath_popup_js().
                    700:                  &Apache::loncommon::start_data_table().
1.270     raeburn   701:                  &Apache::loncommon::start_data_table_header_row();
                    702:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    703:         $header .= '<th>'.&mt('Delete?').'</th>';
                    704:     }
1.271     raeburn   705:     $header .=   '<th>'.&mt('File').'</th>'.
1.265     raeburn   706:                  '<th>'.&mt('Size (MB)').'</th>'.
                    707:                  '<th>'.&mt('Last Modified').'</th>'.
                    708:                  &Apache::loncommon::end_data_table_header_row();
                    709:     my (undef,$crsid,$udom,$uname)=&Apache::lonnet::whichuser();
                    710:     my ($cdom,$cnum) = ($crsid =~ /^($LONCAPA::match_domain)_($LONCAPA::match_courseid)$/);
1.271     raeburn   711:     my ($result,$header_shown,%okfiles,%rows,%legacy,@bad_file_list);
1.265     raeburn   712:     if ($uploadedfile) {
                    713:         my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
1.270     raeburn   714:         my $link = &HTML::Entities::encode($url,'<>&"');
                    715:         my ($path,$name) = ($url =~ m{^(/uploaded/\Q$udom\E/\Q$uname\E/essayresponse.*/)([^/]+)$});
1.265     raeburn   716:         my ($status,$hashref,$error) =
1.270     raeburn   717:             &current_file_info($url,$link,$name,$path);
1.265     raeburn   718:         if ($status eq 'ok') {
                    719:             push(@{$okfiles{$name}},$url);
                    720:             $rows{$url} = $hashref;
1.271     raeburn   721:             $legacy{$url} = 1;
1.265     raeburn   722:             &Apache::lonxml::extlink($url);
                    723:             &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
                    724:         } else {
                    725:             push(@bad_file_list,$error);
                    726:         }
                    727:     }
                    728:     if ($portfiles =~ /[^\s]/) {
                    729:         my $prefix = "/uploaded/$udom/$uname/portfolio";
                    730:         foreach my $file (split(/\s*,\s*/,&unescape($portfiles))) {
                    731:             my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
                    732:             my $url = $prefix.$path.$name;
                    733:             my $uploadedfile = &HTML::Entities::encode($url,'<>&"');
                    734:             my ($status,$hashref,$error) =
                    735:                 &current_file_info($url,$uploadedfile,$name,$path);
                    736:             if ($status eq 'ok') {
                    737:                 push(@{$okfiles{$name}},$url);
                    738:                 $rows{$url} = $hashref;
                    739:             } else {
                    740:                 push(@bad_file_list,$error);
                    741:             }
                    742:         }
                    743:     }
1.276     raeburn   744:     my $num = 0;
1.265     raeburn   745:     foreach my $name (sort(keys(%okfiles))) {
                    746:         if (ref($okfiles{$name}) eq 'ARRAY') {
                    747:             foreach my $url (@{$okfiles{$name}}) {
                    748:                 if (ref($rows{$url}) eq 'HASH') {
                    749:                     my $link = $rows{$url}{link};
                    750:                     my $portfile = $rows{$url}{path}.$rows{$url}{name};
                    751:                     $portfile = &HTML::Entities::encode($portfile,'<>&"');
                    752:                     if ($link) {
                    753:                         my $icon=&Apache::loncommon::icon($url);
                    754:                         unless ($header_shown) {
                    755:                             $result .= $header;
                    756:                             $header_shown = 1;
                    757:                         }
                    758:                         $result.=
1.270     raeburn   759:                             &Apache::loncommon::start_data_table_row()."\n";
                    760:                         if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    761:                             $result .=
                    762:                                  '<td valign="bottom"><input type="checkbox" name="HWFILE'.$jspart.'_'.$id.'_delete"'.
1.273     raeburn   763:                                  ' value="'.$portfile.'" id="HWFILE'.$jspart.'_'.$id.'_'.$num.'_delete" /></td>'."\n";
                    764:                             $num ++;
1.270     raeburn   765:                         }
1.320     raeburn   766:                         my $pathid = 'HWFILE'.$jspart.'_'.$id.'_'.$num.'_path';
                    767:                         my $pathidtext = $pathid.'text';
                    768:                         my ($showname,$showpath);
1.271     raeburn   769:                         if ($legacy{$url}) {
                    770:                             $showname = $name.' '.&mt('not in portfolio');
1.320     raeburn   771:                         } else {
                    772:                             $showname = $name;
                    773:                             $showpath = '<br />'. 
                    774:                                         '<span id="'.$pathidtext.'" class="LC_cusr_subheading">'.
                    775:                                         '<a href="javascript:showPortPath('."'$pathid','$pathidtext'".');" '.
                    776:                                         'class="LC_menubuttons_link">'.
                    777:                                         &mt('(Show path)').'</a></span>'.
                    778:                                         '<div id="'.$pathid.'" class="LC_dccid">'.$rows{$url}{path}.$name.
                    779: '</div>';
1.271     raeburn   780:                         }
1.270     raeburn   781:                         $result .= 
1.265     raeburn   782:                             '<td><a href="'.$link.'"><img src="'.$icon.
1.320     raeburn   783:                             '" border="0" alt="" />'.$showname.'</a>'.$showpath.'</td>'."\n".
1.265     raeburn   784:                             '<td align="right" valign="bottom">'.$rows{$url}{size}.'</td>'."\n".
                    785:                             '<td align="right" valign="bottom">'.$rows{$url}{lastmodified}.'</td>'."\n".
                    786:                             &Apache::loncommon::end_data_table_row();
                    787:                     }
                    788:                 }
                    789:             }
                    790:         }
                    791:     }
                    792:     if ($header_shown) {
1.307     raeburn   793:         $result .= &Apache::loncommon::end_data_table();
                    794:         if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    795:             $result .= '<br /><span class="LC_warning">'.
                    796:                        &mt('Exclude existing file(s) from grading by checking the "Delete?" checkbox(es) and clicking "Submit Answer"').'</span>';
                    797:         }
1.205     albertel  798:     }
                    799:     if (@bad_file_list) {
1.265     raeburn   800:         my $bad_files = '<span class="LC_filename">'.
                    801:             join('</span>, <span class="LC_filename">',@bad_file_list).
                    802:             '</span>';
                    803:         $result.='<p class="LC_error">'.
                    804:                  &mt("These file(s) don't exist: [_1]",$bad_files).
                    805:                  '</p>';
1.205     albertel  806:     }
                    807:     return $result;
1.265     raeburn   808: }
1.205     albertel  809: 
1.265     raeburn   810: sub current_file_info {
                    811:     my ($url,$uploadedfile,$name,$path) = @_;
                    812:     my ($status,$error,%info);
                    813:     my @stat = &Apache::lonnet::stat_file($url);
                    814:     if ((@stat) && ($stat[0] ne 'no_such_dir')) {
                    815:         my ($lastmod,$size);
                    816:         if ($stat[9] =~ /^\d+$/) {
                    817:             $lastmod = &Apache::lonlocal::locallocaltime($stat[9]);
                    818:         }
                    819:         $size = $stat[7]/(1024*1024);
                    820:         $size = sprintf("%.3f",$size);
                    821:         %info = (
                    822:                     link         => $uploadedfile,
                    823:                     name         => $name,
                    824:                     path         => $path,
                    825:                     size         => $size,
                    826:                     lastmodified => $lastmod,
                    827:                 );
                    828:         $status = 'ok';
                    829:     } else {
                    830:         &Apache::lonnet::logthis("bad file is $url");
                    831:         my $icon=&Apache::loncommon::icon($url);
                    832:         $error = '<a href="'.$url.'"><img src="'.$icon.
                    833:                  '" border="0" />'.$uploadedfile.'</a>';
                    834:     }
                    835:     return ($status,\%info,$error);
1.205     albertel  836: }
                    837: 
1.320     raeburn   838: sub portpath_popup_js {
                    839:     my %lt = &Apache::lonlocal::texthash(
                    840:                                           show => '(Show path)',
                    841:                                           hide => '(Hide)',
                    842:                                         );
                    843:     return <<"END";
                    844: <script type="text/javascript"> 
                    845: // <![CDATA[
                    846: 
                    847: function showPortPath(id,idtext) {
                    848:     document.getElementById(id).style.display='block';
                    849:     document.getElementById(id).style.textAlign='left';
                    850:     document.getElementById(id).style.textFace='normal';
                    851:     if (document.getElementById(idtext)) {
                    852:         document.getElementById(idtext).innerHTML ='<a href="javascript:hidePortPath(\\''+id+'\\',\\''+idtext+'\\'); '+
                    853:                                                    '"class="LC_menubuttons_link">$lt{'hide'}</a>&nbsp;';
                    854:     }
                    855:     return;
                    856: }
                    857: 
                    858: function hidePortPath(id,idtext) {
                    859:     if (document.getElementById(id)) {
                    860:         document.getElementById(id).style.display='none';
                    861:     }
                    862:     if (document.getElementById(idtext)) {
                    863:         document.getElementById(idtext).innerHTML ='<a href="javascript:showPortPath(\\''+id+'\\',\\''+idtext+'\\');" '+
                    864:                                                    'class="LC_menubuttons_link">$lt{'show'}</a>';
                    865:     }
                    866:     return;
                    867: }
                    868: 
                    869: // ]]>
                    870: </script>
                    871: 
                    872: END
                    873: }
                    874: 
1.179     albertel  875: sub valid_award {
                    876:     my ($award) =@_;
1.182     albertel  877:     foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
                    878: 			       'NO_RESPONSE',
1.179     albertel  879: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
                    880: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
                    881: 			       'UNIT_FAIL', 'NO_UNIT',
                    882: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
1.272     www       883: 			       'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT', 
                    884:                                'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT', 
1.179     albertel  885: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
1.264     raeburn   886:                                'EXCESS_FILESIZE', 'FILENAME_INUSE', 
                    887: 			       'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT', 
1.261     raeburn   888:                                'ANONYMOUS', 'ANONYMOUS_CREDIT',
                    889:                                'ASSIGNED_SCORE', 'APPROX_ANS',
                    890: 			       'EXACT_ANS','COMMA_FAIL') {
1.179     albertel  891: 	if ($award eq $possibleaward) { return 1; }
                    892:     }
                    893:     return 0;
                    894: }
                    895: 
1.207     albertel  896: {
                    897:     my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
                    898: 		  'TOO_LONG',
                    899: 		  'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
                    900: 		  'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
1.272     www       901: 		  'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',  'NOT_FUNCTION', 
                    902:                   'WRONG_FORMAT', 'INTERNAL_ERROR',
1.207     albertel  903: 		  'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
1.264     raeburn   904: 		  'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'FILENAME_INUSE', 
                    905:                   'DRAFT', 'SUBMITTED',
1.261     raeburn   906:                   'SUBMITTED_CREDIT', 'ANONYMOUS', 'ANONYMOUS_CREDIT',
1.248     raeburn   907:                   'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
1.207     albertel  908:     my $i=0;
                    909:     my %fwd_awards = map { ($_,$i++) } @awards;
                    910:     my $max=scalar(@awards);
                    911:     @awards=reverse(@awards);
1.208     albertel  912:     $i=0;
1.207     albertel  913:     my %rev_awards = map { ($_,$i++) } @awards;
                    914: 
1.232     albertel  915: sub awarddetail_to_awarded {
                    916:     my ($awarddetail) = @_;
                    917:     if ($awarddetail eq 'EXACT_ANS'
                    918: 	|| $awarddetail eq 'APPROX_ANS') {
                    919: 	return 1;
                    920:     }
                    921:     return 0;
                    922: }
                    923: 
1.233     albertel  924: sub hide_award {
                    925:     my ($award) = @_;
                    926:     if (&Apache::lonhomework::show_no_problem_status()) {
                    927: 	return 1;
                    928:     }
                    929:     if ($award =~
1.261     raeburn   930: 	/^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|SUBMITTED_CREDIT|ANONYMOUS|ANONYMOUS_CREDIT|ASSIGNED_SCORE|INCORRECT)/) {
1.233     albertel  931: 	return 1;
                    932:     }
                    933:     return 0;
                    934: }
                    935: 
1.9       albertel  936: sub finalizeawards {
1.232     albertel  937:     my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
1.207     albertel  938:     my $result;
1.136     albertel  939:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135     albertel  940:     if ($result eq '' ) {
                    941: 	my $blankcount;
1.207     albertel  942: 	foreach my $award (@$awardref) {
1.135     albertel  943: 	    if ($award eq '') {
                    944: 		$result='MISSING_ANSWER';
                    945: 		$blankcount++;
                    946: 	    }
                    947: 	}
1.232     albertel  948: 	if ($blankcount == ($#$awardref + 1)) {
                    949: 	    return ('NO_RESPONSE');
                    950: 	}
1.135     albertel  951:     }
1.251     www       952: 
                    953:     if ($Apache::lonxml::internal_error) { $result='INTERNAL_ERROR'; }
                    954: 
1.232     albertel  955:     if (!$final_scantron && defined($result)) { return ($result); }
1.181     albertel  956: 
1.232     albertel  957:     # if in scantron mode, if the award for any response is 
                    958:     # assigned score, then the part gets an assigned score
                    959:     if ($final_scantron 
                    960: 	&& grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
                    961: 	return ('ASSIGNED_SCORE');
                    962:     }
                    963: 
                    964:     # if in scantron mode, if the award for any response is 
                    965:     # correct and there are non-correct responses,
                    966:     # then the part gets an assigned score
                    967:     if ($final_scantron 
                    968: 	&& (grep { $_ eq 'EXACT_ANS' ||
                    969: 		   $_ eq 'APPROX_ANS'  } (@$awardref))
                    970: 	&& (grep { $_ ne 'EXACT_ANS' &&
                    971: 		   $_ ne 'APPROX_ANS'  } (@$awardref))) {
                    972: 	return ('ASSIGNED_SCORE');
                    973:     }
1.181     albertel  974:     # these awards are ordered from most important error through best correct
1.207     albertel  975:     my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
                    976: 
                    977:     my $best = $max;
                    978:     my $j=0;
                    979:     my $which;
                    980:     foreach my $award (@$awardref) {
                    981: 	if ($awards->{$award} < $best) {
                    982: 	    $best  = $awards->{$award};
                    983: 	    $which = $j;
                    984: 	}
                    985: 	$j++;
                    986:     }
1.232     albertel  987: 
1.207     albertel  988:     if (defined($which)) {
                    989: 	if (ref($nameref)) {
                    990: 	    return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
                    991: 	} else {
                    992: 	    return ($$awardref[$which],$$msgref[$which]);
                    993: 	}
1.135     albertel  994:     }
1.136     albertel  995:     return ('ERROR',undef);
1.9       albertel  996: }
1.207     albertel  997: }
1.9       albertel  998: 
1.10      albertel  999: sub decideoutput {
1.282     www      1000:     my ($award,$awarded,$awardmsg,$solved,$previous,$target,$nocorrect)=@_;
1.251     www      1001: 
1.135     albertel 1002:     my $message='';
                   1003:     my $button=0;
                   1004:     my $previousmsg;
1.221     albertel 1005:     my $css_class='orange';
1.148     albertel 1006:     my $added_computer_text=0;
1.221     albertel 1007:     my %possible_class =
                   1008: 	( 'correct'         => 'LC_answer_correct',
                   1009: 	  'charged_try'     => 'LC_answer_charged_try',
                   1010: 	  'not_charged_try' => 'LC_answer_not_charged_try',
                   1011: 	  'no_grade'        => 'LC_answer_no_grade',
                   1012: 	  'no_message'      => 'LC_no_message',
1.135     albertel 1013: 	  );
1.169     albertel 1014: 
1.180     albertel 1015:     my $part = $Apache::inputtags::part;
1.236     raeburn  1016:     my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
                   1017:     my $handgrade = ('yes' eq lc($tohandgrade)); 
1.282     www      1018: #
                   1019: # Should "Computer's Answer" be displayed?
                   1020: # Should not be displayed if still answerable,
                   1021: # if the problem is handgraded,
                   1022: # or if the problem does not give a correct answer
                   1023: #
1.180     albertel 1024:     
1.282     www      1025:     my $computer = ($handgrade || $nocorrect)? ''
1.203     www      1026: 	                       : " ".&mt("Computer's answer now shown above.");
1.180     albertel 1027:     &Apache::lonxml::debug("handgrade has :$handgrade:");
                   1028: 
1.135     albertel 1029:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
                   1030:     
1.194     banghart 1031:     if ($solved =~ /^correct/) {
1.221     albertel 1032:         $css_class=$possible_class{'correct'};
1.170     albertel 1033: 	$message=&mt('You are correct.');
                   1034: 	if ($awarded < 1 && $awarded > 0) {
                   1035: 	    $message=&mt('You are partially correct.');
1.221     albertel 1036: 	    $css_class=$possible_class{'not_charged_try'};
1.170     albertel 1037: 	} elsif ($awarded < 1) {
                   1038: 	    $message=&mt('Incorrect.');
1.221     albertel 1039: 	    $css_class=$possible_class{'charged_try'};
1.170     albertel 1040: 	}
1.291     www      1041: 	if ($handgrade || 
                   1042:             ($env{'request.filename'}=~/\/res\/lib\/templates\/(examupload|DropBox).problem$/)) {
1.172     albertel 1043: 	    $message = &mt("A score has been assigned.");
                   1044: 	    $added_computer_text=1;
1.135     albertel 1045: 	} else {
1.172     albertel 1046: 	    if ($target eq 'tex') {
                   1047: 		$message = '\textbf{'.$message.'}';
                   1048: 	    } else {
                   1049: 		$message = "<b>".$message."</b>";
1.180     albertel 1050: 		$message.= $computer;
1.135     albertel 1051: 	    }
1.172     albertel 1052: 	    $added_computer_text=1;
1.235     albertel 1053: 	    if ($awarded > 0) {
                   1054: 		my ($symb) = &Apache::lonnet::whichuser();
                   1055: 		if (($symb ne '') 
                   1056: 		    &&
                   1057: 		    ($env{'course.'.$env{'request.course.id'}.
1.237     www      1058: 			      '.disable_receipt_display'} ne 'yes') &&
                   1059:                     ($Apache::lonhomework::type ne 'practice')) { 
1.235     albertel 1060: 		    $message.=(($target eq 'web')?'<br />':' ').
1.256     biermanm 1061: 			&mt('Your receipt no. is [_1]',
1.235     albertel 1062: 			    (&Apache::lonnet::receipt($Apache::inputtags::part).
                   1063: 			     (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
                   1064: 		}
1.135     albertel 1065: 	    }
                   1066: 	}
1.299     raeburn  1067:         if ($awarded >= 1) {
1.296     raeburn  1068:             $button=0;
1.299     raeburn  1069:         } elsif (&Apache::lonnet::EXT("resource.$part.retrypartial") !~/^1|on|yes$/i) {
1.296     raeburn  1070:             $button=0;
1.299     raeburn  1071:         } else {
1.296     raeburn  1072:             $button=1;
                   1073:         }
1.135     albertel 1074: 	$previousmsg='';
                   1075:     } elsif ($solved =~ /^excused/) {
                   1076: 	if ($target eq 'tex') {
                   1077: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
                   1078: 	} else {
                   1079: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
                   1080: 	}
1.221     albertel 1081: 	$css_class=$possible_class{'charged_try'};
1.135     albertel 1082: 	$button=0;
                   1083: 	$previousmsg='';
                   1084:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                   1085: 	if ($solved =~ /^incorrect/ || $solved eq '') {
1.144     albertel 1086: 	    $message = &mt("Incorrect").".";
1.221     albertel 1087: 	    $css_class=$possible_class{'charged_try'};
1.135     albertel 1088: 	    $button=1;
                   1089: 	} else {
1.144     albertel 1090: 	    if ($target eq 'tex') {
                   1091: 		$message = '\textbf{'.&mt('You are correct.').'}';
                   1092: 	    } else {
                   1093: 		$message = "<b>".&mt('You are correct.')."</b>";
1.180     albertel 1094: 		$message.= $computer;
1.144     albertel 1095: 	    }
1.148     albertel 1096: 	    $added_computer_text=1;
1.235     albertel 1097: 	    if  ($awarded > 0 
                   1098: 		 && $env{'course.'.
1.165     albertel 1099: 			     $env{'request.course.id'}.
1.235     albertel 1100: 			     '.disable_receipt_display'} ne 'yes') { 
1.135     albertel 1101: 		$message.=(($target eq 'web')?'<br />':' ').
1.235     albertel 1102: 		    &mt('Your receipt is [_1]',
                   1103: 			(&Apache::lonnet::receipt($Apache::inputtags::part).
                   1104: 			 (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1.135     albertel 1105: 	    }
1.221     albertel 1106: 	    $css_class=$possible_class{'correct'};
1.135     albertel 1107: 	    $button=0;
                   1108: 	    $previousmsg='';
                   1109: 	}
                   1110:     } elsif ($award eq 'NO_RESPONSE') {
                   1111: 	$message = '';
1.221     albertel 1112: 	$css_class=$possible_class{'no_feedback'};
1.135     albertel 1113: 	$button=1;
1.182     albertel 1114:     } elsif ($award eq 'EXTRA_ANSWER') {
                   1115: 	$message = &mt('Some extra items were submitted.');
1.221     albertel 1116: 	$css_class=$possible_class{'not_charged_try'};
1.182     albertel 1117: 	$button = 1;
1.135     albertel 1118:     } elsif ($award eq 'MISSING_ANSWER') {
1.245     bisitz   1119: 	$message = &mt('Some items were not submitted.');
                   1120:         if ($target ne 'tex') {
                   1121:            $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
                   1122:         }
1.221     albertel 1123: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1124: 	$button = 1;
                   1125:     } elsif ($award eq 'ERROR') {
1.247     bisitz   1126: 	$message = &mt('An error occurred while grading your answer.');
1.221     albertel 1127: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1128: 	$button = 1;
                   1129:     } elsif ($award eq 'TOO_LONG') {
                   1130: 	$message = &mt("The submitted answer was too long.");
1.221     albertel 1131: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1132: 	$button=1;
                   1133:     } elsif ($award eq 'WANTED_NUMERIC') {
                   1134: 	$message = &mt("This question expects a numeric answer.");
1.221     albertel 1135: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1136: 	$button=1;
                   1137:     } elsif ($award eq 'MISORDERED_RANK') {
1.242     bisitz   1138:         $message = &mt('You have provided an invalid ranking.');
                   1139:         if ($target ne 'tex') {
                   1140:             $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
                   1141:         }
1.221     albertel 1142: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1143: 	$button=1;
1.248     raeburn  1144:     } elsif ($award eq 'EXCESS_FILESIZE') {
1.321     raeburn  1145:         $message = &mt("Submission won't be graded. The combined size of submitted files exceeded the amount allowed.");
1.248     raeburn  1146:         $css_class=$possible_class{'not_charged_try'};
                   1147:         $button=1;
1.264     raeburn  1148:     } elsif ($award eq 'FILENAME_INUSE') {
                   1149:         $message = &mt('You have already uploaded a file with that filename.');
                   1150:         if ($target eq 'tex') {
                   1151:             $message.= "\\\\\n";
                   1152:         } else {
                   1153:             $message .= '<br />';
                   1154:         }
1.315     bisitz   1155:         $message .= &mt('Please use a different filename.');
1.264     raeburn  1156:         $css_class=$possible_class{'not_charged_try'};
                   1157:         $button=1;
1.135     albertel 1158:     } elsif ($award eq 'INVALID_FILETYPE') {
1.255     bisitz   1159: 	$message = &mt("Submission won't be graded. The type of file submitted is not allowed.");
1.221     albertel 1160: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1161: 	$button=1;
                   1162:     } elsif ($award eq 'SIG_FAIL') {
1.145     albertel 1163: 	my ($used,$min,$max)=split(':',$awardmsg);
1.212     albertel 1164: 	my $word = ($used < $min) ? 'more' : 'fewer';
1.250     bisitz   1165: 	$message = &mt("Submission not graded. Use $word digits.",$used);
1.221     albertel 1166: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1167: 	$button=1;
1.137     albertel 1168:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
                   1169: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
                   1170: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel 1171: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel 1172: 	$button=1;
                   1173:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155     albertel 1174: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137     albertel 1175: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel 1176: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel 1177: 	$button=1;
1.140     matthew  1178:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155     albertel 1179: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136     albertel 1180: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel 1181: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1182: 	$button=1;
                   1183:     } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155     albertel 1184: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.221     albertel 1185: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1186: 	$button=1;
                   1187:     } elsif ($award eq 'NO_UNIT') {
1.144     albertel 1188: 	$message = &mt("Units required").'.';
1.135     albertel 1189: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
1.221     albertel 1190: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1191: 	$button=1;
1.153     albertel 1192:     } elsif ($award eq 'COMMA_FAIL') {
                   1193: 	$message = &mt("Proper comma separation is required").'.';
1.221     albertel 1194: 	$css_class=$possible_class{'not_charged_try'};
1.153     albertel 1195: 	$button=1;
1.135     albertel 1196:     } elsif ($award eq 'BAD_FORMULA') {
1.240     www      1197: 	$message = &mt("Unable to understand formula").'.';
                   1198:         if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
1.221     albertel 1199: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1200: 	$button=1;
1.272     www      1201:     } elsif ($award eq 'NOT_FUNCTION') {
                   1202:         $message = &mt("Not a function").'.';
                   1203:         $css_class=$possible_class{'not_charged_try'};
                   1204:         $button=1;
                   1205:     } elsif ($award eq 'WRONG_FORMAT') {
                   1206:         $message = &mt("Wrong format").'.';
                   1207:         $css_class=$possible_class{'not_charged_try'};
                   1208:         $button=1;
                   1209:      } elsif ($award eq 'INTERNAL_ERROR') {
1.251     www      1210:         $message = &mt("An internal error occurred while processing your answer. Please try again later.");
                   1211:         $css_class=$possible_class{'not_charged_try'};
                   1212:         $button=1;
1.135     albertel 1213:     } elsif ($award eq 'INCORRECT') {
1.144     albertel 1214: 	$message = &mt("Incorrect").'.';
1.221     albertel 1215: 	$css_class=$possible_class{'charged_try'};
1.135     albertel 1216: 	$button=1;
                   1217:     } elsif ($award eq 'SUBMITTED') {
                   1218: 	$message = &mt("Your submission has been recorded.");
1.221     albertel 1219: 	$css_class=$possible_class{'no_grade'};
1.135     albertel 1220: 	$button=1;
1.261     raeburn  1221:     } elsif ($award eq 'SUBMITTED_CREDIT') {
                   1222:         $message = &mt("Your submission has been recorded, and credit awarded.");
                   1223:         $css_class=$possible_class{'correct'};
                   1224:         $button=1;
                   1225:     } elsif ($award eq 'ANONYMOUS') {
                   1226:         $message = &mt("Your anonymous submission has been recorded.");
                   1227:         $css_class=$possible_class{'no_grade'};
                   1228:         $button=1;
                   1229:     } elsif ($award eq 'ANONYMOUS_CREDIT') {
                   1230:         $message = &mt("Your anonymous submission has been recorded, and credit awarded.");
                   1231:         $css_class=$possible_class{'correct'};
1.301     raeburn  1232:         $button=1;
1.135     albertel 1233:     } elsif ($award eq 'DRAFT') {
1.258     riegler  1234: 	$message = &mt("Copy saved but not submitted.");
1.221     albertel 1235: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1236: 	$button=1;
                   1237:     } elsif ($award eq 'ASSIGNED_SCORE') {
1.144     albertel 1238: 	$message = &mt("A score has been assigned.");
1.221     albertel 1239: 	$css_class=$possible_class{'correct'};
1.135     albertel 1240: 	$button=0;
1.144     albertel 1241:     } elsif ($award eq '') {
1.186     albertel 1242: 	if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
                   1243: 	    $message = &mt("Nothing submitted.");
1.221     albertel 1244: 	    $css_class=$possible_class{'charged_try'};
1.186     albertel 1245: 	} else {
1.221     albertel 1246: 	    $css_class=$possible_class{'not_charged_try'};
1.186     albertel 1247: 	}
1.144     albertel 1248: 	$button=1;
1.135     albertel 1249:     } else {
                   1250: 	$message = &mt("Unknown message").": $award";
                   1251: 	$button=1;
                   1252:     }
1.209     albertel 1253:     my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1.194     banghart 1254:     foreach my $resid(@Apache::inputtags::response){
                   1255:         if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1.254     raeburn  1256:             if ($target eq 'tex') {
                   1257:                 $message.= "\\\\\n";
                   1258:             } else {
                   1259:                 $message.='<br />';
                   1260:             }
1.198     albertel 1261: 	    my @files = split(/\s*,\s*/,
                   1262: 			      $Apache::lonhomework::history{"resource.$part.$resid.handback"});
                   1263: 	    my $file_msg;
                   1264: 	    foreach my $file (@files) {
1.254     raeburn  1265:                 if ($target eq 'tex') {
                   1266:                     $file_msg.= "\\\\\n".$file;
                   1267:                 } else {
                   1268:                     $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
                   1269:                 }
1.198     albertel 1270: 	    }
                   1271: 	    $message .= &mt('Returned file(s): [_1]',$file_msg);
1.254     raeburn  1272:             if ($target eq 'tex') {
                   1273:                 $message.= "\\\\\n";
                   1274:             } else {
                   1275:                 $message.='<br />';
                   1276:             }
1.198     albertel 1277: 	}
1.194     banghart 1278:     }
                   1279: 
1.233     albertel 1280:     if (&Apache::lonhomework::hide_problem_status()
                   1281: 	&& $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
                   1282: 	&& &hide_award($award)) {
1.304     raeburn  1283:         $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
                   1284:         my @interval= &Apache::lonnet::EXT("resource.$part.interval");
                   1285:         if ($interval[0] =~ /\d+/) {
                   1286:             my $first_access=&Apache::lonnet::get_first_access($interval[1]);
                   1287:             if (defined($first_access)) {
                   1288:                 my $due_date= &Apache::lonnet::EXT("resource.$part.duedate");
                   1289:                 unless (($due_date) && ($due_date < $first_access + $interval[0])) { 
                   1290:                     $message = &mt("Answer Submitted: Your final submission will be graded when the time limit is reached.");
                   1291:                 }
                   1292:             }
                   1293:         }
1.221     albertel 1294: 	$css_class=$possible_class{'no_grade'};
1.135     albertel 1295: 	$button=1;
                   1296:     }
1.148     albertel 1297:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
1.150     albertel 1298: 	!$added_computer_text && $target ne 'tex') {
1.180     albertel 1299: 	$message.= $computer;
1.148     albertel 1300: 	$added_computer_text=1;
1.144     albertel 1301:     }
1.237     www      1302:     if ($Apache::lonhomework::type eq 'practice') {
1.244     raeburn  1303:        if ($target eq 'web') {
                   1304:            $message .= '<br />';
                   1305:        } else {
                   1306:            $message .= ' ';      
                   1307:        }
                   1308:        $message.=&mt('Submissions to practice problems are not permanently recorded.');
1.237     www      1309:     }
1.221     albertel 1310:     return ($button,$css_class,$message,$previousmsg);
1.12      albertel 1311: }
                   1312: 
1.155     albertel 1313: sub markup_unit {
                   1314:     my ($unit,$target)=@_;
                   1315:     if ($target eq 'tex') {
                   1316: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
                   1317:     } else {
                   1318: 	return "<tt>".$unit."</tt>";
                   1319:     }
                   1320: }
                   1321: 
1.88      albertel 1322: sub removealldata {
1.87      albertel 1323:     my ($id)=@_;
                   1324:     foreach my $key (keys(%Apache::lonhomework::results)) {
                   1325: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                   1326: 	    &Apache::lonxml::debug("Removing $key");
                   1327: 	    delete($Apache::lonhomework::results{$key});
                   1328: 	}
                   1329:     }
                   1330: }
                   1331: 
1.142     albertel 1332: sub hidealldata {
                   1333:     my ($id)=@_;
                   1334:     foreach my $key (keys(%Apache::lonhomework::results)) {
                   1335: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                   1336: 	    &Apache::lonxml::debug("Hidding $key");
                   1337: 	    my $newkey=$key;
                   1338: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
                   1339: 	    $Apache::lonhomework::results{$newkey}=
                   1340: 		$Apache::lonhomework::results{$key};
                   1341: 	    delete($Apache::lonhomework::results{$key});
                   1342: 	}
                   1343:     }
                   1344: }
                   1345: 
1.12      albertel 1346: sub setgradedata {
1.136     albertel 1347:     my ($award,$msg,$id,$previously_used) = @_;
1.154     albertel 1348:     if ($Apache::lonhomework::scantronmode && 
1.165     albertel 1349: 	&Apache::lonnet::validCODE($env{'form.CODE'})) {
                   1350: 	$Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154     albertel 1351:     } elsif ($Apache::lonhomework::scantronmode && 
1.165     albertel 1352: 	     $env{'form.CODE'} eq '' &&
1.154     albertel 1353: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
                   1354: 	$Apache::lonhomework::results{"resource.CODE"}='';
1.141     albertel 1355:     }
1.154     albertel 1356: 
1.135     albertel 1357:     if (!$Apache::lonhomework::scantronmode &&
                   1358: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                   1359: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                   1360: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87      albertel 1361: 	return '';
1.284     www      1362:     } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} < 1
1.233     albertel 1363: 	      || $Apache::lonhomework::scantronmode 
                   1364: 	      || &Apache::lonhomework::hide_problem_status()  ) {
1.154     albertel 1365:         # the student doesn't already have it correct,
                   1366: 	# or we are in a mode (scantron orno problem status) where a correct 
                   1367:         # can become incorrect
                   1368: 	# handle assignment of tries and solved status
1.135     albertel 1369: 	my $solvemsg;
                   1370: 	if ($Apache::lonhomework::scantronmode) {
                   1371: 	    $solvemsg='correct_by_scantron';
                   1372: 	} else {
                   1373: 	    $solvemsg='correct_by_student';
                   1374: 	}
                   1375: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                   1376: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                   1377: 	}
                   1378: 	if ( $award eq 'ASSIGNED_SCORE') {
                   1379: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1380: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1381: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1382: 		$solvemsg;
                   1383: 	    my $numawards=scalar(@Apache::inputtags::response);
                   1384: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1385: 	    foreach my $res (@Apache::inputtags::response) {
1.232     albertel 1386: 		if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
                   1387: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                   1388: 			$Apache::lonhomework::results{"resource.$id.$res.awarded"};
                   1389: 		} else {
                   1390: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                   1391: 			&awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
                   1392: 		}
1.135     albertel 1393: 	    }
                   1394: 	    if ($numawards > 0) {
                   1395: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
                   1396: 		    $numawards;
                   1397: 	    }
                   1398: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                   1399: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1400: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1401: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1402: 		$solvemsg;
                   1403: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1.261     raeburn  1404:         } elsif ( $award eq 'SUBMITTED_CREDIT' ) {
                   1405:             $Apache::lonhomework::results{"resource.$id.tries"} =
                   1406:                 $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1407:             $Apache::lonhomework::results{"resource.$id.solved"} =
                   1408:                 'credit_attempted';
                   1409:             $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                   1410:         }  elsif ( $award eq 'ANONYMOUS_CREDIT' ) {
                   1411:             $Apache::lonhomework::results{"resource.$id.tries"} =
                   1412:                 $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1413:             $Apache::lonhomework::results{"resource.$id.solved"} =
                   1414:                 'credit_attempted';
                   1415:             $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1.135     albertel 1416: 	} elsif ( $award eq 'INCORRECT' ) {
                   1417: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1418: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.233     albertel 1419: 	    if (&Apache::lonhomework::hide_problem_status()
                   1420: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1421: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1422: 	    }
                   1423: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1424: 		'incorrect_attempted';
                   1425: 	} elsif ( $award eq 'SUBMITTED' ) {
                   1426: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1427: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1428: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1429: 		'ungraded_attempted';
1.261     raeburn  1430:         }  elsif ( $award eq 'ANONYMOUS' ) {
                   1431:             $Apache::lonhomework::results{"resource.$id.tries"} =
                   1432:                 $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1433:             $Apache::lonhomework::results{"resource.$id.solved"} =
                   1434:                 'ungraded_attempted';
1.135     albertel 1435: 	} elsif ( $award eq 'DRAFT' ) {
                   1436: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
                   1437: 	} elsif ( $award eq 'NO_RESPONSE' ) {
                   1438: 	    #no real response so delete any data that got stored
1.129     albertel 1439: 	    &removealldata($id);
                   1440: 	    return '';
                   1441: 	} else {
1.135     albertel 1442: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1443: 		'incorrect_attempted';
1.233     albertel 1444: 	    if (&Apache::lonhomework::show_no_problem_status()
                   1445: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1446: 		$Apache::lonhomework::results{"resource.$id.tries"} =
                   1447: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1448: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1449: 	    }
1.233     albertel 1450: 
                   1451: 	    if (&Apache::lonhomework::show_some_problem_status()) {
                   1452: 		# clear out the awarded if they had gotten it wrong/right
                   1453: 		# and are now in an error mode	
                   1454: 		$Apache::lonhomework::results{"resource.$id.awarded"} = '';
                   1455: 	    }
1.135     albertel 1456: 	}
1.136     albertel 1457: 	if (defined($msg)) {
                   1458: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
                   1459: 	}
1.135     albertel 1460: 	# did either of the overall awards chage? If so ignore the 
                   1461: 	# previous check
                   1462: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
                   1463: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
                   1464: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
                   1465: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
                   1466: 	    # check if this was a previous submission if it was delete the
                   1467: 	    # unneeded data and update the previously_used attribute
                   1468: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
1.233     albertel 1469: 		if (&Apache::lonhomework::show_problem_status()) {
1.135     albertel 1470: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
                   1471: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                   1472: 		}
                   1473: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
                   1474: 		#delete all data as they student didn't do anything, but save
                   1475: 		#the list of collaborators.
                   1476: 		&removealldata($id);
                   1477: 		#and since they didn't do anything we were never here
                   1478: 		return '';
                   1479: 	    } else {
                   1480: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
                   1481: 	    }
1.101     albertel 1482: 	}
1.283     www      1483:     } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} == 1 ) {
1.135     albertel 1484: 	#delete all data as they student already has it correct
                   1485: 	&removealldata($id);
                   1486: 	#and since they didn't do anything we were never here
                   1487: 	return '';
1.40      albertel 1488:     }
1.135     albertel 1489:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.184     albertel 1490:     if ($award eq 'SUBMITTED') {
                   1491: 	&Apache::response::add_to_gradingqueue();
                   1492:     }
1.288     www      1493:     $Apache::lonhomework::results{"resource.$id.type"} = $Apache::lonhomework::type;
1.289     www      1494:     $Apache::lonhomework::results{"resource.$id.duedate"} = &Apache::lonnet::EXT("resource.$id.duedate");
                   1495:     $Apache::lonhomework::results{"resource.$id.hinttries"} = &Apache::lonnet::EXT("resource.$id.hinttries");
1.290     www      1496:     $Apache::lonhomework::results{"resourse.$id.version"} = &Apache::lonnet::usedversion(); 
1.10      albertel 1497: }
                   1498: 
1.219     albertel 1499: sub find_which_previous {
                   1500:     my ($version) = @_;
                   1501:     my $part = $Apache::inputtags::part;
                   1502:     my (@previous_version);
                   1503:     foreach my $resp (@Apache::inputtags::response) {
                   1504: 	my $key = "$version:resource.$part.$resp.submission";
                   1505: 	my $submission = $Apache::lonhomework::history{$key};
                   1506: 	my %previous = &Apache::response::check_for_previous($submission,
                   1507: 							     $part,$resp,
                   1508: 							     $version);
                   1509: 	push(@previous_version,$previous{'version'});
                   1510:     }
                   1511:     return &previous_match(\@previous_version,
                   1512: 			   scalar(@Apache::inputtags::response));
                   1513: }
                   1514: 
                   1515: sub previous_match {
                   1516:     my ($previous_array,$count) = @_;
                   1517:     my $match = 0;
                   1518:     my @matches;
                   1519:     foreach my $versionar (@$previous_array) {
                   1520: 	foreach my $version (@$versionar) {
                   1521: 	    $matches[$version]++;
                   1522: 	}
                   1523:     }
                   1524:     my $which=0;
                   1525:     foreach my $elem (@matches) {
                   1526: 	if ($elem eq $count) {
                   1527: 	    $match=1;
                   1528: 	    last;
                   1529: 	}
                   1530: 	$which++;
                   1531:     }
                   1532:     return ($match,$which);
                   1533: }
                   1534: 
1.9       albertel 1535: sub grade {
1.135     albertel 1536:     my ($target) = @_;
                   1537:     my $id = $Apache::inputtags::part;
                   1538:     my $response='';
1.165     albertel 1539:     if ( defined $env{'form.submitted'}) {
1.136     albertel 1540: 	my (@awards,@msgs);
1.135     albertel 1541: 	foreach $response (@Apache::inputtags::response) {
                   1542: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                   1543: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
                   1544: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
                   1545: 	    push (@awards,$value);
1.136     albertel 1546: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
                   1547: 	    &Apache::lonxml::debug("got message $value from $response for $id");
                   1548: 	    push (@msgs,$value);
1.135     albertel 1549: 	}
1.232     albertel 1550: 	my ($finalaward,$msg) = 
                   1551: 	    &finalizeawards(\@awards,\@msgs,undef,undef,
                   1552: 			    $Apache::lonhomework::scantronmode);
1.135     albertel 1553: 	my $previously_used;
                   1554: 	if ( $#Apache::inputtags::previous eq $#awards ) {
1.219     albertel 1555: 	    my ($match) =
                   1556: 		&previous_match(\@Apache::inputtags::previous_version,
                   1557: 				scalar(@Apache::inputtags::response));
1.244     raeburn  1558: 
1.135     albertel 1559: 	    if ($match) {
                   1560: 		$previously_used = 'PREVIOUSLY_LAST';
                   1561: 		foreach my $value (@Apache::inputtags::previous) {
                   1562: 		    if ($value eq 'PREVIOUSLY_USED' ) {
                   1563: 			$previously_used = $value;
                   1564: 			last;
                   1565: 		    }
1.75      albertel 1566: 		}
                   1567: 	    }
1.43      albertel 1568: 	}
1.136     albertel 1569: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
                   1570: 	&setgradedata($finalaward,$msg,$id,$previously_used);
1.43      albertel 1571:     }
1.135     albertel 1572:     return '';
1.1       albertel 1573: }
                   1574: 
1.217     albertel 1575: sub get_grade_messages {
1.282     www      1576:     my ($id,$prefix,$target,$status,$nocorrect) = @_;
                   1577: # nocorrect suppresses "Computer's answer now shown above"
1.217     albertel 1578:     my ($message,$latemessage,$trystr,$previousmsg);
                   1579:     my $showbutton = 1;
                   1580: 
                   1581:     my $award = $Apache::lonhomework::history{"$prefix.award"};
                   1582:     my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
                   1583:     my $solved = $Apache::lonhomework::history{"$prefix.solved"};
                   1584:     my $previous = $Apache::lonhomework::history{"$prefix.previous"};
                   1585:     my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
                   1586:     &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
                   1587:     if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
                   1588: 	&Apache::lonxml::debug('Getting message');
1.221     albertel 1589: 	($showbutton,my $css_class,$message,$previousmsg) =
1.217     albertel 1590: 	    &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1.282     www      1591: 			  $target,(($status eq 'CAN_ANSWER') || $nocorrect));
1.217     albertel 1592: 	if ($target eq 'tex') {
                   1593: 	    $message='\vskip 2 mm '.$message.' ';
                   1594: 	} else {
1.221     albertel 1595: 	    $message="<td class=\"$css_class\">$message</td>";
1.217     albertel 1596: 	    if ($previousmsg) {
1.221     albertel 1597: 		$previousmsg="<td class=\"LC_answer_previous\">$previousmsg</td>";
1.217     albertel 1598: 	    }
                   1599: 	}
                   1600:     }
                   1601:     my $tries = $Apache::lonhomework::history{"$prefix.tries"};
                   1602:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1603:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                   1604:     #if tries are set to negative turn off the Tries/Button and messages
                   1605:     if (defined($maxtries) && $maxtries < 0) { return ''; }
                   1606:     if ( $tries eq '' ) { $tries = '0'; }
                   1607:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                   1608:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
1.292     christia 1609:     my $tries_text= &get_tries_text();
1.217     albertel 1610:     if ($showbutton) {
                   1611: 	if ($target eq 'tex') {
                   1612: 	    if ($env{'request.state'} ne "construct"
                   1613: 		&& $Apache::lonhomework::type ne 'exam'
                   1614: 		&& $env{'form.suppress_tries'} ne 'yes') {
1.292     christia 1615: 		$trystr ='{\vskip 1 mm \small '
                   1616:                         .&mt('[_1]'.$tries_text.'[_2] [_3]'
                   1617: 				,'\textit{','}',$tries.'/'.$maxtries ) 
                   1618:                         .'} \vskip 2 mm';
1.217     albertel 1619: 	    } else {
                   1620: 		$trystr = '\vskip 0 mm ';
                   1621: 	    }
                   1622: 	} else {
1.292     christia 1623: 	    my $trial =$tries;
1.217     albertel 1624: 	    if ($Apache::lonhomework::parsing_a_task) {
                   1625: 	    } elsif($env{'request.state'} ne 'construct') {
1.292     christia 1626: 		$trial.="/".&Apache::lonhtmlcommon::direct_parm_link($maxtries,$env{'request.symb'},'maxtries',$id,$target);
1.217     albertel 1627: 	    } else {
                   1628: 		if (defined($Apache::inputtags::params{'maxtries'})) {
1.292     christia 1629: 		    $trial.="/".$Apache::inputtags::params{'maxtries'};
1.217     albertel 1630: 		}
                   1631: 	    }
1.295     raeburn  1632: 	    $trystr = '<td><span class="LC_nobreak">'.&mt($tries_text.' [_1]',$trial).'</span></td>';
1.217     albertel 1633: 	}
                   1634:     }
1.221     albertel 1635: 
1.217     albertel 1636:     if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
                   1637: 	#last submissions was after due date
                   1638: 	$latemessage=&mt(' The last submission was after the Due Date ');;
                   1639: 	if ($target eq 'web') {
1.221     albertel 1640: 	    $latemessage='<td class="LC_answer_late">'.$latemessage.'</td>';
1.217     albertel 1641: 	}
                   1642:     }
                   1643:     return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
                   1644: }
                   1645: 
1.11      albertel 1646: sub gradestatus {
1.223     albertel 1647:     my ($id,$target,$no_previous) = @_;
1.135     albertel 1648:     my $showbutton = 1;
                   1649:     my $message = '';
                   1650:     my $latemessage = '';
                   1651:     my $trystr='';
                   1652:     my $button='';
                   1653:     my $previousmsg='';
                   1654: 
                   1655:     my $status = $Apache::inputtags::status['-1'];
                   1656:     &Apache::lonxml::debug("gradestatus has :$status:");
1.183     albertel 1657:     if ( $status ne 'CLOSED' 
                   1658: 	 && $status ne 'UNAVAILABLE' 
                   1659: 	 && $status ne 'INVALID_ACCESS' 
                   1660: 	 && $status ne 'NEEDS_CHECKIN' 
1.300     raeburn  1661: 	 && $status ne 'NOT_IN_A_SLOT'
                   1662:          && $status ne 'RESERVABLE'
                   1663:          && $status ne 'RESERVABLE_LATER'
                   1664:          && $status ne 'NOTRESERVABLE') {
1.217     albertel 1665: 
1.305     raeburn  1666: 	if ($status eq 'SHOW_ANSWER') {
                   1667:             $showbutton = 0;
                   1668:         }
                   1669: 
1.217     albertel 1670: 	($previousmsg,$latemessage,$message,$trystr) =
                   1671: 	    &get_grade_messages($id,"resource.$id",$target,$status,
                   1672: 				$showbutton);
1.305     raeburn  1673: 	if ($status eq 'CANNOT_ANSWER') {
1.217     albertel 1674: 	    $showbutton = 0;
1.164     albertel 1675: 	}
1.218     albertel 1676: 	if ( $status eq 'SHOW_ANSWER') {
                   1677: 	    undef($previousmsg);
                   1678: 	}
1.135     albertel 1679: 	if ( $showbutton ) { 
                   1680: 	    if ($target ne 'tex') {
1.230     albertel 1681: 		$button = 
1.262     www      1682:             '<input onmouseup="javascript:setSubmittedPart(\''.$id.'\');this.form.action+=\'#'.&escape($id).'\';"
1.260     droeschl 1683:                     type="submit" name="submit_'.$id.'"
                   1684:                     value="'.&mt('Submit Answer').'" />';
1.135     albertel 1685: 	    }
                   1686: 	}
1.217     albertel 1687: 
1.135     albertel 1688:     }
                   1689:     my $output= $previousmsg.$latemessage.$message.$trystr;
                   1690:     if ($output =~ /^\s*$/) {
                   1691: 	return $button;
1.63      sakharuk 1692:     } else {
1.135     albertel 1693: 	if ($target eq 'tex') {
                   1694: 	    return $button.' \vskip 0 mm '.$output.' ';
                   1695: 	} else {
1.223     albertel 1696: 	    $output =
                   1697: 		'<table><tr><td>'.$button.'</td>'.$output;
                   1698: 	    if (!$no_previous) {
                   1699: 		$output.='<td>'.&previous_tries($id,$target).'</td>';
                   1700: 	    }
                   1701: 	    $output.= '</tr></table>';
                   1702: 	    return $output;
1.135     albertel 1703: 	}
1.63      sakharuk 1704:     }
1.11      albertel 1705: }
1.217     albertel 1706: 
                   1707: sub previous_tries {
                   1708:     my ($id,$target) = @_;
                   1709:     my $output;
                   1710:     my $status = $Apache::inputtags::status['-1'];
1.219     albertel 1711: 
                   1712:     my $count;
                   1713:     my %count_lookup;
1.274     raeburn  1714:     my $lastrndseed;
1.318     raeburn  1715:     my $numstamps = 0;
1.219     albertel 1716: 
1.217     albertel 1717:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                   1718: 	my $prefix = $i.":resource.$id";
1.263     raeburn  1719:         my $is_anon; 
                   1720:         if (defined($env{'form.grade_symb'})) {
                   1721:             if (($Apache::lonhomework::history{"$prefix.type"} eq 'anonsurvey') || 
                   1722:                 ($Apache::lonhomework::history{"$prefix.type"} eq 'anonsurveycred')) {
                   1723:                 $is_anon = 1;
                   1724:             }
                   1725:         }
1.217     albertel 1726: 	next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1.219     albertel 1727: 	$count++;
                   1728: 	$count_lookup{$i} = $count;
1.274     raeburn  1729:         my $curr_rndseed = $Apache::lonhomework::history{"$prefix.rndseed"};
1.217     albertel 1730: 	my ($previousmsg,$latemessage,$message,$trystr);
                   1731: 
                   1732: 	($previousmsg,$latemessage,$message,$trystr) =
                   1733: 	    &get_grade_messages($id,"$prefix",$target,$status);
                   1734: 
1.219     albertel 1735: 	if ($previousmsg ne '') {
                   1736: 	    my ($match,$which) = &find_which_previous($i);
                   1737: 	    $message=$previousmsg;
                   1738: 	    my $previous = $count_lookup{$which};
1.226     albertel 1739: 	    $message =~ s{(</td>)}{ as submission \# $previous $1};
1.221     albertel 1740: 	} elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1.233     albertel 1741: 	    if (!(&Apache::lonhomework::hide_problem_status()
1.225     albertel 1742: 		  && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
                   1743: 		&& $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
                   1744: 		) {
                   1745: 		
1.238     bisitz   1746:                 my $txt_correct = &mt('Correct');
1.279     raeburn  1747:                 my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
                   1748:                 if ($awarded < 1 && $awarded > 0) {
1.280     raeburn  1749:                     $txt_correct=&mt('Partially Correct');
1.279     raeburn  1750:                 } elsif ($awarded < 1) {
                   1751:                     if ($awarded eq '') {
                   1752:                         $txt_correct='';
                   1753:                     } else {
                   1754:                         $txt_correct=&mt('Incorrect');
                   1755:                     }
                   1756:                 }
1.221     albertel 1757: 		$message =~ s{(<td.*?>)(.*?)(</td>)}
1.238     bisitz   1758:                              {$1 <strong>$txt_correct</strong>. $3}s;
1.221     albertel 1759: 	    }
1.238     bisitz   1760:             my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1.294     raeburn  1761:             if (($curr_rndseed || $lastrndseed) && ($i > 1)) {
1.274     raeburn  1762:                 if ($curr_rndseed ne $lastrndseed) {
                   1763:                     $trystr .= '<br /><span style="color: green; white-space: nowrap; font-style: italic; font-weight: bold; font-size: 80%;">'.&mt('New problem variation this try.').'</span>';
                   1764:                 }
                   1765:             } 
1.221     albertel 1766: 	    $message =~ s{(</td>)}{ $trystr $1};
1.219     albertel 1767: 	}
1.221     albertel 1768: 	my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
                   1769: 	$message =~ s{(<td.*?>)}{<td>};
                   1770: 	
1.219     albertel 1771: 
1.318     raeburn  1772: 	$output .= '<tr class="'.$class.'">'.
                   1773: 	           '<td align="center">'.$count.'</td>'.$message;
                   1774:         if ((!$is_anon) && ($Apache::lonhomework::history{"$prefix.tries"}) &&
                   1775:             ($Apache::lonhomework::history{"$prefix.award"} ne 'ASSIGNED_SCORE') &&
                   1776:             ($Apache::lonhomework::history{$i.':timestamp'})) {
                   1777:             $output .= '<td>'.&Apache::lonlocal::locallocaltime(
                   1778:                              $Apache::lonhomework::history{$i.':timestamp'}).'</td>';
                   1779:             $numstamps ++;
                   1780:         } else {
                   1781:             $output .= '<td></td>';
                   1782:         }
1.217     albertel 1783: 	foreach my $resid (@Apache::inputtags::response) {
                   1784: 	    my $prefix = $prefix.".$resid";
                   1785: 	    if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
                   1786: 		my $submission =
                   1787: 		    $Apache::inputtags::submission_display{"$prefix.submission"};
                   1788: 		if (!defined($submission)) {
                   1789: 		    $submission = 
                   1790: 			$Apache::lonhomework::history{"$prefix.submission"};
                   1791: 		}
1.263     raeburn  1792:                 if ($is_anon) {
                   1793:                     $output.='<td>'.&mt('(only shown to submitter)').'</td>';
                   1794:                 } else {
                   1795: 		    $output.='<td>'.$submission.'</td>';
                   1796:                 }
1.217     albertel 1797: 	    } else {
                   1798: 		$output.='<td></td>';
                   1799: 	    }
                   1800: 	}
1.221     albertel 1801: 	$output.=&Apache::loncommon::end_data_table_row()."\n";
1.274     raeburn  1802:         $lastrndseed = $curr_rndseed;
1.217     albertel 1803:     }
                   1804:     return if ($output eq '');
1.318     raeburn  1805:     my $headers = '<tr>'.
                   1806:                   '<th>'.&mt('Submission #').'</th>'.
                   1807:                   '<th>'.&mt('Try').'</th><th>';
                   1808:     if ($numstamps) {
                   1809:         $headers .= &mt('When');
                   1810:     }
                   1811:     $headers .= '</th>';
                   1812:     my $colspan = scalar(@Apache::inputtags::response);
                   1813:     if ($colspan > 1) {
                   1814:         $headers .= '<th colspan="'.$colspan.'">';
                   1815:     } else {
                   1816:         $headers .= '<th>';
                   1817:     }
                   1818:     $headers .= &mt('Submitted Answer').'</th></tr>';
1.298     www      1819:     $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1.226     albertel 1820: 
1.263     raeburn  1821:     my $tries_text = &get_tries_text('link');
1.231     albertel 1822:     my $prefix = $env{'form.request.prefix'};
                   1823:     $prefix =~ tr{.}{_};
                   1824:     my $function_name = "LONCAPA_previous_tries_".$prefix.
1.234     albertel 1825: 	$Apache::lonxml::curdepth.'_'.$env{'form.counter'};
1.298     www      1826:     my $result = &Apache::loncommon::modal_adhoc_window($function_name,420,410,$output,&mt($tries_text))."<br />";
1.217     albertel 1827:     return $result;
                   1828: }
                   1829: 
1.263     raeburn  1830: sub get_tries_text {
                   1831:     my ($context) = @_;
                   1832:     my $tries_text;
                   1833:     if ($context eq 'link') {
                   1834:         $tries_text = 'Previous Tries';
                   1835:     } else {
                   1836:         $tries_text = 'Tries';
                   1837:     }
                   1838:     if ( $Apache::lonhomework::type eq 'survey' ||
                   1839:          $Apache::lonhomework::type eq 'surveycred' ||
                   1840:          $Apache::lonhomework::type eq 'anonsurvey' ||
                   1841:          $Apache::lonhomework::type eq 'anonsurveycred' ||
                   1842:          $Apache::lonhomework::parsing_a_task) {
                   1843:         if ($context eq 'link') {
                   1844:             $tries_text = 'Previous Submissions';
                   1845:         } else {
                   1846:             $tries_text = 'Submissions';
                   1847:         }
                   1848:     }
                   1849:     return $tries_text;
                   1850: }
                   1851: 
1.314     raeburn  1852: sub spelling_languages {
                   1853:     my %langchoices;
                   1854:     foreach my $id (&Apache::loncommon::languageids()) {
                   1855:         my $code = &Apache::loncommon::supportedlanguagecode($id);
                   1856:         if ($code ne '') {
                   1857:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
                   1858:         }
                   1859:     }
                   1860:     my @spelllangs = ('none');
                   1861:     foreach my $code ('en','de','he','es','fr','pt','tr') {
                   1862:         push(@spelllangs,[$code,$langchoices{$code}]);
                   1863:     }
                   1864:     return \@spelllangs;
                   1865: }
                   1866: 
1.1       albertel 1867: 1;
                   1868: __END__
1.249     jms      1869: 
                   1870: =pod
                   1871: 
                   1872: =back
                   1873: 
                   1874: =cut
1.43      albertel 1875:  

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