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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.58    ! ng          4: # $Id: inputtags.pm,v 1.57 2002/06/24 21:23:26 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;
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 '' ) {
                    235:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
                    236:   }
                    237:   if ($result eq '' ) {
                    238:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
                    239:   }
                    240:   if ($result eq '' ) { $result='EXACT_ANS'; }
                    241:   return $result
1.9       albertel  242: }
                    243: 
1.10      albertel  244: sub decideoutput {
1.43      albertel  245:   my ($award,$solved,$previous)=@_;
                    246:   my $message='';
                    247:   my $button=0;
                    248:   my $previousmsg;
                    249: 
                    250:   if ($previous) { $previousmsg='You have entered that answer before'; }
                    251: 
                    252:   if      ($solved =~ /^correct/) {
                    253:     $message = "<b>You are correct.</b> Your receipt is ".
                    254:       &Apache::lonnet::receipt;
                    255:     $button=0;
                    256:     $previousmsg='';
                    257:   } elsif ($solved =~ /^excused/) {
                    258:     $message = "<b>You are excused from the problem.</b>";
                    259:     $button=0;
                    260:     $previousmsg='';
                    261:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                    262:     if ($solved =~ /^incorrect/ || $solved eq '') {
                    263:       $message = "Incorrect";
                    264:       $button=1;
1.37      albertel  265:     } else {
1.43      albertel  266:       $message = "<b>You are correct.</b> Your receipt is ".
                    267: 	&Apache::lonnet::receipt;
                    268:       $button=0;
                    269:       $previousmsg='';
                    270:     }
                    271:   } elsif ($award eq 'NO_RESPONSE') {
                    272:     $message = '';
                    273:     $button=1;
                    274:   } elsif ($award eq 'MISSING_ANSWER') {
                    275:     $message = 'Some parts were not submitted';
                    276:     $button = 1;
                    277:   } elsif ($award eq 'WANTED_NUMERIC') {
                    278:     $message = "This question expects a numeric answer";
                    279:     $button=1;
                    280:   } elsif ($award eq 'SIG_FAIL') {
                    281:     $message = "Please adjust significant figures.";# you provided %s significant figures";
                    282:     $button=1;
                    283:   } elsif ($award eq 'UNIT_FAIL') {
                    284:     $message = "Units incorrect."; #Computer reads units as %s";
                    285:     $button=1;
                    286:   } elsif ($award eq 'UNIT_NOTNEEDED') {
                    287:     $message = "Only a number required.";# Computer reads units of %s";
                    288:     $button=1;
                    289:   } elsif ($award eq 'NO_UNIT') {
                    290:     $message = "Units required";
                    291:     $button=1;
                    292:   } elsif ($award eq 'BAD_FORMULA') {
                    293:     $message = "Unable to understand formula";
                    294:     $button=1;
                    295:   } elsif ($award eq 'INCORRECT') {
                    296:     $message = "Incorrect";
                    297:     $button=1;
                    298:   } elsif ($award eq 'SUBMITTED') {
                    299:     $message = "Your submission has been recorded.";
                    300:     $button=1;
                    301:   } else {
                    302:     $message = "Unknown message: $award";
                    303:     $button=1;
                    304:   }
                    305:   return ($button,$message,$previousmsg);
1.12      albertel  306: }
                    307: 
                    308: sub setgradedata {
1.43      albertel  309:   my ($award,$id,$previously_used) = @_;
                    310:   # if the student already has it correct, don't modify the status
1.53      albertel  311:   if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER') {
                    312:     $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
                    313:     return '';
                    314:   } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.43      albertel  315:        /^correct/ ) {
                    316:     #handle assignment of tries and solved status
1.53      albertel  317:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    318:       $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                    319:     }
1.43      albertel  320:     if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                    321:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    322: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    323:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    324: 	'correct_by_student';
                    325:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                    326:     } elsif ( $award eq 'INCORRECT' ) {
                    327:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    328: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    329:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    330: 	'incorrect_attempted';
                    331:     } elsif ( $award eq 'SUBMITTED' ) {
                    332:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    333: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    334:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    335: 	'ungraded_attempted';
                    336:     } elsif ( $award eq 'NO_RESPONSE' ) {
                    337:       return '';
                    338:     } else {
                    339:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    340: 	'incorrect_attempted';
                    341:     }
                    342: 
                    343:     # check if this was a previous submission if it was delete the
                    344:     # unneeded data and update the previously_used attribute
                    345:     if ( $previously_used eq 'PREVIOUSLY_USED') {
                    346:       delete($Apache::lonhomework::results{"resource.$id.tries"});
                    347:       $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                    348:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1.58    ! ng        349:       #delete all data as they student didn't do anything, but save
        !           350:       #the list of collaborators.
1.43      albertel  351:       foreach my $key (keys(%Apache::lonhomework::results)) {
1.58    ! ng        352: 	if (($key =~ /^resource\.$id\./) && ($key !~ /\.collaborators$/)) {
1.43      albertel  353: 	  &Apache::lonxml::debug("Removing $key");
                    354: 	  delete($Apache::lonhomework::results{$key});
                    355: 	}
                    356:       }
                    357:       #and since they didn't do anything we were never here
                    358:       return '';
                    359:     } else {
                    360:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40      albertel  361:     }
1.43      albertel  362:   }
                    363:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  364: }
                    365: 
1.9       albertel  366: sub grade {
1.43      albertel  367:   my ($target) = @_;
                    368:   my $id = $Apache::inputtags::part;
                    369:   my $response='';
                    370:   if ( defined $ENV{'form.submitted'}) {
                    371:     my @awards = ();
                    372:     foreach $response (@Apache::inputtags::responselist) {
                    373:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    374:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1.54      albertel  375:       &Apache::lonxml::debug("keeping $value from $response for $id");
                    376:       push (@awards,$value);
1.43      albertel  377:     }
                    378:     my $finalaward = &finalizeawards(@awards);
                    379:     my $previously_used;
                    380:     if ( $#Apache::inputtags::previous eq $#awards ) {
                    381:       $previously_used = 'PREVIOUSLY_LAST';
                    382:       foreach my $value (@Apache::inputtags::previous) {
                    383: 	if ($value eq 'PREVIOUSLY_USED' ) {
                    384: 	  $previously_used = $value;
                    385: 	  last;
                    386: 	}
                    387:       }
                    388:     }
                    389:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
                    390:     &setgradedata($finalaward,$id,$previously_used);
                    391:   }
                    392:   return '';
1.1       albertel  393: }
                    394: 
1.11      albertel  395: sub gradestatus {
1.43      albertel  396:   my ($id) = @_;
                    397:   my $showbutton = 1;
                    398:   my $message = '';
1.53      albertel  399:   my $latemessage = '';
1.43      albertel  400:   my $trystr='';
                    401:   my $button='';
                    402:   my $previousmsg='';
                    403: 
                    404:   my $status = $Apache::inputtags::status['-1'];
                    405:   &Apache::lonxml::debug("gradestatus has :$status:");
                    406:   if ( $status ne 'CLOSED' ) {  
                    407:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
                    408:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    409:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
                    410:     &Apache::lonxml::debug("Found Award |$award|$solved|");
                    411:     if ( $award ne '' ) {
                    412:       &Apache::lonxml::debug('Getting message');
                    413:       ($showbutton,$message,$previousmsg) =
                    414: 	&decideoutput($award,$solved,$previous);
                    415:       $message="<td bgcolor=\"#aaffaa\">$message</td>";
                    416:       if ($previousmsg) {
                    417: 	$previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
                    418:       }
                    419:     }
                    420:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    421:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    422:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                    423:     if ( $tries eq '' ) { $tries = '0'; }
                    424:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    425:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                    426:     if ( $showbutton ) {
                    427:       $trystr = "<td>Tries $tries/$maxtries</td>";
                    428:     }
                    429:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
                    430:     if ( $showbutton ) { 
1.52      albertel  431:       $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
1.43      albertel  432:     }
1.53      albertel  433:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    434:       #last submissions was after due date
                    435:       $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
                    436:     }
1.43      albertel  437:   }
1.53      albertel  438:   my $output= $previousmsg.$latemessage.$message.$trystr;
1.43      albertel  439:   if ($output =~ /^\s*$/) {
                    440:     return $button;
                    441:   } else {
1.53      albertel  442:     return $button.'<table><tr>'.$output.'</tr></table>';
1.43      albertel  443:   }
1.11      albertel  444: }
1.1       albertel  445: 1;
                    446: __END__
1.43      albertel  447:  

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