File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.60: download - view: text, annotated - select for diffs
Wed Aug 7 16:23:05 2002 UTC (21 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_0_5_1, version_0_5, HEAD
- improving the editability of essayresponses and textfields

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

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