File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.39: download - view: text, annotated - select for diffs
Mon Jul 30 22:24:01 2001 UTC (22 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- once a problem is marked correct their is no way it should revert to incorrect. (this will need to be revisited when exams problems are implemented)

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

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