Annotation of loncom/homework/inputtags.pm, revision 1.31

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

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