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

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

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