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

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

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