File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.207: download - view: text, annotated - select for diffs
Fri Sep 29 23:04:37 2006 UTC (17 years, 8 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- making finalizeawards much much faster

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

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