File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.37: download - view: text, annotated - select for diffs
Thu Jul 12 19:27:48 2001 UTC (22 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
-improved the logic for dealing with incosistant grading messages

    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:   # id of current part, 0 means that no part is current (inside <problem> only
   23:   $Apache::inputtags::part='';
   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>
   26:   @Apache::inputtags::status=();
   27:   #hash of defined params for the current response
   28:   %Apache::inputtags::params=();
   29: }
   30: 
   31: sub start_input {
   32:   my ($parstack,$safeeval)=@_;
   33:   my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
   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: 
   44: sub start_textarea {
   45:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   46:   my $result = "";
   47:   my $id = &start_input($parstack,$safeeval);
   48:   if ($target eq 'web') {
   49:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
   50:     my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
   51:     if ( $cols eq '') { $cols = 80; }
   52:     my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
   53:     if ( $rows eq '') { $rows = 10; }
   54:     $result= '<textarea name="HWVAL'.$Apache::inputtags::response['-1'].'" '.
   55:       "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
   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:     }
   60:   }
   61:   return $result;
   62: }
   63: 
   64: sub end_textarea {
   65:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   66:   if ($target eq 'web') {
   67:     return "</textarea>";
   68:   } 
   69:   &end_input;
   70:   return '';
   71: }
   72: 
   73: sub start_textline {
   74:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   75:   my $result = "";
   76:   if ($target eq 'web') {
   77:     my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
   78:     if ($size eq '') { $size=20; }
   79:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
   80:     $result= '<input type="text" name="HWVAL'.$Apache::inputtags::response['-1'].'" value="'.$oldresponse.'" size="'.$size.'" />';
   81:   }
   82:   if ($target eq 'edit') {
   83:     $result.=&Apache::edit::tag_start($target,$token,&Apache::lonxml::description($token));
   84:     $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
   85:     $result.=&Apache::edit::end_table;
   86:   }
   87:   if ($target eq 'modified') {
   88:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
   89:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
   90:   }
   91:   return $result;
   92: }
   93: 
   94: sub end_textline {
   95:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   96:   if ($target eq 'edit') { return ('','no'); }
   97:   return "";
   98: }
   99: 
  100: sub start_datasubmission {
  101:   return '';
  102: }
  103: 
  104: sub end_datasubmission {
  105:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  106:   if ( $target == 'web' ) {
  107:     return '<input type="submit" name="submit" value="Submit All Data" />';
  108:   }
  109:   return '';
  110: }
  111: 
  112: sub finalizeawards {
  113:   my $result='';
  114:   my $award;
  115:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
  116:   if ($result eq '' ) {
  117:     foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
  118:   }
  119:   if ($result eq '' ) {
  120:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
  121:   }
  122:   if ($result eq '' ) {
  123:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
  124:   }
  125: 
  126:   if ($result eq '' ) {
  127:     foreach $award (@_) { 
  128:       if ($award eq 'UNIT_FAIL' ||
  129: 	  $award eq 'NO_UNIT' ||
  130: 	  $award eq 'UNIT_NOTNEEDED') {
  131: 	$result=$award; last;
  132:       }
  133:     }
  134:   }
  135:   if ($result eq '' ) {
  136:     foreach $award (@_) { 
  137:       if ($award eq 'WANTED_NUMERIC' || 
  138: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
  139:     }
  140:   }
  141:   if ($result eq '' ) {
  142:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
  143:   }
  144:   if ($result eq '' ) {
  145:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
  146:   }
  147:   if ($result eq '' ) {
  148:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
  149:   }
  150:   if ($result eq '' ) {
  151:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
  152:   }
  153:   if ($result eq '' ) { $result='EXACT_ANS'; }
  154:   return $result
  155: }
  156: 
  157: sub decideoutput {
  158:   my ($award,$solved)=@_;
  159:   my $message='';
  160:   my $button=0;
  161:   if      ($solved =~ /^correct/) {
  162:     $message = "<b>You are correct.</b> Your receipt is ".
  163:       &Apache::lonnet::receipt;
  164:     $button=0;
  165:   } elsif ($solved =~ /^excused/) {
  166:     $message = "<b>You are excused from the problem.</b>";
  167:     $button=0;
  168:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  169:     if ($solved =~ /^incorrect/ || $solved eq '') {
  170:       $message = "Incorrect";
  171:       $button=1;
  172:     } else {
  173:       $message = "<b>You are correct.</b> Your receipt is ".
  174: 	&Apache::lonnet::receipt;
  175:       $button=0;
  176:     }
  177:   } elsif ($award eq 'NO_RESPONSE') {
  178:     $message = '';
  179:     $button=1;
  180:   } elsif ($award eq 'MISSING_ANSWER') {
  181:     $message = 'Some parts were not submitted';
  182:     $button = 1;
  183:   } elsif ($award eq 'WANTED_NUMERIC') {
  184:     $message = "This question expects a numeric answer";
  185:     $button=1;
  186:   } elsif ($award eq 'SIG_FAIL') {
  187:     $message = "Please adjust significant figures.";# you provided %s significant figures";
  188:     $button=1;
  189:   } elsif ($award eq 'UNIT_FAIL') {
  190:     $message = "Units incorrect."; #Computer reads units as %s";
  191:     $button=1;
  192:   } elsif ($award eq 'UNIT_NOTNEEDED') {
  193:     $message = "Only a number required.";# Computer reads units of %s";
  194:     $button=1;
  195:   } elsif ($award eq 'NO_UNIT') {
  196:     $message = "Units required";
  197:     $button=1;
  198:   } elsif ($award eq 'BAD_FORMULA') {
  199:     $message = "Unable to understand formula";
  200:     $button=1;
  201:   } elsif ($award eq 'INCORRECT') {
  202:     $message = "Incorrect";
  203:     $button=1;
  204:   } elsif ($award eq 'SUBMITTED') {
  205:     $message = "Your submission has been recorded.";
  206:     $button=1;
  207:   } else {
  208:     $message = "Unknown message: $award";
  209:     $button=1;
  210:   }
  211:   return ($button,$message);
  212: }
  213: 
  214: sub setgradedata {
  215:   my ($award,$id) = @_;
  216:   if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  217:     $Apache::lonhomework::results{"resource.$id.tries"} =
  218:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  219:     $Apache::lonhomework::results{"resource.$id.solved"} =
  220:       'correct_by_student';
  221:     $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  222:   } elsif ( $award eq 'INCORRECT' ) {
  223:     $Apache::lonhomework::results{"resource.$id.tries"} =
  224:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  225:     $Apache::lonhomework::results{"resource.$id.solved"} =
  226:       'incorrect_attempted';
  227:   } elsif ( $award eq 'SUBMITTED' ) {
  228:     $Apache::lonhomework::results{"resource.$id.tries"} =
  229:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  230:     $Apache::lonhomework::results{"resource.$id.solved"} =
  231:       'ungraded_attempted';
  232:   } else {
  233:     $Apache::lonhomework::results{"resource.$id.solved"} =
  234:       'incorrect_attempted';
  235:   }
  236:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
  237: }
  238: 
  239: sub grade {
  240:   my ($target) = @_;
  241:   my $id = $Apache::inputtags::part;
  242: #  my $result='';
  243:   my $response='';
  244:   if ( $target == 'web' ) {
  245:     if ( defined $ENV{'form.submitted'}) {
  246:       my @awards = ();
  247:       &Apache::lonxml::debug("$#Apache::inputtags::responselist");
  248:       foreach $response (@Apache::inputtags::responselist) {
  249: 	&Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  250: 	my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  251: 	if ( $value ne '' ) {
  252: 	  &Apache::lonxml::debug("keeping $value from $response for $id");
  253: 	  push (@awards,$value);
  254: 	} else {
  255: 	  &Apache::lonxml::debug("skipping $value from $response for $id");
  256: 	}
  257:       }
  258:       my $finalaward = &finalizeawards(@awards);
  259:       &Apache::lonxml::debug("final award $finalaward");
  260:       &setgradedata($finalaward,$id);
  261:     }
  262:   }
  263:   return '';
  264: }
  265: 
  266: sub gradestatus {
  267:   my ($id) = @_;
  268:   my $showbutton = 1;
  269:   my $message = '';
  270:   my $trystr='';
  271:   my $button='';
  272:   
  273:   my $status = $Apache::inputtags::status['-1'];
  274:   &Apache::lonxml::debug("gradestatus has :$status:");
  275:   if ( $status ne 'CLOSED' ) {  
  276:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
  277:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  278:     &Apache::lonxml::debug("Found Award |$award|$solved|");
  279:     if ( $award ne '' ) {
  280:       &Apache::lonxml::debug('Getting message');
  281:       ($showbutton,$message) = &decideoutput($award,$solved);
  282:       $message="<br /><table bgcolor=\"#aaffaa\"><tr><td>$message</td></tr></table>";
  283:     }
  284:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  285:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  286:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
  287:     if ( $tries eq '' ) { $tries = '0'; }
  288:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  289:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  290:     if ( $showbutton ) {
  291:       $trystr = "<br />Tries $tries/$maxtries";
  292:     }
  293:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  294:     if ( $showbutton ) { 
  295:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
  296:     }
  297:   }
  298:   return $button.$message.$trystr;
  299: }
  300: 1;
  301: __END__
  302:  

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