File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.216: download - view: text, annotated - select for diffs
Tue Feb 27 19:43:16 2007 UTC (17 years, 2 months ago) by banghart
Branches: MAIN
CVS tags: HEAD
	Bug 5204. Don't show problem receipt numbers where meaningless.

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

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