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

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

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