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

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

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