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

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

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