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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.260.4.2! raeburn     4: # $Id: inputtags.pm,v 1.260.4.1 2009/12/06 21:00:15 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.260.4.2! 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.260.4.2! 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.260.4.2! 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:     }
                    511:     if ( $which eq 'both') { 
                    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.260.4.2! 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.260.4.2! raeburn   519: 
1.160     albertel  520:     }
1.260.4.2! raeburn   521:     $result.=&Apache::lonhtmlcommon::row_closure(1);
1.160     albertel  522:     return $result;
                    523: }
                    524: 
1.260.4.2! raeburn   525: sub current_file_submissions {
1.205     albertel  526:     my ($part,$id) = @_;
1.260.4.2! raeburn   527:     my $jspart=$part;
        !           528:     $jspart=~s/\./_/g;
        !           529:     my $uploadedfile=$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:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
        !           535:         $header .= '<th>'.&mt('Delete?').'</th>';
        !           536:     }
        !           537:     $header .=   '<th>'.&mt('File').'</th>'.
        !           538:                  '<th>'.&mt('Size (MB)').'</th>'.
        !           539:                  '<th>'.&mt('Last Modified').'</th>'.
        !           540:                  &Apache::loncommon::end_data_table_header_row();
        !           541:     my (undef,$crsid,$udom,$uname)=&Apache::lonnet::whichuser();
        !           542:     my ($cdom,$cnum) = ($crsid =~ /^($LONCAPA::match_domain)_($LONCAPA::match_courseid)$/);
        !           543:     my ($result,$header_shown,%okfiles,%rows,%legacy,@bad_file_list);
        !           544:     if ($uploadedfile) {
        !           545:         my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
        !           546:         my $link = &HTML::Entities::encode($url,'<>&"');
        !           547:         my ($path,$name) = ($url =~ m{^(/uploaded/\Q$udom\E/\Q$uname\E/essayresponse.*/)([^/]+)$});
        !           548:         my ($status,$hashref,$error) =
        !           549:             &current_file_info($url,$link,$name,$path);
        !           550:         if ($status eq 'ok') {
        !           551:             push(@{$okfiles{$name}},$url);
        !           552:             $rows{$url} = $hashref;
        !           553:             $legacy{$url} = 1;
        !           554:             &Apache::lonxml::extlink($url);
        !           555:             &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
        !           556:         } else {
        !           557:             push(@bad_file_list,$error);
        !           558:         }
        !           559:     }
        !           560:     if ($portfiles =~ /[^\s]/) {
        !           561:         my $prefix = "/uploaded/$udom/$uname/portfolio";
        !           562:         foreach my $file (split(/\s*,\s*/,&unescape($portfiles))) {
        !           563:             my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
        !           564:             my $url = $prefix.$path.$name;
        !           565:             my $uploadedfile = &HTML::Entities::encode($url,'<>&"');
        !           566:             my ($status,$hashref,$error) =
        !           567:                 &current_file_info($url,$uploadedfile,$name,$path);
        !           568:             if ($status eq 'ok') {
        !           569:                 push(@{$okfiles{$name}},$url);
        !           570:                 $rows{$url} = $hashref;
        !           571:             } else {
        !           572:                 push(@bad_file_list,$error);
        !           573:             }
        !           574:         }
        !           575:     }
        !           576:     foreach my $name (sort(keys(%okfiles))) {
        !           577:         if (ref($okfiles{$name}) eq 'ARRAY') {
        !           578:             foreach my $url (@{$okfiles{$name}}) {
        !           579:                 if (ref($rows{$url}) eq 'HASH') {
        !           580:                     my $link = $rows{$url}{link};
        !           581:                     my $portfile = $rows{$url}{path}.$rows{$url}{name};
        !           582:                     $portfile = &HTML::Entities::encode($portfile,'<>&"');
        !           583:                     if ($link) {
        !           584:                         my $icon=&Apache::loncommon::icon($url);
        !           585:                         unless ($header_shown) {
        !           586:                             $result .= $header;
        !           587:                             $header_shown = 1;
        !           588:                         }
        !           589:                         $result.=
        !           590:                             &Apache::loncommon::start_data_table_row()."\n";
        !           591:                         if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
        !           592:                             $result .=
        !           593:                                  '<td valign="bottom"><input type="checkbox" name="HWFILE'.$jspart.'_'.$id.'_delete"'.
        !           594:                                  ' value="'.$portfile.'" /></td>'."\n";
        !           595:                         }
        !           596:                         my $showname = $rows{$url}{path}.$name;
        !           597:                         if ($legacy{$url}) {
        !           598:                             $showname = $name.' '.&mt('not in portfolio');
        !           599:                         }
        !           600:                         if ($env{'request.uri'} eq '/res/gci/gci/internal/submission.problem') {
        !           601:                             $showname = $name;
        !           602:                         }
        !           603:                         $result .=
        !           604:                             '<td><a href="'.$link.'"><img src="'.$icon.
        !           605:                             '" border="0" />'.$showname.'</a></td>'."\n".
        !           606:                             '<td align="right" valign="bottom">'.$rows{$url}{size}.'</td>'."\n".
        !           607:                             '<td align="right" valign="bottom">'.$rows{$url}{lastmodified}.'</td>'."\n".
        !           608:                             &Apache::loncommon::end_data_table_row();
        !           609:                     }
        !           610:                 }
        !           611:             }
        !           612:         }
        !           613:     }
        !           614:     if ($header_shown) {
        !           615:         $result .= &Apache::loncommon::end_data_table();
1.205     albertel  616:     }
                    617:     if (@bad_file_list) {
1.260.4.2! raeburn   618:        my $bad_files = '<span class="LC_filename">'.
        !           619:            join('</span>, <span class="LC_filename">',@bad_file_list).
        !           620:            '</span>';
        !           621:        $result.='<p class="LC_error">'.
        !           622:                 &mt("These file(s) don't exist: [_1]",$bad_files).
        !           623:                 '</p>';
1.205     albertel  624:     }
                    625:     return $result;
1.260.4.2! raeburn   626: }
1.205     albertel  627: 
1.260.4.2! raeburn   628: sub current_file_info {
        !           629:     my ($url,$uploadedfile,$name,$path) = @_;
        !           630:     my ($status,$error,%info);
        !           631:     my @stat = &Apache::lonnet::stat_file($url);
        !           632:     if ((@stat) && ($stat[0] ne 'no_such_dir')) {
        !           633:         my ($lastmod,$size);
        !           634:         if ($stat[9] =~ /^\d+$/) {
        !           635:             $lastmod = &Apache::lonlocal::locallocaltime($stat[9]);
        !           636:         }
        !           637:         $size = $stat[7]/(1024*1024);
        !           638:         $size = sprintf("%.3f",$size);
        !           639:         %info = (
        !           640:                     link         => $uploadedfile,
        !           641:                     name         => $name,
        !           642:                     path         => $path,
        !           643:                     size         => $size,
        !           644:                     lastmodified => $lastmod,
        !           645:                 );
        !           646:         $status = 'ok';
        !           647:     } else {
        !           648:         &Apache::lonnet::logthis("bad file is $url");
        !           649:         my $icon=&Apache::loncommon::icon($url);
        !           650:         $error = '<a href="'.$url.'"><img src="'.$icon.
        !           651:                  '" border="0" />'.$uploadedfile.'</a>';
        !           652:     }
        !           653:     return ($status,\%info,$error);
1.205     albertel  654: }
                    655: 
1.179     albertel  656: sub valid_award {
                    657:     my ($award) =@_;
1.182     albertel  658:     foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
                    659: 			       'NO_RESPONSE',
1.179     albertel  660: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
                    661: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
                    662: 			       'UNIT_FAIL', 'NO_UNIT',
                    663: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
1.251     www       664: 			       'BAD_FORMULA', 'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT', 
1.179     albertel  665: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
1.260.4.2! raeburn   666:                                'EXCESS_FILESIZE', 'FILENAME_INUSE', 
        !           667:                                'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
1.179     albertel  668: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
                    669: 	if ($award eq $possibleaward) { return 1; }
                    670:     }
                    671:     return 0;
                    672: }
                    673: 
1.207     albertel  674: {
                    675:     my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
                    676: 		  'TOO_LONG',
                    677: 		  'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
                    678: 		  'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
1.251     www       679: 		  'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA', 'INTERNAL_ERROR',
1.207     albertel  680: 		  'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
1.260.4.2! raeburn   681: 		  'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'FILENAME_INUSE',
        !           682:                   'DRAFT', 'SUBMITTED',
1.248     raeburn   683:                   'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
1.207     albertel  684:     my $i=0;
                    685:     my %fwd_awards = map { ($_,$i++) } @awards;
                    686:     my $max=scalar(@awards);
                    687:     @awards=reverse(@awards);
1.208     albertel  688:     $i=0;
1.207     albertel  689:     my %rev_awards = map { ($_,$i++) } @awards;
                    690: 
1.232     albertel  691: sub awarddetail_to_awarded {
                    692:     my ($awarddetail) = @_;
                    693:     if ($awarddetail eq 'EXACT_ANS'
                    694: 	|| $awarddetail eq 'APPROX_ANS') {
                    695: 	return 1;
                    696:     }
                    697:     return 0;
                    698: }
                    699: 
1.233     albertel  700: sub hide_award {
                    701:     my ($award) = @_;
                    702:     if (&Apache::lonhomework::show_no_problem_status()) {
                    703: 	return 1;
                    704:     }
                    705:     if ($award =~
                    706: 	/^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|ASSIGNED_SCORE|INCORRECT)/) {
                    707: 	return 1;
                    708:     }
                    709:     return 0;
                    710: }
                    711: 
1.9       albertel  712: sub finalizeawards {
1.232     albertel  713:     my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
1.207     albertel  714:     my $result;
1.136     albertel  715:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135     albertel  716:     if ($result eq '' ) {
                    717: 	my $blankcount;
1.207     albertel  718: 	foreach my $award (@$awardref) {
1.135     albertel  719: 	    if ($award eq '') {
                    720: 		$result='MISSING_ANSWER';
                    721: 		$blankcount++;
                    722: 	    }
                    723: 	}
1.232     albertel  724: 	if ($blankcount == ($#$awardref + 1)) {
                    725: 	    return ('NO_RESPONSE');
                    726: 	}
1.135     albertel  727:     }
1.251     www       728: 
                    729:     if ($Apache::lonxml::internal_error) { $result='INTERNAL_ERROR'; }
                    730: 
1.232     albertel  731:     if (!$final_scantron && defined($result)) { return ($result); }
1.181     albertel  732: 
1.232     albertel  733:     # if in scantron mode, if the award for any response is 
                    734:     # assigned score, then the part gets an assigned score
                    735:     if ($final_scantron 
                    736: 	&& grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
                    737: 	return ('ASSIGNED_SCORE');
                    738:     }
                    739: 
                    740:     # if in scantron mode, if the award for any response is 
                    741:     # correct and there are non-correct responses,
                    742:     # then the part gets an assigned score
                    743:     if ($final_scantron 
                    744: 	&& (grep { $_ eq 'EXACT_ANS' ||
                    745: 		   $_ eq 'APPROX_ANS'  } (@$awardref))
                    746: 	&& (grep { $_ ne 'EXACT_ANS' &&
                    747: 		   $_ ne 'APPROX_ANS'  } (@$awardref))) {
                    748: 	return ('ASSIGNED_SCORE');
                    749:     }
1.181     albertel  750:     # these awards are ordered from most important error through best correct
1.207     albertel  751:     my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
                    752: 
                    753:     my $best = $max;
                    754:     my $j=0;
                    755:     my $which;
                    756:     foreach my $award (@$awardref) {
                    757: 	if ($awards->{$award} < $best) {
                    758: 	    $best  = $awards->{$award};
                    759: 	    $which = $j;
                    760: 	}
                    761: 	$j++;
                    762:     }
1.232     albertel  763: 
1.207     albertel  764:     if (defined($which)) {
                    765: 	if (ref($nameref)) {
                    766: 	    return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
                    767: 	} else {
                    768: 	    return ($$awardref[$which],$$msgref[$which]);
                    769: 	}
1.135     albertel  770:     }
1.136     albertel  771:     return ('ERROR',undef);
1.9       albertel  772: }
1.207     albertel  773: }
1.9       albertel  774: 
1.10      albertel  775: sub decideoutput {
1.169     albertel  776:     my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
1.251     www       777: 
1.135     albertel  778:     my $message='';
                    779:     my $button=0;
                    780:     my $previousmsg;
1.221     albertel  781:     my $css_class='orange';
1.148     albertel  782:     my $added_computer_text=0;
1.221     albertel  783:     my %possible_class =
                    784: 	( 'correct'         => 'LC_answer_correct',
                    785: 	  'charged_try'     => 'LC_answer_charged_try',
                    786: 	  'not_charged_try' => 'LC_answer_not_charged_try',
                    787: 	  'no_grade'        => 'LC_answer_no_grade',
                    788: 	  'no_message'      => 'LC_no_message',
1.135     albertel  789: 	  );
1.169     albertel  790: 
1.180     albertel  791:     my $part = $Apache::inputtags::part;
1.236     raeburn   792:     my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
                    793:     my $handgrade = ('yes' eq lc($tohandgrade)); 
1.180     albertel  794:     
                    795:     my $computer = ($handgrade)? ''
1.203     www       796: 	                       : " ".&mt("Computer's answer now shown above.");
1.180     albertel  797:     &Apache::lonxml::debug("handgrade has :$handgrade:");
                    798: 
1.135     albertel  799:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
                    800:     
1.194     banghart  801:     if ($solved =~ /^correct/) {
1.221     albertel  802:         $css_class=$possible_class{'correct'};
1.170     albertel  803: 	$message=&mt('You are correct.');
                    804: 	if ($awarded < 1 && $awarded > 0) {
                    805: 	    $message=&mt('You are partially correct.');
1.221     albertel  806: 	    $css_class=$possible_class{'not_charged_try'};
1.170     albertel  807: 	} elsif ($awarded < 1) {
                    808: 	    $message=&mt('Incorrect.');
1.221     albertel  809: 	    $css_class=$possible_class{'charged_try'};
1.170     albertel  810: 	}
1.172     albertel  811: 	if ($env{'request.filename'} =~ 
                    812: 	    m|/res/lib/templates/examupload.problem$|) {
                    813: 	    $message = &mt("A score has been assigned.");
                    814: 	    $added_computer_text=1;
1.135     albertel  815: 	} else {
1.172     albertel  816: 	    if ($target eq 'tex') {
                    817: 		$message = '\textbf{'.$message.'}';
                    818: 	    } else {
                    819: 		$message = "<b>".$message."</b>";
1.180     albertel  820: 		$message.= $computer;
1.135     albertel  821: 	    }
1.172     albertel  822: 	    $added_computer_text=1;
1.235     albertel  823: 	    if ($awarded > 0) {
                    824: 		my ($symb) = &Apache::lonnet::whichuser();
                    825: 		if (($symb ne '') 
                    826: 		    &&
                    827: 		    ($env{'course.'.$env{'request.course.id'}.
1.237     www       828: 			      '.disable_receipt_display'} ne 'yes') &&
                    829:                     ($Apache::lonhomework::type ne 'practice')) { 
1.235     albertel  830: 		    $message.=(($target eq 'web')?'<br />':' ').
1.256     biermanm  831: 			&mt('Your receipt no. is [_1]',
1.235     albertel  832: 			    (&Apache::lonnet::receipt($Apache::inputtags::part).
                    833: 			     (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
                    834: 		}
1.135     albertel  835: 	    }
                    836: 	}
                    837: 	$button=0;
                    838: 	$previousmsg='';
                    839:     } elsif ($solved =~ /^excused/) {
                    840: 	if ($target eq 'tex') {
                    841: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
                    842: 	} else {
                    843: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
                    844: 	}
1.221     albertel  845: 	$css_class=$possible_class{'charged_try'};
1.135     albertel  846: 	$button=0;
                    847: 	$previousmsg='';
                    848:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                    849: 	if ($solved =~ /^incorrect/ || $solved eq '') {
1.144     albertel  850: 	    $message = &mt("Incorrect").".";
1.221     albertel  851: 	    $css_class=$possible_class{'charged_try'};
1.135     albertel  852: 	    $button=1;
                    853: 	} else {
1.144     albertel  854: 	    if ($target eq 'tex') {
                    855: 		$message = '\textbf{'.&mt('You are correct.').'}';
                    856: 	    } else {
                    857: 		$message = "<b>".&mt('You are correct.')."</b>";
1.180     albertel  858: 		$message.= $computer;
1.144     albertel  859: 	    }
1.148     albertel  860: 	    $added_computer_text=1;
1.235     albertel  861: 	    if  ($awarded > 0 
                    862: 		 && $env{'course.'.
1.165     albertel  863: 			     $env{'request.course.id'}.
1.235     albertel  864: 			     '.disable_receipt_display'} ne 'yes') { 
1.135     albertel  865: 		$message.=(($target eq 'web')?'<br />':' ').
1.235     albertel  866: 		    &mt('Your receipt is [_1]',
                    867: 			(&Apache::lonnet::receipt($Apache::inputtags::part).
                    868: 			 (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1.135     albertel  869: 	    }
1.221     albertel  870: 	    $css_class=$possible_class{'correct'};
1.135     albertel  871: 	    $button=0;
                    872: 	    $previousmsg='';
                    873: 	}
                    874:     } elsif ($award eq 'NO_RESPONSE') {
                    875: 	$message = '';
1.221     albertel  876: 	$css_class=$possible_class{'no_feedback'};
1.135     albertel  877: 	$button=1;
1.182     albertel  878:     } elsif ($award eq 'EXTRA_ANSWER') {
                    879: 	$message = &mt('Some extra items were submitted.');
1.221     albertel  880: 	$css_class=$possible_class{'not_charged_try'};
1.182     albertel  881: 	$button = 1;
1.135     albertel  882:     } elsif ($award eq 'MISSING_ANSWER') {
1.245     bisitz    883: 	$message = &mt('Some items were not submitted.');
                    884:         if ($target ne 'tex') {
                    885:            $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
                    886:         }
1.221     albertel  887: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  888: 	$button = 1;
                    889:     } elsif ($award eq 'ERROR') {
1.247     bisitz    890: 	$message = &mt('An error occurred while grading your answer.');
1.221     albertel  891: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  892: 	$button = 1;
                    893:     } elsif ($award eq 'TOO_LONG') {
                    894: 	$message = &mt("The submitted answer was too long.");
1.221     albertel  895: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  896: 	$button=1;
                    897:     } elsif ($award eq 'WANTED_NUMERIC') {
                    898: 	$message = &mt("This question expects a numeric answer.");
1.221     albertel  899: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  900: 	$button=1;
                    901:     } elsif ($award eq 'MISORDERED_RANK') {
1.242     bisitz    902:         $message = &mt('You have provided an invalid ranking.');
                    903:         if ($target ne 'tex') {
                    904:             $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
                    905:         }
1.221     albertel  906: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  907: 	$button=1;
1.248     raeburn   908:     } elsif ($award eq 'EXCESS_FILESIZE') {
                    909:         $message = &mt('Submission won\'t be graded. The combined size of submitted files exceeded the amount allowed.');
                    910:         $css_class=$possible_class{'not_charged_try'};
                    911:         $button=1;
1.260.4.2! raeburn   912:     } elsif ($award eq 'FILENAME_INUSE') {
        !           913:         $message = &mt('You have already uploaded a file with that filename.');
        !           914:         if ($target eq 'tex') {
        !           915:             $message.= "\\\\\n";
        !           916:         } else {
        !           917:             $message .= '<br />';
        !           918:         }
        !           919:         $message .= &mt('Please use a different file name.');
        !           920:         $css_class=$possible_class{'not_charged_try'};
        !           921:         $button=1;
1.135     albertel  922:     } elsif ($award eq 'INVALID_FILETYPE') {
1.255     bisitz    923: 	$message = &mt("Submission won't be graded. The type of file submitted is not allowed.");
1.221     albertel  924: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  925: 	$button=1;
                    926:     } elsif ($award eq 'SIG_FAIL') {
1.145     albertel  927: 	my ($used,$min,$max)=split(':',$awardmsg);
1.212     albertel  928: 	my $word = ($used < $min) ? 'more' : 'fewer';
1.250     bisitz    929: 	$message = &mt("Submission not graded. Use $word digits.",$used);
1.221     albertel  930: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  931: 	$button=1;
1.137     albertel  932:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
                    933: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
                    934: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel  935: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel  936: 	$button=1;
                    937:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155     albertel  938: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137     albertel  939: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel  940: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel  941: 	$button=1;
1.140     matthew   942:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155     albertel  943: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136     albertel  944: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel  945: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  946: 	$button=1;
                    947:     } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155     albertel  948: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.221     albertel  949: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  950: 	$button=1;
                    951:     } elsif ($award eq 'NO_UNIT') {
1.144     albertel  952: 	$message = &mt("Units required").'.';
1.135     albertel  953: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
1.221     albertel  954: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  955: 	$button=1;
1.153     albertel  956:     } elsif ($award eq 'COMMA_FAIL') {
                    957: 	$message = &mt("Proper comma separation is required").'.';
1.221     albertel  958: 	$css_class=$possible_class{'not_charged_try'};
1.153     albertel  959: 	$button=1;
1.135     albertel  960:     } elsif ($award eq 'BAD_FORMULA') {
1.240     www       961: 	$message = &mt("Unable to understand formula").'.';
                    962:         if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
1.221     albertel  963: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  964: 	$button=1;
1.251     www       965:     } elsif ($award eq 'INTERNAL_ERROR') {
                    966:         $message = &mt("An internal error occurred while processing your answer. Please try again later.");
                    967:         $css_class=$possible_class{'not_charged_try'};
                    968:         $button=1;
1.135     albertel  969:     } elsif ($award eq 'INCORRECT') {
1.144     albertel  970: 	$message = &mt("Incorrect").'.';
1.221     albertel  971: 	$css_class=$possible_class{'charged_try'};
1.135     albertel  972: 	$button=1;
                    973:     } elsif ($award eq 'SUBMITTED') {
1.260.4.1  raeburn   974:         if ($env{'request.uri'} eq '/res/gci/gci/internal/submission.problem') {
                    975:             if ($target eq 'web') {
                    976:                 $message .= '<br />'.&mt('Thank you for making a submission to the Geosciences Concept Inventory via the GCI Web Center.');
                    977:             }
                    978:         } else {
                    979: 	    $message = &mt("Your submission has been recorded.");
                    980:         }
1.221     albertel  981: 	$css_class=$possible_class{'no_grade'};
1.135     albertel  982: 	$button=1;
                    983:     } elsif ($award eq 'DRAFT') {
1.258     riegler   984: 	$message = &mt("Copy saved but not submitted.");
1.221     albertel  985: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  986: 	$button=1;
                    987:     } elsif ($award eq 'ASSIGNED_SCORE') {
1.144     albertel  988: 	$message = &mt("A score has been assigned.");
1.221     albertel  989: 	$css_class=$possible_class{'correct'};
1.135     albertel  990: 	$button=0;
1.144     albertel  991:     } elsif ($award eq '') {
1.186     albertel  992: 	if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
                    993: 	    $message = &mt("Nothing submitted.");
1.221     albertel  994: 	    $css_class=$possible_class{'charged_try'};
1.186     albertel  995: 	} else {
1.221     albertel  996: 	    $css_class=$possible_class{'not_charged_try'};
1.186     albertel  997: 	}
1.144     albertel  998: 	$button=1;
1.135     albertel  999:     } else {
                   1000: 	$message = &mt("Unknown message").": $award";
                   1001: 	$button=1;
                   1002:     }
1.209     albertel 1003:     my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1.194     banghart 1004:     foreach my $resid(@Apache::inputtags::response){
                   1005:         if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1.254     raeburn  1006:             if ($target eq 'tex') {
                   1007:                 $message.= "\\\\\n";
                   1008:             } else {
                   1009:                 $message.='<br />';
                   1010:             }
1.198     albertel 1011: 	    my @files = split(/\s*,\s*/,
                   1012: 			      $Apache::lonhomework::history{"resource.$part.$resid.handback"});
                   1013: 	    my $file_msg;
                   1014: 	    foreach my $file (@files) {
1.254     raeburn  1015:                 if ($target eq 'tex') {
                   1016:                     $file_msg.= "\\\\\n".$file;
                   1017:                 } else {
                   1018:                     $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
                   1019:                 }
1.198     albertel 1020: 	    }
                   1021: 	    $message .= &mt('Returned file(s): [_1]',$file_msg);
1.254     raeburn  1022:             if ($target eq 'tex') {
                   1023:                 $message.= "\\\\\n";
                   1024:             } else {
                   1025:                 $message.='<br />';
                   1026:             }
1.198     albertel 1027: 	}
1.194     banghart 1028:     }
                   1029: 
1.233     albertel 1030:     if (&Apache::lonhomework::hide_problem_status()
                   1031: 	&& $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
                   1032: 	&& &hide_award($award)) {
1.260.4.1  raeburn  1033:         if ($env{'request.uri'} eq '/res/gci/gci/internal/submission.problem') {
                   1034:             if ($target eq 'web') {
                   1035:                 $message = &mt("Your submission has been recorded.").'<br />'.
                   1036:                            &mt('Thank you for making a submission to the Geosciences Concept Inventory via the GCI Web Center.');
                   1037:             }
                   1038:         } else {
                   1039: 	    $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
                   1040:         }
1.221     albertel 1041: 	$css_class=$possible_class{'no_grade'};
1.135     albertel 1042: 	$button=1;
                   1043:     }
1.148     albertel 1044:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
1.150     albertel 1045: 	!$added_computer_text && $target ne 'tex') {
1.180     albertel 1046: 	$message.= $computer;
1.148     albertel 1047: 	$added_computer_text=1;
1.144     albertel 1048:     }
1.237     www      1049:     if ($Apache::lonhomework::type eq 'practice') {
1.244     raeburn  1050:        if ($target eq 'web') {
                   1051:            $message .= '<br />';
                   1052:        } else {
                   1053:            $message .= ' ';      
                   1054:        }
                   1055:        $message.=&mt('Submissions to practice problems are not permanently recorded.');
1.237     www      1056:     }
                   1057: 
1.221     albertel 1058:     return ($button,$css_class,$message,$previousmsg);
1.12      albertel 1059: }
                   1060: 
1.155     albertel 1061: sub markup_unit {
                   1062:     my ($unit,$target)=@_;
                   1063:     if ($target eq 'tex') {
                   1064: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
                   1065:     } else {
                   1066: 	return "<tt>".$unit."</tt>";
                   1067:     }
                   1068: }
                   1069: 
1.88      albertel 1070: sub removealldata {
1.87      albertel 1071:     my ($id)=@_;
                   1072:     foreach my $key (keys(%Apache::lonhomework::results)) {
                   1073: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                   1074: 	    &Apache::lonxml::debug("Removing $key");
                   1075: 	    delete($Apache::lonhomework::results{$key});
                   1076: 	}
                   1077:     }
                   1078: }
                   1079: 
1.142     albertel 1080: sub hidealldata {
                   1081:     my ($id)=@_;
                   1082:     foreach my $key (keys(%Apache::lonhomework::results)) {
                   1083: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                   1084: 	    &Apache::lonxml::debug("Hidding $key");
                   1085: 	    my $newkey=$key;
                   1086: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
                   1087: 	    $Apache::lonhomework::results{$newkey}=
                   1088: 		$Apache::lonhomework::results{$key};
                   1089: 	    delete($Apache::lonhomework::results{$key});
                   1090: 	}
                   1091:     }
                   1092: }
                   1093: 
1.12      albertel 1094: sub setgradedata {
1.136     albertel 1095:     my ($award,$msg,$id,$previously_used) = @_;
1.154     albertel 1096:     if ($Apache::lonhomework::scantronmode && 
1.165     albertel 1097: 	&Apache::lonnet::validCODE($env{'form.CODE'})) {
                   1098: 	$Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154     albertel 1099:     } elsif ($Apache::lonhomework::scantronmode && 
1.165     albertel 1100: 	     $env{'form.CODE'} eq '' &&
1.154     albertel 1101: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
                   1102: 	$Apache::lonhomework::results{"resource.CODE"}='';
1.141     albertel 1103:     }
1.154     albertel 1104: 
1.135     albertel 1105:     if (!$Apache::lonhomework::scantronmode &&
                   1106: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                   1107: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                   1108: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87      albertel 1109: 	return '';
1.135     albertel 1110:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.233     albertel 1111: 	      /^correct/ 
                   1112: 	      || $Apache::lonhomework::scantronmode 
                   1113: 	      || &Apache::lonhomework::hide_problem_status()  ) {
1.154     albertel 1114:         # the student doesn't already have it correct,
                   1115: 	# or we are in a mode (scantron orno problem status) where a correct 
                   1116:         # can become incorrect
                   1117: 	# handle assignment of tries and solved status
1.135     albertel 1118: 	my $solvemsg;
                   1119: 	if ($Apache::lonhomework::scantronmode) {
                   1120: 	    $solvemsg='correct_by_scantron';
                   1121: 	} else {
                   1122: 	    $solvemsg='correct_by_student';
                   1123: 	}
                   1124: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                   1125: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                   1126: 	}
                   1127: 	if ( $award eq 'ASSIGNED_SCORE') {
                   1128: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1129: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1130: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1131: 		$solvemsg;
                   1132: 	    my $numawards=scalar(@Apache::inputtags::response);
                   1133: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1134: 	    foreach my $res (@Apache::inputtags::response) {
1.232     albertel 1135: 		if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
                   1136: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                   1137: 			$Apache::lonhomework::results{"resource.$id.$res.awarded"};
                   1138: 		} else {
                   1139: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                   1140: 			&awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
                   1141: 		}
1.135     albertel 1142: 	    }
                   1143: 	    if ($numawards > 0) {
                   1144: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
                   1145: 		    $numawards;
                   1146: 	    }
                   1147: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                   1148: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1149: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1150: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1151: 		$solvemsg;
                   1152: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                   1153: 	} elsif ( $award eq 'INCORRECT' ) {
                   1154: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1155: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.233     albertel 1156: 	    if (&Apache::lonhomework::hide_problem_status()
                   1157: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1158: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1159: 	    }
                   1160: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1161: 		'incorrect_attempted';
                   1162: 	} elsif ( $award eq 'SUBMITTED' ) {
                   1163: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1164: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1165: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1166: 		'ungraded_attempted';
                   1167: 	} elsif ( $award eq 'DRAFT' ) {
                   1168: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
                   1169: 	} elsif ( $award eq 'NO_RESPONSE' ) {
                   1170: 	    #no real response so delete any data that got stored
1.129     albertel 1171: 	    &removealldata($id);
                   1172: 	    return '';
                   1173: 	} else {
1.135     albertel 1174: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1175: 		'incorrect_attempted';
1.233     albertel 1176: 	    if (&Apache::lonhomework::show_no_problem_status()
                   1177: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1178: 		$Apache::lonhomework::results{"resource.$id.tries"} =
                   1179: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1180: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1181: 	    }
1.233     albertel 1182: 
                   1183: 	    if (&Apache::lonhomework::show_some_problem_status()) {
                   1184: 		# clear out the awarded if they had gotten it wrong/right
                   1185: 		# and are now in an error mode	
                   1186: 		$Apache::lonhomework::results{"resource.$id.awarded"} = '';
                   1187: 	    }
1.135     albertel 1188: 	}
1.136     albertel 1189: 	if (defined($msg)) {
                   1190: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
                   1191: 	}
1.135     albertel 1192: 	# did either of the overall awards chage? If so ignore the 
                   1193: 	# previous check
                   1194: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
                   1195: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
                   1196: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
                   1197: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
                   1198: 	    # check if this was a previous submission if it was delete the
                   1199: 	    # unneeded data and update the previously_used attribute
                   1200: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
1.233     albertel 1201: 		if (&Apache::lonhomework::show_problem_status()) {
1.135     albertel 1202: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
                   1203: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                   1204: 		}
                   1205: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
                   1206: 		#delete all data as they student didn't do anything, but save
                   1207: 		#the list of collaborators.
                   1208: 		&removealldata($id);
                   1209: 		#and since they didn't do anything we were never here
                   1210: 		return '';
                   1211: 	    } else {
                   1212: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
                   1213: 	    }
1.101     albertel 1214: 	}
1.135     albertel 1215:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
                   1216: 	      /^correct/ ) {
                   1217: 	#delete all data as they student already has it correct
                   1218: 	&removealldata($id);
                   1219: 	#and since they didn't do anything we were never here
                   1220: 	return '';
1.40      albertel 1221:     }
1.135     albertel 1222:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.184     albertel 1223:     if ($award eq 'SUBMITTED') {
                   1224: 	&Apache::response::add_to_gradingqueue();
                   1225:     }
1.10      albertel 1226: }
                   1227: 
1.219     albertel 1228: sub find_which_previous {
                   1229:     my ($version) = @_;
                   1230:     my $part = $Apache::inputtags::part;
                   1231:     my (@previous_version);
                   1232:     foreach my $resp (@Apache::inputtags::response) {
                   1233: 	my $key = "$version:resource.$part.$resp.submission";
                   1234: 	my $submission = $Apache::lonhomework::history{$key};
                   1235: 	my %previous = &Apache::response::check_for_previous($submission,
                   1236: 							     $part,$resp,
                   1237: 							     $version);
                   1238: 	push(@previous_version,$previous{'version'});
                   1239:     }
                   1240:     return &previous_match(\@previous_version,
                   1241: 			   scalar(@Apache::inputtags::response));
                   1242: }
                   1243: 
                   1244: sub previous_match {
                   1245:     my ($previous_array,$count) = @_;
                   1246:     my $match = 0;
                   1247:     my @matches;
                   1248:     foreach my $versionar (@$previous_array) {
                   1249: 	foreach my $version (@$versionar) {
                   1250: 	    $matches[$version]++;
                   1251: 	}
                   1252:     }
                   1253:     my $which=0;
                   1254:     foreach my $elem (@matches) {
                   1255: 	if ($elem eq $count) {
                   1256: 	    $match=1;
                   1257: 	    last;
                   1258: 	}
                   1259: 	$which++;
                   1260:     }
                   1261:     return ($match,$which);
                   1262: }
                   1263: 
1.9       albertel 1264: sub grade {
1.135     albertel 1265:     my ($target) = @_;
                   1266:     my $id = $Apache::inputtags::part;
                   1267:     my $response='';
1.165     albertel 1268:     if ( defined $env{'form.submitted'}) {
1.136     albertel 1269: 	my (@awards,@msgs);
1.135     albertel 1270: 	foreach $response (@Apache::inputtags::response) {
                   1271: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                   1272: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
                   1273: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
                   1274: 	    push (@awards,$value);
1.136     albertel 1275: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
                   1276: 	    &Apache::lonxml::debug("got message $value from $response for $id");
                   1277: 	    push (@msgs,$value);
1.135     albertel 1278: 	}
1.232     albertel 1279: 	my ($finalaward,$msg) = 
                   1280: 	    &finalizeawards(\@awards,\@msgs,undef,undef,
                   1281: 			    $Apache::lonhomework::scantronmode);
1.135     albertel 1282: 	my $previously_used;
                   1283: 	if ( $#Apache::inputtags::previous eq $#awards ) {
1.219     albertel 1284: 	    my ($match) =
                   1285: 		&previous_match(\@Apache::inputtags::previous_version,
                   1286: 				scalar(@Apache::inputtags::response));
1.244     raeburn  1287: 
1.135     albertel 1288: 	    if ($match) {
                   1289: 		$previously_used = 'PREVIOUSLY_LAST';
                   1290: 		foreach my $value (@Apache::inputtags::previous) {
                   1291: 		    if ($value eq 'PREVIOUSLY_USED' ) {
                   1292: 			$previously_used = $value;
                   1293: 			last;
                   1294: 		    }
1.75      albertel 1295: 		}
                   1296: 	    }
1.43      albertel 1297: 	}
1.136     albertel 1298: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
                   1299: 	&setgradedata($finalaward,$msg,$id,$previously_used);
1.43      albertel 1300:     }
1.135     albertel 1301:     return '';
1.1       albertel 1302: }
                   1303: 
1.217     albertel 1304: sub get_grade_messages {
                   1305:     my ($id,$prefix,$target,$status) = @_;
                   1306: 
                   1307:     my ($message,$latemessage,$trystr,$previousmsg);
                   1308:     my $showbutton = 1;
                   1309: 
                   1310:     my $award = $Apache::lonhomework::history{"$prefix.award"};
                   1311:     my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
                   1312:     my $solved = $Apache::lonhomework::history{"$prefix.solved"};
                   1313:     my $previous = $Apache::lonhomework::history{"$prefix.previous"};
                   1314:     my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
                   1315:     &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
                   1316:     if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
                   1317: 	&Apache::lonxml::debug('Getting message');
1.221     albertel 1318: 	($showbutton,my $css_class,$message,$previousmsg) =
1.217     albertel 1319: 	    &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
                   1320: 			  $target);
                   1321: 	if ($target eq 'tex') {
                   1322: 	    $message='\vskip 2 mm '.$message.' ';
                   1323: 	} else {
1.221     albertel 1324: 	    $message="<td class=\"$css_class\">$message</td>";
1.217     albertel 1325: 	    if ($previousmsg) {
1.221     albertel 1326: 		$previousmsg="<td class=\"LC_answer_previous\">$previousmsg</td>";
1.217     albertel 1327: 	    }
                   1328: 	}
                   1329:     }
                   1330:     my $tries = $Apache::lonhomework::history{"$prefix.tries"};
                   1331:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1332:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                   1333:     #if tries are set to negative turn off the Tries/Button and messages
                   1334:     if (defined($maxtries) && $maxtries < 0) { return ''; }
                   1335:     if ( $tries eq '' ) { $tries = '0'; }
                   1336:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                   1337:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                   1338:     my $tries_text=&mt('Tries');
                   1339:     if ( $Apache::lonhomework::type eq 'survey' ||
                   1340: 	 $Apache::lonhomework::parsing_a_task) {
                   1341: 	$tries_text=&mt('Submissions');
                   1342:     }
                   1343: 
                   1344:     if ($showbutton) {
                   1345: 	if ($target eq 'tex') {
                   1346: 	    if ($env{'request.state'} ne "construct"
                   1347: 		&& $Apache::lonhomework::type ne 'exam'
                   1348: 		&& $env{'form.suppress_tries'} ne 'yes') {
                   1349: 		$trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.
                   1350: 		    $tries.'/'.$maxtries.'} \vskip 2 mm ';
                   1351: 	    } else {
                   1352: 		$trystr = '\vskip 0 mm ';
                   1353: 	    }
                   1354: 	} else {
1.260.4.1  raeburn  1355:             $trystr = '<td><span class="LC_nobreak">';
                   1356:             my %parmhash=&Apache::lonnet::coursedescription($env{'request.course.id'});
                   1357:             if ($parmhash{'suppress_tries'} ne 'yes') {
                   1358:                 $trystr .= "$tries_text $tries";
                   1359: 	        if ($Apache::lonhomework::parsing_a_task) {
                   1360: 	        } elsif($env{'request.state'} ne 'construct') {
                   1361: 		    $trystr.="/$maxtries";
                   1362: 	        } else {
                   1363: 		    if (defined($Apache::inputtags::params{'maxtries'})) {
                   1364: 		        $trystr.="/".$Apache::inputtags::params{'maxtries'};
                   1365: 		    }
                   1366: 	        }
                   1367:             }
1.253     bisitz   1368: 	    $trystr.="</span></td>";
1.217     albertel 1369: 	}
                   1370:     }
1.221     albertel 1371: 
1.217     albertel 1372:     if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
                   1373: 	#last submissions was after due date
                   1374: 	$latemessage=&mt(' The last submission was after the Due Date ');;
                   1375: 	if ($target eq 'web') {
1.221     albertel 1376: 	    $latemessage='<td class="LC_answer_late">'.$latemessage.'</td>';
1.217     albertel 1377: 	}
                   1378:     }
                   1379:     return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
                   1380: }
                   1381: 
1.11      albertel 1382: sub gradestatus {
1.223     albertel 1383:     my ($id,$target,$no_previous) = @_;
1.135     albertel 1384:     my $showbutton = 1;
                   1385:     my $message = '';
                   1386:     my $latemessage = '';
                   1387:     my $trystr='';
                   1388:     my $button='';
                   1389:     my $previousmsg='';
                   1390: 
                   1391:     my $status = $Apache::inputtags::status['-1'];
                   1392:     &Apache::lonxml::debug("gradestatus has :$status:");
1.183     albertel 1393:     if ( $status ne 'CLOSED' 
                   1394: 	 && $status ne 'UNAVAILABLE' 
                   1395: 	 && $status ne 'INVALID_ACCESS' 
                   1396: 	 && $status ne 'NEEDS_CHECKIN' 
                   1397: 	 && $status ne 'NOT_IN_A_SLOT') {  
1.217     albertel 1398: 
                   1399: 	($previousmsg,$latemessage,$message,$trystr) =
                   1400: 	    &get_grade_messages($id,"resource.$id",$target,$status,
                   1401: 				$showbutton);
                   1402: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
                   1403: 	    $showbutton = 0;
1.164     albertel 1404: 	}
1.218     albertel 1405: 	if ( $status eq 'SHOW_ANSWER') {
                   1406: 	    undef($previousmsg);
                   1407: 	}
1.135     albertel 1408: 	if ( $showbutton ) { 
                   1409: 	    if ($target ne 'tex') {
1.260.4.1  raeburn  1410:                 my $submit_text = &mt('Submit Answer');
                   1411:                 if ($env{'request.uri'} eq '/res/gci/gci/internal/submission.problem') {
                   1412:                     $submit_text = &mt('Submit Questions');
                   1413:                 }
1.230     albertel 1414: 		$button = 
1.260     droeschl 1415:             '<input onmouseup="javascript:setSubmittedPart(\''.$id.'\')"
                   1416:                     type="submit" name="submit_'.$id.'"
1.260.4.1  raeburn  1417:                     value="'.$submit_text.'" />';
1.135     albertel 1418: 	    }
                   1419: 	}
1.217     albertel 1420: 
1.135     albertel 1421:     }
                   1422:     my $output= $previousmsg.$latemessage.$message.$trystr;
                   1423:     if ($output =~ /^\s*$/) {
                   1424: 	return $button;
1.63      sakharuk 1425:     } else {
1.135     albertel 1426: 	if ($target eq 'tex') {
                   1427: 	    return $button.' \vskip 0 mm '.$output.' ';
                   1428: 	} else {
1.223     albertel 1429: 	    $output =
                   1430: 		'<table><tr><td>'.$button.'</td>'.$output;
                   1431: 	    if (!$no_previous) {
                   1432: 		$output.='<td>'.&previous_tries($id,$target).'</td>';
                   1433: 	    }
                   1434: 	    $output.= '</tr></table>';
                   1435: 	    return $output;
1.135     albertel 1436: 	}
1.63      sakharuk 1437:     }
1.11      albertel 1438: }
1.217     albertel 1439: 
                   1440: sub previous_tries {
                   1441:     my ($id,$target) = @_;
                   1442:     my $output;
                   1443:     my $status = $Apache::inputtags::status['-1'];
1.219     albertel 1444: 
                   1445:     my $count;
                   1446:     my %count_lookup;
                   1447: 
1.217     albertel 1448:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                   1449: 	my $prefix = $i.":resource.$id";
                   1450: 
                   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: 		}
                   1495: 		$output.='<td>'.$submission.'</td>';
                   1496: 	    } else {
                   1497: 		$output.='<td></td>';
                   1498: 	    }
                   1499: 	}
1.221     albertel 1500: 	$output.=&Apache::loncommon::end_data_table_row()."\n";
1.217     albertel 1501:     }
                   1502:     return if ($output eq '');
1.219     albertel 1503:     my $headers = 
1.222     albertel 1504: 	'<tr>'.'<th>'.&mt('Submission #').'</th><th>'.&mt('Try').
1.219     albertel 1505: 	'</th><th colspan="'.scalar(@Apache::inputtags::response).'">'.
                   1506: 	&mt('Submitted Answer').'</th>';
                   1507:     $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1.217     albertel 1508:     #return $output;
1.229     albertel 1509:     $output = &Apache::loncommon::js_ready($output); 
1.226     albertel 1510:     $output.='<br /><form action=""><center><input type="button" name="close" value="'.&mt('Close Window').'" onClick="window.close()" /></center></form>';
                   1511: 
1.217     albertel 1512:     my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
1.260.4.1  raeburn  1513:     my $pagetitle = 'Previous Tries';
                   1514:     if ($env{'request.uri'} eq '/res/gci/gci/internal/submission.problem') {
                   1515:         $pagetitle = 'Submission History';
                   1516:     }
1.217     albertel 1517:     my $start_page =
1.260.4.1  raeburn  1518: 	&Apache::loncommon::start_page($pagetitle, undef,
1.229     albertel 1519: 				       {'only_body'      => 1,
                   1520: 					'bgcolor'        => '#FFFFFF',
                   1521: 					'js_ready'       => 1,
                   1522: 				        'inherit_jsmath' => 1, });
1.217     albertel 1523:     my $end_page =
                   1524: 	&Apache::loncommon::end_page({'js_ready' => 1,});
1.231     albertel 1525:     my $prefix = $env{'form.request.prefix'};
                   1526:     $prefix =~ tr{.}{_};
                   1527:     my $function_name = "LONCAPA_previous_tries_".$prefix.
1.234     albertel 1528: 	$Apache::lonxml::curdepth.'_'.$env{'form.counter'};
1.260.4.1  raeburn  1529:     my $triestext = &mt('Previous Tries');
                   1530:     if ($env{'request.uri'} eq '/res/gci/gci/internal/submission.problem') {
                   1531:         $triestext = &mt('Submission History');
                   1532:     }
1.217     albertel 1533:     my $result ="<script type=\"text/javascript\">
                   1534: // <![CDATA[
1.231     albertel 1535:     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 1536: // ]]>
1.260.4.1  raeburn  1537: </script><a href=\"javascript:$function_name();void(0);\">".$triestext."</a><br />";
1.217     albertel 1538:     #use Data::Dumper;
                   1539:     #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display));
                   1540:     return $result;
                   1541: }
                   1542: 
1.1       albertel 1543: 1;
                   1544: __END__
1.249     jms      1545: 
                   1546: =pod
                   1547: 
                   1548: =back
                   1549: 
                   1550: =cut
1.43      albertel 1551:  

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