File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.190: download - view: text, annotated - select for diffs
Thu Mar 9 01:11:12 2006 UTC (18 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- consolidating the 'leave blank on scoring form code'
- image response gets blank lines in exam mode
- imageresponse can be scored in exam mode

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.190 2006/03/09 01:11:12 albertel 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: package Apache::inputtags;
   29: use HTML::Entities();
   30: use strict;
   31: use Apache::loncommon;
   32: use Apache::lonlocal;
   33: use Apache::lonnet;
   34: 
   35: BEGIN {
   36:     &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
   37: }
   38: 
   39: #   Initializes a set of global variables used during the parse of the problem.
   40: #
   41: #  @Apache::inputtags::input        - List of current input ids.
   42: #  @Apache::inputtags::inputlist    - List of all input ids seen this problem.
   43: #  @Apache::inputtags::response     - List of all current resopnse ids.
   44: #  @Apache::inputtags::responselist - List of all response ids seen this 
   45: #                                       problem.
   46: #  @Apache::inputtags::hint         - List of all hint ids.
   47: #  @Apache::inputtags::hintlist     - List of all hint ids seen this problem.
   48: #  @Apache::inputtags::previous     - List describing if specific responseds
   49: #                                       have been used
   50: #  @Apache::inputtags::previous_version - Submission responses were used in.
   51: #  $Apache::inputtags::part         - Current part id (valid only in 
   52: #                                       <problem>)
   53: #                                     0 if not in a part.
   54: #  @Apache::inputtags::partlist     - List of part ids seen in the current
   55: #                                       <problem>
   56: #  @Apache::inputtags::status       - List of problem  statuses. First 
   57: #                                     element is the status of the <problem>
   58: #                                     the remainder are for individual <part>s.
   59: #  %Apache::inputtags::params       - Hash of defined parameters for the
   60: #                                     current response.
   61: #  @Apache::inputtags::import       - List of all ids for <import> thes get
   62: #                                     join()ed and prepended.
   63: #  @Apache::inputtags::importlist   - List of all import ids seen.
   64: #  $Apache::inputtags::response_with_no_part
   65: #                                   - Flag set true if we have seen a response
   66: #                                     that is not inside a <part>
   67: #  %Apache::inputtags::answertxt    - <*response> tags store correct
   68: #                                     answer strings for display by <textline/>
   69: #                                     in this hash.
   70: 
   71: sub initialize_inputtags {
   72:     @Apache::inputtags::input=();
   73:     @Apache::inputtags::inputlist=();
   74:     @Apache::inputtags::response=();
   75:     @Apache::inputtags::responselist=();
   76:     @Apache::inputtags::hint=();
   77:     @Apache::inputtags::hintlist=();
   78:     @Apache::inputtags::previous=();
   79:     @Apache::inputtags::previous_version=();
   80:     $Apache::inputtags::part='';
   81:     @Apache::inputtags::partlist=();
   82:     @Apache::inputtags::status=();
   83:     %Apache::inputtags::params=();
   84:     @Apache::inputtags::import=();
   85:     @Apache::inputtags::importlist=();
   86:     $Apache::inputtags::response_with_no_part=0;
   87:     %Apache::inputtags::answertxt=();
   88: }
   89: 
   90: sub check_for_duplicate_ids {
   91:     my %check;
   92:     foreach my $id (@Apache::inputtags::partlist,
   93: 		    @Apache::inputtags::responselist,
   94: 		    @Apache::inputtags::hintlist,
   95: 		    @Apache::inputtags::importlist) {
   96: 	$check{$id}++;
   97:     }
   98:     my @duplicates;
   99:     foreach my $id (sort(keys(%check))) {
  100: 	if ($check{$id} > 1) {
  101: 	    push(@duplicates,$id);
  102: 	}
  103:     }
  104:     if (@duplicates) {
  105: 	&Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
  106:     }
  107: }
  108: 
  109: sub start_input {
  110:     my ($parstack,$safeeval)=@_;
  111:     my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
  112:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
  113:     push (@Apache::inputtags::input,$id);
  114:     push (@Apache::inputtags::inputlist,$id);
  115:     return $id;
  116: }
  117: 
  118: sub end_input {
  119:     pop @Apache::inputtags::input;
  120:     return '';
  121: }
  122: 
  123: sub addchars {
  124:     my ($fieldid,$addchars)=@_;
  125:     my $output='';
  126:     foreach (split(/\,/,$addchars)) {
  127: 	$output.='<a href="javascript:void(document.forms.lonhomework.'.
  128: 	    $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
  129:     }
  130:     return $output;
  131: }
  132: 
  133: sub start_textfield {
  134:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  135:     my $result = "";
  136:     my $id = &start_input($parstack,$safeeval);
  137:     my $resid=$Apache::inputtags::response[-1];
  138:     if ($target eq 'web') {
  139: 	$Apache::lonxml::evaluate--;
  140: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  141: 	    my $partid=$Apache::inputtags::part;
  142: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
  143: 	    my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
  144: 	    if ( $cols eq '') { $cols = 80; }
  145: 	    my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
  146: 	    if ( $rows eq '') { $rows = 16; }
  147: 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
  148: 	    $result='';
  149: 	    if ($addchars) {
  150: 		$result.=&addchars('HWVAL_'.$resid,$addchars);
  151: 	    }
  152: 	    push @Apache::lonxml::htmlareafields,'HWVAL_'.$resid;
  153: 	    $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
  154: 		"rows=\"$rows\" cols=\"$cols\">".$oldresponse;
  155: 	    if ($oldresponse ne '') {
  156: 
  157: 		#get rid of any startup text if the user has already responded
  158: 		&Apache::lonxml::get_all_text("/textfield",$parser,$style);
  159: 	    }
  160: 	} else {
  161: 	    #right or wrong don't show it
  162: 	    #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
  163: 	    $result='';
  164: 	    #get rid of any startup text
  165: 	    &Apache::lonxml::get_all_text("/textfield",$parser,$style);
  166: 	}
  167:     } elsif ($target eq 'grade') {
  168: 	my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
  169: 						   $style);
  170: 	if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
  171: 	    # if the seed text is still there it wasn't a real submission
  172: 	    $env{'form.HWVAL_'.$resid}='';
  173: 	}
  174:     } elsif ($target eq 'edit') {
  175: 	$result.=&Apache::edit::tag_start($target,$token);
  176: 	$result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
  177: 	$result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
  178: 	$result.=&Apache::edit::text_arg
  179: 	    ('Click-On Texts (comma sep):','addchars',$token,10);
  180: 	my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
  181: 						   $style);
  182: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
  183:     } elsif ($target eq 'modified') {
  184: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  185: 						     $safeeval,'rows','cols',
  186: 						     'addchars');
  187: 	if ($constructtag) {
  188: 	    $result = &Apache::edit::rebuild_tag($token);
  189: 	} else {
  190: 	    $result=$token->[4];
  191: 	}
  192: 	$result.=&Apache::edit::modifiedfield("/textfield",$parser);
  193:     } elsif ($target eq 'tex') {
  194: 	my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
  195: 	my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
  196: 	if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
  197: 	    $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
  198: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
  199: 	    $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
  200: 	} else {
  201: 	    my $TeXwidth=$width_of_box/80;
  202: 	    $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
  203: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
  204: 	    $result.='}}}\vskip 2 mm ';
  205: 	}
  206:     }
  207:     return $result;
  208: }
  209: 
  210: sub end_textfield {
  211:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  212:     my $result;
  213:     if ($target eq 'web') {
  214: 	$Apache::lonxml::evaluate++;
  215: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  216: 	    return "</textarea>";
  217: 	}
  218:     } elsif ($target eq 'edit') {
  219: 	$result=&Apache::edit::end_table();
  220:     }
  221:     &end_input;
  222:     return $result;
  223: }
  224: 
  225: sub exam_score_line {
  226:     my ($target) = @_;
  227: 
  228:     my $result;
  229:     if ($target eq 'tex') {
  230: 	my $repetition = &Apache::response::repetition();
  231: 	$result.='\begin{enumerate}';
  232: 	if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
  233: 	foreach my $i (0..$repetition-1) {
  234: 	    $result.='\item[\textbf{'.
  235: 		($Apache::lonxml::counter+$i).
  236: 		'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
  237: 	}
  238: 	$result.= '\end{enumerate}';
  239:     }
  240: 
  241:     return $result;
  242: }
  243: 
  244: sub exam_box {
  245:     my ($target) = @_;
  246:     my $result;
  247: 
  248:     if ($target eq 'tex') {
  249: 	$result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
  250: 	$result .= &exam_score_line($target);
  251:     } elsif ($target eq 'web') {
  252: 	my $id=$Apache::inputtags::response[-1];
  253: 	$result.= '<br /><br />
  254:                    <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
  255:                    </textarea> <br /><br />';
  256:     }
  257:     return $result;
  258: }
  259: 
  260: sub needs_exam_box {
  261:     my ($tagstack) = @_;
  262:     my @tags = ('formularesponse',
  263: 		'stringresponse',
  264: 		'reactionresponse',
  265: 		'organicresponse',
  266: 		);
  267: 
  268:     foreach my $tag (@tags) {
  269: 	if (grep(/\Q$tag\E/,@$tagstack)) {
  270: 	    return 1;
  271: 	}
  272:     }
  273:     return 0;
  274: }
  275: 
  276: sub start_textline {
  277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  278:     my $result = "";
  279:     if ($target eq 'web') {
  280: 	$Apache::lonxml::evaluate--;
  281: 	my $partid=$Apache::inputtags::part;
  282: 	my $id=$Apache::inputtags::response[-1];
  283: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  284: 	    my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  285: 	    my $maxlength;
  286: 	    if ($size eq '') { $size=20; } else {
  287: 		if ($size < 20) { $maxlength=$size; }
  288: 	    }
  289: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
  290: 	    if ($Apache::lonhomework::type ne 'exam') {
  291: 		my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
  292: 		$result='';
  293: 		if ($addchars) {
  294: 		    $result.=&addchars('HWVAL_'.$id,$addchars);
  295: 		}
  296: 		my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
  297: 							$safeeval);
  298: 		if (lc($readonly) eq 'yes') {
  299: 		    $readonly=' readonly="readonly" ';
  300: 		} else {
  301: 		    $readonly='';
  302: 		}
  303: 		$result.= '<input type="text" '.$readonly.' name="HWVAL_'.$id.'" value="'.
  304: 		    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
  305: 	    }
  306: 	    if ($Apache::lonhomework::type eq 'exam'
  307: 		&& &needs_exam_box($tagstack)) {
  308: 		$result.=&exam_box($target);
  309: 	    }
  310: 	} else {
  311: 	    #right or wrong don't show what was last typed in.
  312: 	    $result='<b>'.$Apache::inputtags::answertxt{$id}.'</b>';
  313: 	    #$result='';
  314: 	}
  315:     } elsif ($target eq 'edit') {
  316: 	$result=&Apache::edit::tag_start($target,$token);
  317: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
  318: 	    &Apache::edit::text_arg('Click-On Texts (comma sep):',
  319: 				    'addchars',$token,10);
  320:         $result.=&Apache::edit::select_arg('Readonly:','readonly',
  321: 					   ['no','yes'],$token);
  322: 	$result.=&Apache::edit::end_row();
  323: 	$result.=&Apache::edit::end_table();
  324:     } elsif ($target eq 'modified') {
  325: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  326: 						     $safeeval,'size',
  327: 						     'addchars','readonly');
  328: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  329:     } elsif ($target eq 'tex' 
  330: 	     && $Apache::lonhomework::type ne 'exam') {
  331: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  332: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
  333: 	$result='\framebox['.$size.'][s]{\tiny\strut}';
  334: 
  335:     } elsif ($target eq 'tex' 
  336: 	     && $Apache::lonhomework::type eq 'exam'
  337: 	     && &needs_exam_box($tagstack)) {
  338: 	$result.=&exam_box($target);
  339:     }
  340:     return $result;
  341: }
  342: 
  343: sub end_textline {
  344:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  345:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  346:     elsif ($target eq 'edit') { return ('','no'); }
  347:     return "";
  348: }
  349: 
  350: sub start_hiddenline {
  351:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  352:     my $result = "";
  353:     if ($target eq 'web') {
  354: 	$Apache::lonxml::evaluate--;
  355: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  356: 	    my $partid=$Apache::inputtags::part;
  357: 	    my $id=$Apache::inputtags::response[-1];
  358: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
  359: 	    if ($Apache::lonhomework::type ne 'exam') {
  360: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
  361: 		    $oldresponse.'" />';
  362: 	    }
  363: 	}
  364:     } elsif ($target eq 'edit') {
  365: 	$result=&Apache::edit::tag_start($target,$token);
  366: 	$result.=&Apache::edit::end_table;
  367:     }
  368: 
  369:     if ( ($target eq 'web' || $target eq 'tex')
  370: 	 && $Apache::lonhomework::type eq 'exam'
  371: 	 && &needs_exam_box($tagstack)) {
  372: 	$result.=&exam_box($target);
  373:     }
  374:     return $result;
  375: }
  376: 
  377: sub end_hiddenline {
  378:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  379:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  380:     elsif ($target eq 'edit') { return ('','no'); }
  381:     return "";
  382: }
  383: 
  384: # $part -> partid
  385: # $id -> responseid
  386: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
  387: # $which -> 'uploadedonly'  -> only newly uploaded files
  388: #           'portfolioonly' -> only allow files from portfolio
  389: #           'both' -> allow files from either location
  390: # $extratext -> additional text to go between the link and the input box
  391: # returns a table row <tr> 
  392: sub file_selector {
  393:     my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
  394:     if (!$uploadedfiletypes) { return ''; }
  395: 
  396:     my $jspart=$part;
  397:     $jspart=~s/\./_/g;
  398: 
  399:     my $result;
  400:     
  401:     $result.='<tr><td>';
  402:     if ($uploadedfiletypes ne '*') {
  403: 	$result.=
  404: 	    &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
  405:     }
  406:     if ($which eq 'uploadonly' || $which eq 'both') { 
  407: 	$result.=&mt('Submit a file: (only one file can be uploaded)').
  408: 	    ' <br /><input type="file" size="50" name="HWFILE'.
  409: 	    $jspart.'_'.$id.'" /><br />';
  410: 	my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
  411: 
  412: 	if ($uploadedfile) {
  413: 	    my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
  414: 	    &Apache::lonxml::extlink($url);
  415: 	    &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
  416: 	    my $icon=&Apache::loncommon::icon($url);
  417: 	    my $curfile='<a href="'.$url.'"><img src="'.$icon.
  418: 		'" border="0" />'.$uploadedfile.'</a>';
  419: 	    $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
  420: 	} else {
  421: 	    #$result.=&mt('(Hand in a file you have prepared on your computer)');
  422: 	}
  423:     }
  424:     if ( $which eq 'both') { 
  425: 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
  426:     }
  427:     if ($which eq 'portfolioonly' || $which eq 'both') { 
  428: 	$result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&amp;fieldname=HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
  429: 	    &mt('Select Portfolio Files').'</a><br />'.
  430: 	    '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
  431: 	    '<br />';
  432: 	if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
  433: 	    my (@filelist,@bad_file_list);
  434: 	    foreach my $file (split(',',&Apache::lonnet::unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
  435: 		my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
  436: 		my $url="/uploaded/$domain/$user/portfolio$file";
  437: 		my $icon=&Apache::loncommon::icon($url);
  438: 		push(@filelist,'<a href="'.$url.'"><img src="'.$icon.
  439: 		     '" border="0" />'.$file.'</a>');
  440: 		if (! &Apache::lonnet::stat_file($url)) {
  441: 		    push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
  442: 			 '" border="0" />'.$file.'</a>');
  443: 		}
  444: 	    }
  445: 	    $result.=&mt("Portfolio files previously selected: <strong>[_1]</strong>",join(', ',@filelist));
  446: 	    if (@bad_file_list) {
  447: 		$result.='<br />'.&mt('<font color="red">These file(s) don\'t exist:</font> <strong>[_1]</strong>',join(', ',@bad_file_list));
  448: 	    }
  449: 	}
  450:     }
  451:     $result.='</td></tr>'; 
  452:     return $result;
  453: }
  454: 
  455: sub checkstatus {
  456:     my ($value,$awardref,$msgref)=@_;
  457:     for (my $i=0;$i<=$#$awardref;$i++) {
  458: 	if ($$awardref[$i] eq $value) {
  459: 	    return ($$awardref[$i],$$msgref[$i]);
  460: 	}
  461:     }
  462:     return(undef,undef);
  463: }
  464: 
  465: sub valid_award {
  466:     my ($award) =@_;
  467:     foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
  468: 			       'NO_RESPONSE',
  469: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
  470: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
  471: 			       'UNIT_FAIL', 'NO_UNIT',
  472: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
  473: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
  474: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
  475: 			       'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
  476: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
  477: 	if ($award eq $possibleaward) { return 1; }
  478:     }
  479:     return 0;
  480: }
  481: 
  482: sub finalizeawards {
  483:     my ($awardref,$msgref,$nameref,$reverse)=@_;
  484:     my $result=undef;
  485:     my $award;
  486:     my $msg;
  487:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
  488:     if ($result eq '' ) {
  489: 	my $blankcount;
  490: 	foreach $award (@$awardref) {
  491: 	    if ($award eq '') {
  492: 		$result='MISSING_ANSWER';
  493: 		$blankcount++;
  494: 	    }
  495: 	}
  496: 	if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
  497:     }
  498:     if (defined($result)) { return ($result,$msg); }
  499: 
  500:     # these awards are ordered from most important error through best correct
  501:     
  502:     my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
  503: 		  'TOO_LONG',
  504: 		  'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
  505: 		  'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
  506: 		  'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
  507: 		  'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
  508: 		  'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
  509: 		  'APPROX_ANS', 'EXACT_ANS');
  510:     if ($reverse) { @awards=reverse(@awards); }
  511:     foreach my $possibleaward (@awards) {
  512: 	($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
  513: 	if (defined($result)) { return ($result,$msg); }
  514:     }
  515:     return ('ERROR',undef);
  516: }
  517: 
  518: sub decideoutput {
  519:     my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
  520:     my $message='';
  521:     my $button=0;
  522:     my $previousmsg;
  523:     my $bgcolor='orange';
  524:     my $added_computer_text=0;
  525:     my %possiblecolors =
  526: 	( 'correct' => '#aaffaa',
  527: 	  'charged_try' => '#ffaaaa',
  528: 	  'not_charged_try' => '#ffffaa',
  529: 	  'no_message' => '#fffff',
  530: 	  );
  531: 
  532:     my $part = $Apache::inputtags::part;
  533:     my $handgrade = 
  534: 	('yes' eq lc(&Apache::lonnet::EXT("resource.$part.handgrade")));
  535:     
  536:     my $computer = ($handgrade)? ''
  537: 	                       : " ".&mt("Computer's answer now shown above.");
  538:     &Apache::lonxml::debug("handgrade has :$handgrade:");
  539: 
  540:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
  541:     
  542:     if      ($solved =~ /^correct/) {
  543: 	$bgcolor=$possiblecolors{'correct'};
  544: 	$message=&mt('You are correct.');
  545: 	if ($awarded < 1 && $awarded > 0) {
  546: 	    $message=&mt('You are partially correct.');
  547: 	    $bgcolor=$possiblecolors{'not_charged_try'};
  548: 	} elsif ($awarded < 1) {
  549: 	    $message=&mt('Incorrect.');
  550: 	    $bgcolor=$possiblecolors{'charged_try'};
  551: 	}
  552: 	if ($env{'request.filename'} =~ 
  553: 	    m|/res/lib/templates/examupload.problem$|) {
  554: 	    $message = &mt("A score has been assigned.");
  555: 	    $added_computer_text=1;
  556: 	} else {
  557: 	    if ($target eq 'tex') {
  558: 		$message = '\textbf{'.$message.'}';
  559: 	    } else {
  560: 		$message = "<b>".$message."</b>";
  561: 		$message.= $computer;
  562: 	    }
  563: 	    $added_computer_text=1;
  564: 	    unless ($env{'course.'.
  565: 			     $env{'request.course.id'}.
  566: 			     '.disable_receipt_display'} eq 'yes') { 
  567: 		$message.=(($target eq 'web')?'<br />':' ').
  568: 		    &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
  569: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  570: 	    }
  571: 	}
  572: 	$button=0;
  573: 	$previousmsg='';
  574:     } elsif ($solved =~ /^excused/) {
  575: 	if ($target eq 'tex') {
  576: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
  577: 	} else {
  578: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
  579: 	}
  580: 	$bgcolor=$possiblecolors{'charged_try'};
  581: 	$button=0;
  582: 	$previousmsg='';
  583:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  584: 	if ($solved =~ /^incorrect/ || $solved eq '') {
  585: 	    $message = &mt("Incorrect").".";
  586: 	    $bgcolor=$possiblecolors{'charged_try'};
  587: 	    $button=1;
  588: 	} else {
  589: 	    if ($target eq 'tex') {
  590: 		$message = '\textbf{'.&mt('You are correct.').'}';
  591: 	    } else {
  592: 		$message = "<b>".&mt('You are correct.')."</b>";
  593: 		$message.= $computer;
  594: 	    }
  595: 	    $added_computer_text=1;
  596: 	    unless ($env{'course.'.
  597: 			     $env{'request.course.id'}.
  598: 			     '.disable_receipt_display'} eq 'yes') { 
  599: 		$message.=(($target eq 'web')?'<br />':' ').
  600: 		    'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
  601: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  602: 	    }
  603: 	    $bgcolor=$possiblecolors{'correct'};
  604: 	    $button=0;
  605: 	    $previousmsg='';
  606: 	}
  607:     } elsif ($award eq 'NO_RESPONSE') {
  608: 	$message = '';
  609: 	$bgcolor=$possiblecolors{'no_feedback'};
  610: 	$button=1;
  611:     } elsif ($award eq 'EXTRA_ANSWER') {
  612: 	$message = &mt('Some extra items were submitted.');
  613: 	$bgcolor=$possiblecolors{'not_charged_try'};
  614: 	$button = 1;
  615:     } elsif ($award eq 'MISSING_ANSWER') {
  616: 	$message = &mt('Some items were not submitted.');
  617: 	$bgcolor=$possiblecolors{'not_charged_try'};
  618: 	$button = 1;
  619:     } elsif ($award eq 'ERROR') {
  620: 	$message = &mt('An error occured while grading your answer.');
  621: 	$bgcolor=$possiblecolors{'not_charged_try'};
  622: 	$button = 1;
  623:     } elsif ($award eq 'TOO_LONG') {
  624: 	$message = &mt("The submitted answer was too long.");
  625: 	$bgcolor=$possiblecolors{'not_charged_try'};
  626: 	$button=1;
  627:     } elsif ($award eq 'WANTED_NUMERIC') {
  628: 	$message = &mt("This question expects a numeric answer.");
  629: 	$bgcolor=$possiblecolors{'not_charged_try'};
  630: 	$button=1;
  631:     } elsif ($award eq 'MISORDERED_RANK') {
  632: 	$message = &mt('You have provided an invalid ranking');
  633: 	if ($target ne 'tex') {
  634: 	    $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
  635: 	}
  636: 	$bgcolor=$possiblecolors{'not_charged_try'};
  637: 	$button=1;
  638:     } elsif ($award eq 'INVALID_FILETYPE') {
  639: 	$message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
  640: 	$bgcolor=$possiblecolors{'not_charged_try'};
  641: 	$button=1;
  642:     } elsif ($award eq 'SIG_FAIL') {
  643: 	my ($used,$min,$max)=split(':',$awardmsg);
  644: 	my $word;
  645: 	if ($used < $min) { $word=&mt('more'); }
  646: 	if ($used > $max) { $word=&mt('fewer'); }
  647: 	$message = &mt("Submission not graded.  Use [_2] digits.",$used,$word);
  648: 	$bgcolor=$possiblecolors{'not_charged_try'};
  649: 	$button=1;
  650:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
  651: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
  652: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  653: 	$bgcolor=$possiblecolors{'not_charged_try'};
  654: 	$button=1;
  655:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
  656: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
  657: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  658: 	$bgcolor=$possiblecolors{'not_charged_try'};
  659: 	$button=1;
  660:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
  661: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
  662: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  663: 	$bgcolor=$possiblecolors{'not_charged_try'};
  664: 	$button=1;
  665:     } elsif ($award eq 'UNIT_NOTNEEDED') {
  666: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
  667: 	$bgcolor=$possiblecolors{'not_charged_try'};
  668: 	$button=1;
  669:     } elsif ($award eq 'NO_UNIT') {
  670: 	$message = &mt("Units required").'.';
  671: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
  672: 	$bgcolor=$possiblecolors{'not_charged_try'};
  673: 	$button=1;
  674:     } elsif ($award eq 'COMMA_FAIL') {
  675: 	$message = &mt("Proper comma separation is required").'.';
  676: 	$bgcolor=$possiblecolors{'not_charged_try'};
  677: 	$button=1;
  678:     } elsif ($award eq 'BAD_FORMULA') {
  679: 	$message = &mt("Unable to understand formula");
  680: 	$bgcolor=$possiblecolors{'not_charged_try'};
  681: 	$button=1;
  682:     } elsif ($award eq 'INCORRECT') {
  683: 	$message = &mt("Incorrect").'.';
  684: 	$bgcolor=$possiblecolors{'charged_try'};
  685: 	$button=1;
  686:     } elsif ($award eq 'SUBMITTED') {
  687: 	$message = &mt("Your submission has been recorded.");
  688: 	$bgcolor=$possiblecolors{'correct'};
  689: 	$button=1;
  690:     } elsif ($award eq 'DRAFT') {
  691: 	$message = &mt("A draft copy has been saved.");
  692: 	$bgcolor=$possiblecolors{'not_charged_try'};
  693: 	$button=1;
  694:     } elsif ($award eq 'ASSIGNED_SCORE') {
  695: 	$message = &mt("A score has been assigned.");
  696: 	$bgcolor=$possiblecolors{'correct'};
  697: 	$button=0;
  698:     } elsif ($award eq '') {
  699: 	if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
  700: 	    $message = &mt("Nothing submitted.");
  701: 	    $bgcolor=$possiblecolors{'charged_try'};
  702: 	} else {
  703: 	    $bgcolor=$possiblecolors{'not_charged_try'};
  704: 	}
  705: 	$button=1;
  706:     } else {
  707: 	$message = &mt("Unknown message").": $award";
  708: 	$button=1;
  709:     }
  710:     if (lc($Apache::lonhomework::problemstatus) eq 'no'  && 
  711: 	$Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
  712: 	$message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
  713: 	$bgcolor=$possiblecolors{'correct'};
  714: 	$button=1;
  715:     }
  716:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
  717: 	!$added_computer_text && $target ne 'tex') {
  718: 	$message.= $computer;
  719: 	$added_computer_text=1;
  720:     }
  721:     return ($button,$bgcolor,$message,$previousmsg);
  722: }
  723: 
  724: sub markup_unit {
  725:     my ($unit,$target)=@_;
  726:     if ($target eq 'tex') {
  727: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
  728:     } else {
  729: 	return "<tt>".$unit."</tt>";
  730:     }
  731: }
  732: 
  733: sub removealldata {
  734:     my ($id)=@_;
  735:     foreach my $key (keys(%Apache::lonhomework::results)) {
  736: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  737: 	    &Apache::lonxml::debug("Removing $key");
  738: 	    delete($Apache::lonhomework::results{$key});
  739: 	}
  740:     }
  741: }
  742: 
  743: sub hidealldata {
  744:     my ($id)=@_;
  745:     foreach my $key (keys(%Apache::lonhomework::results)) {
  746: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  747: 	    &Apache::lonxml::debug("Hidding $key");
  748: 	    my $newkey=$key;
  749: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
  750: 	    $Apache::lonhomework::results{$newkey}=
  751: 		$Apache::lonhomework::results{$key};
  752: 	    delete($Apache::lonhomework::results{$key});
  753: 	}
  754:     }
  755: }
  756: 
  757: sub setgradedata {
  758:     my ($award,$msg,$id,$previously_used) = @_;
  759:     if ($Apache::lonhomework::scantronmode && 
  760: 	&Apache::lonnet::validCODE($env{'form.CODE'})) {
  761: 	$Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
  762:     } elsif ($Apache::lonhomework::scantronmode && 
  763: 	     $env{'form.CODE'} eq '' &&
  764: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
  765: 	$Apache::lonhomework::results{"resource.CODE"}='';
  766:     }
  767: 
  768:     if (!$Apache::lonhomework::scantronmode &&
  769: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  770: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
  771: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
  772: 	return '';
  773:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  774: 	      /^correct/ || $Apache::lonhomework::scantronmode ||
  775: 	      lc($Apache::lonhomework::problemstatus) eq 'no') {
  776:         # the student doesn't already have it correct,
  777: 	# or we are in a mode (scantron orno problem status) where a correct 
  778:         # can become incorrect
  779: 	# handle assignment of tries and solved status
  780: 	my $solvemsg;
  781: 	if ($Apache::lonhomework::scantronmode) {
  782: 	    $solvemsg='correct_by_scantron';
  783: 	} else {
  784: 	    $solvemsg='correct_by_student';
  785: 	}
  786: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  787: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
  788: 	}
  789: 	if ( $award eq 'ASSIGNED_SCORE') {
  790: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  791: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  792: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  793: 		$solvemsg;
  794: 	    my $numawards=scalar(@Apache::inputtags::response);
  795: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  796: 	    foreach my $res (@Apache::inputtags::response) {
  797: 		$Apache::lonhomework::results{"resource.$id.awarded"}+=
  798: 		    $Apache::lonhomework::results{"resource.$id.$res.awarded"};
  799: 	    }
  800: 	    if ($numawards > 0) {
  801: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
  802: 		    $numawards;
  803: 	    }
  804: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  805: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  806: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  807: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  808: 		$solvemsg;
  809: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  810: 	} elsif ( $award eq 'INCORRECT' ) {
  811: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  812: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  813: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  814: 		$Apache::lonhomework::scantronmode) {
  815: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  816: 	    }
  817: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  818: 		'incorrect_attempted';
  819: 	} elsif ( $award eq 'SUBMITTED' ) {
  820: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  821: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  822: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  823: 		'ungraded_attempted';
  824: 	} elsif ( $award eq 'DRAFT' ) {
  825: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
  826: 	} elsif ( $award eq 'NO_RESPONSE' ) {
  827: 	    #no real response so delete any data that got stored
  828: 	    &removealldata($id);
  829: 	    return '';
  830: 	} else {
  831: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  832: 		'incorrect_attempted';
  833: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  834: 		$Apache::lonhomework::scantronmode) {
  835: 		$Apache::lonhomework::results{"resource.$id.tries"} =
  836: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  837: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  838: 	    }
  839: 	}
  840: 	if (defined($msg)) {
  841: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
  842: 	}
  843: 	# did either of the overall awards chage? If so ignore the 
  844: 	# previous check
  845: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
  846: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
  847: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
  848: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
  849: 	    # check if this was a previous submission if it was delete the
  850: 	    # unneeded data and update the previously_used attribute
  851: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
  852: 		if (lc($Apache::lonhomework::problemstatus) ne 'no') {
  853: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
  854: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  855: 		}
  856: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  857: 		#delete all data as they student didn't do anything, but save
  858: 		#the list of collaborators.
  859: 		&removealldata($id);
  860: 		#and since they didn't do anything we were never here
  861: 		return '';
  862: 	    } else {
  863: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
  864: 	    }
  865: 	}
  866:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
  867: 	      /^correct/ ) {
  868: 	#delete all data as they student already has it correct
  869: 	&removealldata($id);
  870: 	#and since they didn't do anything we were never here
  871: 	return '';
  872:     }
  873:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
  874:     if ($award eq 'SUBMITTED') {
  875: 	&Apache::response::add_to_gradingqueue();
  876:     }
  877: }
  878: 
  879: sub grade {
  880:     my ($target) = @_;
  881:     my $id = $Apache::inputtags::part;
  882:     my $response='';
  883:     if ( defined $env{'form.submitted'}) {
  884: 	my (@awards,@msgs);
  885: 	foreach $response (@Apache::inputtags::response) {
  886: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  887: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  888: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
  889: 	    push (@awards,$value);
  890: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
  891: 	    &Apache::lonxml::debug("got message $value from $response for $id");
  892: 	    push (@msgs,$value);
  893: 	}
  894: 	my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
  895: 	my $previously_used;
  896: 	if ( $#Apache::inputtags::previous eq $#awards ) {
  897: 	    my $match=0;
  898: 	    my @matches;
  899: 	    foreach my $versionar (@Apache::inputtags::previous_version) {
  900: 		foreach my $version (@$versionar) {
  901: 		    $matches[$version]++;
  902: 		}
  903: 	    }
  904: 	    foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
  905: 	    if ($match) {
  906: 		$previously_used = 'PREVIOUSLY_LAST';
  907: 		foreach my $value (@Apache::inputtags::previous) {
  908: 		    if ($value eq 'PREVIOUSLY_USED' ) {
  909: 			$previously_used = $value;
  910: 			last;
  911: 		    }
  912: 		}
  913: 	    }
  914: 	}
  915: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
  916: 	&setgradedata($finalaward,$msg,$id,$previously_used);
  917:     }
  918:     return '';
  919: }
  920: 
  921: sub gradestatus {
  922:     my ($id,$target) = @_;
  923:     my $showbutton = 1;
  924:     my $bgcolor = '';
  925:     my $message = '';
  926:     my $latemessage = '';
  927:     my $trystr='';
  928:     my $button='';
  929:     my $previousmsg='';
  930: 
  931:     my $status = $Apache::inputtags::status['-1'];
  932:     &Apache::lonxml::debug("gradestatus has :$status:");
  933:     if ( $status ne 'CLOSED' 
  934: 	 && $status ne 'UNAVAILABLE' 
  935: 	 && $status ne 'INVALID_ACCESS' 
  936: 	 && $status ne 'NEEDS_CHECKIN' 
  937: 	 && $status ne 'NOT_IN_A_SLOT') {  
  938: 	my $award = $Apache::lonhomework::history{"resource.$id.award"};
  939: 	my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
  940: 	my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  941: 	my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  942: 	my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
  943: 	&Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
  944: 	if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
  945: 	    &Apache::lonxml::debug('Getting message');
  946: 	    ($showbutton,$bgcolor,$message,$previousmsg) =
  947: 		&decideoutput($award,$awarded,$awardmsg,$solved,$previous,
  948: 			      $target);
  949: 	    if ($target eq 'tex') {
  950: 		$message='\vskip 2 mm '.$message.' ';
  951: 	    } else {
  952: 		$message="<td bgcolor=\"$bgcolor\">$message</td>";
  953: 		if ($previousmsg) {
  954: 		    $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
  955: 		}
  956: 	    }
  957: 	}
  958: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  959: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  960: 	&Apache::lonxml::debug("got maxtries of :$maxtries:");
  961: 	#if tries are set to negative turn off the Tries/Button and messages
  962: 	if (defined($maxtries) && $maxtries < 0) { return ''; }
  963: 	if ( $tries eq '' ) { $tries = '0'; }
  964: 	if ( $maxtries eq '' ) { $maxtries = '2'; } 
  965: 	if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  966: 	my $tries_text=&mt('Tries');
  967: 	if ( $Apache::lonhomework::type eq 'survey' ||
  968: 	     $Apache::lonhomework::parsing_a_task) {
  969: 	    $tries_text=&mt('Submissions');
  970: 	}
  971: 	if ( $showbutton ) {
  972: 	    if ($target eq 'tex') {
  973: 		if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
  974: 		    $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
  975: 		} else {
  976: 		    $trystr = '\vskip 0 mm ';
  977: 		}
  978: 	    } else {
  979: 		$trystr = "<td><nobr>".$tries_text." $tries";
  980: 		if ($Apache::lonhomework::parsing_a_task) {
  981: 		} elsif($env{'request.state'} ne 'construct') {
  982: 		    $trystr.="/$maxtries";
  983: 		} else {
  984: 		    if (defined($Apache::inputtags::params{'maxtries'})) {
  985: 			$trystr.="/".$Apache::inputtags::params{'maxtries'};
  986: 		    }
  987: 		}
  988: 		$trystr.="</nobr></td>";
  989: 	    }
  990: 	}
  991: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  992: 	if ( $showbutton ) { 
  993: 	    if ($target ne 'tex') {
  994: 		$button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
  995: 	    }
  996: 	}
  997: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  998: 	    #last submissions was after due date
  999: 	    $latemessage=&mt(' The last submission was after the Due Date ');;
 1000: 	    if ($target eq 'web') {
 1001: 		$latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
 1002: 	    }
 1003: 	}
 1004:     }
 1005:     my $output= $previousmsg.$latemessage.$message.$trystr;
 1006:     if ($output =~ /^\s*$/) {
 1007: 	return $button;
 1008:     } else {
 1009: 	if ($target eq 'tex') {
 1010: 	    return $button.' \vskip 0 mm '.$output.' ';
 1011: 	} else {
 1012: 	    return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
 1013: 	}
 1014:     }
 1015: }
 1016: 1;
 1017: __END__
 1018:  

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