File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.208: download - view: text, annotated - select for diffs
Thu Oct 12 16:28:21 2006 UTC (17 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- need textline to register itself

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.208 2006/10/12 16:28:21 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:     my $id = &start_input($parstack,$safeeval);
  287:     if ($target eq 'web') {
  288: 	$Apache::lonxml::evaluate--;
  289: 	my $partid=$Apache::inputtags::part;
  290: 	my $id=$Apache::inputtags::response[-1];
  291: 	if (!&Apache::response::show_answer()) {
  292: 	    my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  293: 	    my $maxlength;
  294: 	    if ($size eq '') { $size=20; } else {
  295: 		if ($size < 20) { $maxlength=$size; }
  296: 	    }
  297: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
  298: 	    if ($Apache::lonhomework::type ne 'exam') {
  299: 		my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
  300: 		$result='';
  301: 		if ($addchars) {
  302: 		    $result.=&addchars('HWVAL_'.$id,$addchars);
  303: 		}
  304: 		my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
  305: 							$safeeval);
  306: 		if (lc($readonly) eq 'yes' 
  307: 		    || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
  308: 		    $readonly=' readonly="readonly" ';
  309: 		} else {
  310: 		    $readonly='';
  311: 		}
  312: 		my $name = 'HWVAL_'.$id;
  313: 		if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
  314: 		    $name = "none";
  315: 		}
  316: 		$result.= '<input type="text" '.$readonly.' name="'.$name.'" value="'.
  317: 		    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
  318: 	    }
  319: 	    if ($Apache::lonhomework::type eq 'exam'
  320: 		&& &needs_exam_box($tagstack)) {
  321: 		$result.=&exam_box($target);
  322: 	    }
  323: 	} else {
  324: 	    #right or wrong don't show what was last typed in.
  325: 	    my $count = scalar(@Apache::inputtags::inputlist)-1;
  326: 	    $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
  327: 	    #$result='';
  328: 	}
  329:     } elsif ($target eq 'edit') {
  330: 	$result=&Apache::edit::tag_start($target,$token);
  331: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
  332: 	    &Apache::edit::text_arg('Click-On Texts (comma sep):',
  333: 				    'addchars',$token,10);
  334:         $result.=&Apache::edit::select_arg('Readonly:','readonly',
  335: 					   ['no','yes'],$token);
  336: 	$result.=&Apache::edit::end_row();
  337: 	$result.=&Apache::edit::end_table();
  338:     } elsif ($target eq 'modified') {
  339: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  340: 						     $safeeval,'size',
  341: 						     'addchars','readonly');
  342: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  343:     } elsif ($target eq 'tex' 
  344: 	     && $Apache::lonhomework::type ne 'exam') {
  345: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  346: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
  347: 	$result='\framebox['.$size.'][s]{\tiny\strut}';
  348: 
  349:     } elsif ($target eq 'tex' 
  350: 	     && $Apache::lonhomework::type eq 'exam'
  351: 	     && &needs_exam_box($tagstack)) {
  352: 	$result.=&exam_box($target);
  353:     }
  354:     return $result;
  355: }
  356: 
  357: sub end_textline {
  358:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  359:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  360:     elsif ($target eq 'edit') { return ('','no'); }
  361:     &end_input();
  362:     return "";
  363: }
  364: 
  365: sub start_hiddenline {
  366:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  367:     my $result = "";
  368:     if ($target eq 'web') {
  369: 	$Apache::lonxml::evaluate--;
  370: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  371: 	    my $partid=$Apache::inputtags::part;
  372: 	    my $id=$Apache::inputtags::response[-1];
  373: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
  374: 	    if ($Apache::lonhomework::type ne 'exam') {
  375: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
  376: 		    $oldresponse.'" />';
  377: 	    }
  378: 	}
  379:     } elsif ($target eq 'edit') {
  380: 	$result=&Apache::edit::tag_start($target,$token);
  381: 	$result.=&Apache::edit::end_table;
  382:     }
  383: 
  384:     if ( ($target eq 'web' || $target eq 'tex')
  385: 	 && $Apache::lonhomework::type eq 'exam'
  386: 	 && &needs_exam_box($tagstack)) {
  387: 	$result.=&exam_box($target);
  388:     }
  389:     return $result;
  390: }
  391: 
  392: sub end_hiddenline {
  393:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  394:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  395:     elsif ($target eq 'edit') { return ('','no'); }
  396:     return "";
  397: }
  398: 
  399: # $part -> partid
  400: # $id -> responseid
  401: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
  402: # $which -> 'uploadedonly'  -> only newly uploaded files
  403: #           'portfolioonly' -> only allow files from portfolio
  404: #           'both' -> allow files from either location
  405: # $extratext -> additional text to go between the link and the input box
  406: # returns a table row <tr> 
  407: sub file_selector {
  408:     my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
  409:     if (!$uploadedfiletypes) { return ''; }
  410: 
  411:     my $jspart=$part;
  412:     $jspart=~s/\./_/g;
  413: 
  414:     my $result;
  415:     
  416:     $result.='<tr><td>';
  417:     if ($uploadedfiletypes ne '*') {
  418: 	$result.=
  419: 	    &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
  420:     }
  421:     if ($which eq 'uploadonly' || $which eq 'both') { 
  422: 	$result.=&mt('Submit a file: (only one file can be uploaded)').
  423: 	    ' <br /><input type="file" size="50" name="HWFILE'.
  424: 	    $jspart.'_'.$id.'" /><br />';
  425: 	$result .= &show_past_file_submission($part,$id);
  426:     }
  427:     if ( $which eq 'both') { 
  428: 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
  429:     }
  430:     if ($which eq 'portfolioonly' || $which eq 'both') { 
  431: 	$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"))'."'".'>'.
  432: 	    &mt('Select Portfolio Files').'</a><br />'.
  433: 	    '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
  434: 	    '<br />';
  435: 	$result .= &show_past_portfile_submission($part,$id);
  436: 
  437:     }
  438:     $result.='</td></tr>'; 
  439:     return $result;
  440: }
  441: 
  442: sub show_past_file_submission {
  443:     my ($part,$id) = @_;
  444:     my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
  445: 
  446:     return if (!$uploadedfile);
  447: 
  448:     my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
  449:     &Apache::lonxml::extlink($url);
  450:     &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
  451:     my $icon=&Apache::loncommon::icon($url);
  452:     my $curfile='<a href="'.$url.'"><img src="'.$icon.
  453: 	'" border="0" />'.$uploadedfile.'</a>';
  454:     return &mt('Currently submitted: <tt>[_1]</tt>',$curfile);
  455: 
  456: }
  457: 
  458: sub show_past_portfile_submission {
  459:     my ($part,$id) = @_;
  460:     if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}!~/[^\s]/){
  461: 	return;
  462:     }
  463:     my (@file_list,@bad_file_list);
  464:     foreach my $file (split(/\s*,\s*/,&unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
  465: 	my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
  466: 	my $url="/uploaded/$domain/$user/portfolio$file";
  467: 	my $icon=&Apache::loncommon::icon($url);
  468: 	push(@file_list,'<a href="'.$url.'"><img src="'.$icon.
  469: 	     '" border="0" />'.$file.'</a>');
  470: 	if (! &Apache::lonnet::stat_file($url)) {
  471: 	    &Apache::lonnet::logthis("bad file is $url");
  472: 	    push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
  473: 		 '" border="0" />'.$file.'</a>');
  474: 	}
  475:     }
  476:     my $files = '<span class="LC_filename">'.
  477: 	join('</span>, <span class="LC_filename">',@file_list).
  478: 	'</span>';
  479:     my $result = &mt("Portfolio files previously selected: [_1]",$files);
  480:     if (@bad_file_list) {
  481: 	my $bad_files = '<span class="LC_filename">'.
  482: 	    join('</span>, <span class="LC_filename">',@bad_file_list).
  483: 	    '</span>';
  484: 	$result.='<br />'.&mt('<span class="LC_error">These file(s) don\'t exist:</span> [_1]',$bad_files);
  485:     }
  486:     return $result;
  487: 
  488: }
  489: 
  490: sub valid_award {
  491:     my ($award) =@_;
  492:     foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
  493: 			       'NO_RESPONSE',
  494: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
  495: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
  496: 			       'UNIT_FAIL', 'NO_UNIT',
  497: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
  498: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
  499: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
  500: 			       'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
  501: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
  502: 	if ($award eq $possibleaward) { return 1; }
  503:     }
  504:     return 0;
  505: }
  506: 
  507: {
  508:     my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
  509: 		  'TOO_LONG',
  510: 		  'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
  511: 		  'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
  512: 		  'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
  513: 		  'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
  514: 		  'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
  515: 		  'APPROX_ANS', 'EXACT_ANS');
  516:     my $i=0;
  517:     my %fwd_awards = map { ($_,$i++) } @awards;
  518:     my $max=scalar(@awards);
  519:     @awards=reverse(@awards);
  520:     $i=0;
  521:     my %rev_awards = map { ($_,$i++) } @awards;
  522: 
  523: sub finalizeawards {
  524:     my ($awardref,$msgref,$nameref,$reverse)=@_;
  525:     my $result;
  526:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
  527:     if ($result eq '' ) {
  528: 	my $blankcount;
  529: 	foreach my $award (@$awardref) {
  530: 	    if ($award eq '') {
  531: 		$result='MISSING_ANSWER';
  532: 		$blankcount++;
  533: 	    }
  534: 	}
  535: 	if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
  536:     }
  537:     if (defined($result)) { return ($result); }
  538: 
  539:     # these awards are ordered from most important error through best correct
  540:     my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
  541: 
  542:     my $best = $max;
  543:     my $j=0;
  544:     my $which;
  545:     foreach my $award (@$awardref) {
  546: 	if ($awards->{$award} < $best) {
  547: 	    $best  = $awards->{$award};
  548: 	    $which = $j;
  549: 	}
  550: 	$j++;
  551:     }
  552:     if (defined($which)) {
  553: 	if (ref($nameref)) {
  554: 	    return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
  555: 	} else {
  556: 	    return ($$awardref[$which],$$msgref[$which]);
  557: 	}
  558:     }
  559:     return ('ERROR',undef);
  560: }
  561: }
  562: 
  563: sub decideoutput {
  564:     my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
  565:     my $message='';
  566:     my $button=0;
  567:     my $previousmsg;
  568:     my $bgcolor='orange';
  569:     my $added_computer_text=0;
  570:     my %possiblecolors =
  571: 	( 'correct'         => '#aaffaa',
  572: 	  'charged_try'     => '#ffaaaa',
  573: 	  'not_charged_try' => '#ffffaa',
  574: 	  'no_grade'        => '#ffffaa',
  575: 	  'no_message'      => '#ffffff',
  576: 	  );
  577: 
  578:     my $part = $Apache::inputtags::part;
  579:     my $handgrade = 
  580: 	('yes' eq lc(&Apache::lonnet::EXT("resource.$part.handgrade")));
  581:     
  582:     my $computer = ($handgrade)? ''
  583: 	                       : " ".&mt("Computer's answer now shown above.");
  584:     &Apache::lonxml::debug("handgrade has :$handgrade:");
  585: 
  586:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
  587:     
  588:     if ($solved =~ /^correct/) {
  589:         $bgcolor=$possiblecolors{'correct'};
  590: 	$message=&mt('You are correct.');
  591: 	if ($awarded < 1 && $awarded > 0) {
  592: 	    $message=&mt('You are partially correct.');
  593: 	    $bgcolor=$possiblecolors{'not_charged_try'};
  594: 	} elsif ($awarded < 1) {
  595: 	    $message=&mt('Incorrect.');
  596: 	    $bgcolor=$possiblecolors{'charged_try'};
  597: 	}
  598: 	if ($env{'request.filename'} =~ 
  599: 	    m|/res/lib/templates/examupload.problem$|) {
  600: 	    $message = &mt("A score has been assigned.");
  601: 	    $added_computer_text=1;
  602: 	} else {
  603: 	    if ($target eq 'tex') {
  604: 		$message = '\textbf{'.$message.'}';
  605: 	    } else {
  606: 		$message = "<b>".$message."</b>";
  607: 		$message.= $computer;
  608: 	    }
  609: 	    $added_computer_text=1;
  610: 	    unless ($env{'course.'.
  611: 			     $env{'request.course.id'}.
  612: 			     '.disable_receipt_display'} eq 'yes') { 
  613: 		$message.=(($target eq 'web')?'<br />':' ').
  614: 		    &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
  615: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  616: 	    }
  617: 	}
  618: 	$button=0;
  619: 	$previousmsg='';
  620:     } elsif ($solved =~ /^excused/) {
  621: 	if ($target eq 'tex') {
  622: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
  623: 	} else {
  624: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
  625: 	}
  626: 	$bgcolor=$possiblecolors{'charged_try'};
  627: 	$button=0;
  628: 	$previousmsg='';
  629:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  630: 	if ($solved =~ /^incorrect/ || $solved eq '') {
  631: 	    $message = &mt("Incorrect").".";
  632: 	    $bgcolor=$possiblecolors{'charged_try'};
  633: 	    $button=1;
  634: 	} else {
  635: 	    if ($target eq 'tex') {
  636: 		$message = '\textbf{'.&mt('You are correct.').'}';
  637: 	    } else {
  638: 		$message = "<b>".&mt('You are correct.')."</b>";
  639: 		$message.= $computer;
  640: 	    }
  641: 	    $added_computer_text=1;
  642: 	    unless ($env{'course.'.
  643: 			     $env{'request.course.id'}.
  644: 			     '.disable_receipt_display'} eq 'yes') { 
  645: 		$message.=(($target eq 'web')?'<br />':' ').
  646: 		    'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
  647: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  648: 	    }
  649: 	    $bgcolor=$possiblecolors{'correct'};
  650: 	    $button=0;
  651: 	    $previousmsg='';
  652: 	}
  653:     } elsif ($award eq 'NO_RESPONSE') {
  654: 	$message = '';
  655: 	$bgcolor=$possiblecolors{'no_feedback'};
  656: 	$button=1;
  657:     } elsif ($award eq 'EXTRA_ANSWER') {
  658: 	$message = &mt('Some extra items were submitted.');
  659: 	$bgcolor=$possiblecolors{'not_charged_try'};
  660: 	$button = 1;
  661:     } elsif ($award eq 'MISSING_ANSWER') {
  662: 	$message = &mt('Some items were not submitted.');
  663: 	$bgcolor=$possiblecolors{'not_charged_try'};
  664: 	$button = 1;
  665:     } elsif ($award eq 'ERROR') {
  666: 	$message = &mt('An error occured while grading your answer.');
  667: 	$bgcolor=$possiblecolors{'not_charged_try'};
  668: 	$button = 1;
  669:     } elsif ($award eq 'TOO_LONG') {
  670: 	$message = &mt("The submitted answer was too long.");
  671: 	$bgcolor=$possiblecolors{'not_charged_try'};
  672: 	$button=1;
  673:     } elsif ($award eq 'WANTED_NUMERIC') {
  674: 	$message = &mt("This question expects a numeric answer.");
  675: 	$bgcolor=$possiblecolors{'not_charged_try'};
  676: 	$button=1;
  677:     } elsif ($award eq 'MISORDERED_RANK') {
  678: 	$message = &mt('You have provided an invalid ranking');
  679: 	if ($target ne 'tex') {
  680: 	    $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
  681: 	}
  682: 	$bgcolor=$possiblecolors{'not_charged_try'};
  683: 	$button=1;
  684:     } elsif ($award eq 'INVALID_FILETYPE') {
  685: 	$message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
  686: 	$bgcolor=$possiblecolors{'not_charged_try'};
  687: 	$button=1;
  688:     } elsif ($award eq 'SIG_FAIL') {
  689: 	my ($used,$min,$max)=split(':',$awardmsg);
  690: 	my $word;
  691: 	if ($used < $min) { $word=&mt('more'); }
  692: 	if ($used > $max) { $word=&mt('fewer'); }
  693: 	$message = &mt("Submission not graded.  Use [_2] digits.",$used,$word);
  694: 	$bgcolor=$possiblecolors{'not_charged_try'};
  695: 	$button=1;
  696:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
  697: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
  698: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  699: 	$bgcolor=$possiblecolors{'not_charged_try'};
  700: 	$button=1;
  701:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
  702: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
  703: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  704: 	$bgcolor=$possiblecolors{'not_charged_try'};
  705: 	$button=1;
  706:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
  707: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
  708: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  709: 	$bgcolor=$possiblecolors{'not_charged_try'};
  710: 	$button=1;
  711:     } elsif ($award eq 'UNIT_NOTNEEDED') {
  712: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
  713: 	$bgcolor=$possiblecolors{'not_charged_try'};
  714: 	$button=1;
  715:     } elsif ($award eq 'NO_UNIT') {
  716: 	$message = &mt("Units required").'.';
  717: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
  718: 	$bgcolor=$possiblecolors{'not_charged_try'};
  719: 	$button=1;
  720:     } elsif ($award eq 'COMMA_FAIL') {
  721: 	$message = &mt("Proper comma separation is required").'.';
  722: 	$bgcolor=$possiblecolors{'not_charged_try'};
  723: 	$button=1;
  724:     } elsif ($award eq 'BAD_FORMULA') {
  725: 	$message = &mt("Unable to understand formula");
  726: 	$bgcolor=$possiblecolors{'not_charged_try'};
  727: 	$button=1;
  728:     } elsif ($award eq 'INCORRECT') {
  729: 	$message = &mt("Incorrect").'.';
  730: 	$bgcolor=$possiblecolors{'charged_try'};
  731: 	$button=1;
  732:     } elsif ($award eq 'SUBMITTED') {
  733: 	$message = &mt("Your submission has been recorded.");
  734: 	$bgcolor=$possiblecolors{'no_grade'};
  735: 	$button=1;
  736:     } elsif ($award eq 'DRAFT') {
  737: 	$message = &mt("A draft copy has been saved.");
  738: 	$bgcolor=$possiblecolors{'not_charged_try'};
  739: 	$button=1;
  740:     } elsif ($award eq 'ASSIGNED_SCORE') {
  741: 	$message = &mt("A score has been assigned.");
  742: 	$bgcolor=$possiblecolors{'correct'};
  743: 	$button=0;
  744:     } elsif ($award eq '') {
  745: 	if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
  746: 	    $message = &mt("Nothing submitted.");
  747: 	    $bgcolor=$possiblecolors{'charged_try'};
  748: 	} else {
  749: 	    $bgcolor=$possiblecolors{'not_charged_try'};
  750: 	}
  751: 	$button=1;
  752:     } else {
  753: 	$message = &mt("Unknown message").": $award";
  754: 	$button=1;
  755:     }
  756:     my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
  757:     foreach my $resid(@Apache::inputtags::response){
  758:         if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
  759: 	    $message.='<br />';
  760: 	    my @files = split(/\s*,\s*/,
  761: 			      $Apache::lonhomework::history{"resource.$part.$resid.handback"});
  762: 	    my $file_msg;
  763: 	    foreach my $file (@files) {
  764: 		$file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
  765: 	    }
  766: 	    $message .= &mt('Returned file(s): [_1]',$file_msg);
  767: 	}
  768:     }
  769: 
  770:     if (lc($Apache::lonhomework::problemstatus) eq 'no'  && 
  771: 	$Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
  772: 	$message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
  773: 	$bgcolor=$possiblecolors{'no_grade'};
  774: 	$button=1;
  775:     }
  776:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
  777: 	!$added_computer_text && $target ne 'tex') {
  778: 	$message.= $computer;
  779: 	$added_computer_text=1;
  780:     }
  781:     return ($button,$bgcolor,$message,$previousmsg);
  782: }
  783: 
  784: sub markup_unit {
  785:     my ($unit,$target)=@_;
  786:     if ($target eq 'tex') {
  787: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
  788:     } else {
  789: 	return "<tt>".$unit."</tt>";
  790:     }
  791: }
  792: 
  793: sub removealldata {
  794:     my ($id)=@_;
  795:     foreach my $key (keys(%Apache::lonhomework::results)) {
  796: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  797: 	    &Apache::lonxml::debug("Removing $key");
  798: 	    delete($Apache::lonhomework::results{$key});
  799: 	}
  800:     }
  801: }
  802: 
  803: sub hidealldata {
  804:     my ($id)=@_;
  805:     foreach my $key (keys(%Apache::lonhomework::results)) {
  806: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  807: 	    &Apache::lonxml::debug("Hidding $key");
  808: 	    my $newkey=$key;
  809: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
  810: 	    $Apache::lonhomework::results{$newkey}=
  811: 		$Apache::lonhomework::results{$key};
  812: 	    delete($Apache::lonhomework::results{$key});
  813: 	}
  814:     }
  815: }
  816: 
  817: sub setgradedata {
  818:     my ($award,$msg,$id,$previously_used) = @_;
  819:     if ($Apache::lonhomework::scantronmode && 
  820: 	&Apache::lonnet::validCODE($env{'form.CODE'})) {
  821: 	$Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
  822:     } elsif ($Apache::lonhomework::scantronmode && 
  823: 	     $env{'form.CODE'} eq '' &&
  824: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
  825: 	$Apache::lonhomework::results{"resource.CODE"}='';
  826:     }
  827: 
  828:     if (!$Apache::lonhomework::scantronmode &&
  829: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  830: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
  831: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
  832: 	return '';
  833:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  834: 	      /^correct/ || $Apache::lonhomework::scantronmode ||
  835: 	      lc($Apache::lonhomework::problemstatus) eq 'no') {
  836:         # the student doesn't already have it correct,
  837: 	# or we are in a mode (scantron orno problem status) where a correct 
  838:         # can become incorrect
  839: 	# handle assignment of tries and solved status
  840: 	my $solvemsg;
  841: 	if ($Apache::lonhomework::scantronmode) {
  842: 	    $solvemsg='correct_by_scantron';
  843: 	} else {
  844: 	    $solvemsg='correct_by_student';
  845: 	}
  846: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  847: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
  848: 	}
  849: 	if ( $award eq 'ASSIGNED_SCORE') {
  850: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  851: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  852: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  853: 		$solvemsg;
  854: 	    my $numawards=scalar(@Apache::inputtags::response);
  855: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  856: 	    foreach my $res (@Apache::inputtags::response) {
  857: 		$Apache::lonhomework::results{"resource.$id.awarded"}+=
  858: 		    $Apache::lonhomework::results{"resource.$id.$res.awarded"};
  859: 	    }
  860: 	    if ($numawards > 0) {
  861: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
  862: 		    $numawards;
  863: 	    }
  864: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  865: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  866: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  867: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  868: 		$solvemsg;
  869: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  870: 	} elsif ( $award eq 'INCORRECT' ) {
  871: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  872: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  873: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  874: 		$Apache::lonhomework::scantronmode) {
  875: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  876: 	    }
  877: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  878: 		'incorrect_attempted';
  879: 	} elsif ( $award eq 'SUBMITTED' ) {
  880: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  881: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  882: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  883: 		'ungraded_attempted';
  884: 	} elsif ( $award eq 'DRAFT' ) {
  885: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
  886: 	} elsif ( $award eq 'NO_RESPONSE' ) {
  887: 	    #no real response so delete any data that got stored
  888: 	    &removealldata($id);
  889: 	    return '';
  890: 	} else {
  891: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  892: 		'incorrect_attempted';
  893: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  894: 		$Apache::lonhomework::scantronmode) {
  895: 		$Apache::lonhomework::results{"resource.$id.tries"} =
  896: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  897: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  898: 	    }
  899: 	}
  900: 	if (defined($msg)) {
  901: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
  902: 	}
  903: 	# did either of the overall awards chage? If so ignore the 
  904: 	# previous check
  905: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
  906: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
  907: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
  908: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
  909: 	    # check if this was a previous submission if it was delete the
  910: 	    # unneeded data and update the previously_used attribute
  911: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
  912: 		if (lc($Apache::lonhomework::problemstatus) ne 'no') {
  913: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
  914: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  915: 		}
  916: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  917: 		#delete all data as they student didn't do anything, but save
  918: 		#the list of collaborators.
  919: 		&removealldata($id);
  920: 		#and since they didn't do anything we were never here
  921: 		return '';
  922: 	    } else {
  923: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
  924: 	    }
  925: 	}
  926:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
  927: 	      /^correct/ ) {
  928: 	#delete all data as they student already has it correct
  929: 	&removealldata($id);
  930: 	#and since they didn't do anything we were never here
  931: 	return '';
  932:     }
  933:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
  934:     if ($award eq 'SUBMITTED') {
  935: 	&Apache::response::add_to_gradingqueue();
  936:     }
  937: }
  938: 
  939: sub grade {
  940:     my ($target) = @_;
  941:     my $id = $Apache::inputtags::part;
  942:     my $response='';
  943:     if ( defined $env{'form.submitted'}) {
  944: 	my (@awards,@msgs);
  945: 	foreach $response (@Apache::inputtags::response) {
  946: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  947: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  948: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
  949: 	    push (@awards,$value);
  950: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
  951: 	    &Apache::lonxml::debug("got message $value from $response for $id");
  952: 	    push (@msgs,$value);
  953: 	}
  954: 	my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
  955: 	my $previously_used;
  956: 	if ( $#Apache::inputtags::previous eq $#awards ) {
  957: 	    my $match=0;
  958: 	    my @matches;
  959: 	    foreach my $versionar (@Apache::inputtags::previous_version) {
  960: 		foreach my $version (@$versionar) {
  961: 		    $matches[$version]++;
  962: 		}
  963: 	    }
  964: 	    foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
  965: 	    if ($match) {
  966: 		$previously_used = 'PREVIOUSLY_LAST';
  967: 		foreach my $value (@Apache::inputtags::previous) {
  968: 		    if ($value eq 'PREVIOUSLY_USED' ) {
  969: 			$previously_used = $value;
  970: 			last;
  971: 		    }
  972: 		}
  973: 	    }
  974: 	}
  975: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
  976: 	&setgradedata($finalaward,$msg,$id,$previously_used);
  977:     }
  978:     return '';
  979: }
  980: 
  981: sub gradestatus {
  982:     my ($id,$target) = @_;
  983:     my $showbutton = 1;
  984:     my $bgcolor = '';
  985:     my $message = '';
  986:     my $latemessage = '';
  987:     my $trystr='';
  988:     my $button='';
  989:     my $previousmsg='';
  990: 
  991:     my $status = $Apache::inputtags::status['-1'];
  992:     &Apache::lonxml::debug("gradestatus has :$status:");
  993:     if ( $status ne 'CLOSED' 
  994: 	 && $status ne 'UNAVAILABLE' 
  995: 	 && $status ne 'INVALID_ACCESS' 
  996: 	 && $status ne 'NEEDS_CHECKIN' 
  997: 	 && $status ne 'NOT_IN_A_SLOT') {  
  998: 	my $award = $Apache::lonhomework::history{"resource.$id.award"};
  999: 	my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
 1000: 	my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
 1001: 	my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
 1002: 	my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
 1003: 	&Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
 1004: 	if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
 1005: 	    &Apache::lonxml::debug('Getting message');
 1006: 	    ($showbutton,$bgcolor,$message,$previousmsg) =
 1007: 		&decideoutput($award,$awarded,$awardmsg,$solved,$previous,
 1008: 			      $target);
 1009: 	    if ($target eq 'tex') {
 1010: 		$message='\vskip 2 mm '.$message.' ';
 1011: 	    } else {
 1012: 		$message="<td bgcolor=\"$bgcolor\">$message</td>";
 1013: 		if ($previousmsg) {
 1014: 		    $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
 1015: 		}
 1016: 	    }
 1017: 	}
 1018: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
 1019: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
 1020: 	&Apache::lonxml::debug("got maxtries of :$maxtries:");
 1021: 	#if tries are set to negative turn off the Tries/Button and messages
 1022: 	if (defined($maxtries) && $maxtries < 0) { return ''; }
 1023: 	if ( $tries eq '' ) { $tries = '0'; }
 1024: 	if ( $maxtries eq '' ) { $maxtries = '2'; } 
 1025: 	if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
 1026: 	my $tries_text=&mt('Tries');
 1027: 	if ( $Apache::lonhomework::type eq 'survey' ||
 1028: 	     $Apache::lonhomework::parsing_a_task) {
 1029: 	    $tries_text=&mt('Submissions');
 1030: 	}
 1031: 	if ( $showbutton ) {
 1032: 	    if ($target eq 'tex') {
 1033: 		if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
 1034: 		    $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
 1035: 		} else {
 1036: 		    $trystr = '\vskip 0 mm ';
 1037: 		}
 1038: 	    } else {
 1039: 		$trystr = "<td><nobr>".$tries_text." $tries";
 1040: 		if ($Apache::lonhomework::parsing_a_task) {
 1041: 		} elsif($env{'request.state'} ne 'construct') {
 1042: 		    $trystr.="/$maxtries";
 1043: 		} else {
 1044: 		    if (defined($Apache::inputtags::params{'maxtries'})) {
 1045: 			$trystr.="/".$Apache::inputtags::params{'maxtries'};
 1046: 		    }
 1047: 		}
 1048: 		$trystr.="</nobr></td>";
 1049: 	    }
 1050: 	}
 1051: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
 1052: 	if ( $showbutton ) { 
 1053: 	    if ($target ne 'tex') {
 1054: 		$button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
 1055: 	    }
 1056: 	}
 1057: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
 1058: 	    #last submissions was after due date
 1059: 	    $latemessage=&mt(' The last submission was after the Due Date ');;
 1060: 	    if ($target eq 'web') {
 1061: 		$latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
 1062: 	    }
 1063: 	}
 1064:     }
 1065:     my $output= $previousmsg.$latemessage.$message.$trystr;
 1066:     if ($output =~ /^\s*$/) {
 1067: 	return $button;
 1068:     } else {
 1069: 	if ($target eq 'tex') {
 1070: 	    return $button.' \vskip 0 mm '.$output.' ';
 1071: 	} else {
 1072: 	    return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
 1073: 	}
 1074:     }
 1075: }
 1076: 1;
 1077: __END__
 1078:  

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