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

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

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