File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.102: download - view: text, annotated - select for diffs
Fri May 23 07:04:44 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fxes Bug #941, students submitting answers over 500 characters in length mad capa engine unhappy
- documented INVALID_FILETYPE
- finished off support for ERROR return (and documented)

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

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