File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.58: download - view: text, annotated - select for diffs
Thu Jul 25 20:55:13 2002 UTC (21 years, 9 months ago) by ng
Branches: MAIN
CVS tags: HEAD
Allow collaborators to be saved even if the student does not change the answer.
Done as per Guy's instructions.

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

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