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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.118   ! albertel    4: # $Id: inputtags.pm,v 1.117 2003/09/22 20:49:01 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.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;
1.118   ! albertel  521:       if (lc($Apache::lonhomework::problemstatus) ne 'no') {
        !           522: 	  $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
        !           523:       }
1.43      albertel  524:       $Apache::lonhomework::results{"resource.$id.solved"} =
1.59      ng        525: 	'incorrect_attempted'
1.43      albertel  526:     } elsif ( $award eq 'SUBMITTED' ) {
                    527:       $Apache::lonhomework::results{"resource.$id.tries"} =
                    528: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    529:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    530: 	'ungraded_attempted';
1.59      ng        531:     } elsif ( $award eq 'DRAFT' ) {
                    532:       $Apache::lonhomework::results{"resource.$id.solved"} = '';
1.43      albertel  533:     } elsif ( $award eq 'NO_RESPONSE' ) {
1.87      albertel  534: 	#no real response so delete any data that got stored
                    535: 	&removealldata($id);
                    536: 	return '';
1.43      albertel  537:     } else {
                    538:       $Apache::lonhomework::results{"resource.$id.solved"} =
                    539: 	'incorrect_attempted';
1.101     albertel  540:       if (lc($Apache::lonhomework::problemstatus) eq 'no') {
                    541: 	  $Apache::lonhomework::results{"resource.$id.tries"} =
                    542: 	      $Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.118   ! albertel  543: 	  $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
1.101     albertel  544:       }
1.43      albertel  545:     }
                    546: 
                    547:     # check if this was a previous submission if it was delete the
                    548:     # unneeded data and update the previously_used attribute
                    549:     if ( $previously_used eq 'PREVIOUSLY_USED') {
1.101     albertel  550: 	if (lc($Apache::lonhomework::problemstatus) ne 'no') {
                    551: 	    delete($Apache::lonhomework::results{"resource.$id.tries"});
                    552: 	    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                    553: 	}
1.43      albertel  554:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
1.58      ng        555:       #delete all data as they student didn't do anything, but save
                    556:       #the list of collaborators.
1.87      albertel  557:       &removealldata($id);
1.43      albertel  558:       #and since they didn't do anything we were never here
                    559:       return '';
                    560:     } else {
                    561:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
1.40      albertel  562:     }
1.85      albertel  563:   } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
                    564: 	    /^correct/ ) {
                    565:       #delete all data as they student already has it correct
1.87      albertel  566:       &removealldata($id);
1.85      albertel  567:       #and since they didn't do anything we were never here
                    568:       return '';
1.43      albertel  569:   }
                    570:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  571: }
                    572: 
1.9       albertel  573: sub grade {
1.43      albertel  574:   my ($target) = @_;
                    575:   my $id = $Apache::inputtags::part;
                    576:   my $response='';
                    577:   if ( defined $ENV{'form.submitted'}) {
                    578:     my @awards = ();
1.103     albertel  579:     foreach $response (@Apache::inputtags::response) {
1.43      albertel  580:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    581:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
1.54      albertel  582:       &Apache::lonxml::debug("keeping $value from $response for $id");
                    583:       push (@awards,$value);
1.43      albertel  584:     }
                    585:     my $finalaward = &finalizeawards(@awards);
                    586:     my $previously_used;
                    587:     if ( $#Apache::inputtags::previous eq $#awards ) {
1.75      albertel  588: 	my $match=0;
                    589: 	my @matches;
                    590: 	foreach my $versionar (@Apache::inputtags::previous_version) {
                    591: 	    foreach my $version (@$versionar) {
                    592: 		$matches[$version]++;
                    593: 	    }
                    594: 	}
                    595: 	foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
                    596: 	if ($match) {
                    597: 	    $previously_used = 'PREVIOUSLY_LAST';
                    598: 	    foreach my $value (@Apache::inputtags::previous) {
                    599: 		if ($value eq 'PREVIOUSLY_USED' ) {
                    600: 		    $previously_used = $value;
                    601: 		    last;
                    602: 		}
                    603: 	    }
1.43      albertel  604: 	}
                    605:     }
                    606:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
                    607:     &setgradedata($finalaward,$id,$previously_used);
                    608:   }
                    609:   return '';
1.1       albertel  610: }
                    611: 
1.11      albertel  612: sub gradestatus {
1.63      sakharuk  613:   my ($id,$target) = @_;
1.43      albertel  614:   my $showbutton = 1;
1.105     albertel  615:   my $bgcolor = '';
1.43      albertel  616:   my $message = '';
1.53      albertel  617:   my $latemessage = '';
1.43      albertel  618:   my $trystr='';
                    619:   my $button='';
                    620:   my $previousmsg='';
                    621: 
                    622:   my $status = $Apache::inputtags::status['-1'];
                    623:   &Apache::lonxml::debug("gradestatus has :$status:");
1.77      albertel  624:   if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE') {  
1.43      albertel  625:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
                    626:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    627:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
                    628:     &Apache::lonxml::debug("Found Award |$award|$solved|");
1.112     albertel  629:     if ( $award ne '' || $solved ne '') {
1.43      albertel  630:       &Apache::lonxml::debug('Getting message');
1.105     albertel  631:       ($showbutton,$bgcolor,$message,$previousmsg) =
1.68      sakharuk  632: 	&decideoutput($award,$solved,$previous,$target);
1.63      sakharuk  633:       if ($target eq 'tex') {
                    634: 	$message=' '.$message.' ';
                    635:       } else {
1.105     albertel  636: 	$message="<td bgcolor=\"$bgcolor\">$message</td>";
1.63      sakharuk  637: 	if ($previousmsg) {
1.105     albertel  638: 	  $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
1.63      sakharuk  639: 	}
1.43      albertel  640:       }
                    641:     }
                    642:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    643:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    644:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                    645:     if ( $tries eq '' ) { $tries = '0'; }
                    646:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    647:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                    648:     if ( $showbutton ) {
1.63      sakharuk  649:       if ($target eq 'tex') {
1.97      sakharuk  650: 	  if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam') {
1.116     www       651: 	      $trystr = ' {\vskip 1 mm \small \textit{'.&mt('Tries').'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
1.83      sakharuk  652: 	  } else {
                    653: 	      $trystr = '\vskip 0 mm ';
1.67      sakharuk  654: 	  }
1.63      sakharuk  655:       } else {
1.116     www       656:          $trystr = "<td>".&mt('Tries')." $tries/$maxtries</td>";
1.63      sakharuk  657:       }
1.43      albertel  658:     }
                    659:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
                    660:     if ( $showbutton ) { 
1.63      sakharuk  661:       if ($target ne 'tex') {
1.116     www       662:         $button = '<br /><input type="submit" name="submit" value="'.&mt('Submit Answer').'" />';
1.63      sakharuk  663:       }
1.43      albertel  664:     }
1.53      albertel  665:     if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    666:       #last submissions was after due date
1.63      sakharuk  667:       if ($target eq 'tex') {
                    668: 	  $latemessage=' The last submission was after the Due Date ';
                    669:       } else {
                    670:         $latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
                    671:       }
1.53      albertel  672:     }
1.43      albertel  673:   }
1.53      albertel  674:   my $output= $previousmsg.$latemessage.$message.$trystr;
1.43      albertel  675:   if ($output =~ /^\s*$/) {
                    676:     return $button;
                    677:   } else {
1.63      sakharuk  678:     if ($target eq 'tex') {
                    679:       return $button.' \vskip 0 mm '.$output.' ';
                    680:     } else {
                    681:       return $button.'<table><tr>'.$output.'</tr></table>';
                    682:     }
1.43      albertel  683:   }
1.11      albertel  684: }
1.1       albertel  685: 1;
                    686: __END__
1.43      albertel  687:  

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