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

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

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