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

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

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