File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.44: download - view: text, annotated - select for diffs
Sat Aug 18 07:56:51 2001 UTC (22 years, 9 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- renamed <datasubmission> to <dataresponse>
- improved <dataresponse> so that it is functional (kinda not to use parameter.html)

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

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