File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.260.4.2: download - view: text, annotated - select for diffs
Mon Sep 13 04:26:26 2010 UTC (13 years, 8 months ago) by raeburn
Branches: GCI_3
Diff to branchpoint 1.260: preferred, unified
- Customization for GCI_3
  - 1.264, 1.265, 1.268, 1.269, 1.270, 1.271.
  - Suppress "Submission Type" row and path in name of file
    for submissions to dropbox in GCI Submission problem.

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

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