File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.157: download - view: text, annotated - select for diffs
Fri Feb 18 05:41:31 2005 UTC (19 years, 3 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- <textline readonly="yes" />

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.157 2005/02/18 05:41:31 albertel 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: 		my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
  226: 							$safeeval);
  227: 		if (lc($readonly) eq 'yes') {
  228: 		    $readonly=' readonly="readonly" ';
  229: 		}
  230: 		$result.= '<input type="text" '.$readonly.' name="HWVAL_'.$id.'" value="'.
  231: 		    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
  232: 	    }
  233: 	} else {
  234: 	    #right or wrong don't show what was last typed in.
  235: 	    $result='<i>'.$Apache::inputtags::answertxt{$id}.'</i>';
  236: 	    #$result='';
  237: 	}
  238:     } elsif ($target eq 'edit') {
  239: 	$result=&Apache::edit::tag_start($target,$token);
  240: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
  241: 	    &Apache::edit::text_arg('Click-On Texts (comma sep):',
  242: 				    'addchars',$token,10);
  243:         $result.=&Apache::edit::select_arg('Readonly:','readonly',
  244: 					   ['no','yes'],$token);
  245: 	$result.=&Apache::edit::end_row();
  246: 	$result.=&Apache::edit::end_table();
  247:     } elsif ($target eq 'modified') {
  248: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
  249: 						     $safeeval,'size',
  250: 						     'addchars','readonly');
  251: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  252:     } elsif ($target eq 'tex' and $Apache::lonhomework::type ne 'exam') {
  253: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  254: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
  255: 	$result='\framebox['.$size.'][s]{\tiny\strut}';
  256:     }
  257:     return $result;
  258: }
  259: 
  260: sub end_textline {
  261:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  262:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  263:     elsif ($target eq 'edit') { return ('','no'); }
  264:     return "";
  265: }
  266: 
  267: sub start_hiddenline {
  268:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  269:     my $result = "";
  270:     if ($target eq 'web') {
  271: 	$Apache::lonxml::evaluate--;
  272: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  273: 	    my $partid=$Apache::inputtags::part;
  274: 	    my $id=$Apache::inputtags::response[-1];
  275: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
  276: 	    if ($Apache::lonhomework::type ne 'exam') {
  277: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
  278: 		    $oldresponse.'" />';
  279: 	    }
  280: 	}
  281:     } elsif ($target eq 'edit') {
  282: 	$result=&Apache::edit::tag_start($target,$token);
  283: 	$result.=&Apache::edit::end_table;
  284:     }
  285:     return $result;
  286: }
  287: 
  288: sub end_hiddenline {
  289:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  290:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
  291:     elsif ($target eq 'edit') { return ('','no'); }
  292:     return "";
  293: }
  294: 
  295: sub checkstatus {
  296:     my ($value,$awardref,$msgref)=@_;
  297:     for (my $i=0;$i<=$#$awardref;$i++) {
  298: 	if ($$awardref[$i] eq $value) {
  299: 	    return ($$awardref[$i],$$msgref[$i]);
  300: 	}
  301:     }
  302:     return(undef,undef);
  303: }
  304: 
  305: sub finalizeawards {
  306:     my ($awardref,$msgref)=@_;
  307:     my $result=undef;
  308:     my $award;
  309:     my $msg;
  310:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
  311:     if ($result eq '' ) {
  312: 	my $blankcount;
  313: 	foreach $award (@$awardref) {
  314: 	    if ($award eq '') {
  315: 		$result='MISSING_ANSWER';
  316: 		$blankcount++;
  317: 	    }
  318: 	}
  319: 	if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
  320:     }
  321:     if (defined($result)) { return ($result,$msg); }
  322:     foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
  323: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
  324: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
  325: 			       'UNIT_FAIL', 'NO_UNIT',
  326: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
  327: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
  328: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
  329: 			       'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
  330: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
  331: 	($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
  332: 	if (defined($result)) { return ($result,$msg); }
  333:     }
  334:     return ('ERROR',undef);
  335: }
  336: 
  337: sub decideoutput {
  338:     my ($award,$awardmsg,$solved,$previous,$target)=@_;
  339:     my $message='';
  340:     my $button=0;
  341:     my $previousmsg;
  342:     my $bgcolor='orange';
  343:     my $added_computer_text=0;
  344:     my %possiblecolors =
  345: 	( 'correct' => '#aaffaa',
  346: 	  'charged_try' => '#ffaaaa',
  347: 	  'not_charged_try' => '#ffffaa',
  348: 	  'no_message' => '#fffff',
  349: 	  );
  350:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
  351:     
  352:     if      ($solved =~ /^correct/) {
  353: 	if ($award eq 'ASSIGNED_SCORE') {
  354: 	    $message = &mt("A score has been assigned.");
  355: 	} else {
  356: 	    if ($target eq 'tex') {
  357: 		$message = '\textbf{'.&mt('You are correct.').'}';
  358: 	    } else {
  359: 		$message = "<b>".&mt('You are correct.')."</b>";
  360: 		$message.=" ".&mt("Computer's answer now shown above.");
  361: 	    }
  362: 	    $added_computer_text=1;
  363: 	    unless ($ENV{'course.'.
  364: 			     $ENV{'request.course.id'}.
  365: 			     '.disable_receipt_display'} eq 'yes') { 
  366: 		$message.=(($target eq 'web')?'<br />':' ').
  367: 		    &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
  368: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  369: 	    }
  370: 	}
  371: 	$bgcolor=$possiblecolors{'correct'};
  372: 	$button=0;
  373: 	$previousmsg='';
  374:     } elsif ($solved =~ /^excused/) {
  375: 	if ($target eq 'tex') {
  376: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
  377: 	} else {
  378: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
  379: 	}
  380: 	$bgcolor=$possiblecolors{'charged_try'};
  381: 	$button=0;
  382: 	$previousmsg='';
  383:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  384: 	if ($solved =~ /^incorrect/ || $solved eq '') {
  385: 	    $message = &mt("Incorrect").".";
  386: 	    $bgcolor=$possiblecolors{'charged_try'};
  387: 	    $button=1;
  388: 	} else {
  389: 	    if ($target eq 'tex') {
  390: 		$message = '\textbf{'.&mt('You are correct.').'}';
  391: 	    } else {
  392: 		$message = "<b>".&mt('You are correct.')."</b>";
  393: 		$message.=" ".&mt("Computer's answer now shown above.");
  394: 	    }
  395: 	    $added_computer_text=1;
  396: 	    unless ($ENV{'course.'.
  397: 			     $ENV{'request.course.id'}.
  398: 			     '.disable_receipt_display'} eq 'yes') { 
  399: 		$message.=(($target eq 'web')?'<br />':' ').
  400: 		    'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
  401: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  402: 	    }
  403: 	    $bgcolor=$possiblecolors{'correct'};
  404: 	    $button=0;
  405: 	    $previousmsg='';
  406: 	}
  407:     } elsif ($award eq 'NO_RESPONSE') {
  408: 	$message = '';
  409: 	$bgcolor=$possiblecolors{'no_feedback'};
  410: 	$button=1;
  411:     } elsif ($award eq 'MISSING_ANSWER') {
  412: 	$message = &mt('Some items were not submitted.');
  413: 	$bgcolor=$possiblecolors{'not_charged_try'};
  414: 	$button = 1;
  415:     } elsif ($award eq 'ERROR') {
  416: 	$message = &mt('An error occured while grading your answer.');
  417: 	$bgcolor=$possiblecolors{'not_charged_try'};
  418: 	$button = 1;
  419:     } elsif ($award eq 'TOO_LONG') {
  420: 	$message = &mt("The submitted answer was too long.");
  421: 	$bgcolor=$possiblecolors{'not_charged_try'};
  422: 	$button=1;
  423:     } elsif ($award eq 'WANTED_NUMERIC') {
  424: 	$message = &mt("This question expects a numeric answer.");
  425: 	$bgcolor=$possiblecolors{'not_charged_try'};
  426: 	$button=1;
  427:     } elsif ($award eq 'MISORDERED_RANK') {
  428: 	$message = &mt('You have provided an invalid ranking');
  429: 	if ($target ne 'tex') {
  430: 	    $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems').'.';
  431: 	}
  432: 	$bgcolor=$possiblecolors{'not_charged_try'};
  433: 	$button=1;
  434:     } elsif ($award eq 'INVALID_FILETYPE') {
  435: 	$message = &mt('The filetype extension of the file you uploaded is not allowed.');
  436: 	$bgcolor=$possiblecolors{'not_charged_try'};
  437: 	$button=1;
  438:     } elsif ($award eq 'SIG_FAIL') {
  439: 	my ($used,$min,$max)=split(':',$awardmsg);
  440: 	my $word;
  441: 	if ($used < $min) { $word=&mt('more'); }
  442: 	if ($used > $max) { $word=&mt('fewer'); }
  443: 	$message = &mt("Submission not graded.  Use [_2] digits.",$used,$word);
  444: 	$bgcolor=$possiblecolors{'not_charged_try'};
  445: 	$button=1;
  446:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
  447: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
  448: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  449: 	$bgcolor=$possiblecolors{'not_charged_try'};
  450: 	$button=1;
  451:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
  452: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
  453: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  454: 	$bgcolor=$possiblecolors{'not_charged_try'};
  455: 	$button=1;
  456:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
  457: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
  458: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  459: 	$bgcolor=$possiblecolors{'not_charged_try'};
  460: 	$button=1;
  461:     } elsif ($award eq 'UNIT_NOTNEEDED') {
  462: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
  463: 	$bgcolor=$possiblecolors{'not_charged_try'};
  464: 	$button=1;
  465:     } elsif ($award eq 'NO_UNIT') {
  466: 	$message = &mt("Units required").'.';
  467: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
  468: 	$bgcolor=$possiblecolors{'not_charged_try'};
  469: 	$button=1;
  470:     } elsif ($award eq 'COMMA_FAIL') {
  471: 	$message = &mt("Proper comma separation is required").'.';
  472: 	$bgcolor=$possiblecolors{'not_charged_try'};
  473: 	$button=1;
  474:     } elsif ($award eq 'BAD_FORMULA') {
  475: 	$message = &mt("Unable to understand formula");
  476: 	$bgcolor=$possiblecolors{'not_charged_try'};
  477: 	$button=1;
  478:     } elsif ($award eq 'INCORRECT') {
  479: 	$message = &mt("Incorrect").'.';
  480: 	$bgcolor=$possiblecolors{'charged_try'};
  481: 	$button=1;
  482:     } elsif ($award eq 'SUBMITTED') {
  483: 	$message = &mt("Your submission has been recorded.");
  484: 	$bgcolor=$possiblecolors{'correct'};
  485: 	$button=1;
  486:     } elsif ($award eq 'DRAFT') {
  487: 	$message = &mt("A draft copy has been saved.");
  488: 	$bgcolor=$possiblecolors{'not_charged_try'};
  489: 	$button=1;
  490:     } elsif ($award eq 'ASSIGNED_SCORE') {
  491: 	$message = &mt("A score has been assigned.");
  492: 	$bgcolor=$possiblecolors{'correct'};
  493: 	$button=0;
  494:     } elsif ($award eq '') {
  495: 	$bgcolor=$possiblecolors{'not_charged_try'};
  496: 	$button=1;
  497:     } else {
  498: 	$message = &mt("Unknown message").": $award";
  499: 	$button=1;
  500:     }
  501:     if (lc($Apache::lonhomework::problemstatus) eq 'no'  && 
  502: 	$Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
  503: 	$message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
  504: 	$bgcolor=$possiblecolors{'correct'};
  505: 	$button=1;
  506:     }
  507:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
  508: 	!$added_computer_text && $target ne 'tex') {
  509: 	$message.=" ".&mt("Computer's answer now shown above.");
  510: 	$added_computer_text=1;
  511:     }
  512:     return ($button,$bgcolor,$message,$previousmsg);
  513: }
  514: 
  515: sub markup_unit {
  516:     my ($unit,$target)=@_;
  517:     if ($target eq 'tex') {
  518: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
  519:     } else {
  520: 	return "<tt>".$unit."</tt>";
  521:     }
  522: }
  523: 
  524: sub removealldata {
  525:     my ($id)=@_;
  526:     foreach my $key (keys(%Apache::lonhomework::results)) {
  527: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  528: 	    &Apache::lonxml::debug("Removing $key");
  529: 	    delete($Apache::lonhomework::results{$key});
  530: 	}
  531:     }
  532: }
  533: 
  534: sub hidealldata {
  535:     my ($id)=@_;
  536:     foreach my $key (keys(%Apache::lonhomework::results)) {
  537: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  538: 	    &Apache::lonxml::debug("Hidding $key");
  539: 	    my $newkey=$key;
  540: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
  541: 	    $Apache::lonhomework::results{$newkey}=
  542: 		$Apache::lonhomework::results{$key};
  543: 	    delete($Apache::lonhomework::results{$key});
  544: 	}
  545:     }
  546: }
  547: 
  548: sub setgradedata {
  549:     my ($award,$msg,$id,$previously_used) = @_;
  550:     if ($Apache::lonhomework::scantronmode && 
  551: 	&Apache::lonnet::validCODE($ENV{'form.CODE'})) {
  552: 	$Apache::lonhomework::results{"resource.CODE"}=$ENV{'form.CODE'};
  553:     } elsif ($Apache::lonhomework::scantronmode && 
  554: 	     $ENV{'form.CODE'} eq '' &&
  555: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
  556: 	$Apache::lonhomework::results{"resource.CODE"}='';
  557:     }
  558: 
  559:     if (!$Apache::lonhomework::scantronmode &&
  560: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  561: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
  562: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
  563: 	return '';
  564:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  565: 	      /^correct/ || $Apache::lonhomework::scantronmode ||
  566: 	      lc($Apache::lonhomework::problemstatus) eq 'no') {
  567:         # the student doesn't already have it correct,
  568: 	# or we are in a mode (scantron orno problem status) where a correct 
  569:         # can become incorrect
  570: 	# handle assignment of tries and solved status
  571: 	my $solvemsg;
  572: 	if ($Apache::lonhomework::scantronmode) {
  573: 	    $solvemsg='correct_by_scantron';
  574: 	} else {
  575: 	    $solvemsg='correct_by_student';
  576: 	}
  577: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  578: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
  579: 	}
  580: 	if ( $award eq 'ASSIGNED_SCORE') {
  581: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  582: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  583: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  584: 		$solvemsg;
  585: 	    my $numawards=scalar(@Apache::inputtags::response);
  586: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  587: 	    foreach my $res (@Apache::inputtags::response) {
  588: 		$Apache::lonhomework::results{"resource.$id.awarded"}+=
  589: 		    $Apache::lonhomework::results{"resource.$id.$res.awarded"};
  590: 	    }
  591: 	    if ($numawards > 0) {
  592: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
  593: 		    $numawards;
  594: 	    }
  595: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  596: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  597: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  598: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  599: 		$solvemsg;
  600: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  601: 	} elsif ( $award eq 'INCORRECT' ) {
  602: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  603: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  604: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  605: 		$Apache::lonhomework::scantronmode) {
  606: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  607: 	    }
  608: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  609: 		'incorrect_attempted';
  610: 	} elsif ( $award eq 'SUBMITTED' ) {
  611: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  612: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  613: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  614: 		'ungraded_attempted';
  615: 	} elsif ( $award eq 'DRAFT' ) {
  616: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
  617: 	} elsif ( $award eq 'NO_RESPONSE' ) {
  618: 	    #no real response so delete any data that got stored
  619: 	    &removealldata($id);
  620: 	    return '';
  621: 	} else {
  622: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  623: 		'incorrect_attempted';
  624: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  625: 		$Apache::lonhomework::scantronmode) {
  626: 		$Apache::lonhomework::results{"resource.$id.tries"} =
  627: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  628: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  629: 	    }
  630: 	}
  631: 	if (defined($msg)) {
  632: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
  633: 	}
  634: 	# did either of the overall awards chage? If so ignore the 
  635: 	# previous check
  636: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
  637: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
  638: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
  639: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
  640: 	    # check if this was a previous submission if it was delete the
  641: 	    # unneeded data and update the previously_used attribute
  642: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
  643: 		if (lc($Apache::lonhomework::problemstatus) ne 'no') {
  644: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
  645: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  646: 		}
  647: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  648: 		#delete all data as they student didn't do anything, but save
  649: 		#the list of collaborators.
  650: 		&removealldata($id);
  651: 		#and since they didn't do anything we were never here
  652: 		return '';
  653: 	    } else {
  654: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
  655: 	    }
  656: 	}
  657:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
  658: 	      /^correct/ ) {
  659: 	#delete all data as they student already has it correct
  660: 	&removealldata($id);
  661: 	#and since they didn't do anything we were never here
  662: 	return '';
  663:     }
  664:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
  665: }
  666: 
  667: sub grade {
  668:     my ($target) = @_;
  669:     my $id = $Apache::inputtags::part;
  670:     my $response='';
  671:     if ( defined $ENV{'form.submitted'}) {
  672: 	my (@awards,@msgs);
  673: 	foreach $response (@Apache::inputtags::response) {
  674: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  675: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  676: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
  677: 	    push (@awards,$value);
  678: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
  679: 	    &Apache::lonxml::debug("got message $value from $response for $id");
  680: 	    push (@msgs,$value);
  681: 	}
  682: 	my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
  683: 	my $previously_used;
  684: 	if ( $#Apache::inputtags::previous eq $#awards ) {
  685: 	    my $match=0;
  686: 	    my @matches;
  687: 	    foreach my $versionar (@Apache::inputtags::previous_version) {
  688: 		foreach my $version (@$versionar) {
  689: 		    $matches[$version]++;
  690: 		}
  691: 	    }
  692: 	    foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
  693: 	    if ($match) {
  694: 		$previously_used = 'PREVIOUSLY_LAST';
  695: 		foreach my $value (@Apache::inputtags::previous) {
  696: 		    if ($value eq 'PREVIOUSLY_USED' ) {
  697: 			$previously_used = $value;
  698: 			last;
  699: 		    }
  700: 		}
  701: 	    }
  702: 	}
  703: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
  704: 	&setgradedata($finalaward,$msg,$id,$previously_used);
  705:     }
  706:     return '';
  707: }
  708: 
  709: sub gradestatus {
  710:     my ($id,$target) = @_;
  711:     my $showbutton = 1;
  712:     my $bgcolor = '';
  713:     my $message = '';
  714:     my $latemessage = '';
  715:     my $trystr='';
  716:     my $button='';
  717:     my $previousmsg='';
  718: 
  719:     my $status = $Apache::inputtags::status['-1'];
  720:     &Apache::lonxml::debug("gradestatus has :$status:");
  721:     if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
  722: 	 $status ne 'INVALID_ACCESS') {  
  723: 	my $award = $Apache::lonhomework::history{"resource.$id.award"};
  724: 	my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  725: 	my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  726: 	my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
  727: 	&Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
  728: 	if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
  729: 	    &Apache::lonxml::debug('Getting message');
  730: 	    ($showbutton,$bgcolor,$message,$previousmsg) =
  731: 		&decideoutput($award,$awardmsg,$solved,$previous,$target);
  732: 	    if ($target eq 'tex') {
  733: 		$message='\vskip 2 mm '.$message.' ';
  734: 	    } else {
  735: 		$message="<td bgcolor=\"$bgcolor\">$message</td>";
  736: 		if ($previousmsg) {
  737: 		    $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
  738: 		}
  739: 	    }
  740: 	}
  741: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  742: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  743: 	&Apache::lonxml::debug("got maxtries of :$maxtries:");
  744: 	#if tries are set to negative turn off the Tries/Button and messages
  745: 	if (defined($maxtries) && $maxtries < 0) { return ''; }
  746: 	if ( $tries eq '' ) { $tries = '0'; }
  747: 	if ( $maxtries eq '' ) { $maxtries = '2'; } 
  748: 	if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  749: 	my $tries_text=&mt('Tries');
  750: 	if ( $Apache::lonhomework::type eq 'survey') { $tries_text=&mt('Submissions'); }
  751: 	if ( $showbutton ) {
  752: 	    if ($target eq 'tex') {
  753: 		if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $ENV{'form.suppress_tries'} ne 'yes') {
  754: 		    $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
  755: 		} else {
  756: 		    $trystr = '\vskip 0 mm ';
  757: 		}
  758: 	    } else {
  759: 		$trystr = "<td><nobr>".$tries_text." $tries";
  760: 		if($ENV{'request.state'} ne 'construct') {
  761: 		    $trystr.="/$maxtries";
  762: 		} else {
  763: 		    if (defined($Apache::inputtags::params{'maxtries'})) {
  764: 			$trystr.="/".$Apache::inputtags::params{'maxtries'};
  765: 		    }
  766: 		}
  767: 		$trystr.="</nobr></td>";
  768: 	    }
  769: 	}
  770: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  771: 	if ( $showbutton ) { 
  772: 	    if ($target ne 'tex') {
  773: 		$button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
  774: 	    }
  775: 	}
  776: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  777: 	    #last submissions was after due date
  778: 	    if ($target eq 'tex') {
  779: 		$latemessage=' The last submission was after the Due Date ';
  780: 	    } else {
  781: 		$latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
  782: 	    }
  783: 	}
  784:     }
  785:     my $output= $previousmsg.$latemessage.$message.$trystr;
  786:     if ($output =~ /^\s*$/) {
  787: 	return $button;
  788:     } else {
  789: 	if ($target eq 'tex') {
  790: 	    return $button.' \vskip 0 mm '.$output.' ';
  791: 	} else {
  792: 	    return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
  793: 	}
  794:     }
  795: }
  796: 1;
  797: __END__
  798:  

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