File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.55: download - view: text, annotated - select for diffs
Mon Mar 11 21:37:55 2002 UTC (22 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- fixes Bug #94
- some cleanups

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.55 2002/03/11 21:37:55 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 'ERROR') {$result='ERROR'; last;}}
  201:   }
  202:   if ($result eq '' ) {
  203:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
  204:   }
  205: 
  206:   if ($result eq '' ) {
  207:     foreach $award (@_) { 
  208:       if ($award eq 'UNIT_FAIL' ||
  209: 	  $award eq 'NO_UNIT' ||
  210: 	  $award eq 'UNIT_NOTNEEDED') {
  211: 	$result=$award; last;
  212:       }
  213:     }
  214:   }
  215:   if ($result eq '' ) {
  216:     foreach $award (@_) { 
  217:       if ($award eq 'WANTED_NUMERIC' || 
  218: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
  219:     }
  220:   }
  221:   if ($result eq '' ) {
  222:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
  223:   }
  224:   if ($result eq '' ) {
  225:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
  226:   }
  227:   if ($result eq '' ) {
  228:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
  229:   }
  230:   if ($result eq '' ) {
  231:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
  232:   }
  233:   if ($result eq '' ) { $result='EXACT_ANS'; }
  234:   return $result
  235: }
  236: 
  237: sub decideoutput {
  238:   my ($award,$solved,$previous)=@_;
  239:   my $message='';
  240:   my $button=0;
  241:   my $previousmsg;
  242: 
  243:   if ($previous) { $previousmsg='You have entered that answer before'; }
  244: 
  245:   if      ($solved =~ /^correct/) {
  246:     $message = "<b>You are correct.</b> Your receipt is ".
  247:       &Apache::lonnet::receipt;
  248:     $button=0;
  249:     $previousmsg='';
  250:   } elsif ($solved =~ /^excused/) {
  251:     $message = "<b>You are excused from the problem.</b>";
  252:     $button=0;
  253:     $previousmsg='';
  254:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  255:     if ($solved =~ /^incorrect/ || $solved eq '') {
  256:       $message = "Incorrect";
  257:       $button=1;
  258:     } else {
  259:       $message = "<b>You are correct.</b> Your receipt is ".
  260: 	&Apache::lonnet::receipt;
  261:       $button=0;
  262:       $previousmsg='';
  263:     }
  264:   } elsif ($award eq 'NO_RESPONSE') {
  265:     $message = '';
  266:     $button=1;
  267:   } elsif ($award eq 'MISSING_ANSWER') {
  268:     $message = 'Some parts were not submitted';
  269:     $button = 1;
  270:   } elsif ($award eq 'WANTED_NUMERIC') {
  271:     $message = "This question expects a numeric answer";
  272:     $button=1;
  273:   } elsif ($award eq 'SIG_FAIL') {
  274:     $message = "Please adjust significant figures.";# you provided %s significant figures";
  275:     $button=1;
  276:   } elsif ($award eq 'UNIT_FAIL') {
  277:     $message = "Units incorrect."; #Computer reads units as %s";
  278:     $button=1;
  279:   } elsif ($award eq 'UNIT_NOTNEEDED') {
  280:     $message = "Only a number required.";# Computer reads units of %s";
  281:     $button=1;
  282:   } elsif ($award eq 'NO_UNIT') {
  283:     $message = "Units required";
  284:     $button=1;
  285:   } elsif ($award eq 'BAD_FORMULA') {
  286:     $message = "Unable to understand formula";
  287:     $button=1;
  288:   } elsif ($award eq 'INCORRECT') {
  289:     $message = "Incorrect";
  290:     $button=1;
  291:   } elsif ($award eq 'SUBMITTED') {
  292:     $message = "Your submission has been recorded.";
  293:     $button=1;
  294:   } else {
  295:     $message = "Unknown message: $award";
  296:     $button=1;
  297:   }
  298:   return ($button,$message,$previousmsg);
  299: }
  300: 
  301: sub setgradedata {
  302:   my ($award,$id,$previously_used) = @_;
  303:   # if the student already has it correct, don't modify the status
  304:   if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER') {
  305:     $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
  306:     return '';
  307:   } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  308:        /^correct/ ) {
  309:     #handle assignment of tries and solved status
  310:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  311:       $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
  312:     }
  313:     if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  314:       $Apache::lonhomework::results{"resource.$id.tries"} =
  315: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  316:       $Apache::lonhomework::results{"resource.$id.solved"} =
  317: 	'correct_by_student';
  318:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  319:     } elsif ( $award eq 'INCORRECT' ) {
  320:       $Apache::lonhomework::results{"resource.$id.tries"} =
  321: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  322:       $Apache::lonhomework::results{"resource.$id.solved"} =
  323: 	'incorrect_attempted';
  324:     } elsif ( $award eq 'SUBMITTED' ) {
  325:       $Apache::lonhomework::results{"resource.$id.tries"} =
  326: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  327:       $Apache::lonhomework::results{"resource.$id.solved"} =
  328: 	'ungraded_attempted';
  329:     } elsif ( $award eq 'NO_RESPONSE' ) {
  330:       return '';
  331:     } else {
  332:       $Apache::lonhomework::results{"resource.$id.solved"} =
  333: 	'incorrect_attempted';
  334:     }
  335: 
  336:     # check if this was a previous submission if it was delete the
  337:     # unneeded data and update the previously_used attribute
  338:     if ( $previously_used eq 'PREVIOUSLY_USED') {
  339:       delete($Apache::lonhomework::results{"resource.$id.tries"});
  340:       $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  341:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  342:       #delete all data as they student didn't do anything
  343:       foreach my $key (keys(%Apache::lonhomework::results)) {
  344: 	if ($key =~ /^resource\.$id\./) {
  345: 	  &Apache::lonxml::debug("Removing $key");
  346: 	  delete($Apache::lonhomework::results{$key});
  347: 	}
  348:       }
  349:       #and since they didn't do anything we were never here
  350:       return '';
  351:     } else {
  352:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
  353:     }
  354:   }
  355:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
  356: }
  357: 
  358: sub grade {
  359:   my ($target) = @_;
  360:   my $id = $Apache::inputtags::part;
  361:   my $response='';
  362:   if ( defined $ENV{'form.submitted'}) {
  363:     my @awards = ();
  364:     foreach $response (@Apache::inputtags::responselist) {
  365:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  366:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  367:       &Apache::lonxml::debug("keeping $value from $response for $id");
  368:       push (@awards,$value);
  369:     }
  370:     my $finalaward = &finalizeawards(@awards);
  371:     my $previously_used;
  372:     if ( $#Apache::inputtags::previous eq $#awards ) {
  373:       $previously_used = 'PREVIOUSLY_LAST';
  374:       foreach my $value (@Apache::inputtags::previous) {
  375: 	if ($value eq 'PREVIOUSLY_USED' ) {
  376: 	  $previously_used = $value;
  377: 	  last;
  378: 	}
  379:       }
  380:     }
  381:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
  382:     &setgradedata($finalaward,$id,$previously_used);
  383:   }
  384:   return '';
  385: }
  386: 
  387: sub gradestatus {
  388:   my ($id) = @_;
  389:   my $showbutton = 1;
  390:   my $message = '';
  391:   my $latemessage = '';
  392:   my $trystr='';
  393:   my $button='';
  394:   my $previousmsg='';
  395: 
  396:   my $status = $Apache::inputtags::status['-1'];
  397:   &Apache::lonxml::debug("gradestatus has :$status:");
  398:   if ( $status ne 'CLOSED' ) {  
  399:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
  400:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  401:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  402:     &Apache::lonxml::debug("Found Award |$award|$solved|");
  403:     if ( $award ne '' ) {
  404:       &Apache::lonxml::debug('Getting message');
  405:       ($showbutton,$message,$previousmsg) =
  406: 	&decideoutput($award,$solved,$previous);
  407:       $message="<td bgcolor=\"#aaffaa\">$message</td>";
  408:       if ($previousmsg) {
  409: 	$previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
  410:       }
  411:     }
  412:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  413:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  414:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
  415:     if ( $tries eq '' ) { $tries = '0'; }
  416:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  417:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  418:     if ( $showbutton ) {
  419:       $trystr = "<td>Tries $tries/$maxtries</td>";
  420:     }
  421:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  422:     if ( $showbutton ) { 
  423:       $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
  424:     }
  425:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  426:       #last submissions was after due date
  427:       $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
  428:     }
  429:   }
  430:   my $output= $previousmsg.$latemessage.$message.$trystr;
  431:   if ($output =~ /^\s*$/) {
  432:     return $button;
  433:   } else {
  434:     return $button.'<table><tr>'.$output.'</tr></table>';
  435:   }
  436: }
  437: 1;
  438: __END__
  439:  

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