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

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

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