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

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

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