File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.45: download - view: text, annotated - select for diffs
Wed Oct 3 12:48:47 2001 UTC (22 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- inputtags are smarter now about when the pages doesn't have a form

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

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