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

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.33      albertel   14:   # list of current input ids
1.7       albertel   15:   @Apache::inputtags::input=();
1.33      albertel   16:   # list of all input ids seen in this problem
1.14      albertel   17:   @Apache::inputtags::inputlist=();
1.33      albertel   18:   # list of all current response ids
1.7       albertel   19:   @Apache::inputtags::response=();
1.33      albertel   20:   #list of all response ids seen in this problem
1.9       albertel   21:   @Apache::inputtags::responselist=();
1.33      albertel   22:   # id of current part, 0 means that no part is current (inside <problem> only
1.8       albertel   23:   $Apache::inputtags::part='';
1.33      albertel   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>
1.18      albertel   26:   @Apache::inputtags::status=();
1.33      albertel   27:   #hash of defined params for the current response
1.7       albertel   28:   %Apache::inputtags::params=();
1.1       albertel   29: }
                     30: 
1.14      albertel   31: sub start_input {
                     32:   my ($parstack,$safeeval)=@_;
1.34      albertel   33:   my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
1.38      albertel   34:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
1.14      albertel   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: 
1.6       albertel   45: sub start_textarea {
1.35      albertel   46:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.6       albertel   47:   my $result = "";
1.14      albertel   48:   my $id = &start_input($parstack,$safeeval);
1.6       albertel   49:   if ($target eq 'web') {
1.30      albertel   50:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
1.34      albertel   51:     my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
1.31      albertel   52:     if ( $cols eq '') { $cols = 80; }
1.34      albertel   53:     my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.31      albertel   54:     if ( $rows eq '') { $rows = 10; }
                     55:     $result= '<textarea name="HWVAL'.$Apache::inputtags::response['-1'].'" '.
                     56:       "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
1.30      albertel   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:     }
1.6       albertel   61:   }
                     62:   return $result;
                     63: }
                     64: 
                     65: sub end_textarea {
1.35      albertel   66:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.10      albertel   67:   if ($target eq 'web') {
                     68:     return "</textarea>";
                     69:   } 
1.14      albertel   70:   &end_input;
1.10      albertel   71:   return '';
1.6       albertel   72: }
                     73: 
1.1       albertel   74: sub start_textline {
1.35      albertel   75:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.1       albertel   76:   my $result = "";
                     77:   if ($target eq 'web') {
1.34      albertel   78:     my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
1.10      albertel   79:     if ($size eq '') { $size=20; }
1.15      albertel   80:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
1.28      albertel   81:     $result= '<input type="text" name="HWVAL'.$Apache::inputtags::response['-1'].'" value="'.$oldresponse.'" size="'.$size.'" />';
1.1       albertel   82:   }
1.36      albertel   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:   }
1.1       albertel   92:   return $result;
                     93: }
                     94: 
                     95: sub end_textline {
1.35      albertel   96:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.36      albertel   97:   if ($target eq 'edit') { return ('','no'); }
1.6       albertel   98:   return "";
1.1       albertel   99: }
                    100: 
1.6       albertel  101: sub start_datasubmission {
1.34      albertel  102:   return '';
1.6       albertel  103: }
                    104: 
                    105: sub end_datasubmission {
1.35      albertel  106:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.6       albertel  107:   if ( $target == 'web' ) {
1.28      albertel  108:     return '<input type="submit" name="submit" value="Submit All Data" />';
1.2       albertel  109:   }
1.10      albertel  110:   return '';
1.9       albertel  111: }
                    112: 
                    113: sub finalizeawards {
                    114:   my $result='';
                    115:   my $award;
                    116:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
1.15      albertel  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:   }
1.29      albertel  123:   if ($result eq '' ) {
                    124:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
                    125:   }
1.15      albertel  126: 
1.9       albertel  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 '' ) {
1.30      albertel  149:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
                    150:   }
                    151:   if ($result eq '' ) {
1.9       albertel  152:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
                    153:   }
                    154:   if ($result eq '' ) { $result='EXACT_ANS'; }
                    155:   return $result
                    156: }
                    157: 
1.10      albertel  158: sub decideoutput {
1.32      albertel  159:   my ($award,$solved)=@_;
1.10      albertel  160:   my $message='';
                    161:   my $button=0;
1.37      albertel  162:   if      ($solved =~ /^correct/) {
                    163:     $message = "<b>You are correct.</b> Your receipt is ".
                    164:       &Apache::lonnet::receipt;
1.12      albertel  165:     $button=0;
1.37      albertel  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:     }
1.13      albertel  178:   } elsif ($award eq 'NO_RESPONSE') {
                    179:     $message = '';
                    180:     $button=1;
1.14      albertel  181:   } elsif ($award eq 'MISSING_ANSWER') {
                    182:     $message = 'Some parts were not submitted';
                    183:     $button = 1;
1.10      albertel  184:   } elsif ($award eq 'WANTED_NUMERIC') {
                    185:     $message = "This question expects a numeric answer";
1.12      albertel  186:     $button=1;
1.10      albertel  187:   } elsif ($award eq 'SIG_FAIL') {
1.21      albertel  188:     $message = "Please adjust significant figures.";# you provided %s significant figures";
1.12      albertel  189:     $button=1;
1.10      albertel  190:   } elsif ($award eq 'UNIT_FAIL') {
1.21      albertel  191:     $message = "Units incorrect."; #Computer reads units as %s";
1.12      albertel  192:     $button=1;
1.10      albertel  193:   } elsif ($award eq 'UNIT_NOTNEEDED') {
1.21      albertel  194:     $message = "Only a number required.";# Computer reads units of %s";
1.12      albertel  195:     $button=1;
1.10      albertel  196:   } elsif ($award eq 'NO_UNIT') {
                    197:     $message = "Units required";
1.12      albertel  198:     $button=1;
1.10      albertel  199:   } elsif ($award eq 'BAD_FORMULA') {
                    200:     $message = "Unable to understand formula";
1.12      albertel  201:     $button=1;
1.10      albertel  202:   } elsif ($award eq 'INCORRECT') {
                    203:     $message = "Incorrect";
1.12      albertel  204:     $button=1;
1.30      albertel  205:   } elsif ($award eq 'SUBMITTED') {
                    206:     $message = "Your submission has been recorded.";
                    207:     $button=1;
1.10      albertel  208:   } else {
                    209:     $message = "Unknown message: $award";
1.12      albertel  210:     $button=1;
1.10      albertel  211:   }
1.12      albertel  212:   return ($button,$message);
                    213: }
                    214: 
                    215: sub setgradedata {
                    216:   my ($award,$id) = @_;
1.39    ! albertel  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:     }
1.12      albertel  242:   }
1.15      albertel  243:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  244: }
                    245: 
1.9       albertel  246: sub grade {
                    247:   my ($target) = @_;
                    248:   my $id = $Apache::inputtags::part;
1.20      albertel  249: #  my $result='';
1.9       albertel  250:   my $response='';
                    251:   if ( $target == 'web' ) {
1.25      albertel  252:     if ( defined $ENV{'form.submitted'}) {
1.9       albertel  253:       my @awards = ();
1.10      albertel  254:       &Apache::lonxml::debug("$#Apache::inputtags::responselist");
1.9       albertel  255:       foreach $response (@Apache::inputtags::responselist) {
1.15      albertel  256: 	&Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    257: 	my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1.9       albertel  258: 	if ( $value ne '' ) {
1.30      albertel  259: 	  &Apache::lonxml::debug("keeping $value from $response for $id");
1.9       albertel  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");
1.12      albertel  267:       &setgradedata($finalaward,$id);
1.9       albertel  268:     }
                    269:   }
1.20      albertel  270:   return '';
1.1       albertel  271: }
                    272: 
1.11      albertel  273: sub gradestatus {
1.12      albertel  274:   my ($id) = @_;
                    275:   my $showbutton = 1;
                    276:   my $message = '';
1.13      albertel  277:   my $trystr='';
1.18      albertel  278:   my $button='';
1.17      albertel  279:   
1.19      albertel  280:   my $status = $Apache::inputtags::status['-1'];
                    281:   &Apache::lonxml::debug("gradestatus has :$status:");
1.22      albertel  282:   if ( $status ne 'CLOSED' ) {  
1.17      albertel  283:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
1.32      albertel  284:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    285:     &Apache::lonxml::debug("Found Award |$award|$solved|");
1.17      albertel  286:     if ( $award ne '' ) {
                    287:       &Apache::lonxml::debug('Getting message');
1.32      albertel  288:       ($showbutton,$message) = &decideoutput($award,$solved);
1.39    ! albertel  289:       $message="<td bgcolor=\"#aaffaa\">$message</td>";
1.17      albertel  290:     }
                    291:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    292:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
1.18      albertel  293:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
1.17      albertel  294:     if ( $tries eq '' ) { $tries = '0'; }
                    295:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
1.23      albertel  296:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
1.17      albertel  297:     if ( $showbutton ) {
1.39    ! albertel  298:       $trystr = "<td>Tries $tries/$maxtries</td>";
1.17      albertel  299:     }
1.22      albertel  300:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
1.17      albertel  301:     if ( $showbutton ) { 
1.27      albertel  302:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
1.17      albertel  303:     }
1.12      albertel  304:   }
1.39    ! albertel  305:   return $button.'<table><tr>'.$message.$trystr.'</tr></table>';
1.11      albertel  306: }
1.1       albertel  307: 1;
                    308: __END__
                    309:  

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