File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.33: download - view: text, annotated - select for diffs
Sat May 19 00:23:38 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- added some commentary

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

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