File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.147: download - view: text, annotated - select for diffs
Mon Jun 28 15:01:44 2004 UTC (19 years, 11 months ago) by sakharuk
Branches: MAIN
CVS tags: version_1_1_99_2, version_1_1_99_1, HEAD
Bug 3070 (Suppress "tries 0/10"  in printing) is fixed.

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

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