File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.75: download - view: text, annotated - select for diffs
Wed Nov 27 19:25:56 2002 UTC (21 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Fixes BUG#965, multiple responses in the same part (AND style)
  now properly detect whether a situation is a previous submission or
  not, previously it would detect a situation in which the same individual
  response had appeared before (but not together) as a previously seen
  submission, now it properly only detects when a group of submissions
  is the same.

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.75 2002/11/27 19:25:56 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: 
   34: BEGIN {
   35:   &Apache::lonxml::register('Apache::inputtags',('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 problem date statuses, the first element is for <problem>
   55:   # if there is a second element it is for the current <part>
   56:   @Apache::inputtags::status=();
   57:   # hash of defined params for the current response
   58:   %Apache::inputtags::params=();
   59:   # list of all ids, for <import>, these get join()ed and prepended
   60:   @Apache::inputtags::import=();
   61: }
   62: 
   63: sub start_input {
   64:   my ($parstack,$safeeval)=@_;
   65:   my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
   66:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
   67:   push (@Apache::inputtags::input,$id);
   68:   push (@Apache::inputtags::inputlist,$id);
   69:   return $id;
   70: }
   71: 
   72: sub end_input {
   73:   pop @Apache::inputtags::input;
   74:   return '';
   75: }
   76: 
   77: sub start_textfield {
   78:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   79:   my $result = "";
   80:   my $id = &start_input($parstack,$safeeval);
   81:   my $resid=$Apache::inputtags::response[-1];
   82:   if ($target eq 'web') {
   83:     $Apache::lonxml::evaluate--;
   84:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
   85: 	my $partid=$Apache::inputtags::part;
   86: 	my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"});
   87: 	my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
   88: 	if ( $cols eq '') { $cols = 80; }
   89: 	my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
   90: 	if ( $rows eq '') { $rows = 10; }
   91: 	$result= '<textarea name="HWVAL'.$resid.'" '.
   92: 	    "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
   93: 	if ($oldresponse ne '') {
   94: 	    #get rid of any startup text if the user has already responded
   95: 	    &Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
   96: 	}
   97:     } else {
   98: 	#right or wrong don't show it
   99: 	#$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
  100: 	$result='';
  101: 	#get rid of any startup text
  102: 	&Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
  103:     }
  104:   } elsif ($target eq 'grade') {
  105:     my $seedtext=&Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
  106:     if ($seedtext eq $ENV{'form.HWVAL'.$resid}) {
  107:       # if the seed text is still there it wasn't a real submission
  108:       $ENV{'form.HWVAL'.$resid}='';
  109:     }
  110:   } elsif ($target eq 'edit') {
  111:     $result.=&Apache::edit::tag_start($target,$token);
  112:     $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
  113:     $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
  114:     my $bodytext=&Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
  115:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
  116:   } elsif ($target eq 'modified') {
  117:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  118: 						 $safeeval,'rows','cols');
  119:     if ($constructtag) {
  120:       $result = &Apache::edit::rebuild_tag($token);
  121:     } else {
  122:       $result=$token->[4];
  123:     }
  124:     $result.=&Apache::edit::modifiedfield();
  125:   }
  126:   return $result;
  127: }
  128: 
  129: sub end_textfield {
  130:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  131:   my $result;
  132:   if ($target eq 'web') {
  133:     $Apache::lonxml::evaluate++;
  134:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  135:       return "</textarea>";
  136:     }
  137:   } elsif ($target eq 'edit') {
  138:     $result=&Apache::edit::end_table();
  139:   }
  140:   &end_input;
  141:   return $result;
  142: }
  143: 
  144: sub start_textline {
  145:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  146:   my $result = "";
  147:   if ($target eq 'web') {
  148:     $Apache::lonxml::evaluate--;
  149:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  150:       my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  151:       my $maxlength;
  152:       if ($size eq '') { $size=20; } else {
  153: 	if ($size < 20) { $maxlength=$size; }
  154:       }
  155:       my $partid=$Apache::inputtags::part;
  156:       my $id=$Apache::inputtags::response[-1];
  157:       my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"});
  158:       if ($Apache::lonhomework::type ne 'exam') {
  159:         $result= '<input type="text" name="HWVAL'.$id.'" value="'.
  160: 	    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
  161:       }
  162:     } else {
  163:       #right or wrong don't show what was last typed in.
  164:       #$result='<i>'.$oldresponse.'</i>';
  165:       $result='';
  166:     }
  167:   } elsif ($target eq 'edit') {
  168:     $result=&Apache::edit::tag_start($target,$token);
  169:     $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
  170:     $result.=&Apache::edit::end_table;
  171:   } elsif ($target eq 'modified') {
  172:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
  173:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  174:   } elsif ($target eq 'tex') {
  175:       $result='\framebox[1cm][s]{\hfill}';
  176:   }
  177:   return $result;
  178: }
  179: 
  180: sub end_textline {
  181:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  182:   if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  183:   elsif ($target eq 'edit') { return ('','no'); }
  184:   return "";
  185: }
  186: 
  187: sub finalizeawards {
  188:   my $result='';
  189:   my $award;
  190:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
  191:   if ($result eq '' ) {
  192:     my $blankcount;
  193:     foreach $award (@_) {
  194:       if ($award eq '') {
  195: 	$result='MISSING_ANSWER';
  196: 	$blankcount++;
  197:       }
  198:     }
  199:     if ($blankcount == ($#_ + 1)) { $result = 'NO_RESPONSE'; }
  200:   }
  201:   if ($result eq '' ) {
  202:     foreach $award (@_) { if ($award eq 'MISSING_ANSWER') {$result='MISSING_ANSWER'; last;}}
  203:   }
  204:   if ($result eq '' ) {
  205:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
  206:   }
  207:   if ($result eq '' ) {
  208:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
  209:   }
  210: 
  211:   if ($result eq '' ) {
  212:     foreach $award (@_) { 
  213:       if ($award eq 'UNIT_FAIL' ||
  214: 	  $award eq 'NO_UNIT' ||
  215: 	  $award eq 'UNIT_NOTNEEDED') {
  216: 	$result=$award; last;
  217:       }
  218:     }
  219:   }
  220:   if ($result eq '' ) {
  221:     foreach $award (@_) { 
  222:       if ($award eq 'WANTED_NUMERIC' || 
  223: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
  224:     }
  225:   }
  226:   if ($result eq '' ) {
  227:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
  228:   }
  229:   if ($result eq '' ) {
  230:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
  231:   }
  232:   if ($result eq '' ) {
  233:     foreach $award (@_) { if ($award eq 'DRAFT') {$result=$award; last;} }
  234:   }
  235:   if ($result eq '' ) {
  236:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
  237:   }
  238:   if ($result eq '' ) {
  239:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
  240:   }
  241:   if ($result eq '' ) { $result='EXACT_ANS'; }
  242:   return $result
  243: }
  244: 
  245: sub decideoutput {
  246:   my ($award,$solved,$previous,$target)=@_;
  247:   my $message='';
  248:   my $button=0;
  249:   my $previousmsg;
  250: 
  251:   if ($previous) { $previousmsg='You have entered that answer before'; }
  252: 
  253:   if      ($solved =~ /^correct/) {
  254:     if ($target eq 'tex') {
  255:       $message = '\textbf{You are correct}. Your receipt is '.
  256:       &Apache::lonnet::receipt;
  257:     } else {
  258:       $message = "<b>You are correct.</b> Your receipt is ".
  259:       &Apache::lonnet::receipt;
  260:     }
  261:     $button=0;
  262:     $previousmsg='';
  263:   } elsif ($solved =~ /^excused/) {
  264:     $message = "<b>You are excused from the problem.</b>";
  265:     $button=0;
  266:     $previousmsg='';
  267:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  268:     if ($solved =~ /^incorrect/ || $solved eq '') {
  269:       $message = "Incorrect";
  270:       $button=1;
  271:     } else {
  272:       $message = "<b>You are correct.</b> Your receipt is ".
  273: 	&Apache::lonnet::receipt;
  274:       $button=0;
  275:       $previousmsg='';
  276:     }
  277:   } elsif ($award eq 'NO_RESPONSE') {
  278:     $message = '';
  279:     $button=1;
  280:   } elsif ($award eq 'MISSING_ANSWER') {
  281:     $message = 'Some parts were not submitted';
  282:     $button = 1;
  283:   } elsif ($award eq 'WANTED_NUMERIC') {
  284:     $message = "This question expects a numeric answer";
  285:     $button=1;
  286:   } elsif ($award eq 'SIG_FAIL') {
  287:     $message = "Please adjust significant figures.";# you provided %s significant figures";
  288:     $button=1;
  289:   } elsif ($award eq 'UNIT_FAIL') {
  290:     $message = "Units incorrect."; #Computer reads units as %s";
  291:     $button=1;
  292:   } elsif ($award eq 'UNIT_NOTNEEDED') {
  293:     $message = "Only a number required.";# Computer reads units of %s";
  294:     $button=1;
  295:   } elsif ($award eq 'NO_UNIT') {
  296:     $message = "Units required";
  297:     $button=1;
  298:   } elsif ($award eq 'BAD_FORMULA') {
  299:     $message = "Unable to understand formula";
  300:     $button=1;
  301:   } elsif ($award eq 'INCORRECT') {
  302:     $message = "Incorrect";
  303:     $button=1;
  304:   } elsif ($award eq 'SUBMITTED') {
  305:     $message = "Your submission has been recorded.";
  306:     $button=1;
  307:   } elsif ($award eq 'DRAFT') {
  308:     $message = "A draft copy has been saved.";
  309:     $button=1;
  310:   } else {
  311:     $message = "Unknown message: $award";
  312:     $button=1;
  313:   }
  314:   return ($button,$message,$previousmsg);
  315: }
  316: 
  317: sub setgradedata {
  318:   my ($award,$id,$previously_used) = @_;
  319:   # if the student already has it correct, don't modify the status
  320:   if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  321:       $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
  322:     $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
  323:     return '';
  324:   } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  325:        /^correct/ ) {
  326:     #handle assignment of tries and solved status
  327:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  328:       $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
  329:     }
  330:     if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  331:       $Apache::lonhomework::results{"resource.$id.tries"} =
  332: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  333:       $Apache::lonhomework::results{"resource.$id.solved"} =
  334: 	'correct_by_student';
  335:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  336:     } elsif ( $award eq 'INCORRECT' ) {
  337:       $Apache::lonhomework::results{"resource.$id.tries"} =
  338: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  339:       $Apache::lonhomework::results{"resource.$id.solved"} =
  340: 	'incorrect_attempted'
  341:     } elsif ( $award eq 'SUBMITTED' ) {
  342:       $Apache::lonhomework::results{"resource.$id.tries"} =
  343: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  344:       $Apache::lonhomework::results{"resource.$id.solved"} =
  345: 	'ungraded_attempted';
  346:     } elsif ( $award eq 'DRAFT' ) {
  347:       $Apache::lonhomework::results{"resource.$id.solved"} = '';
  348:     } elsif ( $award eq 'NO_RESPONSE' ) {
  349:       return '';
  350:     } else {
  351:       $Apache::lonhomework::results{"resource.$id.solved"} =
  352: 	'incorrect_attempted';
  353:     }
  354: 
  355:     # check if this was a previous submission if it was delete the
  356:     # unneeded data and update the previously_used attribute
  357:     if ( $previously_used eq 'PREVIOUSLY_USED') {
  358:       delete($Apache::lonhomework::results{"resource.$id.tries"});
  359:       $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  360:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  361:       #delete all data as they student didn't do anything, but save
  362:       #the list of collaborators.
  363:       foreach my $key (keys(%Apache::lonhomework::results)) {
  364: 	if (($key =~ /^resource\.$id\./) && ($key !~ /\.collaborators$/)) {
  365: 	  &Apache::lonxml::debug("Removing $key");
  366:   delete($Apache::lonhomework::results{$key});
  367: 	}
  368:       }
  369:       #and since they didn't do anything we were never here
  370:       return '';
  371:     } else {
  372:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
  373:     }
  374:   }
  375:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
  376: }
  377: 
  378: sub grade {
  379:   my ($target) = @_;
  380:   my $id = $Apache::inputtags::part;
  381:   my $response='';
  382:   if ( defined $ENV{'form.submitted'}) {
  383:     my @awards = ();
  384:     foreach $response (@Apache::inputtags::responselist) {
  385:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  386:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  387:       &Apache::lonxml::debug("keeping $value from $response for $id");
  388:       push (@awards,$value);
  389:     }
  390:     my $finalaward = &finalizeawards(@awards);
  391:     my $previously_used;
  392:     if ( $#Apache::inputtags::previous eq $#awards ) {
  393: 	my $match=0;
  394: 	my @matches;
  395: 	foreach my $versionar (@Apache::inputtags::previous_version) {
  396: 	    foreach my $version (@$versionar) {
  397: 		$matches[$version]++;
  398: 	    }
  399: 	}
  400: 	foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
  401: 	if ($match) {
  402: 	    $previously_used = 'PREVIOUSLY_LAST';
  403: 	    foreach my $value (@Apache::inputtags::previous) {
  404: 		if ($value eq 'PREVIOUSLY_USED' ) {
  405: 		    $previously_used = $value;
  406: 		    last;
  407: 		}
  408: 	    }
  409: 	}
  410:     }
  411:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
  412:     &setgradedata($finalaward,$id,$previously_used);
  413:   }
  414:   return '';
  415: }
  416: 
  417: sub gradestatus {
  418:   my ($id,$target) = @_;
  419:   my $showbutton = 1;
  420:   my $message = '';
  421:   my $latemessage = '';
  422:   my $trystr='';
  423:   my $button='';
  424:   my $previousmsg='';
  425: 
  426:   my $status = $Apache::inputtags::status['-1'];
  427:   &Apache::lonxml::debug("gradestatus has :$status:");
  428:   if ( $status ne 'CLOSED' ) {  
  429:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
  430:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  431:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  432:     &Apache::lonxml::debug("Found Award |$award|$solved|");
  433:     if ( $award ne '' ) {
  434:       &Apache::lonxml::debug('Getting message');
  435:       ($showbutton,$message,$previousmsg) =
  436: 	&decideoutput($award,$solved,$previous,$target);
  437:       if ($target eq 'tex') {
  438: 	$message=' '.$message.' ';
  439:       } else {
  440: 	$message="<td bgcolor=\"#aaffaa\">$message</td>";
  441: 	if ($previousmsg) {
  442: 	  $previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
  443: 	}
  444:       }
  445:     }
  446:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  447:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  448:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
  449:     if ( $tries eq '' ) { $tries = '0'; }
  450:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  451:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  452:     if ( $showbutton ) {
  453:       if ($target eq 'tex') {
  454: 	  if ($ENV{'request.state'} ne "construct") {
  455: 	      $trystr = ' {\small \textit{Tries} '.$tries.'/'.$maxtries.'} \vskip 0 mm ';
  456: 	  }
  457:       } else {
  458:          $trystr = "<td>Tries $tries/$maxtries</td>";
  459:       }
  460:     }
  461:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  462:     if ( $showbutton ) { 
  463:       if ($target ne 'tex') {
  464:         $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
  465:       }
  466:     }
  467:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  468:       #last submissions was after due date
  469:       if ($target eq 'tex') {
  470: 	  $latemessage=' The last submission was after the Due Date ';
  471:       } else {
  472:         $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
  473:       }
  474:     }
  475:   }
  476:   my $output= $previousmsg.$latemessage.$message.$trystr;
  477:   if ($output =~ /^\s*$/) {
  478:     return $button;
  479:   } else {
  480:     if ($target eq 'tex') {
  481:       return $button.' \vskip 0 mm '.$output.' ';
  482:     } else {
  483:       return $button.'<table><tr>'.$output.'</tr></table>';
  484:     }
  485:   }
  486: }
  487: 1;
  488: __END__
  489:  

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