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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.157   ! albertel    4: # $Id: inputtags.pm,v 1.156 2005/01/31 22:00:40 albertel Exp $
1.47      albertel    5: #
                      6: # Copyright Michigan State University Board of Trustees
                      7: #
                      8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
                      9: #
                     10: # LON-CAPA is free software; you can redistribute it and/or modify
                     11: # it under the terms of the GNU General Public License as published by
                     12: # the Free Software Foundation; either version 2 of the License, or
                     13: # (at your option) any later version.
                     14: #
                     15: # LON-CAPA is distributed in the hope that it will be useful,
                     16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
                     17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     18: # GNU General Public License for more details.
                     19: #
                     20: # You should have received a copy of the GNU General Public License
                     21: # along with LON-CAPA; if not, write to the Free Software
                     22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
                     23: #
                     24: # /home/httpd/html/adm/gpl.txt
                     25: #
                     26: # http://www.lon-capa.org/
1.1       albertel   27: 
                     28: package Apache::inputtags;
1.55      albertel   29: use HTML::Entities();
1.1       albertel   30: use strict;
1.82      www        31: use Apache::loncommon;
1.115     www        32: use Apache::lonlocal;
1.1       albertel   33: 
1.50      harris41   34: BEGIN {
1.135     albertel   35:     &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1       albertel   36: }
                     37: 
1.43      albertel   38: 
1.1       albertel   39: sub initialize_inputtags {
1.135     albertel   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;
1.144     albertel   68:     # storage location so the begin <*response> tag can generate the correct
                     69:     # answer string for display by the <textline />
                     70:     %Apache::inputtags::answertxt=();
1.103     albertel   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:     }
1.1       albertel   89: }
                     90: 
1.14      albertel   91: sub start_input {
1.135     albertel   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;
1.14      albertel   98: }
                     99: 
                    100: sub end_input {
1.135     albertel  101:     pop @Apache::inputtags::input;
                    102:     return '';
1.14      albertel  103: }
                    104: 
1.124     www       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: 
1.48      albertel  115: sub start_textfield {
1.135     albertel  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;
1.138     albertel  124: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
1.135     albertel  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);
1.143     www       128: 	    if ( $rows eq '') { $rows = 16; }
1.135     albertel  129: 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    130: 	    $result='';
                    131: 	    if ($addchars) {
                    132: 		$result.=&addchars('HWVAL_'.$resid,$addchars);
                    133: 	    }
1.143     www       134: 	    push @Apache::lonxml::htmlareafields,'HWVAL_'.$resid;
                    135: 	    $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
1.135     albertel  136: 		"rows=\"$rows\" cols=\"$cols\">".$oldresponse;
                    137: 	    if ($oldresponse ne '') {
1.143     www       138: 
1.135     albertel  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
1.84      albertel  147: 	    &Apache::lonxml::get_all_text("/textfield",$parser);
1.61      albertel  148: 	}
1.135     albertel  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: 	}
1.60      albertel  186:     }
1.135     albertel  187:     return $result;
1.6       albertel  188: }
                    189: 
1.48      albertel  190: sub end_textfield {
1.135     albertel  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;
1.6       albertel  203: }
                    204: 
1.1       albertel  205: sub start_textline {
1.135     albertel  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: 	    }
1.138     albertel  218: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.135     albertel  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: 		}
1.157   ! albertel  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="'.
1.135     albertel  231: 		    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
                    232: 	    }
                    233: 	} else {
                    234: 	    #right or wrong don't show what was last typed in.
1.144     albertel  235: 	    $result='<i>'.$Apache::inputtags::answertxt{$id}.'</i>';
                    236: 	    #$result='';
1.135     albertel  237: 	}
                    238:     } elsif ($target eq 'edit') {
                    239: 	$result=&Apache::edit::tag_start($target,$token);
                    240: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157   ! albertel  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();
1.135     albertel  247:     } elsif ($target eq 'modified') {
1.157   ! albertel  248: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
        !           249: 						     $safeeval,'size',
        !           250: 						     'addchars','readonly');
1.135     albertel  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;
1.1       albertel  258: }
                    259: 
                    260: sub end_textline {
1.135     albertel  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 "";
1.9       albertel  265: }
                    266: 
1.98      albertel  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];
1.138     albertel  275: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.98      albertel  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 {
1.135     albertel  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 "";
1.98      albertel  293: }
                    294: 
1.136     albertel  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: 
1.9       albertel  305: sub finalizeawards {
1.136     albertel  306:     my ($awardref,$msgref)=@_;
                    307:     my $result=undef;
1.135     albertel  308:     my $award;
1.136     albertel  309:     my $msg;
                    310:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135     albertel  311:     if ($result eq '' ) {
                    312: 	my $blankcount;
1.136     albertel  313: 	foreach $award (@$awardref) {
1.135     albertel  314: 	    if ($award eq '') {
                    315: 		$result='MISSING_ANSWER';
                    316: 		$blankcount++;
                    317: 	    }
                    318: 	}
1.136     albertel  319: 	if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
1.135     albertel  320:     }
1.136     albertel  321:     if (defined($result)) { return ($result,$msg); }
                    322:     foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
1.137     albertel  323: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
                    324: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
                    325: 			       'UNIT_FAIL', 'NO_UNIT',
1.136     albertel  326: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
                    327: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
                    328: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
                    329: 			       'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
1.153     albertel  330: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
1.136     albertel  331: 	($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
                    332: 	if (defined($result)) { return ($result,$msg); }
1.135     albertel  333:     }
1.136     albertel  334:     return ('ERROR',undef);
1.9       albertel  335: }
                    336: 
1.10      albertel  337: sub decideoutput {
1.136     albertel  338:     my ($award,$awardmsg,$solved,$previous,$target)=@_;
1.135     albertel  339:     my $message='';
                    340:     my $button=0;
                    341:     my $previousmsg;
                    342:     my $bgcolor='orange';
1.148     albertel  343:     my $added_computer_text=0;
1.135     albertel  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>";
1.150     albertel  360: 		$message.=" ".&mt("Computer's answer now shown above.");
1.135     albertel  361: 	    }
1.148     albertel  362: 	    $added_computer_text=1;
1.135     albertel  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 '') {
1.144     albertel  385: 	    $message = &mt("Incorrect").".";
1.135     albertel  386: 	    $bgcolor=$possiblecolors{'charged_try'};
                    387: 	    $button=1;
                    388: 	} else {
1.144     albertel  389: 	    if ($target eq 'tex') {
                    390: 		$message = '\textbf{'.&mt('You are correct.').'}';
                    391: 	    } else {
                    392: 		$message = "<b>".&mt('You are correct.')."</b>";
1.150     albertel  393: 		$message.=" ".&mt("Computer's answer now shown above.");
1.144     albertel  394: 	    }
1.148     albertel  395: 	    $added_computer_text=1;
1.135     albertel  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') {
1.145     albertel  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);
1.135     albertel  444: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    445: 	$button=1;
1.137     albertel  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') {
1.155     albertel  452: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137     albertel  453: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
                    454: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    455: 	$button=1;
1.140     matthew   456:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155     albertel  457: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136     albertel  458: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.135     albertel  459: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    460: 	$button=1;
                    461:     } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155     albertel  462: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.135     albertel  463: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    464: 	$button=1;
                    465:     } elsif ($award eq 'NO_UNIT') {
1.144     albertel  466: 	$message = &mt("Units required").'.';
1.135     albertel  467: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
                    468: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    469: 	$button=1;
1.153     albertel  470:     } elsif ($award eq 'COMMA_FAIL') {
                    471: 	$message = &mt("Proper comma separation is required").'.';
                    472: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    473: 	$button=1;
1.135     albertel  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') {
1.144     albertel  479: 	$message = &mt("Incorrect").'.';
1.135     albertel  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') {
1.144     albertel  487: 	$message = &mt("A draft copy has been saved.");
1.135     albertel  488: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    489: 	$button=1;
                    490:     } elsif ($award eq 'ASSIGNED_SCORE') {
1.144     albertel  491: 	$message = &mt("A score has been assigned.");
1.135     albertel  492: 	$bgcolor=$possiblecolors{'correct'};
                    493: 	$button=0;
1.144     albertel  494:     } elsif ($award eq '') {
                    495: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    496: 	$button=1;
1.135     albertel  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:     }
1.148     albertel  507:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
1.150     albertel  508: 	!$added_computer_text && $target ne 'tex') {
1.149     albertel  509: 	$message.=" ".&mt("Computer's answer now shown above.");
1.148     albertel  510: 	$added_computer_text=1;
1.144     albertel  511:     }
1.135     albertel  512:     return ($button,$bgcolor,$message,$previousmsg);
1.12      albertel  513: }
                    514: 
1.155     albertel  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: 
1.88      albertel  524: sub removealldata {
1.87      albertel  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: 
1.142     albertel  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: 
1.12      albertel  548: sub setgradedata {
1.136     albertel  549:     my ($award,$msg,$id,$previously_used) = @_;
1.154     albertel  550:     if ($Apache::lonhomework::scantronmode && 
                    551: 	&Apache::lonnet::validCODE($ENV{'form.CODE'})) {
1.141     albertel  552: 	$Apache::lonhomework::results{"resource.CODE"}=$ENV{'form.CODE'};
1.154     albertel  553:     } elsif ($Apache::lonhomework::scantronmode && 
                    554: 	     $ENV{'form.CODE'} eq '' &&
                    555: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
                    556: 	$Apache::lonhomework::results{"resource.CODE"}='';
1.141     albertel  557:     }
1.154     albertel  558: 
1.135     albertel  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;
1.87      albertel  563: 	return '';
1.135     albertel  564:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
                    565: 	      /^correct/ || $Apache::lonhomework::scantronmode ||
                    566: 	      lc($Apache::lonhomework::problemstatus) eq 'no') {
1.154     albertel  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
1.135     albertel  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;
1.152     albertel  604: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
                    605: 		$Apache::lonhomework::scantronmode) {
1.135     albertel  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
1.129     albertel  619: 	    &removealldata($id);
                    620: 	    return '';
                    621: 	} else {
1.135     albertel  622: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                    623: 		'incorrect_attempted';
1.152     albertel  624: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
                    625: 		$Apache::lonhomework::scantronmode) {
1.135     albertel  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: 	}
1.136     albertel  631: 	if (defined($msg)) {
                    632: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
                    633: 	}
1.135     albertel  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: 	    }
1.101     albertel  656: 	}
1.135     albertel  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 '';
1.40      albertel  663:     }
1.135     albertel  664:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.10      albertel  665: }
                    666: 
1.9       albertel  667: sub grade {
1.135     albertel  668:     my ($target) = @_;
                    669:     my $id = $Apache::inputtags::part;
                    670:     my $response='';
                    671:     if ( defined $ENV{'form.submitted'}) {
1.136     albertel  672: 	my (@awards,@msgs);
1.135     albertel  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);
1.136     albertel  678: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
                    679: 	    &Apache::lonxml::debug("got message $value from $response for $id");
                    680: 	    push (@msgs,$value);
1.135     albertel  681: 	}
1.136     albertel  682: 	my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135     albertel  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: 		    }
1.75      albertel  700: 		}
                    701: 	    }
1.43      albertel  702: 	}
1.136     albertel  703: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
                    704: 	&setgradedata($finalaward,$msg,$id,$previously_used);
1.43      albertel  705:     }
1.135     albertel  706:     return '';
1.1       albertel  707: }
                    708: 
1.11      albertel  709: sub gradestatus {
1.135     albertel  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"};
1.136     albertel  726: 	my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
                    727: 	&Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144     albertel  728: 	if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135     albertel  729: 	    &Apache::lonxml::debug('Getting message');
                    730: 	    ($showbutton,$bgcolor,$message,$previousmsg) =
1.136     albertel  731: 		&decideoutput($award,$awardmsg,$solved,$previous,$target);
1.135     albertel  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') {
1.147     sakharuk  753: 		if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $ENV{'form.suppress_tries'} ne 'yes') {
1.135     albertel  754: 		    $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
                    755: 		} else {
                    756: 		    $trystr = '\vskip 0 mm ';
                    757: 		}
                    758: 	    } else {
1.136     albertel  759: 		$trystr = "<td><nobr>".$tries_text." $tries";
1.135     albertel  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: 		}
1.136     albertel  767: 		$trystr.="</nobr></td>";
1.135     albertel  768: 	    }
                    769: 	}
                    770: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
                    771: 	if ( $showbutton ) { 
                    772: 	    if ($target ne 'tex') {
1.156     albertel  773: 		$button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1.135     albertel  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;
1.63      sakharuk  788:     } else {
1.135     albertel  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: 	}
1.63      sakharuk  794:     }
1.11      albertel  795: }
1.1       albertel  796: 1;
                    797: __END__
1.43      albertel  798:  

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