Annotation of loncom/homework/inputtags.pm, revision 1.13

1.1       albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
                      3: 
                      4: package Apache::inputtags;
                      5: use strict;
                      6: 
                      7: sub BEGIN {
1.10      albertel    8:   &Apache::lonxml::register('Apache::inputtags',('textarea','textline','datasubmission'));
1.1       albertel    9: }
                     10: 
1.7       albertel   11: 
1.1       albertel   12: sub initialize_inputtags {
1.7       albertel   13:   @Apache::inputtags::input=();
                     14:   @Apache::inputtags::response=();
1.9       albertel   15:   @Apache::inputtags::responselist=();
1.7       albertel   16:   @Apache::inputtags::answergroup=();
1.8       albertel   17:   $Apache::inputtags::part='';
1.7       albertel   18:   %Apache::inputtags::params=();
1.1       albertel   19: }
                     20: 
1.6       albertel   21: sub start_textarea {
                     22:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     23:   my $result = "";
                     24:   if ($target eq 'web') {
1.7       albertel   25:     $result= '<textarea name="HWVAL'.$Apache::inputtags::curvalue.'" value="">';
1.6       albertel   26:   }
                     27:   return $result;
                     28: }
                     29: 
                     30: sub end_textarea {
                     31:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.10      albertel   32:   if ($target eq 'web') {
                     33:     return "</textarea>";
                     34:   } 
                     35:   return '';
1.6       albertel   36: }
                     37: 
1.1       albertel   38: sub start_textline {
                     39:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     40:   my $result = "";
                     41:   if ($target eq 'web') {
1.10      albertel   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; }
1.7       albertel   46:     $result= '<input type="text" name="HWVAL'.$Apache::inputtags::curvalue.'" value="" size="'.$size.'">';
1.1       albertel   47:   }
                     48:   return $result;
                     49: }
                     50: 
                     51: sub end_textline {
                     52:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.6       albertel   53:   return "";
1.1       albertel   54: }
                     55: 
1.10      albertel   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: #}
1.6       albertel   89: 
                     90: 
                     91: sub start_datasubmission {
                     92: }
                     93: 
                     94: sub end_datasubmission {
                     95:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     96:   if ( $target == 'web' ) {
1.7       albertel   97:     return '<input type="submit" name="submit" value="Submit All Data">';
1.2       albertel   98:   }
1.10      albertel   99:   return '';
1.9       albertel  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: 
1.10      albertel  135: sub decideoutput {
                    136:   my ($award)=@_;
                    137:   my $message='';
                    138:   my $button=0;
                    139:   if      ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
1.12      albertel  140:     $message = "Correct computer gets %s";
                    141:     $button=0;
1.13    ! albertel  142:   } elsif ($award eq 'NO_RESPONSE') {
        !           143:     $message = '';
        !           144:     $button=1;
1.10      albertel  145:   } elsif ($award eq 'WANTED_NUMERIC') {
                    146:     $message = "This question expects a numeric answer";
1.12      albertel  147:     $button=1;
1.10      albertel  148:   } elsif ($award eq 'SIG_FAIL') {
                    149:     $message = "Please adjust significant figures, you provided %s significant figures";
1.12      albertel  150:     $button=1;
1.10      albertel  151:   } elsif ($award eq 'UNIT_FAIL') {
                    152:     $message = "Units incorrect, Computer reads units as %s";
1.12      albertel  153:     $button=1;
1.10      albertel  154:   } elsif ($award eq 'UNIT_NOTNEEDED') {
                    155:     $message = "Only a number required, Computer reads units of %s";
1.12      albertel  156:     $button=1;
1.10      albertel  157:   } elsif ($award eq 'NO_UNIT') {
                    158:     $message = "Units required";
1.12      albertel  159:     $button=1;
1.10      albertel  160:   } elsif ($award eq 'BAD_FORMULA') {
                    161:     $message = "Unable to understand formula";
1.12      albertel  162:     $button=1;
1.10      albertel  163:   } elsif ($award eq 'INCORRECT') {
                    164:     $message = "Incorrect";
1.12      albertel  165:     $button=1;
1.10      albertel  166:   } else {
                    167:     $message = "Unknown message: $award";
1.12      albertel  168:     $button=1;
1.10      albertel  169:   }
1.12      albertel  170:   return ($button,$message);
                    171: }
                    172: 
                    173: sub setgradedata {
                    174:   my ($award,$id) = @_;
                    175:   if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                    176:     $Apache::lonhomework::results{"resource.tries.$id"} =
                    177:       $Apache::lonhomework::history{"resource.tries.$id"} + 1;
                    178:     $Apache::lonhomework::results{"resource.solved.$id"} =
                    179:       'correct_by_student';
                    180:     $Apache::lonhomework::results{"resource.awarded.$id"} = '1';
                    181:   } elsif ( $award eq 'INCORRECT' ) {
                    182:     $Apache::lonhomework::results{"resource.tries.$id"} =
                    183:       $Apache::lonhomework::history{"resource.tries.$id"} + 1;
                    184:     $Apache::lonhomework::results{"resource.solved.$id"} =
                    185:       'incorrect_attempted';
                    186:   } else {
                    187:     $Apache::lonhomework::results{"resource.solved.$id"} =
                    188:       'incorrect_attempted';
                    189:   }
                    190:   $Apache::lonhomework::results{"resource.award.$id"} = $award;
1.10      albertel  191: }
                    192: 
1.9       albertel  193: sub grade {
                    194:   my ($target) = @_;
                    195:   my $id = $Apache::inputtags::part;
                    196:   my $result='';
                    197:   my $response='';
                    198:   if ( $target == 'web' ) {
                    199:     if ( defined $ENV{'form.submit'}) {
                    200:       my @awards = ();
1.10      albertel  201:       &Apache::lonxml::debug("$#Apache::inputtags::responselist");
1.9       albertel  202:       foreach $response (@Apache::inputtags::responselist) {
1.10      albertel  203: 	&Apache::lonxml::debug("looking for response.awarddetail.$id.$response");
                    204: 	my $value=$Apache::lonhomework::results{"resource.awarddetail.$id.$response"};
1.9       albertel  205: 	if ( $value ne '' ) {
                    206: 	  &Apache::lonxml::debug("keep ing $value from $response for $id");
                    207: 	  push (@awards,$value);
                    208: 	} else {
                    209: 	  &Apache::lonxml::debug("skipping $value from $response for $id");
                    210: 	}
                    211:       }
                    212:       my $finalaward = &finalizeawards(@awards);
                    213:       &Apache::lonxml::debug("final award $finalaward");
1.12      albertel  214:       &setgradedata($finalaward,$id);
1.9       albertel  215:     }
                    216:   }
                    217:   return $result;
1.1       albertel  218: }
                    219: 
1.11      albertel  220: sub gradestatus {
1.12      albertel  221:   my ($id) = @_;
                    222:   my $showbutton = 1;
                    223:   my $message = '';
                    224:   my $award = $Apache::lonhomework::history{"resource.award.$id"};
                    225:   &Apache::lonxml::debug("Found Award |$award|");
                    226:   if ( $award ne '' ) {
                    227:     &Apache::lonxml::debug('Getting message');
                    228:     ($showbutton,$message) = &decideoutput($award);
                    229:   }
                    230:   my $button='';
                    231:   my $tries = $Apache::lonhomework::history{"resource.tries.$id"};
                    232:   my $maxtries = $Apache::lonhomework::history{"resource.maxtries.$id"};
                    233:   if ( $tries eq '' ) { $tries = '0'; }
1.13    ! albertel  234:   if ( $maxtries eq '' ) { $maxtries = '2'; } 
        !           235:   my $trystr='';
        !           236:   if ( $showbutton ) {
        !           237:     $trystr = "<br></br>Tries $tries/$maxtries";
        !           238:   }
1.12      albertel  239:   if ( $tries >= $maxtries ) { $showbutton = 0; }
                    240:   if ( $showbutton ) { 
                    241:     $button = '<input type="submit" name="submit" value="Submit All Answers"><br></br>';
                    242:   }
                    243:   return $button.$message.$trystr;
1.11      albertel  244: }
1.1       albertel  245: 1;
                    246: __END__
                    247:  

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