File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.103: download - view: text, annotated - select for diffs
Fri May 23 16:26:28 2003 UTC (20 years, 11 months ago) by albertel
Branches: MAIN
CVS tags: version_0_99_1, version_0_99_0, conference_2003, HEAD
- fixes BUG#1364, produces an error messages if a duplicated id is seen while in CSTR space and viewing a problem.
- now tracks all part numbers seen, and tracks all import ids seen
- properly use @Apace::inputtags::response for the list of response ids in this part
  and responselist for all ids in the problem

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

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