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

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.33    ! albertel   14:   # list of current input ids
1.7       albertel   15:   @Apache::inputtags::input=();
1.33    ! albertel   16:   # list of all input ids seen in this problem
1.14      albertel   17:   @Apache::inputtags::inputlist=();
1.33    ! albertel   18:   # list of all current response ids
1.7       albertel   19:   @Apache::inputtags::response=();
1.33    ! albertel   20:   #list of all response ids seen in this problem
1.9       albertel   21:   @Apache::inputtags::responselist=();
1.33    ! albertel   22:   # id of current part, 0 means that no part is current (inside <problem> only
1.8       albertel   23:   $Apache::inputtags::part='';
1.33    ! albertel   24:   # list of problem date statuses, the first element is for <problem> 
        !            25:   #if there is a second element it is for the current <part>
1.18      albertel   26:   @Apache::inputtags::status=();
1.33    ! albertel   27:   #hash of defined params for the current response
1.7       albertel   28:   %Apache::inputtags::params=();
1.1       albertel   29: }
                     30: 
1.14      albertel   31: sub start_input {
                     32:   my ($parstack,$safeeval)=@_;
                     33:   my $args ='';
                     34:   if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                     35:   my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
                     36:   push (@Apache::inputtags::input,$id);
                     37:   push (@Apache::inputtags::inputlist,$id);
                     38:   return $id;
                     39: }
                     40: 
                     41: sub end_input {
                     42:   pop @Apache::inputtags::input;
                     43:   return '';
                     44: }
                     45: 
1.6       albertel   46: sub start_textarea {
                     47:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     48:   my $result = "";
1.14      albertel   49:   my $id = &start_input($parstack,$safeeval);
1.6       albertel   50:   if ($target eq 'web') {
1.30      albertel   51:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
1.31      albertel   52:     my $args ='';
                     53:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                     54:     my $cols = &Apache::run::run("{$args;".'return $cols}',$safeeval);
                     55:     if ( $cols eq '') { $cols = 80; }
                     56:     my $rows = &Apache::run::run("{$args;".'return $rows}',$safeeval);
                     57:     if ( $rows eq '') { $rows = 10; }
                     58:     $result= '<textarea name="HWVAL'.$Apache::inputtags::response['-1'].'" '.
                     59:       "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
1.30      albertel   60:     if ($oldresponse ne '') {
                     61:       #get rid of any startup text if the user has already responded
                     62:       &Apache::lonxml::get_all_text("/textarea",$$parser[$#$parser]);
                     63:     }
1.6       albertel   64:   }
                     65:   return $result;
                     66: }
                     67: 
                     68: sub end_textarea {
                     69:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.10      albertel   70:   if ($target eq 'web') {
                     71:     return "</textarea>";
                     72:   } 
1.14      albertel   73:   &end_input;
1.10      albertel   74:   return '';
1.6       albertel   75: }
                     76: 
1.1       albertel   77: sub start_textline {
                     78:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     79:   my $result = "";
                     80:   if ($target eq 'web') {
1.10      albertel   81:     my $args ='';
                     82:     if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                     83:     my $size = &Apache::run::run("{$args;".'return $size}',$safeeval);
                     84:     if ($size eq '') { $size=20; }
1.15      albertel   85:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
1.28      albertel   86:     $result= '<input type="text" name="HWVAL'.$Apache::inputtags::response['-1'].'" value="'.$oldresponse.'" size="'.$size.'" />';
1.1       albertel   87:   }
                     88:   return $result;
                     89: }
                     90: 
                     91: sub end_textline {
                     92:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
1.6       albertel   93:   return "";
1.1       albertel   94: }
                     95: 
1.10      albertel   96: #answergroup is deprecated
                     97: #sub start_answergroup {
                     98: #  my ($target,$token,$parstack,$parser,$safeeval)=@_;
                     99: #  my $args ='';
                    100: #  if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    101: #  my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
                    102: #  push (@Apache::inputtags::answergroup,$id);
                    103: #  if ($target == 'web') {
                    104: #    
                    105: #  }
                    106: #  return '';
                    107: #}
                    108: #
                    109: #sub end_answergroup {
                    110: #  my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    111: #  return ''; # <answergroup> has ben deprecated
                    112: #  my $args ='';
                    113: #  if ( $#$parstack > -1 ) { $args=$$parstack[$#$parstack]; }
                    114: #  my $id = &Apache::run::run("{$args;".'return $id}',$safeeval);
                    115: #  push (@Apache::inputtags::answergroup,$id);
                    116: #  my $result='';
1.28      albertel  117: #  my $button='<input type="submit" name="submit" value="Submit All Answers" />';
1.10      albertel  118: #  my $showbutton='1';
                    119: #  my $usedtry='1';
                    120: #  my $response='';
                    121: #  if ( $target == 'web' ) {
1.25      albertel  122: #    if ( defined $ENV{'form.submitted'}) {
1.10      albertel  123: #    }
                    124: #    if ($showbutton > 0) { $result.=$button }
                    125: #  }
                    126: #  pop @Apache::inputtags::answergroup;
                    127: #  return $result;
                    128: #}
1.6       albertel  129: 
                    130: 
                    131: sub start_datasubmission {
                    132: }
                    133: 
                    134: sub end_datasubmission {
                    135:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
                    136:   if ( $target == 'web' ) {
1.28      albertel  137:     return '<input type="submit" name="submit" value="Submit All Data" />';
1.2       albertel  138:   }
1.10      albertel  139:   return '';
1.9       albertel  140: }
                    141: 
                    142: sub finalizeawards {
                    143:   my $result='';
                    144:   my $award;
                    145:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
1.15      albertel  146:   if ($result eq '' ) {
                    147:     foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
                    148:   }
                    149:   if ($result eq '' ) {
                    150:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
                    151:   }
1.29      albertel  152:   if ($result eq '' ) {
                    153:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
                    154:   }
1.15      albertel  155: 
1.9       albertel  156:   if ($result eq '' ) {
                    157:     foreach $award (@_) { 
                    158:       if ($award eq 'UNIT_FAIL' ||
                    159: 	  $award eq 'NO_UNIT' ||
                    160: 	  $award eq 'UNIT_NOTNEEDED') {
                    161: 	$result=$award; last;
                    162:       }
                    163:     }
                    164:   }
                    165:   if ($result eq '' ) {
                    166:     foreach $award (@_) { 
                    167:       if ($award eq 'WANTED_NUMERIC' || 
                    168: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
                    169:     }
                    170:   }
                    171:   if ($result eq '' ) {
                    172:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
                    173:   }
                    174:   if ($result eq '' ) {
                    175:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
                    176:   }
                    177:   if ($result eq '' ) {
1.30      albertel  178:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
                    179:   }
                    180:   if ($result eq '' ) {
1.9       albertel  181:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
                    182:   }
                    183:   if ($result eq '' ) { $result='EXACT_ANS'; }
                    184:   return $result
                    185: }
                    186: 
1.10      albertel  187: sub decideoutput {
1.32      albertel  188:   my ($award,$solved)=@_;
1.10      albertel  189:   my $message='';
                    190:   my $button=0;
1.32      albertel  191:   if      ($solved =~ /^correct/ || $award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
1.24      www       192:     $message = "<b>You are correct.</b> Your receipt is ".&Apache::lonnet::receipt;
1.12      albertel  193:     $button=0;
1.13      albertel  194:   } elsif ($award eq 'NO_RESPONSE') {
                    195:     $message = '';
                    196:     $button=1;
1.14      albertel  197:   } elsif ($award eq 'MISSING_ANSWER') {
                    198:     $message = 'Some parts were not submitted';
                    199:     $button = 1;
1.10      albertel  200:   } elsif ($award eq 'WANTED_NUMERIC') {
                    201:     $message = "This question expects a numeric answer";
1.12      albertel  202:     $button=1;
1.10      albertel  203:   } elsif ($award eq 'SIG_FAIL') {
1.21      albertel  204:     $message = "Please adjust significant figures.";# you provided %s significant figures";
1.12      albertel  205:     $button=1;
1.10      albertel  206:   } elsif ($award eq 'UNIT_FAIL') {
1.21      albertel  207:     $message = "Units incorrect."; #Computer reads units as %s";
1.12      albertel  208:     $button=1;
1.10      albertel  209:   } elsif ($award eq 'UNIT_NOTNEEDED') {
1.21      albertel  210:     $message = "Only a number required.";# Computer reads units of %s";
1.12      albertel  211:     $button=1;
1.10      albertel  212:   } elsif ($award eq 'NO_UNIT') {
                    213:     $message = "Units required";
1.12      albertel  214:     $button=1;
1.10      albertel  215:   } elsif ($award eq 'BAD_FORMULA') {
                    216:     $message = "Unable to understand formula";
1.12      albertel  217:     $button=1;
1.10      albertel  218:   } elsif ($award eq 'INCORRECT') {
                    219:     $message = "Incorrect";
1.12      albertel  220:     $button=1;
1.30      albertel  221:   } elsif ($award eq 'SUBMITTED') {
                    222:     $message = "Your submission has been recorded.";
                    223:     $button=1;
1.10      albertel  224:   } else {
                    225:     $message = "Unknown message: $award";
1.12      albertel  226:     $button=1;
1.10      albertel  227:   }
1.12      albertel  228:   return ($button,$message);
                    229: }
                    230: 
                    231: sub setgradedata {
                    232:   my ($award,$id) = @_;
                    233:   if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
1.15      albertel  234:     $Apache::lonhomework::results{"resource.$id.tries"} =
                    235:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    236:     $Apache::lonhomework::results{"resource.$id.solved"} =
1.12      albertel  237:       'correct_by_student';
1.15      albertel  238:     $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1.12      albertel  239:   } elsif ( $award eq 'INCORRECT' ) {
1.15      albertel  240:     $Apache::lonhomework::results{"resource.$id.tries"} =
                    241:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    242:     $Apache::lonhomework::results{"resource.$id.solved"} =
1.12      albertel  243:       'incorrect_attempted';
1.30      albertel  244:   } elsif ( $award eq 'SUBMITTED' ) {
                    245:     $Apache::lonhomework::results{"resource.$id.tries"} =
                    246:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    247:     $Apache::lonhomework::results{"resource.$id.solved"} =
                    248:       'ungraded_attempted';
1.12      albertel  249:   } else {
1.15      albertel  250:     $Apache::lonhomework::results{"resource.$id.solved"} =
1.12      albertel  251:       'incorrect_attempted';
                    252:   }
1.15      albertel  253:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  254: }
                    255: 
1.9       albertel  256: sub grade {
                    257:   my ($target) = @_;
                    258:   my $id = $Apache::inputtags::part;
1.20      albertel  259: #  my $result='';
1.9       albertel  260:   my $response='';
                    261:   if ( $target == 'web' ) {
1.25      albertel  262:     if ( defined $ENV{'form.submitted'}) {
1.9       albertel  263:       my @awards = ();
1.10      albertel  264:       &Apache::lonxml::debug("$#Apache::inputtags::responselist");
1.9       albertel  265:       foreach $response (@Apache::inputtags::responselist) {
1.15      albertel  266: 	&Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    267: 	my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1.9       albertel  268: 	if ( $value ne '' ) {
1.30      albertel  269: 	  &Apache::lonxml::debug("keeping $value from $response for $id");
1.9       albertel  270: 	  push (@awards,$value);
                    271: 	} else {
                    272: 	  &Apache::lonxml::debug("skipping $value from $response for $id");
                    273: 	}
                    274:       }
                    275:       my $finalaward = &finalizeawards(@awards);
                    276:       &Apache::lonxml::debug("final award $finalaward");
1.12      albertel  277:       &setgradedata($finalaward,$id);
1.9       albertel  278:     }
                    279:   }
1.20      albertel  280:   return '';
1.1       albertel  281: }
                    282: 
1.11      albertel  283: sub gradestatus {
1.12      albertel  284:   my ($id) = @_;
                    285:   my $showbutton = 1;
                    286:   my $message = '';
1.13      albertel  287:   my $trystr='';
1.18      albertel  288:   my $button='';
1.17      albertel  289:   
1.19      albertel  290:   my $status = $Apache::inputtags::status['-1'];
                    291:   &Apache::lonxml::debug("gradestatus has :$status:");
1.22      albertel  292:   if ( $status ne 'CLOSED' ) {  
1.17      albertel  293:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
1.32      albertel  294:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    295:     &Apache::lonxml::debug("Found Award |$award|$solved|");
1.17      albertel  296:     if ( $award ne '' ) {
                    297:       &Apache::lonxml::debug('Getting message');
1.32      albertel  298:       ($showbutton,$message) = &decideoutput($award,$solved);
1.26      albertel  299:       $message="<br /><table bgcolor=\"#aaffaa\"><tr><td>$message</td></tr></table>";
1.17      albertel  300:     }
                    301:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    302:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1.18      albertel  303:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
1.17      albertel  304:     if ( $tries eq '' ) { $tries = '0'; }
                    305:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
1.23      albertel  306:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
1.17      albertel  307:     if ( $showbutton ) {
1.27      albertel  308:       $trystr = "<br />Tries $tries/$maxtries";
1.17      albertel  309:     }
1.22      albertel  310:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
1.17      albertel  311:     if ( $showbutton ) { 
1.27      albertel  312:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
1.17      albertel  313:     }
1.12      albertel  314:   }
                    315:   return $button.$message.$trystr;
1.11      albertel  316: }
1.1       albertel  317: 1;
                    318: __END__
                    319:  

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