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

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

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