File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.148: download - view: text, annotated - select for diffs
Thu Jul 15 19:44:54 2004 UTC (19 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_1_1_99_3, HEAD
 - fixes double display of 'Computer's Answer now shown' BUG#3199

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.148 2004/07/15 19:44:54 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: 
   34: BEGIN {
   35:     &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
   36: }
   37: 
   38: 
   39: sub initialize_inputtags {
   40:     # list of current input ids
   41:     @Apache::inputtags::input=();
   42:     # list of all input ids seen in this problem
   43:     @Apache::inputtags::inputlist=();
   44:     # list of all current response ids
   45:     @Apache::inputtags::response=();
   46:     # list of all response ids seen in this problem
   47:     @Apache::inputtags::responselist=();
   48:     # list of whether or not a specific response was previously used
   49:     @Apache::inputtags::previous=();
   50:     # submission it was used in
   51:     @Apache::inputtags::previous_version=();
   52:     # id of current part, 0 means that no part is current 
   53:     # (inside <problem> only
   54:     $Apache::inputtags::part='';
   55:     # list of all part ids seen
   56:     @Apache::inputtags::partlist=();
   57:     # list of problem date statuses, the first element is for <problem>
   58:     # if there is a second element it is for the current <part>
   59:     @Apache::inputtags::status=();
   60:     # hash of defined params for the current response
   61:     %Apache::inputtags::params=();
   62:     # list of all ids, for <import>, these get join()ed and prepended
   63:     @Apache::inputtags::import=();
   64:     # list of all import ids seen
   65:     @Apache::inputtags::importlist=();
   66:     # just used to note whether we have seen a response that isn't in a part
   67:     $Apache::inputtags::response_with_no_part=0;
   68:     # storage location so the begin <*response> tag can generate the correct
   69:     # answer string for display by the <textline />
   70:     %Apache::inputtags::answertxt=();
   71: }
   72: 
   73: sub check_for_duplicate_ids {
   74:     my %check;
   75:     foreach my $id (@Apache::inputtags::partlist,
   76: 		    @Apache::inputtags::responselist,
   77: 		    @Apache::inputtags::importlist) {
   78: 	$check{$id}++;
   79:     }
   80:     my @duplicates;
   81:     foreach my $id (sort(keys(%check))) {
   82: 	if ($check{$id} > 1) {
   83: 	    push(@duplicates,$id);
   84: 	}
   85:     }
   86:     if (@duplicates) {
   87: 	&Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
   88:     }
   89: }
   90: 
   91: sub start_input {
   92:     my ($parstack,$safeeval)=@_;
   93:     my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
   94:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
   95:     push (@Apache::inputtags::input,$id);
   96:     push (@Apache::inputtags::inputlist,$id);
   97:     return $id;
   98: }
   99: 
  100: sub end_input {
  101:     pop @Apache::inputtags::input;
  102:     return '';
  103: }
  104: 
  105: sub addchars {
  106:     my ($fieldid,$addchars)=@_;
  107:     my $output='';
  108:     foreach (split(/\,/,$addchars)) {
  109: 	$output.='<a href="javascript:void(document.forms.lonhomework.'.
  110: 	    $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
  111:     }
  112:     return $output;
  113: }
  114: 
  115: sub start_textfield {
  116:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  117:     my $result = "";
  118:     my $id = &start_input($parstack,$safeeval);
  119:     my $resid=$Apache::inputtags::response[-1];
  120:     if ($target eq 'web') {
  121: 	$Apache::lonxml::evaluate--;
  122: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  123: 	    my $partid=$Apache::inputtags::part;
  124: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
  125: 	    my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
  126: 	    if ( $cols eq '') { $cols = 80; }
  127: 	    my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
  128: 	    if ( $rows eq '') { $rows = 16; }
  129: 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
  130: 	    $result='';
  131: 	    if ($addchars) {
  132: 		$result.=&addchars('HWVAL_'.$resid,$addchars);
  133: 	    }
  134: 	    push @Apache::lonxml::htmlareafields,'HWVAL_'.$resid;
  135: 	    $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
  136: 		"rows=\"$rows\" cols=\"$cols\">".$oldresponse;
  137: 	    if ($oldresponse ne '') {
  138: 
  139: 		#get rid of any startup text if the user has already responded
  140: 		&Apache::lonxml::get_all_text("/textfield",$parser);
  141: 	    }
  142: 	} else {
  143: 	    #right or wrong don't show it
  144: 	    #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
  145: 	    $result='';
  146: 	    #get rid of any startup text
  147: 	    &Apache::lonxml::get_all_text("/textfield",$parser);
  148: 	}
  149:     } elsif ($target eq 'grade') {
  150: 	my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser);
  151: 	if ($seedtext eq $ENV{'form.HWVAL_'.$resid}) {
  152: 	    # if the seed text is still there it wasn't a real submission
  153: 	    $ENV{'form.HWVAL_'.$resid}='';
  154: 	}
  155:     } elsif ($target eq 'edit') {
  156: 	$result.=&Apache::edit::tag_start($target,$token);
  157: 	$result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
  158: 	$result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
  159: 	$result.=&Apache::edit::text_arg
  160: 	    ('Click-On Texts (comma sep):','addchars',$token,10);
  161: 	my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser);
  162: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
  163:     } elsif ($target eq 'modified') {
  164: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  165: 						     $safeeval,'rows','cols',
  166: 						     'addchars');
  167: 	if ($constructtag) {
  168: 	    $result = &Apache::edit::rebuild_tag($token);
  169: 	} else {
  170: 	    $result=$token->[4];
  171: 	}
  172: 	$result.=&Apache::edit::modifiedfield("/textfield",$parser);
  173:     } elsif ($target eq 'tex') {
  174: 	my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
  175: 	my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
  176: 	if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
  177: 	    $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
  178: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
  179: 	    $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
  180: 	} else {
  181: 	    my $TeXwidth=$width_of_box/80;
  182: 	    $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
  183: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
  184: 	    $result.='}}}\vskip 2 mm ';
  185: 	}
  186:     }
  187:     return $result;
  188: }
  189: 
  190: sub end_textfield {
  191:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  192:     my $result;
  193:     if ($target eq 'web') {
  194: 	$Apache::lonxml::evaluate++;
  195: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  196: 	    return "</textarea>";
  197: 	}
  198:     } elsif ($target eq 'edit') {
  199: 	$result=&Apache::edit::end_table();
  200:     }
  201:     &end_input;
  202:     return $result;
  203: }
  204: 
  205: sub start_textline {
  206:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  207:     my $result = "";
  208:     if ($target eq 'web') {
  209: 	$Apache::lonxml::evaluate--;
  210: 	my $partid=$Apache::inputtags::part;
  211: 	my $id=$Apache::inputtags::response[-1];
  212: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  213: 	    my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  214: 	    my $maxlength;
  215: 	    if ($size eq '') { $size=20; } else {
  216: 		if ($size < 20) { $maxlength=$size; }
  217: 	    }
  218: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
  219: 	    if ($Apache::lonhomework::type ne 'exam') {
  220: 		my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
  221: 		$result='';
  222: 		if ($addchars) {
  223: 		    $result.=&addchars('HWVAL_'.$id,$addchars);
  224: 		}
  225: 		$result.= '<input type="text" name="HWVAL_'.$id.'" value="'.
  226: 		    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
  227: 	    }
  228: 	} else {
  229: 	    #right or wrong don't show what was last typed in.
  230: 	    $result='<i>'.$Apache::inputtags::answertxt{$id}.'</i>';
  231: 	    #$result='';
  232: 	}
  233:     } elsif ($target eq 'edit') {
  234: 	$result=&Apache::edit::tag_start($target,$token);
  235: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
  236: 	    &Apache::edit::text_arg
  237: 	    ('Click-On Texts (comma sep):','addchars',$token,10)."</td></tr>";
  238: 	$result.=&Apache::edit::end_table;
  239:     } elsif ($target eq 'modified') {
  240: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size','addchars');
  241: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  242:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  243: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  244: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
  245: 	$result='\framebox['.$size.'][s]{\tiny\strut}';
  246:     }
  247:     return $result;
  248: }
  249: 
  250: sub end_textline {
  251:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  252:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  253:     elsif ($target eq 'edit') { return ('','no'); }
  254:     return "";
  255: }
  256: 
  257: sub start_hiddenline {
  258:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  259:     my $result = "";
  260:     if ($target eq 'web') {
  261: 	$Apache::lonxml::evaluate--;
  262: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  263: 	    my $partid=$Apache::inputtags::part;
  264: 	    my $id=$Apache::inputtags::response[-1];
  265: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
  266: 	    if ($Apache::lonhomework::type ne 'exam') {
  267: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
  268: 		    $oldresponse.'" />';
  269: 	    }
  270: 	}
  271:     } elsif ($target eq 'edit') {
  272: 	$result=&Apache::edit::tag_start($target,$token);
  273: 	$result.=&Apache::edit::end_table;
  274:     }
  275:     return $result;
  276: }
  277: 
  278: sub end_hiddenline {
  279:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  280:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  281:     elsif ($target eq 'edit') { return ('','no'); }
  282:     return "";
  283: }
  284: 
  285: sub checkstatus {
  286:     my ($value,$awardref,$msgref)=@_;
  287:     for (my $i=0;$i<=$#$awardref;$i++) {
  288: 	if ($$awardref[$i] eq $value) {
  289: 	    return ($$awardref[$i],$$msgref[$i]);
  290: 	}
  291:     }
  292:     return(undef,undef);
  293: }
  294: 
  295: sub finalizeawards {
  296:     my ($awardref,$msgref)=@_;
  297:     my $result=undef;
  298:     my $award;
  299:     my $msg;
  300:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
  301:     if ($result eq '' ) {
  302: 	my $blankcount;
  303: 	foreach $award (@$awardref) {
  304: 	    if ($award eq '') {
  305: 		$result='MISSING_ANSWER';
  306: 		$blankcount++;
  307: 	    }
  308: 	}
  309: 	if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
  310:     }
  311:     if (defined($result)) { return ($result,$msg); }
  312:     foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
  313: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
  314: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
  315: 			       'UNIT_FAIL', 'NO_UNIT',
  316: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
  317: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
  318: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
  319: 			       'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
  320: 			       'APPROX_ANS', 'EXACT_ANS') {
  321: 	($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
  322: 	if (defined($result)) { return ($result,$msg); }
  323:     }
  324:     return ('ERROR',undef);
  325: }
  326: 
  327: sub decideoutput {
  328:     my ($award,$awardmsg,$solved,$previous,$target)=@_;
  329:     my $message='';
  330:     my $button=0;
  331:     my $previousmsg;
  332:     my $bgcolor='orange';
  333:     my $added_computer_text=0;
  334:     my %possiblecolors =
  335: 	( 'correct' => '#aaffaa',
  336: 	  'charged_try' => '#ffaaaa',
  337: 	  'not_charged_try' => '#ffffaa',
  338: 	  'no_message' => '#fffff',
  339: 	  );
  340:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
  341:     
  342:     if      ($solved =~ /^correct/) {
  343: 	if ($award eq 'ASSIGNED_SCORE') {
  344: 	    $message = &mt("A score has been assigned.");
  345: 	} else {
  346: 	    if ($target eq 'tex') {
  347: 		$message = '\textbf{'.&mt('You are correct.').'}';
  348: 	    } else {
  349: 		$message = "<b>".&mt('You are correct.')."</b>";
  350: 	    }
  351: 	    $message.=" ".&mt("Computer's answer now shown.");
  352: 	    $added_computer_text=1;
  353: 	    unless ($ENV{'course.'.
  354: 			     $ENV{'request.course.id'}.
  355: 			     '.disable_receipt_display'} eq 'yes') { 
  356: 		$message.=(($target eq 'web')?'<br />':' ').
  357: 		    &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
  358: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  359: 	    }
  360: 	}
  361: 	$bgcolor=$possiblecolors{'correct'};
  362: 	$button=0;
  363: 	$previousmsg='';
  364:     } elsif ($solved =~ /^excused/) {
  365: 	if ($target eq 'tex') {
  366: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
  367: 	} else {
  368: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
  369: 	}
  370: 	$bgcolor=$possiblecolors{'charged_try'};
  371: 	$button=0;
  372: 	$previousmsg='';
  373:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  374: 	if ($solved =~ /^incorrect/ || $solved eq '') {
  375: 	    $message = &mt("Incorrect").".";
  376: 	    $bgcolor=$possiblecolors{'charged_try'};
  377: 	    $button=1;
  378: 	} else {
  379: 	    if ($target eq 'tex') {
  380: 		$message = '\textbf{'.&mt('You are correct.').'}';
  381: 	    } else {
  382: 		$message = "<b>".&mt('You are correct.')."</b>";
  383: 	    }
  384: 	    $message.=" ".&mt("Computer's answer now shown.");
  385: 	    $added_computer_text=1;
  386: 	    unless ($ENV{'course.'.
  387: 			     $ENV{'request.course.id'}.
  388: 			     '.disable_receipt_display'} eq 'yes') { 
  389: 		$message.=(($target eq 'web')?'<br />':' ').
  390: 		    'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
  391: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  392: 	    }
  393: 	    $bgcolor=$possiblecolors{'correct'};
  394: 	    $button=0;
  395: 	    $previousmsg='';
  396: 	}
  397:     } elsif ($award eq 'NO_RESPONSE') {
  398: 	$message = '';
  399: 	$bgcolor=$possiblecolors{'no_feedback'};
  400: 	$button=1;
  401:     } elsif ($award eq 'MISSING_ANSWER') {
  402: 	$message = &mt('Some items were not submitted.');
  403: 	$bgcolor=$possiblecolors{'not_charged_try'};
  404: 	$button = 1;
  405:     } elsif ($award eq 'ERROR') {
  406: 	$message = &mt('An error occured while grading your answer.');
  407: 	$bgcolor=$possiblecolors{'not_charged_try'};
  408: 	$button = 1;
  409:     } elsif ($award eq 'TOO_LONG') {
  410: 	$message = &mt("The submitted answer was too long.");
  411: 	$bgcolor=$possiblecolors{'not_charged_try'};
  412: 	$button=1;
  413:     } elsif ($award eq 'WANTED_NUMERIC') {
  414: 	$message = &mt("This question expects a numeric answer.");
  415: 	$bgcolor=$possiblecolors{'not_charged_try'};
  416: 	$button=1;
  417:     } elsif ($award eq 'MISORDERED_RANK') {
  418: 	$message = &mt('You have provided an invalid ranking');
  419: 	if ($target ne 'tex') {
  420: 	    $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems').'.';
  421: 	}
  422: 	$bgcolor=$possiblecolors{'not_charged_try'};
  423: 	$button=1;
  424:     } elsif ($award eq 'INVALID_FILETYPE') {
  425: 	$message = &mt('The filetype extension of the file you uploaded is not allowed.');
  426: 	$bgcolor=$possiblecolors{'not_charged_try'};
  427: 	$button=1;
  428:     } elsif ($award eq 'SIG_FAIL') {
  429: 	my ($used,$min,$max)=split(':',$awardmsg);
  430: 	my $word;
  431: 	if ($used < $min) { $word=&mt('more'); }
  432: 	if ($used > $max) { $word=&mt('fewer'); }
  433: 	$message = &mt("Submission not graded.  Use [_2] digits.",$used,$word);
  434: 	$bgcolor=$possiblecolors{'not_charged_try'};
  435: 	$button=1;
  436:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
  437: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
  438: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  439: 	$bgcolor=$possiblecolors{'not_charged_try'};
  440: 	$button=1;
  441:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
  442: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]"','<tt>'.$awardmsg.'</tt>.');
  443: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  444: 	$bgcolor=$possiblecolors{'not_charged_try'};
  445: 	$button=1;
  446:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
  447: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units ','<tt>'.$awardmsg.'</tt>.');
  448: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  449: 	$bgcolor=$possiblecolors{'not_charged_try'};
  450: 	$button=1;
  451:     } elsif ($award eq 'UNIT_NOTNEEDED') {
  452: 	$message = &mt('Only a number required. Computer reads units of "[_1]"','<tt>'.$awardmsg.'</tt>.');
  453: 	$bgcolor=$possiblecolors{'not_charged_try'};
  454: 	$button=1;
  455:     } elsif ($award eq 'NO_UNIT') {
  456: 	$message = &mt("Units required").'.';
  457: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
  458: 	$bgcolor=$possiblecolors{'not_charged_try'};
  459: 	$button=1;
  460:     } elsif ($award eq 'BAD_FORMULA') {
  461: 	$message = &mt("Unable to understand formula");
  462: 	$bgcolor=$possiblecolors{'not_charged_try'};
  463: 	$button=1;
  464:     } elsif ($award eq 'INCORRECT') {
  465: 	$message = &mt("Incorrect").'.';
  466: 	$bgcolor=$possiblecolors{'charged_try'};
  467: 	$button=1;
  468:     } elsif ($award eq 'SUBMITTED') {
  469: 	$message = &mt("Your submission has been recorded.");
  470: 	$bgcolor=$possiblecolors{'correct'};
  471: 	$button=1;
  472:     } elsif ($award eq 'DRAFT') {
  473: 	$message = &mt("A draft copy has been saved.");
  474: 	$bgcolor=$possiblecolors{'not_charged_try'};
  475: 	$button=1;
  476:     } elsif ($award eq 'ASSIGNED_SCORE') {
  477: 	$message = &mt("A score has been assigned.");
  478: 	$bgcolor=$possiblecolors{'correct'};
  479: 	$button=0;
  480:     } elsif ($award eq '') {
  481: 	$bgcolor=$possiblecolors{'not_charged_try'};
  482: 	$button=1;
  483:     } else {
  484: 	$message = &mt("Unknown message").": $award";
  485: 	$button=1;
  486:     }
  487:     if (lc($Apache::lonhomework::problemstatus) eq 'no'  && 
  488: 	$Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
  489: 	$message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
  490: 	$bgcolor=$possiblecolors{'correct'};
  491: 	$button=1;
  492:     }
  493:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
  494: 	!$added_computer_text) {
  495: 	$message.=" ".&mt("Computer's answer now shown.");
  496: 	$added_computer_text=1;
  497:     }
  498:     return ($button,$bgcolor,$message,$previousmsg);
  499: }
  500: 
  501: sub removealldata {
  502:     my ($id)=@_;
  503:     foreach my $key (keys(%Apache::lonhomework::results)) {
  504: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  505: 	    &Apache::lonxml::debug("Removing $key");
  506: 	    delete($Apache::lonhomework::results{$key});
  507: 	}
  508:     }
  509: }
  510: 
  511: sub hidealldata {
  512:     my ($id)=@_;
  513:     foreach my $key (keys(%Apache::lonhomework::results)) {
  514: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  515: 	    &Apache::lonxml::debug("Hidding $key");
  516: 	    my $newkey=$key;
  517: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
  518: 	    $Apache::lonhomework::results{$newkey}=
  519: 		$Apache::lonhomework::results{$key};
  520: 	    delete($Apache::lonhomework::results{$key});
  521: 	}
  522:     }
  523: }
  524: 
  525: sub setgradedata {
  526:     my ($award,$msg,$id,$previously_used) = @_;
  527:     # if the student already has it correct, don't modify the status
  528:     if ($Apache::lonhomework::scantronmode && defined($ENV{'form.CODE'})) {
  529: 	$Apache::lonhomework::results{"resource.CODE"}=$ENV{'form.CODE'};
  530:     }
  531:     if (!$Apache::lonhomework::scantronmode &&
  532: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  533: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
  534: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
  535: 	return '';
  536:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  537: 	      /^correct/ || $Apache::lonhomework::scantronmode ||
  538: 	      lc($Apache::lonhomework::problemstatus) eq 'no') {
  539: 	#handle assignment of tries and solved status
  540: 	my $solvemsg;
  541: 	if ($Apache::lonhomework::scantronmode) {
  542: 	    $solvemsg='correct_by_scantron';
  543: 	} else {
  544: 	    $solvemsg='correct_by_student';
  545: 	}
  546: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  547: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
  548: 	}
  549: 	if ( $award eq 'ASSIGNED_SCORE') {
  550: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  551: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  552: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  553: 		$solvemsg;
  554: 	    my $numawards=scalar(@Apache::inputtags::response);
  555: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  556: 	    foreach my $res (@Apache::inputtags::response) {
  557: 		$Apache::lonhomework::results{"resource.$id.awarded"}+=
  558: 		    $Apache::lonhomework::results{"resource.$id.$res.awarded"};
  559: 	    }
  560: 	    if ($numawards > 0) {
  561: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
  562: 		    $numawards;
  563: 	    }
  564: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  565: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  566: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  567: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  568: 		$solvemsg;
  569: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  570: 	} elsif ( $award eq 'INCORRECT' ) {
  571: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  572: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  573: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no') {
  574: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  575: 	    }
  576: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  577: 		'incorrect_attempted';
  578: 	} elsif ( $award eq 'SUBMITTED' ) {
  579: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  580: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  581: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  582: 		'ungraded_attempted';
  583: 	} elsif ( $award eq 'DRAFT' ) {
  584: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
  585: 	} elsif ( $award eq 'NO_RESPONSE' ) {
  586: 	    #no real response so delete any data that got stored
  587: 	    &removealldata($id);
  588: 	    return '';
  589: 	} else {
  590: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  591: 		'incorrect_attempted';
  592: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no') {
  593: 		$Apache::lonhomework::results{"resource.$id.tries"} =
  594: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  595: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  596: 	    }
  597: 	}
  598: 	if (defined($msg)) {
  599: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
  600: 	}
  601: 	# did either of the overall awards chage? If so ignore the 
  602: 	# previous check
  603: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
  604: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
  605: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
  606: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
  607: 	    # check if this was a previous submission if it was delete the
  608: 	    # unneeded data and update the previously_used attribute
  609: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
  610: 		if (lc($Apache::lonhomework::problemstatus) ne 'no') {
  611: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
  612: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  613: 		}
  614: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  615: 		#delete all data as they student didn't do anything, but save
  616: 		#the list of collaborators.
  617: 		&removealldata($id);
  618: 		#and since they didn't do anything we were never here
  619: 		return '';
  620: 	    } else {
  621: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
  622: 	    }
  623: 	}
  624:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
  625: 	      /^correct/ ) {
  626: 	#delete all data as they student already has it correct
  627: 	&removealldata($id);
  628: 	#and since they didn't do anything we were never here
  629: 	return '';
  630:     }
  631:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
  632: }
  633: 
  634: sub grade {
  635:     my ($target) = @_;
  636:     my $id = $Apache::inputtags::part;
  637:     my $response='';
  638:     if ( defined $ENV{'form.submitted'}) {
  639: 	my (@awards,@msgs);
  640: 	foreach $response (@Apache::inputtags::response) {
  641: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  642: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  643: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
  644: 	    push (@awards,$value);
  645: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
  646: 	    &Apache::lonxml::debug("got message $value from $response for $id");
  647: 	    push (@msgs,$value);
  648: 	}
  649: 	my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
  650: 	my $previously_used;
  651: 	if ( $#Apache::inputtags::previous eq $#awards ) {
  652: 	    my $match=0;
  653: 	    my @matches;
  654: 	    foreach my $versionar (@Apache::inputtags::previous_version) {
  655: 		foreach my $version (@$versionar) {
  656: 		    $matches[$version]++;
  657: 		}
  658: 	    }
  659: 	    foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
  660: 	    if ($match) {
  661: 		$previously_used = 'PREVIOUSLY_LAST';
  662: 		foreach my $value (@Apache::inputtags::previous) {
  663: 		    if ($value eq 'PREVIOUSLY_USED' ) {
  664: 			$previously_used = $value;
  665: 			last;
  666: 		    }
  667: 		}
  668: 	    }
  669: 	}
  670: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
  671: 	&setgradedata($finalaward,$msg,$id,$previously_used);
  672:     }
  673:     return '';
  674: }
  675: 
  676: sub gradestatus {
  677:     my ($id,$target) = @_;
  678:     my $showbutton = 1;
  679:     my $bgcolor = '';
  680:     my $message = '';
  681:     my $latemessage = '';
  682:     my $trystr='';
  683:     my $button='';
  684:     my $previousmsg='';
  685: 
  686:     my $status = $Apache::inputtags::status['-1'];
  687:     &Apache::lonxml::debug("gradestatus has :$status:");
  688:     if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
  689: 	 $status ne 'INVALID_ACCESS') {  
  690: 	my $award = $Apache::lonhomework::history{"resource.$id.award"};
  691: 	my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  692: 	my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  693: 	my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
  694: 	&Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
  695: 	if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
  696: 	    &Apache::lonxml::debug('Getting message');
  697: 	    ($showbutton,$bgcolor,$message,$previousmsg) =
  698: 		&decideoutput($award,$awardmsg,$solved,$previous,$target);
  699: 	    if ($target eq 'tex') {
  700: 		$message='\vskip 2 mm '.$message.' ';
  701: 	    } else {
  702: 		$message="<td bgcolor=\"$bgcolor\">$message</td>";
  703: 		if ($previousmsg) {
  704: 		    $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
  705: 		}
  706: 	    }
  707: 	}
  708: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  709: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  710: 	&Apache::lonxml::debug("got maxtries of :$maxtries:");
  711: 	#if tries are set to negative turn off the Tries/Button and messages
  712: 	if (defined($maxtries) && $maxtries < 0) { return ''; }
  713: 	if ( $tries eq '' ) { $tries = '0'; }
  714: 	if ( $maxtries eq '' ) { $maxtries = '2'; } 
  715: 	if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  716: 	my $tries_text=&mt('Tries');
  717: 	if ( $Apache::lonhomework::type eq 'survey') { $tries_text=&mt('Submissions'); }
  718: 	if ( $showbutton ) {
  719: 	    if ($target eq 'tex') {
  720: 		if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $ENV{'form.suppress_tries'} ne 'yes') {
  721: 		    $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
  722: 		} else {
  723: 		    $trystr = '\vskip 0 mm ';
  724: 		}
  725: 	    } else {
  726: 		$trystr = "<td><nobr>".$tries_text." $tries";
  727: 		if($ENV{'request.state'} ne 'construct') {
  728: 		    $trystr.="/$maxtries";
  729: 		} else {
  730: 		    if (defined($Apache::inputtags::params{'maxtries'})) {
  731: 			$trystr.="/".$Apache::inputtags::params{'maxtries'};
  732: 		    }
  733: 		}
  734: 		$trystr.="</nobr></td>";
  735: 	    }
  736: 	}
  737: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  738: 	if ( $showbutton ) { 
  739: 	    if ($target ne 'tex') {
  740: 		$button = '<input type="submit" name="submit" value="'.&mt('Submit Answer').'" />';
  741: 	    }
  742: 	}
  743: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  744: 	    #last submissions was after due date
  745: 	    if ($target eq 'tex') {
  746: 		$latemessage=' The last submission was after the Due Date ';
  747: 	    } else {
  748: 		$latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
  749: 	    }
  750: 	}
  751:     }
  752:     my $output= $previousmsg.$latemessage.$message.$trystr;
  753:     if ($output =~ /^\s*$/) {
  754: 	return $button;
  755:     } else {
  756: 	if ($target eq 'tex') {
  757: 	    return $button.' \vskip 0 mm '.$output.' ';
  758: 	} else {
  759: 	    return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
  760: 	}
  761:     }
  762: }
  763: 1;
  764: __END__
  765:  

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