File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.143: download - view: text, annotated - select for diffs
Thu Jun 3 14:28:39 2004 UTC (19 years, 11 months ago) by www
Branches: MAIN
CVS tags: HEAD
HTMLarea - only have 5 minutes.

There are lots of odd problems with Mozilla, not just meta-refresh, but
also popup windows and one random segmentation fault.

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

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