File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.34: download - view: text, annotated - select for diffs
Fri Jun 1 15:52:50 2001 UTC (23 years ago) by albertel
Branches: MAIN
CVS tags: HEAD
- modified to use get_param

    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,$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,$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,$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:   return $result;
   83: }
   84: 
   85: sub end_textline {
   86:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   87:   return "";
   88: }
   89: 
   90: sub start_datasubmission {
   91:   return '';
   92: }
   93: 
   94: sub end_datasubmission {
   95:   my ($target,$token,$parstack,$parser,$safeeval)=@_;
   96:   if ( $target == 'web' ) {
   97:     return '<input type="submit" name="submit" value="Submit All Data" />';
   98:   }
   99:   return '';
  100: }
  101: 
  102: sub finalizeawards {
  103:   my $result='';
  104:   my $award;
  105:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
  106:   if ($result eq '' ) {
  107:     foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
  108:   }
  109:   if ($result eq '' ) {
  110:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
  111:   }
  112:   if ($result eq '' ) {
  113:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
  114:   }
  115: 
  116:   if ($result eq '' ) {
  117:     foreach $award (@_) { 
  118:       if ($award eq 'UNIT_FAIL' ||
  119: 	  $award eq 'NO_UNIT' ||
  120: 	  $award eq 'UNIT_NOTNEEDED') {
  121: 	$result=$award; last;
  122:       }
  123:     }
  124:   }
  125:   if ($result eq '' ) {
  126:     foreach $award (@_) { 
  127:       if ($award eq 'WANTED_NUMERIC' || 
  128: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
  129:     }
  130:   }
  131:   if ($result eq '' ) {
  132:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
  133:   }
  134:   if ($result eq '' ) {
  135:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
  136:   }
  137:   if ($result eq '' ) {
  138:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
  139:   }
  140:   if ($result eq '' ) {
  141:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
  142:   }
  143:   if ($result eq '' ) { $result='EXACT_ANS'; }
  144:   return $result
  145: }
  146: 
  147: sub decideoutput {
  148:   my ($award,$solved)=@_;
  149:   my $message='';
  150:   my $button=0;
  151:   if      ($solved =~ /^correct/ || $award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  152:     $message = "<b>You are correct.</b> Your receipt is ".&Apache::lonnet::receipt;
  153:     $button=0;
  154:   } elsif ($award eq 'NO_RESPONSE') {
  155:     $message = '';
  156:     $button=1;
  157:   } elsif ($award eq 'MISSING_ANSWER') {
  158:     $message = 'Some parts were not submitted';
  159:     $button = 1;
  160:   } elsif ($award eq 'WANTED_NUMERIC') {
  161:     $message = "This question expects a numeric answer";
  162:     $button=1;
  163:   } elsif ($award eq 'SIG_FAIL') {
  164:     $message = "Please adjust significant figures.";# you provided %s significant figures";
  165:     $button=1;
  166:   } elsif ($award eq 'UNIT_FAIL') {
  167:     $message = "Units incorrect."; #Computer reads units as %s";
  168:     $button=1;
  169:   } elsif ($award eq 'UNIT_NOTNEEDED') {
  170:     $message = "Only a number required.";# Computer reads units of %s";
  171:     $button=1;
  172:   } elsif ($award eq 'NO_UNIT') {
  173:     $message = "Units required";
  174:     $button=1;
  175:   } elsif ($award eq 'BAD_FORMULA') {
  176:     $message = "Unable to understand formula";
  177:     $button=1;
  178:   } elsif ($award eq 'INCORRECT') {
  179:     $message = "Incorrect";
  180:     $button=1;
  181:   } elsif ($award eq 'SUBMITTED') {
  182:     $message = "Your submission has been recorded.";
  183:     $button=1;
  184:   } else {
  185:     $message = "Unknown message: $award";
  186:     $button=1;
  187:   }
  188:   return ($button,$message);
  189: }
  190: 
  191: sub setgradedata {
  192:   my ($award,$id) = @_;
  193:   if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  194:     $Apache::lonhomework::results{"resource.$id.tries"} =
  195:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  196:     $Apache::lonhomework::results{"resource.$id.solved"} =
  197:       'correct_by_student';
  198:     $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  199:   } elsif ( $award eq 'INCORRECT' ) {
  200:     $Apache::lonhomework::results{"resource.$id.tries"} =
  201:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  202:     $Apache::lonhomework::results{"resource.$id.solved"} =
  203:       'incorrect_attempted';
  204:   } elsif ( $award eq 'SUBMITTED' ) {
  205:     $Apache::lonhomework::results{"resource.$id.tries"} =
  206:       $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  207:     $Apache::lonhomework::results{"resource.$id.solved"} =
  208:       'ungraded_attempted';
  209:   } else {
  210:     $Apache::lonhomework::results{"resource.$id.solved"} =
  211:       'incorrect_attempted';
  212:   }
  213:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
  214: }
  215: 
  216: sub grade {
  217:   my ($target) = @_;
  218:   my $id = $Apache::inputtags::part;
  219: #  my $result='';
  220:   my $response='';
  221:   if ( $target == 'web' ) {
  222:     if ( defined $ENV{'form.submitted'}) {
  223:       my @awards = ();
  224:       &Apache::lonxml::debug("$#Apache::inputtags::responselist");
  225:       foreach $response (@Apache::inputtags::responselist) {
  226: 	&Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  227: 	my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  228: 	if ( $value ne '' ) {
  229: 	  &Apache::lonxml::debug("keeping $value from $response for $id");
  230: 	  push (@awards,$value);
  231: 	} else {
  232: 	  &Apache::lonxml::debug("skipping $value from $response for $id");
  233: 	}
  234:       }
  235:       my $finalaward = &finalizeawards(@awards);
  236:       &Apache::lonxml::debug("final award $finalaward");
  237:       &setgradedata($finalaward,$id);
  238:     }
  239:   }
  240:   return '';
  241: }
  242: 
  243: sub gradestatus {
  244:   my ($id) = @_;
  245:   my $showbutton = 1;
  246:   my $message = '';
  247:   my $trystr='';
  248:   my $button='';
  249:   
  250:   my $status = $Apache::inputtags::status['-1'];
  251:   &Apache::lonxml::debug("gradestatus has :$status:");
  252:   if ( $status ne 'CLOSED' ) {  
  253:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
  254:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  255:     &Apache::lonxml::debug("Found Award |$award|$solved|");
  256:     if ( $award ne '' ) {
  257:       &Apache::lonxml::debug('Getting message');
  258:       ($showbutton,$message) = &decideoutput($award,$solved);
  259:       $message="<br /><table bgcolor=\"#aaffaa\"><tr><td>$message</td></tr></table>";
  260:     }
  261:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  262:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  263:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
  264:     if ( $tries eq '' ) { $tries = '0'; }
  265:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  266:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  267:     if ( $showbutton ) {
  268:       $trystr = "<br />Tries $tries/$maxtries";
  269:     }
  270:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  271:     if ( $showbutton ) { 
  272:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
  273:     }
  274:   }
  275:   return $button.$message.$trystr;
  276: }
  277: 1;
  278: __END__
  279:  

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