File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.124: download - view: text, annotated - select for diffs
Tue Nov 25 23:04:58 2003 UTC (20 years, 5 months ago) by www
Branches: MAIN
CVS tags: version_1_1_X, version_1_1_1, version_1_1_0, version_1_0_99_3, version_1_0_99_2, version_1_0_99_1, version_1_0_99, HEAD
Be able to specify pre-fabricated special characters or phrases to insert
into text lines and fields (for example, German umlauts).

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