File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.12: download - view: text, annotated - select for diffs
Tue Dec 5 22:07:26 2000 UTC (23 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- properly sets the datastructures and delivers grades

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

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