File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.269: download - view: text, annotated - select for diffs
Fri Aug 27 17:21:01 2010 UTC (13 years, 8 months ago) by raeburn
Branches: MAIN
CVS tags: version_2_10_0_RC1, HEAD
- Fix parentheses in regexp used for file listing for a file previously
  uploaded on a pre-2.10 server, when viewing current submissions on 2.10.

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

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