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

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

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