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

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

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