File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.158: download - view: text, annotated - select for diffs
Tue Mar 1 03:23:16 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- only output the readonly attibute when it's yes

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

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