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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.49    ! matthew     4: # $Id: inputtags.pm,v 1.48 2002/01/08 21:09:23 albertel Exp $
1.47      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
                     27: #
1.43      albertel   28: # 2/19 Guy 
1.1       albertel   29: 
                     30: package Apache::inputtags;
                     31: use strict;
                     32: 
                     33: sub BEGIN {
1.48      albertel   34:   &Apache::lonxml::register('Apache::inputtags',('textfield','textline','datasubmission'));
1.1       albertel   35: }
                     36: 
1.43      albertel   37: 
1.1       albertel   38: sub initialize_inputtags {
1.43      albertel   39:   # list of current input ids
                     40:   @Apache::inputtags::input=();
                     41:   # list of all input ids seen in this problem
                     42:   @Apache::inputtags::inputlist=();
                     43:   # list of all current response ids
                     44:   @Apache::inputtags::response=();
                     45:   # list of all response ids seen in this problem
                     46:   @Apache::inputtags::responselist=();
                     47:   # list of whether or not a specific response was previously used
                     48:   @Apache::inputtags::previous=();
                     49:   # id of current part, 0 means that no part is current (inside <problem> only
                     50:   $Apache::inputtags::part='';
1.46      albertel   51:   # list of problem date statuses, the first element is for <problem>
                     52:   # if there is a second element it is for the current <part>
1.43      albertel   53:   @Apache::inputtags::status=();
1.46      albertel   54:   # hash of defined params for the current response
1.43      albertel   55:   %Apache::inputtags::params=();
1.46      albertel   56:   # list of all ids, for <import>, these get join()ed and prepended
                     57:   @Apache::inputtags::import=();
1.1       albertel   58: }
                     59: 
1.14      albertel   60: sub start_input {
1.43      albertel   61:   my ($parstack,$safeeval)=@_;
                     62:   my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
                     63:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
                     64:   push (@Apache::inputtags::input,$id);
                     65:   push (@Apache::inputtags::inputlist,$id);
                     66:   return $id;
1.14      albertel   67: }
                     68: 
                     69: sub end_input {
1.43      albertel   70:   pop @Apache::inputtags::input;
                     71:   return '';
1.14      albertel   72: }
                     73: 
1.48      albertel   74: sub start_textfield {
1.43      albertel   75:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                     76:   my $result = "";
                     77:   my $id = &start_input($parstack,$safeeval);
                     78:   if ($target eq 'web') {
                     79:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
                     80:     my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                     81:     if ( $cols eq '') { $cols = 80; }
                     82:     my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
                     83:     if ( $rows eq '') { $rows = 10; }
1.45      albertel   84:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                     85:       $result= '<textarea name="HWVAL'.$Apache::inputtags::response['-1'].'" '.
                     86: 	"rows=\"$rows\" cols=\"$cols\">".$oldresponse;
                     87:     } else {
                     88:       $result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
                     89:     }
1.43      albertel   90:     if ($oldresponse ne '') {
                     91:       #get rid of any startup text if the user has already responded
1.48      albertel   92:       &Apache::lonxml::get_all_text("/textfield",$$parser[$#$parser]);
1.30      albertel   93:     }
1.43      albertel   94:   }
                     95:   return $result;
1.6       albertel   96: }
                     97: 
1.48      albertel   98: sub end_textfield {
1.43      albertel   99:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    100:   if ($target eq 'web') {
1.45      albertel  101:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    102:       return "</textarea>";
                    103:     }
                    104:   }
1.43      albertel  105:   &end_input;
                    106:   return '';
1.6       albertel  107: }
                    108: 
1.1       albertel  109: sub start_textline {
1.43      albertel  110:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    111:   my $result = "";
                    112:   if ($target eq 'web') {
                    113:     my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    114:     if ($size eq '') { $size=20; }
1.44      albertel  115:     my $partid=$Apache::inputtags::part;
                    116:     my $id=$Apache::inputtags::response['-1'];
                    117:     my $oldresponse = 
                    118:       $Apache::lonhomework::history{"resource.$partid.$id.submission"};
1.45      albertel  119:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    120:       $result= '<input type="text" name="HWVAL'.$id.'" value="'.
                    121: 	$oldresponse.'" size="'.$size.'" />';
                    122:     } else {
                    123:       $result='<i>'.$oldresponse.'</i>';
                    124:     }
1.44      albertel  125:   } elsif ($target eq 'edit') {
1.49    ! matthew   126:     $result=&Apache::edit::tag_start($target,$token);
1.43      albertel  127:     $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
                    128:     $result.=&Apache::edit::end_table;
1.44      albertel  129:   } elsif ($target eq 'modified') {
1.43      albertel  130:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
                    131:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    132:   }
                    133:   return $result;
1.1       albertel  134: }
                    135: 
                    136: sub end_textline {
1.43      albertel  137:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    138:   if ($target eq 'edit') { return ('','no'); }
                    139:   return "";
1.1       albertel  140: }
                    141: 
1.6       albertel  142: sub start_datasubmission {
1.44      albertel  143:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    144:   my $id = &Apache::response::start_response($parstack,$safeeval);
                    145:   my $result;
                    146:   if ($target eq 'meta') {
                    147:     $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    148: 						   $token->[2]->{'type'},
                    149: 						   $token->[2]->{'display'});
                    150:     $result .= &Apache::response::meta_package_write('datasubmission');
                    151:   }
                    152:   return $result;
1.6       albertel  153: }
                    154: 
                    155: sub end_datasubmission {
1.43      albertel  156:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.44      albertel  157:   my $result;
                    158:   if ( $target eq 'web' ) {
                    159:   } elsif ($target eq 'grade' ) {
                    160:     if ( defined $ENV{'form.submitted'}) {
                    161:       &Apache::response::setup_params('datasubmission');
                    162:       my $partid = $Apache::inputtags::part;
                    163:       my $id = $Apache::inputtags::response['-1'];
                    164:       my $response = $ENV{'form.HWVAL'.$id};
                    165:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    166:       if ( $response =~ /[^\s]/) {
                    167: 	$Apache::lonhomework::results{"resource.$partid.$id.$name"}=
                    168: 	  $response;
                    169:       }
                    170:     }
1.43      albertel  171:   }
1.44      albertel  172:   &Apache::response::end_response;
                    173:   return $result;
1.9       albertel  174: }
                    175: 
                    176: sub finalizeawards {
1.43      albertel  177:   my $result='';
                    178:   my $award;
                    179:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
                    180:   if ($result eq '' ) {
                    181:     foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
                    182:   }
                    183:   if ($result eq '' ) {
                    184:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
                    185:   }
                    186:   if ($result eq '' ) {
                    187:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
                    188:   }
                    189: 
                    190:   if ($result eq '' ) {
                    191:     foreach $award (@_) { 
                    192:       if ($award eq 'UNIT_FAIL' ||
                    193: 	  $award eq 'NO_UNIT' ||
                    194: 	  $award eq 'UNIT_NOTNEEDED') {
                    195: 	$result=$award; last;
                    196:       }
                    197:     }
                    198:   }
                    199:   if ($result eq '' ) {
                    200:     foreach $award (@_) { 
                    201:       if ($award eq 'WANTED_NUMERIC' || 
                    202: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
                    203:     }
                    204:   }
                    205:   if ($result eq '' ) {
                    206:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
                    207:   }
                    208:   if ($result eq '' ) {
                    209:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
                    210:   }
                    211:   if ($result eq '' ) {
                    212:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
                    213:   }
                    214:   if ($result eq '' ) {
                    215:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
                    216:   }
                    217:   if ($result eq '' ) { $result='EXACT_ANS'; }
                    218:   return $result
1.9       albertel  219: }
                    220: 
1.10      albertel  221: sub decideoutput {
1.43      albertel  222:   my ($award,$solved,$previous)=@_;
                    223:   my $message='';
                    224:   my $button=0;
                    225:   my $previousmsg;
                    226: 
                    227:   if ($previous) { $previousmsg='You have entered that answer before'; }
                    228: 
                    229:   if      ($solved =~ /^correct/) {
                    230:     $message = "<b>You are correct.</b> Your receipt is ".
                    231:       &Apache::lonnet::receipt;
                    232:     $button=0;
                    233:     $previousmsg='';
                    234:   } elsif ($solved =~ /^excused/) {
                    235:     $message = "<b>You are excused from the problem.</b>";
                    236:     $button=0;
                    237:     $previousmsg='';
                    238:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                    239:     if ($solved =~ /^incorrect/ || $solved eq '') {
                    240:       $message = "Incorrect";
                    241:       $button=1;
1.37      albertel  242:     } else {
1.43      albertel  243:       $message = "<b>You are correct.</b> Your receipt is ".
                    244: 	&Apache::lonnet::receipt;
                    245:       $button=0;
                    246:       $previousmsg='';
                    247:     }
                    248:   } elsif ($award eq 'NO_RESPONSE') {
                    249:     $message = '';
                    250:     $button=1;
                    251:   } elsif ($award eq 'MISSING_ANSWER') {
                    252:     $message = 'Some parts were not submitted';
                    253:     $button = 1;
                    254:   } elsif ($award eq 'WANTED_NUMERIC') {
                    255:     $message = "This question expects a numeric answer";
                    256:     $button=1;
                    257:   } elsif ($award eq 'SIG_FAIL') {
                    258:     $message = "Please adjust significant figures.";# you provided %s significant figures";
                    259:     $button=1;
                    260:   } elsif ($award eq 'UNIT_FAIL') {
                    261:     $message = "Units incorrect."; #Computer reads units as %s";
                    262:     $button=1;
                    263:   } elsif ($award eq 'UNIT_NOTNEEDED') {
                    264:     $message = "Only a number required.";# Computer reads units of %s";
                    265:     $button=1;
                    266:   } elsif ($award eq 'NO_UNIT') {
                    267:     $message = "Units required";
                    268:     $button=1;
                    269:   } elsif ($award eq 'BAD_FORMULA') {
                    270:     $message = "Unable to understand formula";
                    271:     $button=1;
                    272:   } elsif ($award eq 'INCORRECT') {
                    273:     $message = "Incorrect";
                    274:     $button=1;
                    275:   } elsif ($award eq 'SUBMITTED') {
                    276:     $message = "Your submission has been recorded.";
                    277:     $button=1;
                    278:   } else {
                    279:     $message = "Unknown message: $award";
                    280:     $button=1;
                    281:   }
                    282:   return ($button,$message,$previousmsg);
1.12      albertel  283: }
                    284: 
                    285: sub setgradedata {
1.43      albertel  286:   my ($award,$id,$previously_used) = @_;
                    287:   # if the student already has it correct, don't modify the status
                    288:   if ( $Apache::lonhomework::history{"resource.$id.solved"} !~
                    289:        /^correct/ ) {
                    290:     #handle assignment of tries and solved status
                    291:     if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                    292:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    293: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    294:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    295: 	'correct_by_student';
                    296:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                    297:     } elsif ( $award eq 'INCORRECT' ) {
                    298:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    299: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    300:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    301: 	'incorrect_attempted';
                    302:     } elsif ( $award eq 'SUBMITTED' ) {
                    303:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    304: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    305:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    306: 	'ungraded_attempted';
                    307:     } elsif ( $award eq 'NO_RESPONSE' ) {
                    308:       return '';
                    309:     } else {
                    310:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    311: 	'incorrect_attempted';
                    312:     }
                    313: 
                    314:     # check if this was a previous submission if it was delete the
                    315:     # unneeded data and update the previously_used attribute
                    316:     if ( $previously_used eq 'PREVIOUSLY_USED') {
                    317:       delete($Apache::lonhomework::results{"resource.$id.tries"});
                    318:       $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                    319:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
                    320:       #delete all data as they student didn't do anything
                    321:       foreach my $key (keys(%Apache::lonhomework::results)) {
                    322: 	if ($key =~ /^resource\.$id\./) {
                    323: 	  &Apache::lonxml::debug("Removing $key");
                    324: 	  delete($Apache::lonhomework::results{$key});
                    325: 	}
                    326:       }
                    327:       #and since they didn't do anything we were never here
                    328:       return '';
                    329:     } else {
                    330:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40      albertel  331:     }
1.43      albertel  332:   }
                    333:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  334: }
                    335: 
1.9       albertel  336: sub grade {
1.43      albertel  337:   my ($target) = @_;
                    338:   my $id = $Apache::inputtags::part;
                    339:   my $response='';
                    340:   if ( defined $ENV{'form.submitted'}) {
                    341:     my @awards = ();
                    342:     foreach $response (@Apache::inputtags::responselist) {
                    343:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    344:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
                    345:       if ( $value ne '' ) {
                    346: 	&Apache::lonxml::debug("keeping $value from $response for $id");
                    347: 	push (@awards,$value);
                    348:       } else {
                    349: 	&Apache::lonxml::debug("skipping $value from $response for $id");
                    350:       }
                    351:     }
                    352:     my $finalaward = &finalizeawards(@awards);
                    353:     my $previously_used;
                    354:     if ( $#Apache::inputtags::previous eq $#awards ) {
                    355:       $previously_used = 'PREVIOUSLY_LAST';
                    356:       foreach my $value (@Apache::inputtags::previous) {
                    357: 	if ($value eq 'PREVIOUSLY_USED' ) {
                    358: 	  $previously_used = $value;
                    359: 	  last;
                    360: 	}
                    361:       }
                    362:     }
                    363:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
                    364:     &setgradedata($finalaward,$id,$previously_used);
                    365:   }
                    366:   return '';
1.1       albertel  367: }
                    368: 
1.11      albertel  369: sub gradestatus {
1.43      albertel  370:   my ($id) = @_;
                    371:   my $showbutton = 1;
                    372:   my $message = '';
                    373:   my $trystr='';
                    374:   my $button='';
                    375:   my $previousmsg='';
                    376: 
                    377:   my $status = $Apache::inputtags::status['-1'];
                    378:   &Apache::lonxml::debug("gradestatus has :$status:");
                    379:   if ( $status ne 'CLOSED' ) {  
                    380:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
                    381:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    382:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
                    383:     &Apache::lonxml::debug("Found Award |$award|$solved|");
                    384:     if ( $award ne '' ) {
                    385:       &Apache::lonxml::debug('Getting message');
                    386:       ($showbutton,$message,$previousmsg) =
                    387: 	&decideoutput($award,$solved,$previous);
                    388:       $message="<td bgcolor=\"#aaffaa\">$message</td>";
                    389:       if ($previousmsg) {
                    390: 	$previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
                    391:       }
                    392:     }
                    393:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    394:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    395:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                    396:     if ( $tries eq '' ) { $tries = '0'; }
                    397:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    398:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                    399:     if ( $showbutton ) {
                    400:       $trystr = "<td>Tries $tries/$maxtries</td>";
                    401:     }
                    402:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
                    403:     if ( $showbutton ) { 
                    404:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
                    405:     }
                    406:   }
                    407:   my $output= $previousmsg.$message.$trystr;
                    408:   if ($output =~ /^\s*$/) {
                    409:     return $button;
                    410:   } else {
                    411:     return $button.'<table><tr>'.$previousmsg.$message.$trystr.'</tr></table>';
                    412:   }
1.11      albertel  413: }
1.1       albertel  414: 1;
                    415: __END__
1.43      albertel  416:  

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