File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.56: download - view: text, annotated - select for diffs
Mon Apr 1 21:26:57 2002 UTC (22 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: stable_2002_april, HEAD
- if some parts of the <optionresponse> are not submitted don't count a try
  fixes BUG#191

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

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