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

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

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