File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.271.2.1: download - view: text, annotated - select for diffs
Tue Nov 9 17:41:47 2010 UTC (13 years, 7 months ago) by raeburn
Branches: version_2_10_X
- Backport 1.273.

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

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