File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.29: download - view: text, annotated - select for diffs
Wed Mar 21 18:24:36 2001 UTC (23 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: HEAD
- now detects when a screwup occurs and doesn't give credit

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: # 2/19 Guy 
    4: 
    5: package Apache::inputtags;
    6: use strict;
    7: 
    8: sub BEGIN {
    9:   &Apache::lonxml::register('Apache::inputtags',('textarea','textline','datasubmission'));
   10: }
   11: 
   12: 
   13: sub initialize_inputtags {
   14:   @Apache::inputtags::input=();
   15:   @Apache::inputtags::inputlist=();
   16:   @Apache::inputtags::response=();
   17:   @Apache::inputtags::responselist=();
   18:   @Apache::inputtags::answergroup=();
   19:   $Apache::inputtags::part='';
   20:   @Apache::inputtags::status=();
   21:   %Apache::inputtags::params=();
   22: }
   23: 
   24: sub start_input {
   25:   my ($parstack,$safeeval)=@_;
   26:   my $args ='';
   27:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   28:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
   29:   push (@Apache::inputtags::input,$id);
   30:   push (@Apache::inputtags::inputlist,$id);
   31:   return $id;
   32: }
   33: 
   34: sub end_input {
   35:   pop @Apache::inputtags::input;
   36:   return '';
   37: }
   38: 
   39: sub start_textarea {
   40:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   41:   my $result = "";
   42:   my $id = &start_input($parstack,$safeeval);
   43:   if ($target eq 'web') {
   44:     $result= '<textarea name="HWVAL'.$id.'" value="">';
   45:   }
   46:   return $result;
   47: }
   48: 
   49: sub end_textarea {
   50:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   51:   if ($target eq 'web') {
   52:     return "</textarea>";
   53:   } 
   54:   &end_input;
   55:   return '';
   56: }
   57: 
   58: sub start_textline {
   59:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   60:   my $result = "";
   61:   if ($target eq 'web') {
   62:     my $args ='';
   63:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   64:     my $size = &Apache::run::run("{$args;".'return $size}',$safeeval);
   65:     if ($size eq '') { $size=20; }
   66:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
   67:     $result= '<input type="text" name="HWVAL'.$Apache::inputtags::response['-1'].'" value="'.$oldresponse.'" size="'.$size.'" />';
   68:   }
   69:   return $result;
   70: }
   71: 
   72: sub end_textline {
   73:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   74:   return "";
   75: }
   76: 
   77: #answergroup is deprecated
   78: #sub start_answergroup {
   79: #  my ($target,$token,$parstack,$parser,$safeeval)=@_;
   80: #  my $args ='';
   81: #  if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   82: #  my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
   83: #  push (@Apache::inputtags::answergroup,$id);
   84: #  if ($target == 'web') {
   85: #    
   86: #  }
   87: #  return '';
   88: #}
   89: #
   90: #sub end_answergroup {
   91: #  my ($target,$token,$parstack,$parser,$safeeval)=@_;
   92: #  return ''; # <answergroup> has ben deprecated
   93: #  my $args ='';
   94: #  if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
   95: #  my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
   96: #  push (@Apache::inputtags::answergroup,$id);
   97: #  my $result='';
   98: #  my $button='<input type="submit" name="submit" value="Submit All Answers" />';
   99: #  my $showbutton='1';
  100: #  my $usedtry='1';
  101: #  my $response='';
  102: #  if ( $target == 'web' ) {
  103: #    if ( defined $ENV{'form.submitted'}) {
  104: #    }
  105: #    if ($showbutton > 0) { $result.=$button }
  106: #  }
  107: #  pop @Apache::inputtags::answergroup;
  108: #  return $result;
  109: #}
  110: 
  111: 
  112: sub start_datasubmission {
  113: }
  114: 
  115: sub end_datasubmission {
  116:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
  117:   if ( $target == 'web' ) {
  118:     return '<input type="submit" name="submit" value="Submit All Data" />';
  119:   }
  120:   return '';
  121: }
  122: 
  123: sub finalizeawards {
  124:   my $result='';
  125:   my $award;
  126:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
  127:   if ($result eq '' ) {
  128:     foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
  129:   }
  130:   if ($result eq '' ) {
  131:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
  132:   }
  133:   if ($result eq '' ) {
  134:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
  135:   }
  136: 
  137:   if ($result eq '' ) {
  138:     foreach $award (@_) { 
  139:       if ($award eq 'UNIT_FAIL' ||
  140: 	  $award eq 'NO_UNIT' ||
  141: 	  $award eq 'UNIT_NOTNEEDED') {
  142: 	$result=$award; last;
  143:       }
  144:     }
  145:   }
  146:   if ($result eq '' ) {
  147:     foreach $award (@_) { 
  148:       if ($award eq 'WANTED_NUMERIC' || 
  149: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
  150:     }
  151:   }
  152:   if ($result eq '' ) {
  153:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
  154:   }
  155:   if ($result eq '' ) {
  156:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
  157:   }
  158:   if ($result eq '' ) {
  159:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
  160:   }
  161:   if ($result eq '' ) { $result='EXACT_ANS'; }
  162:   return $result
  163: }
  164: 
  165: sub decideoutput {
  166:   my ($award)=@_;
  167:   my $message='';
  168:   my $button=0;
  169:   if      ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  170:     $message = "<b>You are correct.</b> Your receipt is ".&Apache::lonnet::receipt;
  171:     $button=0;
  172:   } elsif ($award eq 'NO_RESPONSE') {
  173:     $message = '';
  174:     $button=1;
  175:   } elsif ($award eq 'MISSING_ANSWER') {
  176:     $message = 'Some parts were not submitted';
  177:     $button = 1;
  178:   } elsif ($award eq 'WANTED_NUMERIC') {
  179:     $message = "This question expects a numeric answer";
  180:     $button=1;
  181:   } elsif ($award eq 'SIG_FAIL') {
  182:     $message = "Please adjust significant figures.";# you provided %s significant figures";
  183:     $button=1;
  184:   } elsif ($award eq 'UNIT_FAIL') {
  185:     $message = "Units incorrect."; #Computer reads units as %s";
  186:     $button=1;
  187:   } elsif ($award eq 'UNIT_NOTNEEDED') {
  188:     $message = "Only a number required.";# Computer reads units of %s";
  189:     $button=1;
  190:   } elsif ($award eq 'NO_UNIT') {
  191:     $message = "Units required";
  192:     $button=1;
  193:   } elsif ($award eq 'BAD_FORMULA') {
  194:     $message = "Unable to understand formula";
  195:     $button=1;
  196:   } elsif ($award eq 'INCORRECT') {
  197:     $message = "Incorrect";
  198:     $button=1;
  199:   } else {
  200:     $message = "Unknown message: $award";
  201:     $button=1;
  202:   }
  203:   return ($button,$message);
  204: }
  205: 
  206: sub setgradedata {
  207:   my ($award,$id) = @_;
  208:   if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  209:     $Apache::lonhomework::results{"resource.$id.tries"} =
  210:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  211:     $Apache::lonhomework::results{"resource.$id.solved"} =
  212:       'correct_by_student';
  213:     $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  214:   } elsif ( $award eq 'INCORRECT' ) {
  215:     $Apache::lonhomework::results{"resource.$id.tries"} =
  216:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  217:     $Apache::lonhomework::results{"resource.$id.solved"} =
  218:       'incorrect_attempted';
  219:   } else {
  220:     $Apache::lonhomework::results{"resource.$id.solved"} =
  221:       'incorrect_attempted';
  222:   }
  223:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
  224: }
  225: 
  226: sub grade {
  227:   my ($target) = @_;
  228:   my $id = $Apache::inputtags::part;
  229: #  my $result='';
  230:   my $response='';
  231:   if ( $target == 'web' ) {
  232:     if ( defined $ENV{'form.submitted'}) {
  233:       my @awards = ();
  234:       &Apache::lonxml::debug("$#Apache::inputtags::responselist");
  235:       foreach $response (@Apache::inputtags::responselist) {
  236: 	&Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  237: 	my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  238: 	if ( $value ne '' ) {
  239: 	  &Apache::lonxml::debug("keep ing $value from $response for $id");
  240: 	  push (@awards,$value);
  241: 	} else {
  242: 	  &Apache::lonxml::debug("skipping $value from $response for $id");
  243: 	}
  244:       }
  245:       my $finalaward = &finalizeawards(@awards);
  246:       &Apache::lonxml::debug("final award $finalaward");
  247:       &setgradedata($finalaward,$id);
  248:     }
  249:   }
  250:   return '';
  251: }
  252: 
  253: sub gradestatus {
  254:   my ($id) = @_;
  255:   my $showbutton = 1;
  256:   my $message = '';
  257:   my $trystr='';
  258:   my $button='';
  259:   
  260:   my $status = $Apache::inputtags::status['-1'];
  261:   &Apache::lonxml::debug("gradestatus has :$status:");
  262:   if ( $status ne 'CLOSED' ) {  
  263:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
  264:     &Apache::lonxml::debug("Found Award |$award|");
  265:     if ( $award ne '' ) {
  266:       &Apache::lonxml::debug('Getting message');
  267:       ($showbutton,$message) = &decideoutput($award);
  268:       $message="<br /><table bgcolor=\"#aaffaa\"><tr><td>$message</td></tr></table>";
  269:     }
  270:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  271:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  272:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
  273:     if ( $tries eq '' ) { $tries = '0'; }
  274:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  275:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  276:     if ( $showbutton ) {
  277:       $trystr = "<br />Tries $tries/$maxtries";
  278:     }
  279:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  280:     if ( $showbutton ) { 
  281:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
  282:     }
  283:   }
  284:   return $button.$message.$trystr;
  285: }
  286: 1;
  287: __END__
  288:  

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