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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.54    ! albertel    4: # $Id: inputtags.pm,v 1.53 2002/03/01 00:23:04 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: 
1.50      harris41   33: 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.51      albertel   92:       &Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
                     93:     }
                     94:   }
                     95:   if ($target eq 'grade') {
                     96:     my $seedtext=&Apache::lonxml::get_all_text("/textfield",$$parser[-1]);
                     97:     if ($seedtext eq $ENV{'form.HWVAL'.$Apache::inputtags::response[-1]}) {
                     98:       # if the seed text is still there it wasn't a real submission
                     99:       $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']}='';
1.30      albertel  100:     }
1.43      albertel  101:   }
                    102:   return $result;
1.6       albertel  103: }
                    104: 
1.48      albertel  105: sub end_textfield {
1.43      albertel  106:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    107:   if ($target eq 'web') {
1.45      albertel  108:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    109:       return "</textarea>";
                    110:     }
                    111:   }
1.43      albertel  112:   &end_input;
                    113:   return '';
1.6       albertel  114: }
                    115: 
1.1       albertel  116: sub start_textline {
1.43      albertel  117:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    118:   my $result = "";
                    119:   if ($target eq 'web') {
                    120:     my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    121:     if ($size eq '') { $size=20; }
1.44      albertel  122:     my $partid=$Apache::inputtags::part;
                    123:     my $id=$Apache::inputtags::response['-1'];
                    124:     my $oldresponse = 
                    125:       $Apache::lonhomework::history{"resource.$partid.$id.submission"};
1.45      albertel  126:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    127:       $result= '<input type="text" name="HWVAL'.$id.'" value="'.
                    128: 	$oldresponse.'" size="'.$size.'" />';
                    129:     } else {
                    130:       $result='<i>'.$oldresponse.'</i>';
                    131:     }
1.44      albertel  132:   } elsif ($target eq 'edit') {
1.49      matthew   133:     $result=&Apache::edit::tag_start($target,$token);
1.43      albertel  134:     $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
                    135:     $result.=&Apache::edit::end_table;
1.44      albertel  136:   } elsif ($target eq 'modified') {
1.43      albertel  137:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
                    138:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    139:   }
                    140:   return $result;
1.1       albertel  141: }
                    142: 
                    143: sub end_textline {
1.43      albertel  144:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    145:   if ($target eq 'edit') { return ('','no'); }
                    146:   return "";
1.1       albertel  147: }
                    148: 
1.6       albertel  149: sub start_datasubmission {
1.44      albertel  150:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    151:   my $id = &Apache::response::start_response($parstack,$safeeval);
                    152:   my $result;
                    153:   if ($target eq 'meta') {
                    154:     $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
                    155: 						   $token->[2]->{'type'},
                    156: 						   $token->[2]->{'display'});
                    157:     $result .= &Apache::response::meta_package_write('datasubmission');
                    158:   }
                    159:   return $result;
1.6       albertel  160: }
                    161: 
                    162: sub end_datasubmission {
1.43      albertel  163:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.44      albertel  164:   my $result;
                    165:   if ( $target eq 'web' ) {
                    166:   } elsif ($target eq 'grade' ) {
                    167:     if ( defined $ENV{'form.submitted'}) {
                    168:       &Apache::response::setup_params('datasubmission');
                    169:       my $partid = $Apache::inputtags::part;
                    170:       my $id = $Apache::inputtags::response['-1'];
                    171:       my $response = $ENV{'form.HWVAL'.$id};
                    172:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
                    173:       if ( $response =~ /[^\s]/) {
                    174: 	$Apache::lonhomework::results{"resource.$partid.$id.$name"}=
                    175: 	  $response;
                    176:       }
                    177:     }
1.43      albertel  178:   }
1.44      albertel  179:   &Apache::response::end_response;
                    180:   return $result;
1.9       albertel  181: }
                    182: 
                    183: sub finalizeawards {
1.43      albertel  184:   my $result='';
                    185:   my $award;
                    186:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
                    187:   if ($result eq '' ) {
1.54    ! albertel  188:     my $blankcount;
        !           189:     foreach $award (@_) {
        !           190:       if ($award eq '') {
        !           191: 	$result='MISSING_ANSWER';
        !           192: 	$blankcount++;
        !           193:       }
        !           194:     }
        !           195:     if ($blankcount == ($#_ + 1)) { $result = 'NO_RESPONSE'; }
1.43      albertel  196:   }
                    197:   if ($result eq '' ) {
                    198:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
                    199:   }
                    200:   if ($result eq '' ) {
                    201:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
                    202:   }
                    203: 
                    204:   if ($result eq '' ) {
                    205:     foreach $award (@_) { 
                    206:       if ($award eq 'UNIT_FAIL' ||
                    207: 	  $award eq 'NO_UNIT' ||
                    208: 	  $award eq 'UNIT_NOTNEEDED') {
                    209: 	$result=$award; last;
                    210:       }
                    211:     }
                    212:   }
                    213:   if ($result eq '' ) {
                    214:     foreach $award (@_) { 
                    215:       if ($award eq 'WANTED_NUMERIC' || 
                    216: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
                    217:     }
                    218:   }
                    219:   if ($result eq '' ) {
                    220:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
                    221:   }
                    222:   if ($result eq '' ) {
                    223:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
                    224:   }
                    225:   if ($result eq '' ) {
                    226:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
                    227:   }
                    228:   if ($result eq '' ) {
                    229:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
                    230:   }
                    231:   if ($result eq '' ) { $result='EXACT_ANS'; }
                    232:   return $result
1.9       albertel  233: }
                    234: 
1.10      albertel  235: sub decideoutput {
1.43      albertel  236:   my ($award,$solved,$previous)=@_;
                    237:   my $message='';
                    238:   my $button=0;
                    239:   my $previousmsg;
                    240: 
                    241:   if ($previous) { $previousmsg='You have entered that answer before'; }
                    242: 
                    243:   if      ($solved =~ /^correct/) {
                    244:     $message = "<b>You are correct.</b> Your receipt is ".
                    245:       &Apache::lonnet::receipt;
                    246:     $button=0;
                    247:     $previousmsg='';
                    248:   } elsif ($solved =~ /^excused/) {
                    249:     $message = "<b>You are excused from the problem.</b>";
                    250:     $button=0;
                    251:     $previousmsg='';
                    252:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                    253:     if ($solved =~ /^incorrect/ || $solved eq '') {
                    254:       $message = "Incorrect";
                    255:       $button=1;
1.37      albertel  256:     } else {
1.43      albertel  257:       $message = "<b>You are correct.</b> Your receipt is ".
                    258: 	&Apache::lonnet::receipt;
                    259:       $button=0;
                    260:       $previousmsg='';
                    261:     }
                    262:   } elsif ($award eq 'NO_RESPONSE') {
                    263:     $message = '';
                    264:     $button=1;
                    265:   } elsif ($award eq 'MISSING_ANSWER') {
                    266:     $message = 'Some parts were not submitted';
                    267:     $button = 1;
                    268:   } elsif ($award eq 'WANTED_NUMERIC') {
                    269:     $message = "This question expects a numeric answer";
                    270:     $button=1;
                    271:   } elsif ($award eq 'SIG_FAIL') {
                    272:     $message = "Please adjust significant figures.";# you provided %s significant figures";
                    273:     $button=1;
                    274:   } elsif ($award eq 'UNIT_FAIL') {
                    275:     $message = "Units incorrect."; #Computer reads units as %s";
                    276:     $button=1;
                    277:   } elsif ($award eq 'UNIT_NOTNEEDED') {
                    278:     $message = "Only a number required.";# Computer reads units of %s";
                    279:     $button=1;
                    280:   } elsif ($award eq 'NO_UNIT') {
                    281:     $message = "Units required";
                    282:     $button=1;
                    283:   } elsif ($award eq 'BAD_FORMULA') {
                    284:     $message = "Unable to understand formula";
                    285:     $button=1;
                    286:   } elsif ($award eq 'INCORRECT') {
                    287:     $message = "Incorrect";
                    288:     $button=1;
                    289:   } elsif ($award eq 'SUBMITTED') {
                    290:     $message = "Your submission has been recorded.";
                    291:     $button=1;
                    292:   } else {
                    293:     $message = "Unknown message: $award";
                    294:     $button=1;
                    295:   }
                    296:   return ($button,$message,$previousmsg);
1.12      albertel  297: }
                    298: 
                    299: sub setgradedata {
1.43      albertel  300:   my ($award,$id,$previously_used) = @_;
                    301:   # if the student already has it correct, don't modify the status
1.53      albertel  302:   if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER') {
                    303:     $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
                    304:     return '';
                    305:   } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.43      albertel  306:        /^correct/ ) {
                    307:     #handle assignment of tries and solved status
1.53      albertel  308:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    309:       $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                    310:     }
1.43      albertel  311:     if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                    312:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    313: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    314:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    315: 	'correct_by_student';
                    316:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                    317:     } elsif ( $award eq 'INCORRECT' ) {
                    318:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    319: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    320:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    321: 	'incorrect_attempted';
                    322:     } elsif ( $award eq 'SUBMITTED' ) {
                    323:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    324: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    325:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    326: 	'ungraded_attempted';
                    327:     } elsif ( $award eq 'NO_RESPONSE' ) {
                    328:       return '';
                    329:     } else {
                    330:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    331: 	'incorrect_attempted';
                    332:     }
                    333: 
                    334:     # check if this was a previous submission if it was delete the
                    335:     # unneeded data and update the previously_used attribute
                    336:     if ( $previously_used eq 'PREVIOUSLY_USED') {
                    337:       delete($Apache::lonhomework::results{"resource.$id.tries"});
                    338:       $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                    339:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
                    340:       #delete all data as they student didn't do anything
                    341:       foreach my $key (keys(%Apache::lonhomework::results)) {
                    342: 	if ($key =~ /^resource\.$id\./) {
                    343: 	  &Apache::lonxml::debug("Removing $key");
                    344: 	  delete($Apache::lonhomework::results{$key});
                    345: 	}
                    346:       }
                    347:       #and since they didn't do anything we were never here
                    348:       return '';
                    349:     } else {
                    350:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40      albertel  351:     }
1.43      albertel  352:   }
                    353:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  354: }
                    355: 
1.9       albertel  356: sub grade {
1.43      albertel  357:   my ($target) = @_;
                    358:   my $id = $Apache::inputtags::part;
                    359:   my $response='';
                    360:   if ( defined $ENV{'form.submitted'}) {
                    361:     my @awards = ();
                    362:     foreach $response (@Apache::inputtags::responselist) {
                    363:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    364:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1.54    ! albertel  365:       &Apache::lonxml::debug("keeping $value from $response for $id");
        !           366:       push (@awards,$value);
1.43      albertel  367:     }
                    368:     my $finalaward = &finalizeawards(@awards);
                    369:     my $previously_used;
                    370:     if ( $#Apache::inputtags::previous eq $#awards ) {
                    371:       $previously_used = 'PREVIOUSLY_LAST';
                    372:       foreach my $value (@Apache::inputtags::previous) {
                    373: 	if ($value eq 'PREVIOUSLY_USED' ) {
                    374: 	  $previously_used = $value;
                    375: 	  last;
                    376: 	}
                    377:       }
                    378:     }
                    379:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
                    380:     &setgradedata($finalaward,$id,$previously_used);
                    381:   }
                    382:   return '';
1.1       albertel  383: }
                    384: 
1.11      albertel  385: sub gradestatus {
1.43      albertel  386:   my ($id) = @_;
                    387:   my $showbutton = 1;
                    388:   my $message = '';
1.53      albertel  389:   my $latemessage = '';
1.43      albertel  390:   my $trystr='';
                    391:   my $button='';
                    392:   my $previousmsg='';
                    393: 
                    394:   my $status = $Apache::inputtags::status['-1'];
                    395:   &Apache::lonxml::debug("gradestatus has :$status:");
                    396:   if ( $status ne 'CLOSED' ) {  
                    397:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
                    398:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    399:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
                    400:     &Apache::lonxml::debug("Found Award |$award|$solved|");
                    401:     if ( $award ne '' ) {
                    402:       &Apache::lonxml::debug('Getting message');
                    403:       ($showbutton,$message,$previousmsg) =
                    404: 	&decideoutput($award,$solved,$previous);
                    405:       $message="<td bgcolor=\"#aaffaa\">$message</td>";
                    406:       if ($previousmsg) {
                    407: 	$previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
                    408:       }
                    409:     }
                    410:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    411:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    412:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                    413:     if ( $tries eq '' ) { $tries = '0'; }
                    414:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    415:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                    416:     if ( $showbutton ) {
                    417:       $trystr = "<td>Tries $tries/$maxtries</td>";
                    418:     }
                    419:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
                    420:     if ( $showbutton ) { 
1.52      albertel  421:       $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
1.43      albertel  422:     }
1.53      albertel  423:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    424:       #last submissions was after due date
                    425:       $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
                    426:     }
1.43      albertel  427:   }
1.53      albertel  428:   my $output= $previousmsg.$latemessage.$message.$trystr;
1.43      albertel  429:   if ($output =~ /^\s*$/) {
                    430:     return $button;
                    431:   } else {
1.53      albertel  432:     return $button.'<table><tr>'.$output.'</tr></table>';
1.43      albertel  433:   }
1.11      albertel  434: }
1.1       albertel  435: 1;
                    436: __END__
1.43      albertel  437:  

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