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

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

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