File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.72: download - view: text, annotated - select for diffs
Wed Nov 6 22:40:29 2002 UTC (21 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- now if size is set, and it is less than 20, the student is restricted from typing in more than size characters
- BUG#890

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

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