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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.190   ! albertel    4: # $Id: inputtags.pm,v 1.189 2006/03/09 00:43:05 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.165     albertel   33: use Apache::lonnet;
1.1       albertel   34: 
1.50      harris41   35: BEGIN {
1.135     albertel   36:     &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1       albertel   37: }
                     38: 
1.177     foxr       39: #   Initializes a set of global variables used during the parse of the problem.
                     40: #
1.178     albertel   41: #  @Apache::inputtags::input        - List of current input ids.
                     42: #  @Apache::inputtags::inputlist    - List of all input ids seen this problem.
                     43: #  @Apache::inputtags::response     - List of all current resopnse ids.
                     44: #  @Apache::inputtags::responselist - List of all response ids seen this 
                     45: #                                       problem.
                     46: #  @Apache::inputtags::hint         - List of all hint ids.
                     47: #  @Apache::inputtags::hintlist     - List of all hint ids seen this problem.
                     48: #  @Apache::inputtags::previous     - List describing if specific responseds
                     49: #                                       have been used
                     50: #  @Apache::inputtags::previous_version - Submission responses were used in.
                     51: #  $Apache::inputtags::part         - Current part id (valid only in 
                     52: #                                       <problem>)
                     53: #                                     0 if not in a part.
                     54: #  @Apache::inputtags::partlist     - List of part ids seen in the current
                     55: #                                       <problem>
                     56: #  @Apache::inputtags::status       - List of problem  statuses. First 
                     57: #                                     element is the status of the <problem>
                     58: #                                     the remainder are for individual <part>s.
                     59: #  %Apache::inputtags::params       - Hash of defined parameters for the
                     60: #                                     current response.
                     61: #  @Apache::inputtags::import       - List of all ids for <import> thes get
                     62: #                                     join()ed and prepended.
                     63: #  @Apache::inputtags::importlist   - List of all import ids seen.
                     64: #  $Apache::inputtags::response_with_no_part
                     65: #                                   - Flag set true if we have seen a response
                     66: #                                     that is not inside a <part>
                     67: #  %Apache::inputtags::answertxt    - <*response> tags store correct
                     68: #                                     answer strings for display by <textline/>
                     69: #                                     in this hash.
                     70: 
1.1       albertel   71: sub initialize_inputtags {
1.135     albertel   72:     @Apache::inputtags::input=();
                     73:     @Apache::inputtags::inputlist=();
1.174     albertel   74:     @Apache::inputtags::response=();
1.135     albertel   75:     @Apache::inputtags::responselist=();
1.174     albertel   76:     @Apache::inputtags::hint=();
1.173     albertel   77:     @Apache::inputtags::hintlist=();
1.135     albertel   78:     @Apache::inputtags::previous=();
                     79:     @Apache::inputtags::previous_version=();
                     80:     $Apache::inputtags::part='';
                     81:     @Apache::inputtags::partlist=();
                     82:     @Apache::inputtags::status=();
                     83:     %Apache::inputtags::params=();
                     84:     @Apache::inputtags::import=();
                     85:     @Apache::inputtags::importlist=();
                     86:     $Apache::inputtags::response_with_no_part=0;
1.144     albertel   87:     %Apache::inputtags::answertxt=();
1.103     albertel   88: }
                     89: 
                     90: sub check_for_duplicate_ids {
                     91:     my %check;
                     92:     foreach my $id (@Apache::inputtags::partlist,
                     93: 		    @Apache::inputtags::responselist,
1.173     albertel   94: 		    @Apache::inputtags::hintlist,
1.103     albertel   95: 		    @Apache::inputtags::importlist) {
                     96: 	$check{$id}++;
                     97:     }
                     98:     my @duplicates;
                     99:     foreach my $id (sort(keys(%check))) {
                    100: 	if ($check{$id} > 1) {
                    101: 	    push(@duplicates,$id);
                    102: 	}
                    103:     }
                    104:     if (@duplicates) {
                    105: 	&Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
                    106:     }
1.1       albertel  107: }
                    108: 
1.14      albertel  109: sub start_input {
1.135     albertel  110:     my ($parstack,$safeeval)=@_;
                    111:     my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
                    112:     if ($id eq '') { $id = $Apache::lonxml::curdepth; }
                    113:     push (@Apache::inputtags::input,$id);
                    114:     push (@Apache::inputtags::inputlist,$id);
                    115:     return $id;
1.14      albertel  116: }
                    117: 
                    118: sub end_input {
1.135     albertel  119:     pop @Apache::inputtags::input;
                    120:     return '';
1.14      albertel  121: }
                    122: 
1.124     www       123: sub addchars {
                    124:     my ($fieldid,$addchars)=@_;
                    125:     my $output='';
                    126:     foreach (split(/\,/,$addchars)) {
                    127: 	$output.='<a href="javascript:void(document.forms.lonhomework.'.
                    128: 	    $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
                    129:     }
                    130:     return $output;
                    131: }
                    132: 
1.48      albertel  133: sub start_textfield {
1.185     albertel  134:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.135     albertel  135:     my $result = "";
                    136:     my $id = &start_input($parstack,$safeeval);
                    137:     my $resid=$Apache::inputtags::response[-1];
                    138:     if ($target eq 'web') {
                    139: 	$Apache::lonxml::evaluate--;
                    140: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    141: 	    my $partid=$Apache::inputtags::part;
1.138     albertel  142: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
1.135     albertel  143: 	    my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    144: 	    if ( $cols eq '') { $cols = 80; }
                    145: 	    my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.143     www       146: 	    if ( $rows eq '') { $rows = 16; }
1.135     albertel  147: 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    148: 	    $result='';
                    149: 	    if ($addchars) {
                    150: 		$result.=&addchars('HWVAL_'.$resid,$addchars);
                    151: 	    }
1.143     www       152: 	    push @Apache::lonxml::htmlareafields,'HWVAL_'.$resid;
                    153: 	    $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
1.135     albertel  154: 		"rows=\"$rows\" cols=\"$cols\">".$oldresponse;
                    155: 	    if ($oldresponse ne '') {
1.143     www       156: 
1.135     albertel  157: 		#get rid of any startup text if the user has already responded
1.185     albertel  158: 		&Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.135     albertel  159: 	    }
                    160: 	} else {
                    161: 	    #right or wrong don't show it
                    162: 	    #$result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
                    163: 	    $result='';
                    164: 	    #get rid of any startup text
1.185     albertel  165: 	    &Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.61      albertel  166: 	}
1.135     albertel  167:     } elsif ($target eq 'grade') {
1.185     albertel  168: 	my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    169: 						   $style);
1.165     albertel  170: 	if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
1.135     albertel  171: 	    # if the seed text is still there it wasn't a real submission
1.165     albertel  172: 	    $env{'form.HWVAL_'.$resid}='';
1.135     albertel  173: 	}
                    174:     } elsif ($target eq 'edit') {
                    175: 	$result.=&Apache::edit::tag_start($target,$token);
                    176: 	$result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
                    177: 	$result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
                    178: 	$result.=&Apache::edit::text_arg
                    179: 	    ('Click-On Texts (comma sep):','addchars',$token,10);
1.185     albertel  180: 	my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    181: 						   $style);
1.135     albertel  182: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
                    183:     } elsif ($target eq 'modified') {
                    184: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    185: 						     $safeeval,'rows','cols',
                    186: 						     'addchars');
                    187: 	if ($constructtag) {
                    188: 	    $result = &Apache::edit::rebuild_tag($token);
                    189: 	} else {
                    190: 	    $result=$token->[4];
                    191: 	}
                    192: 	$result.=&Apache::edit::modifiedfield("/textfield",$parser);
                    193:     } elsif ($target eq 'tex') {
                    194: 	my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
                    195: 	my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    196: 	if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
                    197: 	    $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
                    198: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    199: 	    $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
                    200: 	} else {
                    201: 	    my $TeXwidth=$width_of_box/80;
                    202: 	    $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
                    203: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    204: 	    $result.='}}}\vskip 2 mm ';
                    205: 	}
1.60      albertel  206:     }
1.135     albertel  207:     return $result;
1.6       albertel  208: }
                    209: 
1.48      albertel  210: sub end_textfield {
1.135     albertel  211:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    212:     my $result;
                    213:     if ($target eq 'web') {
                    214: 	$Apache::lonxml::evaluate++;
                    215: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    216: 	    return "</textarea>";
                    217: 	}
                    218:     } elsif ($target eq 'edit') {
                    219: 	$result=&Apache::edit::end_table();
                    220:     }
                    221:     &end_input;
                    222:     return $result;
1.6       albertel  223: }
                    224: 
1.190   ! albertel  225: sub exam_score_line {
1.188     albertel  226:     my ($target) = @_;
1.190   ! albertel  227: 
1.188     albertel  228:     my $result;
                    229:     if ($target eq 'tex') {
                    230: 	my $repetition = &Apache::response::repetition();
                    231: 	$result.='\begin{enumerate}';
1.190   ! albertel  232: 	if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
1.188     albertel  233: 	foreach my $i (0..$repetition-1) {
                    234: 	    $result.='\item[\textbf{'.
                    235: 		($Apache::lonxml::counter+$i).
                    236: 		'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
                    237: 	}
                    238: 	$result.= '\end{enumerate}';
1.190   ! albertel  239:     }
        !           240: 
        !           241:     return $result;
        !           242: }
        !           243: 
        !           244: sub exam_box {
        !           245:     my ($target) = @_;
        !           246:     my $result;
1.188     albertel  247: 
1.190   ! albertel  248:     if ($target eq 'tex') {
        !           249: 	$result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
        !           250: 	$result .= &exam_score_line($target);
1.188     albertel  251:     } elsif ($target eq 'web') {
                    252: 	my $id=$Apache::inputtags::response[-1];
                    253: 	$result.= '<br /><br />
                    254:                    <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
                    255:                    </textarea> <br /><br />';
                    256:     }
                    257:     return $result;
                    258: }
                    259: 
                    260: sub needs_exam_box {
                    261:     my ($tagstack) = @_;
                    262:     my @tags = ('formularesponse',
                    263: 		'stringresponse',
                    264: 		'reactionresponse',
                    265: 		'organicresponse',
                    266: 		);
                    267: 
                    268:     foreach my $tag (@tags) {
                    269: 	if (grep(/\Q$tag\E/,@$tagstack)) {
                    270: 	    return 1;
                    271: 	}
                    272:     }
                    273:     return 0;
                    274: }
                    275: 
1.1       albertel  276: sub start_textline {
1.135     albertel  277:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    278:     my $result = "";
                    279:     if ($target eq 'web') {
                    280: 	$Apache::lonxml::evaluate--;
                    281: 	my $partid=$Apache::inputtags::part;
                    282: 	my $id=$Apache::inputtags::response[-1];
                    283: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    284: 	    my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    285: 	    my $maxlength;
                    286: 	    if ($size eq '') { $size=20; } else {
                    287: 		if ($size < 20) { $maxlength=$size; }
                    288: 	    }
1.138     albertel  289: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.135     albertel  290: 	    if ($Apache::lonhomework::type ne 'exam') {
                    291: 		my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    292: 		$result='';
                    293: 		if ($addchars) {
                    294: 		    $result.=&addchars('HWVAL_'.$id,$addchars);
                    295: 		}
1.157     albertel  296: 		my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
                    297: 							$safeeval);
                    298: 		if (lc($readonly) eq 'yes') {
                    299: 		    $readonly=' readonly="readonly" ';
1.158     albertel  300: 		} else {
                    301: 		    $readonly='';
1.157     albertel  302: 		}
                    303: 		$result.= '<input type="text" '.$readonly.' name="HWVAL_'.$id.'" value="'.
1.135     albertel  304: 		    $oldresponse.'" size="'.$size.'" maxlength="'.$maxlength.'" />';
                    305: 	    }
1.188     albertel  306: 	    if ($Apache::lonhomework::type eq 'exam'
                    307: 		&& &needs_exam_box($tagstack)) {
                    308: 		$result.=&exam_box($target);
                    309: 	    }
1.135     albertel  310: 	} else {
                    311: 	    #right or wrong don't show what was last typed in.
1.168     albertel  312: 	    $result='<b>'.$Apache::inputtags::answertxt{$id}.'</b>';
1.144     albertel  313: 	    #$result='';
1.135     albertel  314: 	}
                    315:     } elsif ($target eq 'edit') {
                    316: 	$result=&Apache::edit::tag_start($target,$token);
                    317: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157     albertel  318: 	    &Apache::edit::text_arg('Click-On Texts (comma sep):',
                    319: 				    'addchars',$token,10);
                    320:         $result.=&Apache::edit::select_arg('Readonly:','readonly',
                    321: 					   ['no','yes'],$token);
                    322: 	$result.=&Apache::edit::end_row();
                    323: 	$result.=&Apache::edit::end_table();
1.135     albertel  324:     } elsif ($target eq 'modified') {
1.157     albertel  325: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    326: 						     $safeeval,'size',
                    327: 						     'addchars','readonly');
1.135     albertel  328: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.188     albertel  329:     } elsif ($target eq 'tex' 
                    330: 	     && $Apache::lonhomework::type ne 'exam') {
1.135     albertel  331: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    332: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
                    333: 	$result='\framebox['.$size.'][s]{\tiny\strut}';
1.188     albertel  334: 
                    335:     } elsif ($target eq 'tex' 
                    336: 	     && $Apache::lonhomework::type eq 'exam'
                    337: 	     && &needs_exam_box($tagstack)) {
                    338: 	$result.=&exam_box($target);
1.135     albertel  339:     }
                    340:     return $result;
1.1       albertel  341: }
                    342: 
                    343: sub end_textline {
1.135     albertel  344:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    345:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    346:     elsif ($target eq 'edit') { return ('','no'); }
                    347:     return "";
1.9       albertel  348: }
                    349: 
1.98      albertel  350: sub start_hiddenline {
                    351:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    352:     my $result = "";
                    353:     if ($target eq 'web') {
                    354: 	$Apache::lonxml::evaluate--;
                    355: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    356: 	    my $partid=$Apache::inputtags::part;
                    357: 	    my $id=$Apache::inputtags::response[-1];
1.138     albertel  358: 	    my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$id.submission"},'<>&"');
1.98      albertel  359: 	    if ($Apache::lonhomework::type ne 'exam') {
                    360: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
                    361: 		    $oldresponse.'" />';
                    362: 	    }
                    363: 	}
                    364:     } elsif ($target eq 'edit') {
                    365: 	$result=&Apache::edit::tag_start($target,$token);
                    366: 	$result.=&Apache::edit::end_table;
                    367:     }
1.189     albertel  368: 
                    369:     if ( ($target eq 'web' || $target eq 'tex')
                    370: 	 && $Apache::lonhomework::type eq 'exam'
                    371: 	 && &needs_exam_box($tagstack)) {
                    372: 	$result.=&exam_box($target);
                    373:     }
1.98      albertel  374:     return $result;
                    375: }
                    376: 
                    377: sub end_hiddenline {
1.135     albertel  378:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    379:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    380:     elsif ($target eq 'edit') { return ('','no'); }
                    381:     return "";
1.98      albertel  382: }
                    383: 
1.160     albertel  384: # $part -> partid
                    385: # $id -> responseid
                    386: # $uploadefiletypes -> comma seperated list of extensions allowed or * for any
                    387: # $which -> 'uploadedonly'  -> only newly uploaded files
                    388: #           'portfolioonly' -> only allow files from portfolio
                    389: #           'both' -> allow files from either location
1.175     albertel  390: # $extratext -> additional text to go between the link and the input box
1.160     albertel  391: # returns a table row <tr> 
                    392: sub file_selector {
1.175     albertel  393:     my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
1.160     albertel  394:     if (!$uploadedfiletypes) { return ''; }
1.167     albertel  395: 
                    396:     my $jspart=$part;
                    397:     $jspart=~s/\./_/g;
                    398: 
1.160     albertel  399:     my $result;
                    400:     
1.162     albertel  401:     $result.='<tr><td>';
                    402:     if ($uploadedfiletypes ne '*') {
                    403: 	$result.=
                    404: 	    &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
                    405:     }
1.160     albertel  406:     if ($which eq 'uploadonly' || $which eq 'both') { 
                    407: 	$result.=&mt('Submit a file: (only one file can be uploaded)').
                    408: 	    ' <br /><input type="file" size="50" name="HWFILE'.
1.167     albertel  409: 	    $jspart.'_'.$id.'" /><br />';
1.160     albertel  410: 	my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
                    411: 
                    412: 	if ($uploadedfile) {
                    413: 	    my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
1.176     albertel  414: 	    &Apache::lonxml::extlink($url);
1.160     albertel  415: 	    &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
                    416: 	    my $icon=&Apache::loncommon::icon($url);
                    417: 	    my $curfile='<a href="'.$url.'"><img src="'.$icon.
                    418: 		'" border="0" />'.$uploadedfile.'</a>';
                    419: 	    $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
                    420: 	} else {
                    421: 	    #$result.=&mt('(Hand in a file you have prepared on your computer)');
                    422: 	}
                    423:     }
                    424:     if ( $which eq 'both') { 
                    425: 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
                    426:     }
                    427:     if ($which eq 'portfolioonly' || $which eq 'both') { 
1.175     albertel  428: 	$result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&amp;fieldname=HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
1.160     albertel  429: 	    &mt('Select Portfolio Files').'</a><br />'.
1.167     albertel  430: 	    '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
1.160     albertel  431: 	    '<br />';
                    432: 	if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
1.187     albertel  433: 	    my (@filelist,@bad_file_list);
1.160     albertel  434: 	    foreach my $file (split(',',&Apache::lonnet::unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
                    435: 		my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
                    436: 		my $url="/uploaded/$domain/$user/portfolio$file";
                    437: 		my $icon=&Apache::loncommon::icon($url);
1.161     albertel  438: 		push(@filelist,'<a href="'.$url.'"><img src="'.$icon.
                    439: 		     '" border="0" />'.$file.'</a>');
1.187     albertel  440: 		if (! &Apache::lonnet::stat_file($url)) {
                    441: 		    push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
                    442: 			 '" border="0" />'.$file.'</a>');
                    443: 		}
1.160     albertel  444: 	    }
1.161     albertel  445: 	    $result.=&mt("Portfolio files previously selected: <strong>[_1]</strong>",join(', ',@filelist));
1.187     albertel  446: 	    if (@bad_file_list) {
                    447: 		$result.='<br />'.&mt('<font color="red">These file(s) don\'t exist:</font> <strong>[_1]</strong>',join(', ',@bad_file_list));
                    448: 	    }
1.160     albertel  449: 	}
                    450:     }
                    451:     $result.='</td></tr>'; 
                    452:     return $result;
                    453: }
                    454: 
1.136     albertel  455: sub checkstatus {
                    456:     my ($value,$awardref,$msgref)=@_;
                    457:     for (my $i=0;$i<=$#$awardref;$i++) {
                    458: 	if ($$awardref[$i] eq $value) {
                    459: 	    return ($$awardref[$i],$$msgref[$i]);
                    460: 	}
                    461:     }
                    462:     return(undef,undef);
                    463: }
                    464: 
1.179     albertel  465: sub valid_award {
                    466:     my ($award) =@_;
1.182     albertel  467:     foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
                    468: 			       'NO_RESPONSE',
1.179     albertel  469: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
                    470: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
                    471: 			       'UNIT_FAIL', 'NO_UNIT',
                    472: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
                    473: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
                    474: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
                    475: 			       'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
                    476: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
                    477: 	if ($award eq $possibleaward) { return 1; }
                    478:     }
                    479:     return 0;
                    480: }
                    481: 
1.9       albertel  482: sub finalizeawards {
1.181     albertel  483:     my ($awardref,$msgref,$nameref,$reverse)=@_;
1.136     albertel  484:     my $result=undef;
1.135     albertel  485:     my $award;
1.136     albertel  486:     my $msg;
                    487:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135     albertel  488:     if ($result eq '' ) {
                    489: 	my $blankcount;
1.136     albertel  490: 	foreach $award (@$awardref) {
1.135     albertel  491: 	    if ($award eq '') {
                    492: 		$result='MISSING_ANSWER';
                    493: 		$blankcount++;
                    494: 	    }
                    495: 	}
1.136     albertel  496: 	if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
1.135     albertel  497:     }
1.136     albertel  498:     if (defined($result)) { return ($result,$msg); }
1.181     albertel  499: 
                    500:     # these awards are ordered from most important error through best correct
                    501:     
1.182     albertel  502:     my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
                    503: 		  'TOO_LONG',
1.181     albertel  504: 		  'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
                    505: 		  'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
                    506: 		  'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
                    507: 		  'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
                    508: 		  'INVALID_FILETYPE', 'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
                    509: 		  'APPROX_ANS', 'EXACT_ANS');
                    510:     if ($reverse) { @awards=reverse(@awards); }
                    511:     foreach my $possibleaward (@awards) {
1.136     albertel  512: 	($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
                    513: 	if (defined($result)) { return ($result,$msg); }
1.135     albertel  514:     }
1.136     albertel  515:     return ('ERROR',undef);
1.9       albertel  516: }
                    517: 
1.10      albertel  518: sub decideoutput {
1.169     albertel  519:     my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
1.135     albertel  520:     my $message='';
                    521:     my $button=0;
                    522:     my $previousmsg;
                    523:     my $bgcolor='orange';
1.148     albertel  524:     my $added_computer_text=0;
1.135     albertel  525:     my %possiblecolors =
                    526: 	( 'correct' => '#aaffaa',
                    527: 	  'charged_try' => '#ffaaaa',
                    528: 	  'not_charged_try' => '#ffffaa',
                    529: 	  'no_message' => '#fffff',
                    530: 	  );
1.169     albertel  531: 
1.180     albertel  532:     my $part = $Apache::inputtags::part;
                    533:     my $handgrade = 
                    534: 	('yes' eq lc(&Apache::lonnet::EXT("resource.$part.handgrade")));
                    535:     
                    536:     my $computer = ($handgrade)? ''
                    537: 	                       : " ".&mt("Computer's answer now shown above.");
                    538:     &Apache::lonxml::debug("handgrade has :$handgrade:");
                    539: 
1.135     albertel  540:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
                    541:     
                    542:     if      ($solved =~ /^correct/) {
1.169     albertel  543: 	$bgcolor=$possiblecolors{'correct'};
1.170     albertel  544: 	$message=&mt('You are correct.');
                    545: 	if ($awarded < 1 && $awarded > 0) {
                    546: 	    $message=&mt('You are partially correct.');
                    547: 	    $bgcolor=$possiblecolors{'not_charged_try'};
                    548: 	} elsif ($awarded < 1) {
                    549: 	    $message=&mt('Incorrect.');
                    550: 	    $bgcolor=$possiblecolors{'charged_try'};
                    551: 	}
1.172     albertel  552: 	if ($env{'request.filename'} =~ 
                    553: 	    m|/res/lib/templates/examupload.problem$|) {
                    554: 	    $message = &mt("A score has been assigned.");
                    555: 	    $added_computer_text=1;
1.135     albertel  556: 	} else {
1.172     albertel  557: 	    if ($target eq 'tex') {
                    558: 		$message = '\textbf{'.$message.'}';
                    559: 	    } else {
                    560: 		$message = "<b>".$message."</b>";
1.180     albertel  561: 		$message.= $computer;
1.135     albertel  562: 	    }
1.172     albertel  563: 	    $added_computer_text=1;
1.165     albertel  564: 	    unless ($env{'course.'.
                    565: 			     $env{'request.course.id'}.
1.135     albertel  566: 			     '.disable_receipt_display'} eq 'yes') { 
                    567: 		$message.=(($target eq 'web')?'<br />':' ').
                    568: 		    &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
                    569: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
                    570: 	    }
                    571: 	}
                    572: 	$button=0;
                    573: 	$previousmsg='';
                    574:     } elsif ($solved =~ /^excused/) {
                    575: 	if ($target eq 'tex') {
                    576: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
                    577: 	} else {
                    578: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
                    579: 	}
                    580: 	$bgcolor=$possiblecolors{'charged_try'};
                    581: 	$button=0;
                    582: 	$previousmsg='';
                    583:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                    584: 	if ($solved =~ /^incorrect/ || $solved eq '') {
1.144     albertel  585: 	    $message = &mt("Incorrect").".";
1.135     albertel  586: 	    $bgcolor=$possiblecolors{'charged_try'};
                    587: 	    $button=1;
                    588: 	} else {
1.144     albertel  589: 	    if ($target eq 'tex') {
                    590: 		$message = '\textbf{'.&mt('You are correct.').'}';
                    591: 	    } else {
                    592: 		$message = "<b>".&mt('You are correct.')."</b>";
1.180     albertel  593: 		$message.= $computer;
1.144     albertel  594: 	    }
1.148     albertel  595: 	    $added_computer_text=1;
1.165     albertel  596: 	    unless ($env{'course.'.
                    597: 			     $env{'request.course.id'}.
1.135     albertel  598: 			     '.disable_receipt_display'} eq 'yes') { 
                    599: 		$message.=(($target eq 'web')?'<br />':' ').
                    600: 		    'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
                    601: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
                    602: 	    }
                    603: 	    $bgcolor=$possiblecolors{'correct'};
                    604: 	    $button=0;
                    605: 	    $previousmsg='';
                    606: 	}
                    607:     } elsif ($award eq 'NO_RESPONSE') {
                    608: 	$message = '';
                    609: 	$bgcolor=$possiblecolors{'no_feedback'};
                    610: 	$button=1;
1.182     albertel  611:     } elsif ($award eq 'EXTRA_ANSWER') {
                    612: 	$message = &mt('Some extra items were submitted.');
                    613: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    614: 	$button = 1;
1.135     albertel  615:     } elsif ($award eq 'MISSING_ANSWER') {
                    616: 	$message = &mt('Some items were not submitted.');
                    617: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    618: 	$button = 1;
                    619:     } elsif ($award eq 'ERROR') {
                    620: 	$message = &mt('An error occured while grading your answer.');
                    621: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    622: 	$button = 1;
                    623:     } elsif ($award eq 'TOO_LONG') {
                    624: 	$message = &mt("The submitted answer was too long.");
                    625: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    626: 	$button=1;
                    627:     } elsif ($award eq 'WANTED_NUMERIC') {
                    628: 	$message = &mt("This question expects a numeric answer.");
                    629: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    630: 	$button=1;
                    631:     } elsif ($award eq 'MISORDERED_RANK') {
                    632: 	$message = &mt('You have provided an invalid ranking');
                    633: 	if ($target ne 'tex') {
1.159     albertel  634: 	    $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
1.135     albertel  635: 	}
                    636: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    637: 	$button=1;
                    638:     } elsif ($award eq 'INVALID_FILETYPE') {
1.166     albertel  639: 	$message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
1.135     albertel  640: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    641: 	$button=1;
                    642:     } elsif ($award eq 'SIG_FAIL') {
1.145     albertel  643: 	my ($used,$min,$max)=split(':',$awardmsg);
                    644: 	my $word;
                    645: 	if ($used < $min) { $word=&mt('more'); }
                    646: 	if ($used > $max) { $word=&mt('fewer'); }
                    647: 	$message = &mt("Submission not graded.  Use [_2] digits.",$used,$word);
1.135     albertel  648: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    649: 	$button=1;
1.137     albertel  650:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
                    651: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
                    652: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
                    653: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    654: 	$button=1;
                    655:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155     albertel  656: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137     albertel  657: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
                    658: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    659: 	$button=1;
1.140     matthew   660:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155     albertel  661: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136     albertel  662: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.135     albertel  663: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    664: 	$button=1;
                    665:     } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155     albertel  666: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.135     albertel  667: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    668: 	$button=1;
                    669:     } elsif ($award eq 'NO_UNIT') {
1.144     albertel  670: 	$message = &mt("Units required").'.';
1.135     albertel  671: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
                    672: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    673: 	$button=1;
1.153     albertel  674:     } elsif ($award eq 'COMMA_FAIL') {
                    675: 	$message = &mt("Proper comma separation is required").'.';
                    676: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    677: 	$button=1;
1.135     albertel  678:     } elsif ($award eq 'BAD_FORMULA') {
                    679: 	$message = &mt("Unable to understand formula");
                    680: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    681: 	$button=1;
                    682:     } elsif ($award eq 'INCORRECT') {
1.144     albertel  683: 	$message = &mt("Incorrect").'.';
1.135     albertel  684: 	$bgcolor=$possiblecolors{'charged_try'};
                    685: 	$button=1;
                    686:     } elsif ($award eq 'SUBMITTED') {
                    687: 	$message = &mt("Your submission has been recorded.");
                    688: 	$bgcolor=$possiblecolors{'correct'};
                    689: 	$button=1;
                    690:     } elsif ($award eq 'DRAFT') {
1.144     albertel  691: 	$message = &mt("A draft copy has been saved.");
1.135     albertel  692: 	$bgcolor=$possiblecolors{'not_charged_try'};
                    693: 	$button=1;
                    694:     } elsif ($award eq 'ASSIGNED_SCORE') {
1.144     albertel  695: 	$message = &mt("A score has been assigned.");
1.135     albertel  696: 	$bgcolor=$possiblecolors{'correct'};
                    697: 	$button=0;
1.144     albertel  698:     } elsif ($award eq '') {
1.186     albertel  699: 	if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
                    700: 	    $message = &mt("Nothing submitted.");
                    701: 	    $bgcolor=$possiblecolors{'charged_try'};
                    702: 	} else {
                    703: 	    $bgcolor=$possiblecolors{'not_charged_try'};
                    704: 	}
1.144     albertel  705: 	$button=1;
1.135     albertel  706:     } else {
                    707: 	$message = &mt("Unknown message").": $award";
                    708: 	$button=1;
                    709:     }
                    710:     if (lc($Apache::lonhomework::problemstatus) eq 'no'  && 
                    711: 	$Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
                    712: 	$message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
                    713: 	$bgcolor=$possiblecolors{'correct'};
                    714: 	$button=1;
                    715:     }
1.148     albertel  716:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
1.150     albertel  717: 	!$added_computer_text && $target ne 'tex') {
1.180     albertel  718: 	$message.= $computer;
1.148     albertel  719: 	$added_computer_text=1;
1.144     albertel  720:     }
1.135     albertel  721:     return ($button,$bgcolor,$message,$previousmsg);
1.12      albertel  722: }
                    723: 
1.155     albertel  724: sub markup_unit {
                    725:     my ($unit,$target)=@_;
                    726:     if ($target eq 'tex') {
                    727: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
                    728:     } else {
                    729: 	return "<tt>".$unit."</tt>";
                    730:     }
                    731: }
                    732: 
1.88      albertel  733: sub removealldata {
1.87      albertel  734:     my ($id)=@_;
                    735:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    736: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                    737: 	    &Apache::lonxml::debug("Removing $key");
                    738: 	    delete($Apache::lonhomework::results{$key});
                    739: 	}
                    740:     }
                    741: }
                    742: 
1.142     albertel  743: sub hidealldata {
                    744:     my ($id)=@_;
                    745:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    746: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                    747: 	    &Apache::lonxml::debug("Hidding $key");
                    748: 	    my $newkey=$key;
                    749: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
                    750: 	    $Apache::lonhomework::results{$newkey}=
                    751: 		$Apache::lonhomework::results{$key};
                    752: 	    delete($Apache::lonhomework::results{$key});
                    753: 	}
                    754:     }
                    755: }
                    756: 
1.12      albertel  757: sub setgradedata {
1.136     albertel  758:     my ($award,$msg,$id,$previously_used) = @_;
1.154     albertel  759:     if ($Apache::lonhomework::scantronmode && 
1.165     albertel  760: 	&Apache::lonnet::validCODE($env{'form.CODE'})) {
                    761: 	$Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154     albertel  762:     } elsif ($Apache::lonhomework::scantronmode && 
1.165     albertel  763: 	     $env{'form.CODE'} eq '' &&
1.154     albertel  764: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
                    765: 	$Apache::lonhomework::results{"resource.CODE"}='';
1.141     albertel  766:     }
1.154     albertel  767: 
1.135     albertel  768:     if (!$Apache::lonhomework::scantronmode &&
                    769: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                    770: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                    771: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87      albertel  772: 	return '';
1.135     albertel  773:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
                    774: 	      /^correct/ || $Apache::lonhomework::scantronmode ||
                    775: 	      lc($Apache::lonhomework::problemstatus) eq 'no') {
1.154     albertel  776:         # the student doesn't already have it correct,
                    777: 	# or we are in a mode (scantron orno problem status) where a correct 
                    778:         # can become incorrect
                    779: 	# handle assignment of tries and solved status
1.135     albertel  780: 	my $solvemsg;
                    781: 	if ($Apache::lonhomework::scantronmode) {
                    782: 	    $solvemsg='correct_by_scantron';
                    783: 	} else {
                    784: 	    $solvemsg='correct_by_student';
                    785: 	}
                    786: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    787: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                    788: 	}
                    789: 	if ( $award eq 'ASSIGNED_SCORE') {
                    790: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                    791: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    792: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                    793: 		$solvemsg;
                    794: 	    my $numawards=scalar(@Apache::inputtags::response);
                    795: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                    796: 	    foreach my $res (@Apache::inputtags::response) {
                    797: 		$Apache::lonhomework::results{"resource.$id.awarded"}+=
                    798: 		    $Apache::lonhomework::results{"resource.$id.$res.awarded"};
                    799: 	    }
                    800: 	    if ($numawards > 0) {
                    801: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
                    802: 		    $numawards;
                    803: 	    }
                    804: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                    805: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                    806: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    807: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                    808: 		$solvemsg;
                    809: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                    810: 	} elsif ( $award eq 'INCORRECT' ) {
                    811: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                    812: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.152     albertel  813: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
                    814: 		$Apache::lonhomework::scantronmode) {
1.135     albertel  815: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                    816: 	    }
                    817: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                    818: 		'incorrect_attempted';
                    819: 	} elsif ( $award eq 'SUBMITTED' ) {
                    820: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                    821: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    822: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                    823: 		'ungraded_attempted';
                    824: 	} elsif ( $award eq 'DRAFT' ) {
                    825: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
                    826: 	} elsif ( $award eq 'NO_RESPONSE' ) {
                    827: 	    #no real response so delete any data that got stored
1.129     albertel  828: 	    &removealldata($id);
                    829: 	    return '';
                    830: 	} else {
1.135     albertel  831: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                    832: 		'incorrect_attempted';
1.152     albertel  833: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
                    834: 		$Apache::lonhomework::scantronmode) {
1.135     albertel  835: 		$Apache::lonhomework::results{"resource.$id.tries"} =
                    836: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    837: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                    838: 	    }
                    839: 	}
1.136     albertel  840: 	if (defined($msg)) {
                    841: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
                    842: 	}
1.135     albertel  843: 	# did either of the overall awards chage? If so ignore the 
                    844: 	# previous check
                    845: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
                    846: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
                    847: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
                    848: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
                    849: 	    # check if this was a previous submission if it was delete the
                    850: 	    # unneeded data and update the previously_used attribute
                    851: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
                    852: 		if (lc($Apache::lonhomework::problemstatus) ne 'no') {
                    853: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
                    854: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                    855: 		}
                    856: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
                    857: 		#delete all data as they student didn't do anything, but save
                    858: 		#the list of collaborators.
                    859: 		&removealldata($id);
                    860: 		#and since they didn't do anything we were never here
                    861: 		return '';
                    862: 	    } else {
                    863: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
                    864: 	    }
1.101     albertel  865: 	}
1.135     albertel  866:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
                    867: 	      /^correct/ ) {
                    868: 	#delete all data as they student already has it correct
                    869: 	&removealldata($id);
                    870: 	#and since they didn't do anything we were never here
                    871: 	return '';
1.40      albertel  872:     }
1.135     albertel  873:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.184     albertel  874:     if ($award eq 'SUBMITTED') {
                    875: 	&Apache::response::add_to_gradingqueue();
                    876:     }
1.10      albertel  877: }
                    878: 
1.9       albertel  879: sub grade {
1.135     albertel  880:     my ($target) = @_;
                    881:     my $id = $Apache::inputtags::part;
                    882:     my $response='';
1.165     albertel  883:     if ( defined $env{'form.submitted'}) {
1.136     albertel  884: 	my (@awards,@msgs);
1.135     albertel  885: 	foreach $response (@Apache::inputtags::response) {
                    886: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                    887: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
                    888: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
                    889: 	    push (@awards,$value);
1.136     albertel  890: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
                    891: 	    &Apache::lonxml::debug("got message $value from $response for $id");
                    892: 	    push (@msgs,$value);
1.135     albertel  893: 	}
1.136     albertel  894: 	my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
1.135     albertel  895: 	my $previously_used;
                    896: 	if ( $#Apache::inputtags::previous eq $#awards ) {
                    897: 	    my $match=0;
                    898: 	    my @matches;
                    899: 	    foreach my $versionar (@Apache::inputtags::previous_version) {
                    900: 		foreach my $version (@$versionar) {
                    901: 		    $matches[$version]++;
                    902: 		}
                    903: 	    }
                    904: 	    foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
                    905: 	    if ($match) {
                    906: 		$previously_used = 'PREVIOUSLY_LAST';
                    907: 		foreach my $value (@Apache::inputtags::previous) {
                    908: 		    if ($value eq 'PREVIOUSLY_USED' ) {
                    909: 			$previously_used = $value;
                    910: 			last;
                    911: 		    }
1.75      albertel  912: 		}
                    913: 	    }
1.43      albertel  914: 	}
1.136     albertel  915: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
                    916: 	&setgradedata($finalaward,$msg,$id,$previously_used);
1.43      albertel  917:     }
1.135     albertel  918:     return '';
1.1       albertel  919: }
                    920: 
1.11      albertel  921: sub gradestatus {
1.135     albertel  922:     my ($id,$target) = @_;
                    923:     my $showbutton = 1;
                    924:     my $bgcolor = '';
                    925:     my $message = '';
                    926:     my $latemessage = '';
                    927:     my $trystr='';
                    928:     my $button='';
                    929:     my $previousmsg='';
                    930: 
                    931:     my $status = $Apache::inputtags::status['-1'];
                    932:     &Apache::lonxml::debug("gradestatus has :$status:");
1.183     albertel  933:     if ( $status ne 'CLOSED' 
                    934: 	 && $status ne 'UNAVAILABLE' 
                    935: 	 && $status ne 'INVALID_ACCESS' 
                    936: 	 && $status ne 'NEEDS_CHECKIN' 
                    937: 	 && $status ne 'NOT_IN_A_SLOT') {  
1.135     albertel  938: 	my $award = $Apache::lonhomework::history{"resource.$id.award"};
1.169     albertel  939: 	my $awarded = $Apache::lonhomework::history{"resource.$id.awarded"};
1.135     albertel  940: 	my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
                    941: 	my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
1.136     albertel  942: 	my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
                    943: 	&Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
1.144     albertel  944: 	if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
1.135     albertel  945: 	    &Apache::lonxml::debug('Getting message');
                    946: 	    ($showbutton,$bgcolor,$message,$previousmsg) =
1.169     albertel  947: 		&decideoutput($award,$awarded,$awardmsg,$solved,$previous,
                    948: 			      $target);
1.135     albertel  949: 	    if ($target eq 'tex') {
                    950: 		$message='\vskip 2 mm '.$message.' ';
                    951: 	    } else {
                    952: 		$message="<td bgcolor=\"$bgcolor\">$message</td>";
                    953: 		if ($previousmsg) {
                    954: 		    $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
                    955: 		}
                    956: 	    }
                    957: 	}
                    958: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
                    959: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                    960: 	&Apache::lonxml::debug("got maxtries of :$maxtries:");
                    961: 	#if tries are set to negative turn off the Tries/Button and messages
                    962: 	if (defined($maxtries) && $maxtries < 0) { return ''; }
                    963: 	if ( $tries eq '' ) { $tries = '0'; }
                    964: 	if ( $maxtries eq '' ) { $maxtries = '2'; } 
                    965: 	if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                    966: 	my $tries_text=&mt('Tries');
1.164     albertel  967: 	if ( $Apache::lonhomework::type eq 'survey' ||
                    968: 	     $Apache::lonhomework::parsing_a_task) {
                    969: 	    $tries_text=&mt('Submissions');
                    970: 	}
1.135     albertel  971: 	if ( $showbutton ) {
                    972: 	    if ($target eq 'tex') {
1.165     albertel  973: 		if ($env{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $env{'form.suppress_tries'} ne 'yes') {
1.135     albertel  974: 		    $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
                    975: 		} else {
                    976: 		    $trystr = '\vskip 0 mm ';
                    977: 		}
                    978: 	    } else {
1.136     albertel  979: 		$trystr = "<td><nobr>".$tries_text." $tries";
1.164     albertel  980: 		if ($Apache::lonhomework::parsing_a_task) {
1.165     albertel  981: 		} elsif($env{'request.state'} ne 'construct') {
1.135     albertel  982: 		    $trystr.="/$maxtries";
                    983: 		} else {
                    984: 		    if (defined($Apache::inputtags::params{'maxtries'})) {
                    985: 			$trystr.="/".$Apache::inputtags::params{'maxtries'};
                    986: 		    }
                    987: 		}
1.136     albertel  988: 		$trystr.="</nobr></td>";
1.135     albertel  989: 	    }
                    990: 	}
                    991: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
                    992: 	if ( $showbutton ) { 
                    993: 	    if ($target ne 'tex') {
1.156     albertel  994: 		$button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
1.135     albertel  995: 	    }
                    996: 	}
                    997: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    998: 	    #last submissions was after due date
1.163     albertel  999: 	    $latemessage=&mt(' The last submission was after the Due Date ');;
                   1000: 	    if ($target eq 'web') {
                   1001: 		$latemessage='<td bgcolor="#ffaaaa">'.$latemessage.'</td>';
1.135     albertel 1002: 	    }
                   1003: 	}
                   1004:     }
                   1005:     my $output= $previousmsg.$latemessage.$message.$trystr;
                   1006:     if ($output =~ /^\s*$/) {
                   1007: 	return $button;
1.63      sakharuk 1008:     } else {
1.135     albertel 1009: 	if ($target eq 'tex') {
                   1010: 	    return $button.' \vskip 0 mm '.$output.' ';
                   1011: 	} else {
                   1012: 	    return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
                   1013: 	}
1.63      sakharuk 1014:     }
1.11      albertel 1015: }
1.1       albertel 1016: 1;
                   1017: __END__
1.43      albertel 1018:  

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