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

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

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