File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.266: download - view: text, annotated - select for diffs
Sat Aug 7 19:24:03 2010 UTC (13 years, 9 months ago) by raeburn
Branches: MAIN
CVS tags: HEAD
- Eliminate routines no longer needed following switch from FCKEditor 2 to CKEdi
tor 3
- Eliminate checking for environment.wysiwygeditor (no longer used).
- Access to "Richtext" editor available via link on each textarea for which it is
  to be made available.
- When editing an HTML document provide "Edit Math" button to allow
  insertion of TeX within the textarea. Button is hidden if Richtext editor in use.
- Additional tags permitted by clear_out_html() - <pre> and <chem>.

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

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