File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.125: download - view: text, annotated - select for diffs
Mon Dec 22 22:24:21 2003 UTC (20 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#2519 , no maximm number of tries in CSTR space, so don't make one up

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

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