File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.122: download - view: text, annotated - select for diffs
Thu Oct 30 20:52:54 2003 UTC (20 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#1974, <textarea>s delete the initial linefeeds if they exist, now checks old value and preserves the \n from the old value if it had been there.

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

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