File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.15: download - view: text, annotated - select for diffs
Tue Dec 12 14:54:10 2000 UTC (23 years, 5 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- changed order of resource.type.ids.ids to resource.ids.ids.type
- added detection of return code ERROR

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

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