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

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

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