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

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

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