File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.117: download - view: text, annotated - select for diffs
Mon Sep 22 20:49:01 2003 UTC (20 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added &Apache::response::show_answer() to better check for if problemstatus inhibiting should be occuring or not

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

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