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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.104   ! albertel    4: # $Id: inputtags.pm,v 1.103 2003/05/23 16:26:28 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;
1.82      www        33: use Apache::loncommon;
1.1       albertel   34: 
1.50      harris41   35: BEGIN {
1.98      albertel   36:   &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1       albertel   37: }
                     38: 
1.43      albertel   39: 
1.1       albertel   40: sub initialize_inputtags {
1.43      albertel   41:   # list of current input ids
                     42:   @Apache::inputtags::input=();
                     43:   # list of all input ids seen in this problem
                     44:   @Apache::inputtags::inputlist=();
                     45:   # list of all current response ids
                     46:   @Apache::inputtags::response=();
                     47:   # list of all response ids seen in this problem
                     48:   @Apache::inputtags::responselist=();
                     49:   # list of whether or not a specific response was previously used
                     50:   @Apache::inputtags::previous=();
1.75      albertel   51:   # submission it was used in
                     52:   @Apache::inputtags::previous_version=();
1.43      albertel   53:   # id of current part, 0 means that no part is current (inside <problem> only
                     54:   $Apache::inputtags::part='';
1.103     albertel   55:   # list of all part ids seen
                     56:   @Apache::inputtags::partlist=();
1.46      albertel   57:   # list of problem date statuses, the first element is for <problem>
                     58:   # if there is a second element it is for the current <part>
1.43      albertel   59:   @Apache::inputtags::status=();
1.46      albertel   60:   # hash of defined params for the current response
1.43      albertel   61:   %Apache::inputtags::params=();
1.46      albertel   62:   # list of all ids, for <import>, these get join()ed and prepended
                     63:   @Apache::inputtags::import=();
1.103     albertel   64:   # list of all import ids seen
                     65:   @Apache::inputtags::importlist=();
                     66: }
                     67: 
                     68: sub check_for_duplicate_ids {
                     69:     my %check;
                     70:     foreach my $id (@Apache::inputtags::partlist,
                     71: 		    @Apache::inputtags::responselist,
                     72: 		    @Apache::inputtags::importlist) {
                     73: 	$check{$id}++;
                     74:     }
                     75:     my @duplicates;
                     76:     foreach my $id (sort(keys(%check))) {
                     77: 	if ($check{$id} > 1) {
                     78: 	    push(@duplicates,$id);
                     79: 	}
                     80:     }
                     81:     if (@duplicates) {
                     82: 	&Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
                     83:     }
1.1       albertel   84: }
                     85: 
1.14      albertel   86: sub start_input {
1.43      albertel   87:   my ($parstack,$safeeval)=@_;
                     88:   my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
                     89:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
                     90:   push (@Apache::inputtags::input,$id);
                     91:   push (@Apache::inputtags::inputlist,$id);
                     92:   return $id;
1.14      albertel   93: }
                     94: 
                     95: sub end_input {
1.43      albertel   96:   pop @Apache::inputtags::input;
                     97:   return '';
1.14      albertel   98: }
                     99: 
1.48      albertel  100: sub start_textfield {
1.43      albertel  101:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    102:   my $result = "";
                    103:   my $id = &start_input($parstack,$safeeval);
1.55      albertel  104:   my $resid=$Apache::inputtags::response[-1];
1.43      albertel  105:   if ($target eq 'web') {
1.57      albertel  106:     $Apache::lonxml::evaluate--;
1.100     albertel  107:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.61      albertel  108: 	my $partid=$Apache::inputtags::part;
                    109: 	my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"});
                    110: 	my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    111: 	if ( $cols eq '') { $cols = 80; }
                    112: 	my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
                    113: 	if ( $rows eq '') { $rows = 10; }
1.89      albertel  114: 	$result= '<textarea name="HWVAL_'.$resid.'" '.
1.61      albertel  115: 	    "rows=\"$rows\" cols=\"$cols\">".$oldresponse;
                    116: 	if ($oldresponse ne '') {
                    117: 	    #get rid of any startup text if the user has already responded
1.84      albertel  118: 	    &Apache::lonxml::get_all_text("/textfield",$parser);
1.61      albertel  119: 	}
1.45      albertel  120:     } else {
1.61      albertel  121: 	#right or wrong don't show it
                    122: 	#$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
                    123: 	$result='';
                    124: 	#get rid of any startup text
1.84      albertel  125: 	&Apache::lonxml::get_all_text("/textfield",$parser);
1.51      albertel  126:     }
1.60      albertel  127:   } elsif ($target eq 'grade') {
1.84      albertel  128:     my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser);
1.89      albertel  129:     if ($seedtext eq $ENV{'form.HWVAL_'.$resid}) {
1.51      albertel  130:       # if the seed text is still there it wasn't a real submission
1.89      albertel  131:       $ENV{'form.HWVAL_'.$resid}='';
1.30      albertel  132:     }
1.60      albertel  133:   } elsif ($target eq 'edit') {
                    134:     $result.=&Apache::edit::tag_start($target,$token);
                    135:     $result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
                    136:     $result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
1.84      albertel  137:     my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser);
1.62      albertel  138:     $result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
1.60      albertel  139:   } elsif ($target eq 'modified') {
                    140:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    141: 						 $safeeval,'rows','cols');
                    142:     if ($constructtag) {
                    143:       $result = &Apache::edit::rebuild_tag($token);
                    144:     } else {
                    145:       $result=$token->[4];
                    146:     }
                    147:     $result.=&Apache::edit::modifiedfield();
1.78      sakharuk  148:   } elsif ($target eq 'tex') {
1.94      sakharuk  149:       my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
                    150:       my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
1.78      sakharuk  151:       if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
                    152: 	  $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
1.94      sakharuk  153: 	  for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    154: 	  $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
                    155:       } else {
                    156: 	  my $TeXwidth=$width_of_box/80;
                    157: 	  $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
                    158: 	  for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    159: 	  $result.='}}}\vskip 2 mm ';
1.78      sakharuk  160:       }
1.43      albertel  161:   }
                    162:   return $result;
1.6       albertel  163: }
                    164: 
1.48      albertel  165: sub end_textfield {
1.43      albertel  166:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.60      albertel  167:   my $result;
1.43      albertel  168:   if ($target eq 'web') {
1.57      albertel  169:     $Apache::lonxml::evaluate++;
1.45      albertel  170:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    171:       return "</textarea>";
                    172:     }
1.60      albertel  173:   } elsif ($target eq 'edit') {
                    174:     $result=&Apache::edit::end_table();
1.45      albertel  175:   }
1.43      albertel  176:   &end_input;
1.60      albertel  177:   return $result;
1.6       albertel  178: }
                    179: 
1.1       albertel  180: sub start_textline {
1.43      albertel  181:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    182:   my $result = "";
                    183:   if ($target eq 'web') {
1.57      albertel  184:     $Apache::lonxml::evaluate--;
1.99      albertel  185:     my $partid=$Apache::inputtags::part;
                    186:     my $id=$Apache::inputtags::response[-1];
1.100     albertel  187:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.61      albertel  188:       my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
1.72      albertel  189:       my $maxlength;
                    190:       if ($size eq '') { $size=20; } else {
                    191: 	if ($size < 20) { $maxlength=$size; }
                    192:       }
1.61      albertel  193:       my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"});
1.73      sakharuk  194:       if ($Apache::lonhomework::type ne 'exam') {
1.89      albertel  195:         $result= '<input type="text" name="HWVAL_'.$id.'" value="'.
1.73      sakharuk  196: 	    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
                    197:       }
1.45      albertel  198:     } else {
1.61      albertel  199:       #right or wrong don't show what was last typed in.
                    200:       #$result='<i>'.$oldresponse.'</i>';
                    201:       $result='';
1.45      albertel  202:     }
1.44      albertel  203:   } elsif ($target eq 'edit') {
1.49      matthew   204:     $result=&Apache::edit::tag_start($target,$token);
1.43      albertel  205:     $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
                    206:     $result.=&Apache::edit::end_table;
1.44      albertel  207:   } elsif ($target eq 'modified') {
1.43      albertel  208:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
                    209:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.78      sakharuk  210:   } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
1.76      sakharuk  211:       my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    212:       if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
1.96      sakharuk  213:       $result='\framebox['.$size.'][s]{\tiny\strut}';
1.43      albertel  214:   }
                    215:   return $result;
1.1       albertel  216: }
                    217: 
                    218: sub end_textline {
1.43      albertel  219:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
1.57      albertel  220:   if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    221:   elsif ($target eq 'edit') { return ('','no'); }
1.43      albertel  222:   return "";
1.9       albertel  223: }
                    224: 
1.98      albertel  225: sub start_hiddenline {
                    226:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    227:     my $result = "";
                    228:     if ($target eq 'web') {
                    229: 	$Apache::lonxml::evaluate--;
                    230: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    231: 	    my $partid=$Apache::inputtags::part;
                    232: 	    my $id=$Apache::inputtags::response[-1];
                    233: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"});
                    234: 	    if ($Apache::lonhomework::type ne 'exam') {
                    235: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
                    236: 		    $oldresponse.'" />';
                    237: 	    }
                    238: 	}
                    239:     } elsif ($target eq 'edit') {
                    240: 	$result=&Apache::edit::tag_start($target,$token);
                    241: 	$result.=&Apache::edit::end_table;
                    242:     }
                    243:     return $result;
                    244: }
                    245: 
                    246: sub end_hiddenline {
                    247:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    248:   if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    249:   elsif ($target eq 'edit') { return ('','no'); }
                    250:   return "";
                    251: }
                    252: 
1.9       albertel  253: sub finalizeawards {
1.43      albertel  254:   my $result='';
                    255:   my $award;
                    256:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
                    257:   if ($result eq '' ) {
1.54      albertel  258:     my $blankcount;
                    259:     foreach $award (@_) {
                    260:       if ($award eq '') {
                    261: 	$result='MISSING_ANSWER';
                    262: 	$blankcount++;
                    263:       }
                    264:     }
                    265:     if ($blankcount == ($#_ + 1)) { $result = 'NO_RESPONSE'; }
1.56      albertel  266:   }
                    267:   if ($result eq '' ) {
                    268:     foreach $award (@_) { if ($award eq 'MISSING_ANSWER') {$result='MISSING_ANSWER'; last;}}
1.43      albertel  269:   }
                    270:   if ($result eq '' ) {
                    271:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
                    272:   }
                    273:   if ($result eq '' ) {
                    274:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
                    275:   }
1.102     albertel  276:   if ($result eq '' ) {
                    277:     foreach $award (@_) { if ($award eq 'TOO_LONG') {$result='TOO_LONG'; last;}}
                    278:   }
1.43      albertel  279:   if ($result eq '' ) {
                    280:     foreach $award (@_) { 
                    281:       if ($award eq 'UNIT_FAIL' ||
                    282: 	  $award eq 'NO_UNIT' ||
                    283: 	  $award eq 'UNIT_NOTNEEDED') {
                    284: 	$result=$award; last;
                    285:       }
                    286:     }
                    287:   }
                    288:   if ($result eq '' ) {
                    289:     foreach $award (@_) { 
                    290:       if ($award eq 'WANTED_NUMERIC' || 
                    291: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
                    292:     }
                    293:   }
                    294:   if ($result eq '' ) {
                    295:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
                    296:   }
                    297:   if ($result eq '' ) {
                    298:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
                    299:   }
                    300:   if ($result eq '' ) {
1.79      albertel  301:       foreach $award (@_) { if ($award eq 'MISORDERED_RANK') {$result=$award; last;} }
                    302:   }
                    303:   if ($result eq '' ) {
1.80      www       304:       foreach $award (@_) { if ($award eq 'INVALID_FILETYPE') {$result=$award; last;} }
                    305:   }
                    306:   if ($result eq '' ) {
1.59      ng        307:     foreach $award (@_) { if ($award eq 'DRAFT') {$result=$award; last;} }
                    308:   }
                    309:   if ($result eq '' ) {
1.43      albertel  310:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
                    311:   }
                    312:   if ($result eq '' ) {
1.92      albertel  313:     foreach $award (@_) { if ($award eq 'ASSIGNED_SCORE') {$result=$award; last;} }
                    314:   }
                    315:   if ($result eq '' ) {
1.43      albertel  316:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
                    317:   }
                    318:   if ($result eq '' ) { $result='EXACT_ANS'; }
                    319:   return $result
1.9       albertel  320: }
                    321: 
1.10      albertel  322: sub decideoutput {
1.68      sakharuk  323:   my ($award,$solved,$previous,$target)=@_;
1.43      albertel  324:   my $message='';
                    325:   my $button=0;
                    326:   my $previousmsg;
                    327: 
                    328:   if ($previous) { $previousmsg='You have entered that answer before'; }
                    329: 
                    330:   if      ($solved =~ /^correct/) {
1.92      albertel  331:       if ($award eq 'ASSIGNED_SCORE') {
                    332: 	  $message = "A score has been assigned.";
1.91      sakharuk  333:       } else {
1.92      albertel  334: 	  if ($target eq 'tex') {
                    335: 	      $message = '\textbf{You are correct}.';
                    336: 	  } else {
                    337: 	      $message = "<b>You are correct.</b>";
                    338: 	  }
1.104   ! albertel  339: 	  $message.=' Your receipt is '.&Apache::lonnet::receipt;
1.91      sakharuk  340:       }
                    341:       $button=0;
                    342:       $previousmsg='';
1.43      albertel  343:   } elsif ($solved =~ /^excused/) {
1.91      sakharuk  344:       $message = "<b>You are excused from the problem.</b>";
1.43      albertel  345:       $button=0;
                    346:       $previousmsg='';
1.91      sakharuk  347:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                    348:       if ($solved =~ /^incorrect/ || $solved eq '') {
                    349: 	  $message = "Incorrect";
                    350: 	  $button=1;
                    351:       } else {
                    352: 	  $message = "<b>You are correct.</b> Your receipt is ".
                    353: 	   &Apache::lonnet::receipt;
                    354: 	  $button=0;
                    355: 	  $previousmsg='';
                    356:       }
1.43      albertel  357:   } elsif ($award eq 'NO_RESPONSE') {
1.91      sakharuk  358:       $message = '';
                    359:       $button=1;
1.43      albertel  360:   } elsif ($award eq 'MISSING_ANSWER') {
1.91      sakharuk  361:       $message = 'Some parts were not submitted';
                    362:       $button = 1;
1.102     albertel  363:   } elsif ($award eq 'ERROR') {
                    364:       $message = 'An error occured while trying to grade answer.';
                    365:       $button = 1;
                    366:   } elsif ($award eq 'TOO_LONG') {
                    367:       $message = "The submitted answer was too long.";
                    368:       $button=1;
1.43      albertel  369:   } elsif ($award eq 'WANTED_NUMERIC') {
1.91      sakharuk  370:       $message = "This question expects a numeric answer";
                    371:       $button=1;
1.79      albertel  372:   } elsif ($award eq 'MISORDERED_RANK') {
1.91      sakharuk  373:       $message = 'You have provided an invalid ranking, please refer to '.
                    374:        &Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems').'.';
                    375:       $button=1;
1.80      www       376:   } elsif ($award eq 'INVALID_FILETYPE') {
1.91      sakharuk  377:       $message = 'The filetype extension of the file you uploaded is not allowed.';
                    378:       $button=1;
1.43      albertel  379:   } elsif ($award eq 'SIG_FAIL') {
1.91      sakharuk  380:       $message = "Please adjust significant figures.";# you provided %s significant figures";
                    381:       $button=1;
1.43      albertel  382:   } elsif ($award eq 'UNIT_FAIL') {
1.91      sakharuk  383:       $message = "Units incorrect. ".
1.82      www       384:        &Apache::loncommon::help_open_topic('Physical_Units'); #Computer reads units as %s";
1.91      sakharuk  385:       $button=1;
1.43      albertel  386:   } elsif ($award eq 'UNIT_NOTNEEDED') {
1.91      sakharuk  387:       $message = "Only a number required.";# Computer reads units of %s";
                    388:       $button=1;
1.43      albertel  389:   } elsif ($award eq 'NO_UNIT') {
1.91      sakharuk  390:       $message = "Units required".
1.82      www       391:        &Apache::loncommon::help_open_topic('Physical_Units');
1.91      sakharuk  392:       $button=1;
1.43      albertel  393:   } elsif ($award eq 'BAD_FORMULA') {
1.91      sakharuk  394:       $message = "Unable to understand formula";
                    395:       $button=1;
1.43      albertel  396:   } elsif ($award eq 'INCORRECT') {
1.91      sakharuk  397:       $message = "Incorrect";
                    398:       $button=1;
1.43      albertel  399:   } elsif ($award eq 'SUBMITTED') {
1.91      sakharuk  400:       $message = "Your submission has been recorded.";
                    401:       $button=1;
1.59      ng        402:   } elsif ($award eq 'DRAFT') {
1.91      sakharuk  403:       $message = "A draft copy has been saved.";
                    404:       $button=1;
1.92      albertel  405:   } elsif ($award eq 'ASSIGNED_SCORE') {
                    406:       $message = "A score has been assigned.";
                    407:       $button=0;
1.43      albertel  408:   } else {
1.91      sakharuk  409:       $message = "Unknown message: $award";
                    410:       $button=1;
1.98      albertel  411:   }
                    412:   if (lc($Apache::lonhomework::problemstatus) eq 'no') {
1.91      sakharuk  413:       $message = "Answer Submitted";
                    414:       $button=1;
1.43      albertel  415:   }
                    416:   return ($button,$message,$previousmsg);
1.12      albertel  417: }
                    418: 
1.88      albertel  419: sub removealldata {
1.87      albertel  420:     my ($id)=@_;
                    421:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    422: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                    423: 	    &Apache::lonxml::debug("Removing $key");
                    424: 	    delete($Apache::lonhomework::results{$key});
                    425: 	}
                    426:     }
                    427: }
                    428: 
1.12      albertel  429: sub setgradedata {
1.43      albertel  430:   my ($award,$id,$previously_used) = @_;
                    431:   # if the student already has it correct, don't modify the status
1.71      albertel  432:   if ($Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                    433:       $Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
1.53      albertel  434:     $Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
                    435:     return '';
                    436:   } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.101     albertel  437:        /^correct/ || $Apache::lonhomework::scantronmode ||
                    438: 	    lc($Apache::lonhomework::problemstatus) eq 'no') {
1.43      albertel  439:     #handle assignment of tries and solved status
1.93      albertel  440:     my $solvemsg;
                    441:     if ($Apache::lonhomework::scantronmode) {
                    442: 	$solvemsg='correct_by_scantron';
                    443:     } else {
                    444: 	$solvemsg='correct_by_student';
                    445:     }
1.53      albertel  446:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    447:       $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                    448:     }
1.92      albertel  449:     if ( $award eq 'ASSIGNED_SCORE') {
                    450: 	$Apache::lonhomework::results{"resource.$id.tries"} =
                    451: 	    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    452: 	$Apache::lonhomework::results{"resource.$id.solved"} =
1.93      albertel  453: 	    $solvemsg;
1.103     albertel  454: 	my $numawards=scalar(@Apache::inputtags::response);
1.92      albertel  455: 	$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1.103     albertel  456: 	foreach my $res (@Apache::inputtags::response) {
1.92      albertel  457: 	    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                    458: 	       $Apache::lonhomework::results{"resource.$id.$res.awarded"};
                    459: 	}
1.93      albertel  460: 	if ($numawards > 0) {
                    461: 	    $Apache::lonhomework::results{"resource.$id.awarded"}/=
                    462: 		$numawards;
                    463: 	}
1.92      albertel  464:     } elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
1.43      albertel  465:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    466: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    467:       $Apache::lonhomework::results{"resource.$id.solved"} =
1.93      albertel  468: 	$solvemsg;
1.43      albertel  469:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                    470:     } elsif ( $award eq 'INCORRECT' ) {
                    471:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    472: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    473:       $Apache::lonhomework::results{"resource.$id.solved"} =
1.59      ng        474: 	'incorrect_attempted'
1.43      albertel  475:     } elsif ( $award eq 'SUBMITTED' ) {
                    476:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    477: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    478:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    479: 	'ungraded_attempted';
1.59      ng        480:     } elsif ( $award eq 'DRAFT' ) {
                    481:       $Apache::lonhomework::results{"resource.$id.solved"} = '';
1.43      albertel  482:     } elsif ( $award eq 'NO_RESPONSE' ) {
1.87      albertel  483: 	#no real response so delete any data that got stored
                    484: 	&removealldata($id);
                    485: 	return '';
1.43      albertel  486:     } else {
                    487:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    488: 	'incorrect_attempted';
1.101     albertel  489:       if (lc($Apache::lonhomework::problemstatus) eq 'no') {
                    490: 	  $Apache::lonhomework::results{"resource.$id.tries"} =
                    491: 	      $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    492:       }
1.43      albertel  493:     }
                    494: 
                    495:     # check if this was a previous submission if it was delete the
                    496:     # unneeded data and update the previously_used attribute
                    497:     if ( $previously_used eq 'PREVIOUSLY_USED') {
1.101     albertel  498: 	if (lc($Apache::lonhomework::problemstatus) ne 'no') {
                    499: 	    delete($Apache::lonhomework::results{"resource.$id.tries"});
                    500: 	    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                    501: 	}
1.43      albertel  502:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1.58      ng        503:       #delete all data as they student didn't do anything, but save
                    504:       #the list of collaborators.
1.87      albertel  505:       &removealldata($id);
1.43      albertel  506:       #and since they didn't do anything we were never here
                    507:       return '';
                    508:     } else {
                    509:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40      albertel  510:     }
1.85      albertel  511:   } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
                    512: 	    /^correct/ ) {
                    513:       #delete all data as they student already has it correct
1.87      albertel  514:       &removealldata($id);
1.85      albertel  515:       #and since they didn't do anything we were never here
                    516:       return '';
1.43      albertel  517:   }
                    518:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  519: }
                    520: 
1.9       albertel  521: sub grade {
1.43      albertel  522:   my ($target) = @_;
                    523:   my $id = $Apache::inputtags::part;
                    524:   my $response='';
                    525:   if ( defined $ENV{'form.submitted'}) {
                    526:     my @awards = ();
1.103     albertel  527:     foreach $response (@Apache::inputtags::response) {
1.43      albertel  528:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    529:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1.54      albertel  530:       &Apache::lonxml::debug("keeping $value from $response for $id");
                    531:       push (@awards,$value);
1.43      albertel  532:     }
                    533:     my $finalaward = &finalizeawards(@awards);
                    534:     my $previously_used;
                    535:     if ( $#Apache::inputtags::previous eq $#awards ) {
1.75      albertel  536: 	my $match=0;
                    537: 	my @matches;
                    538: 	foreach my $versionar (@Apache::inputtags::previous_version) {
                    539: 	    foreach my $version (@$versionar) {
                    540: 		$matches[$version]++;
                    541: 	    }
                    542: 	}
                    543: 	foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
                    544: 	if ($match) {
                    545: 	    $previously_used = 'PREVIOUSLY_LAST';
                    546: 	    foreach my $value (@Apache::inputtags::previous) {
                    547: 		if ($value eq 'PREVIOUSLY_USED' ) {
                    548: 		    $previously_used = $value;
                    549: 		    last;
                    550: 		}
                    551: 	    }
1.43      albertel  552: 	}
                    553:     }
                    554:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
                    555:     &setgradedata($finalaward,$id,$previously_used);
                    556:   }
                    557:   return '';
1.1       albertel  558: }
                    559: 
1.11      albertel  560: sub gradestatus {
1.63      sakharuk  561:   my ($id,$target) = @_;
1.43      albertel  562:   my $showbutton = 1;
                    563:   my $message = '';
1.53      albertel  564:   my $latemessage = '';
1.43      albertel  565:   my $trystr='';
                    566:   my $button='';
                    567:   my $previousmsg='';
                    568: 
                    569:   my $status = $Apache::inputtags::status['-1'];
                    570:   &Apache::lonxml::debug("gradestatus has :$status:");
1.77      albertel  571:   if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE') {  
1.43      albertel  572:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
                    573:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    574:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
                    575:     &Apache::lonxml::debug("Found Award |$award|$solved|");
                    576:     if ( $award ne '' ) {
                    577:       &Apache::lonxml::debug('Getting message');
                    578:       ($showbutton,$message,$previousmsg) =
1.68      sakharuk  579: 	&decideoutput($award,$solved,$previous,$target);
1.63      sakharuk  580:       if ($target eq 'tex') {
                    581: 	$message=' '.$message.' ';
                    582:       } else {
                    583: 	$message="<td bgcolor=\"#aaffaa\">$message</td>";
                    584: 	if ($previousmsg) {
                    585: 	  $previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
                    586: 	}
1.43      albertel  587:       }
                    588:     }
                    589:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    590:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    591:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                    592:     if ( $tries eq '' ) { $tries = '0'; }
                    593:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    594:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                    595:     if ( $showbutton ) {
1.63      sakharuk  596:       if ($target eq 'tex') {
1.97      sakharuk  597: 	  if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam') {
                    598: 	      $trystr = ' {\vskip 1 mm \small \textit{Tries} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
1.83      sakharuk  599: 	  } else {
                    600: 	      $trystr = '\vskip 0 mm ';
1.67      sakharuk  601: 	  }
1.63      sakharuk  602:       } else {
                    603:          $trystr = "<td>Tries $tries/$maxtries</td>";
                    604:       }
1.43      albertel  605:     }
                    606:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
                    607:     if ( $showbutton ) { 
1.63      sakharuk  608:       if ($target ne 'tex') {
                    609:         $button = '<br /><input type="submit" name="submit" value="Submit Answer" />';
                    610:       }
1.43      albertel  611:     }
1.53      albertel  612:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    613:       #last submissions was after due date
1.63      sakharuk  614:       if ($target eq 'tex') {
                    615: 	  $latemessage=' The last submission was after the Due Date ';
                    616:       } else {
                    617:         $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
                    618:       }
1.53      albertel  619:     }
1.43      albertel  620:   }
1.53      albertel  621:   my $output= $previousmsg.$latemessage.$message.$trystr;
1.43      albertel  622:   if ($output =~ /^\s*$/) {
                    623:     return $button;
                    624:   } else {
1.63      sakharuk  625:     if ($target eq 'tex') {
                    626:       return $button.' \vskip 0 mm '.$output.' ';
                    627:     } else {
                    628:       return $button.'<table><tr>'.$output.'</tr></table>';
                    629:     }
1.43      albertel  630:   }
1.11      albertel  631: }
1.1       albertel  632: 1;
                    633: __END__
1.43      albertel  634:  

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