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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.249   ! jms         4: # $Id: inputtags.pm,v 1.248 2008/11/16 02:46:25 raeburn 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: 
1.249   ! jms        28: =pod
        !            29: 
        !            30: =head1 NAME
        !            31: 
        !            32: Apache::inputtags
        !            33: 
        !            34: =head1 SYNOPSIS
        !            35: 
        !            36: 
        !            37: 
        !            38: This is part of the LearningOnline Network with CAPA project
        !            39: described at http://www.lon-capa.org.
        !            40: 
        !            41: 
        !            42: =head1 NOTABLE SUBROUTINES
        !            43: 
        !            44: =over
        !            45: 
        !            46: =item 
        !            47: 
        !            48: =back
        !            49: 
        !            50: =cut
        !            51: 
1.1       albertel   52: package Apache::inputtags;
1.55      albertel   53: use HTML::Entities();
1.1       albertel   54: use strict;
1.82      www        55: use Apache::loncommon;
1.115     www        56: use Apache::lonlocal;
1.165     albertel   57: use Apache::lonnet;
1.192     www        58: use LONCAPA;
                     59:  
1.1       albertel   60: 
1.50      harris41   61: BEGIN {
1.135     albertel   62:     &Apache::lonxml::register('Apache::inputtags',('hiddenline','textfield','textline'));
1.1       albertel   63: }
                     64: 
1.249   ! jms        65: =pod
        !            66: 
        !            67: =item initialize_inputtags()
        !            68: 
        !            69: Initializes a set of global variables used during the parse of the problem.
        !            70: 
        !            71: @Apache::inputtags::input        - List of current input ids.
        !            72: @Apache::inputtags::inputlist    - List of all input ids seen this problem.
        !            73: @Apache::inputtags::response     - List of all current resopnse ids.
        !            74: @Apache::inputtags::responselist - List of all response ids seen this 
        !            75:                                      problem.
        !            76: @Apache::inputtags::hint         - List of all hint ids.
        !            77: @Apache::inputtags::hintlist     - List of all hint ids seen this problem.
        !            78: @Apache::inputtags::previous     - List describing if specific responseds
        !            79:                                      have been used
        !            80: @Apache::inputtags::previous_version - Submission responses were used in.
        !            81: $Apache::inputtags::part         - Current part id (valid only in 
        !            82:                                      <problem>)
        !            83:                                    0 if not in a part.
        !            84: @Apache::inputtags::partlist     - List of part ids seen in the current
        !            85:                                      <problem>
        !            86: @Apache::inputtags::status       - List of problem  statuses. First 
        !            87:                                    element is the status of the <problem>
        !            88:                                    the remainder are for individual <part>s.
        !            89: %Apache::inputtags::params       - Hash of defined parameters for the
        !            90:                                    current response.
        !            91: @Apache::inputtags::import       - List of all ids for <import> thes get
        !            92:                                    join()ed and prepended.
        !            93: @Apache::inputtags::importlist   - List of all import ids seen.
        !            94: $Apache::inputtags::response_with_no_part
        !            95:                                  - Flag set true if we have seen a response
        !            96:                                    that is not inside a <part>
        !            97: %Apache::inputtags::answertxt    - <*response> tags store correct
        !            98:                                    answer strings for display by <textline/>
        !            99:                                    in this hash.
        !           100: %Apache::inputtags::submission_display
        !           101:                                  - <*response> tags store improved display
        !           102:                                    of submission strings for display by part
        !           103:                                    end.
        !           104: 
        !           105: =cut
1.178     albertel  106: 
1.1       albertel  107: sub initialize_inputtags {
1.135     albertel  108:     @Apache::inputtags::input=();
                    109:     @Apache::inputtags::inputlist=();
1.174     albertel  110:     @Apache::inputtags::response=();
1.135     albertel  111:     @Apache::inputtags::responselist=();
1.174     albertel  112:     @Apache::inputtags::hint=();
1.173     albertel  113:     @Apache::inputtags::hintlist=();
1.135     albertel  114:     @Apache::inputtags::previous=();
                    115:     @Apache::inputtags::previous_version=();
                    116:     $Apache::inputtags::part='';
                    117:     @Apache::inputtags::partlist=();
                    118:     @Apache::inputtags::status=();
                    119:     %Apache::inputtags::params=();
                    120:     @Apache::inputtags::import=();
                    121:     @Apache::inputtags::importlist=();
                    122:     $Apache::inputtags::response_with_no_part=0;
1.144     albertel  123:     %Apache::inputtags::answertxt=();
1.217     albertel  124:     %Apache::inputtags::submission_display=();
1.103     albertel  125: }
                    126: 
                    127: sub check_for_duplicate_ids {
                    128:     my %check;
                    129:     foreach my $id (@Apache::inputtags::partlist,
                    130: 		    @Apache::inputtags::responselist,
1.173     albertel  131: 		    @Apache::inputtags::hintlist,
1.103     albertel  132: 		    @Apache::inputtags::importlist) {
                    133: 	$check{$id}++;
                    134:     }
                    135:     my @duplicates;
                    136:     foreach my $id (sort(keys(%check))) {
                    137: 	if ($check{$id} > 1) {
                    138: 	    push(@duplicates,$id);
                    139: 	}
                    140:     }
                    141:     if (@duplicates) {
                    142: 	&Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
                    143:     }
1.1       albertel  144: }
                    145: 
1.14      albertel  146: sub start_input {
1.135     albertel  147:     my ($parstack,$safeeval)=@_;
1.228     albertel  148:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.135     albertel  149:     push (@Apache::inputtags::input,$id);
                    150:     push (@Apache::inputtags::inputlist,$id);
                    151:     return $id;
1.14      albertel  152: }
                    153: 
                    154: sub end_input {
1.135     albertel  155:     pop @Apache::inputtags::input;
                    156:     return '';
1.14      albertel  157: }
                    158: 
1.124     www       159: sub addchars {
                    160:     my ($fieldid,$addchars)=@_;
                    161:     my $output='';
                    162:     foreach (split(/\,/,$addchars)) {
                    163: 	$output.='<a href="javascript:void(document.forms.lonhomework.'.
                    164: 	    $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
                    165:     }
                    166:     return $output;
                    167: }
                    168: 
1.48      albertel  169: sub start_textfield {
1.185     albertel  170:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.135     albertel  171:     my $result = "";
                    172:     my $id = &start_input($parstack,$safeeval);
                    173:     my $resid=$Apache::inputtags::response[-1];
                    174:     if ($target eq 'web') {
                    175: 	$Apache::lonxml::evaluate--;
1.205     albertel  176: 	my $partid=$Apache::inputtags::part;
                    177: 	my $oldresponse = &HTML::Entities::encode($Apache::lonhomework::history{"resource.$partid.$resid.submission"},'<>&"');
1.135     albertel  178: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    179: 	    my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    180: 	    if ( $cols eq '') { $cols = 80; }
                    181: 	    my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.143     www       182: 	    if ( $rows eq '') { $rows = 16; }
1.135     albertel  183: 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    184: 	    $result='';
                    185: 	    if ($addchars) {
                    186: 		$result.=&addchars('HWVAL_'.$resid,$addchars);
                    187: 	    }
1.191     albertel  188: 	    &Apache::lonhtmlcommon::add_htmlareafields('HWVAL_'.$resid);
1.143     www       189: 	    $result.= '<textarea wrap="hard" name="HWVAL_'.$resid.'" id="HWVAL_'.$resid.'" '.
1.135     albertel  190: 		"rows=\"$rows\" cols=\"$cols\">".$oldresponse;
                    191: 	    if ($oldresponse ne '') {
1.143     www       192: 
1.135     albertel  193: 		#get rid of any startup text if the user has already responded
1.185     albertel  194: 		&Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.135     albertel  195: 	    }
                    196: 	} else {
1.205     albertel  197: 	    #show past answer in the essayresponse case
                    198: 	    if ($oldresponse =~ /\S/
                    199: 		&& &Apache::londefdef::is_inside_of($tagstack,
                    200: 						    'essayresponse') ) {
                    201: 		$result='<table class="LC_pastsubmission"><tr><td>'.
                    202: 		    $oldresponse.'</td></tr></table>';
                    203: 	    }
1.135     albertel  204: 	    #get rid of any startup text
1.185     albertel  205: 	    &Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.61      albertel  206: 	}
1.135     albertel  207:     } elsif ($target eq 'grade') {
1.185     albertel  208: 	my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    209: 						   $style);
1.165     albertel  210: 	if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
1.135     albertel  211: 	    # if the seed text is still there it wasn't a real submission
1.165     albertel  212: 	    $env{'form.HWVAL_'.$resid}='';
1.135     albertel  213: 	}
                    214:     } elsif ($target eq 'edit') {
                    215: 	$result.=&Apache::edit::tag_start($target,$token);
                    216: 	$result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
                    217: 	$result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
                    218: 	$result.=&Apache::edit::text_arg
                    219: 	    ('Click-On Texts (comma sep):','addchars',$token,10);
1.185     albertel  220: 	my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    221: 						   $style);
1.135     albertel  222: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
                    223:     } elsif ($target eq 'modified') {
                    224: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    225: 						     $safeeval,'rows','cols',
                    226: 						     'addchars');
                    227: 	if ($constructtag) {
                    228: 	    $result = &Apache::edit::rebuild_tag($token);
                    229: 	} else {
                    230: 	    $result=$token->[4];
                    231: 	}
                    232: 	$result.=&Apache::edit::modifiedfield("/textfield",$parser);
                    233:     } elsif ($target eq 'tex') {
                    234: 	my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
                    235: 	my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    236: 	if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
                    237: 	    $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
                    238: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    239: 	    $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
                    240: 	} else {
                    241: 	    my $TeXwidth=$width_of_box/80;
                    242: 	    $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
                    243: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    244: 	    $result.='}}}\vskip 2 mm ';
                    245: 	}
1.60      albertel  246:     }
1.135     albertel  247:     return $result;
1.6       albertel  248: }
                    249: 
1.48      albertel  250: sub end_textfield {
1.135     albertel  251:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    252:     my $result;
                    253:     if ($target eq 'web') {
                    254: 	$Apache::lonxml::evaluate++;
                    255: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    256: 	    return "</textarea>";
                    257: 	}
                    258:     } elsif ($target eq 'edit') {
                    259: 	$result=&Apache::edit::end_table();
                    260:     }
                    261:     &end_input;
                    262:     return $result;
1.6       albertel  263: }
                    264: 
1.190     albertel  265: sub exam_score_line {
1.188     albertel  266:     my ($target) = @_;
1.190     albertel  267: 
1.188     albertel  268:     my $result;
                    269:     if ($target eq 'tex') {
                    270: 	my $repetition = &Apache::response::repetition();
                    271: 	$result.='\begin{enumerate}';
1.190     albertel  272: 	if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
1.188     albertel  273: 	foreach my $i (0..$repetition-1) {
                    274: 	    $result.='\item[\textbf{'.
                    275: 		($Apache::lonxml::counter+$i).
                    276: 		'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
                    277: 	}
                    278: 	$result.= '\end{enumerate}';
1.190     albertel  279:     }
                    280: 
                    281:     return $result;
                    282: }
                    283: 
                    284: sub exam_box {
                    285:     my ($target) = @_;
                    286:     my $result;
1.188     albertel  287: 
1.190     albertel  288:     if ($target eq 'tex') {
                    289: 	$result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
                    290: 	$result .= &exam_score_line($target);
1.188     albertel  291:     } elsif ($target eq 'web') {
                    292: 	my $id=$Apache::inputtags::response[-1];
                    293: 	$result.= '<br /><br />
                    294:                    <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
                    295:                    </textarea> <br /><br />';
                    296:     }
                    297:     return $result;
                    298: }
                    299: 
                    300: sub needs_exam_box {
                    301:     my ($tagstack) = @_;
                    302:     my @tags = ('formularesponse',
                    303: 		'stringresponse',
                    304: 		'reactionresponse',
                    305: 		'organicresponse',
                    306: 		);
                    307: 
                    308:     foreach my $tag (@tags) {
                    309: 	if (grep(/\Q$tag\E/,@$tagstack)) {
                    310: 	    return 1;
                    311: 	}
                    312:     }
                    313:     return 0;
                    314: }
                    315: 
1.1       albertel  316: sub start_textline {
1.135     albertel  317:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    318:     my $result = "";
1.210     albertel  319:     my $input_id = &start_input($parstack,$safeeval);
1.135     albertel  320:     if ($target eq 'web') {
                    321: 	$Apache::lonxml::evaluate--;
                    322: 	my $partid=$Apache::inputtags::part;
                    323: 	my $id=$Apache::inputtags::response[-1];
1.204     albertel  324: 	if (!&Apache::response::show_answer()) {
1.135     albertel  325: 	    my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    326: 	    my $maxlength;
                    327: 	    if ($size eq '') { $size=20; } else {
1.214     albertel  328: 		if ($size < 20) {
                    329: 		    $maxlength = ' maxlength="'.$size.'"';
                    330: 		}
1.135     albertel  331: 	    }
1.210     albertel  332: 	    my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    333: 	    &Apache::lonxml::debug("oldresponse $oldresponse is ".ref($oldresponse));
                    334: 
                    335: 	    if (ref($oldresponse) eq 'ARRAY') {
                    336: 		$oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
                    337: 	    }
                    338: 	    $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
1.241     www       339:             $oldresponse =~ s/^\s+//;
                    340:             $oldresponse =~ s/\s+$//;
                    341:             $oldresponse =~ s/\s+/ /g;
1.135     albertel  342: 	    if ($Apache::lonhomework::type ne 'exam') {
                    343: 		my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    344: 		$result='';
                    345: 		if ($addchars) {
                    346: 		    $result.=&addchars('HWVAL_'.$id,$addchars);
                    347: 		}
1.157     albertel  348: 		my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
                    349: 							$safeeval);
1.193     albertel  350: 		if (lc($readonly) eq 'yes' 
                    351: 		    || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
1.157     albertel  352: 		    $readonly=' readonly="readonly" ';
1.158     albertel  353: 		} else {
                    354: 		    $readonly='';
1.157     albertel  355: 		}
1.193     albertel  356: 		my $name = 'HWVAL_'.$id;
                    357: 		if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
                    358: 		    $name = "none";
                    359: 		}
1.214     albertel  360: 		$result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');" type="text" '.$readonly.' name="'.$name.'" value="'.
                    361: 		    $oldresponse.'" size="'.$size.'"'.$maxlength.' />';
1.135     albertel  362: 	    }
1.188     albertel  363: 	    if ($Apache::lonhomework::type eq 'exam'
                    364: 		&& &needs_exam_box($tagstack)) {
                    365: 		$result.=&exam_box($target);
                    366: 	    }
1.135     albertel  367: 	} else {
                    368: 	    #right or wrong don't show what was last typed in.
1.208     albertel  369: 	    my $count = scalar(@Apache::inputtags::inputlist)-1;
                    370: 	    $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
1.144     albertel  371: 	    #$result='';
1.135     albertel  372: 	}
                    373:     } elsif ($target eq 'edit') {
                    374: 	$result=&Apache::edit::tag_start($target,$token);
                    375: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157     albertel  376: 	    &Apache::edit::text_arg('Click-On Texts (comma sep):',
                    377: 				    'addchars',$token,10);
                    378:         $result.=&Apache::edit::select_arg('Readonly:','readonly',
                    379: 					   ['no','yes'],$token);
                    380: 	$result.=&Apache::edit::end_row();
                    381: 	$result.=&Apache::edit::end_table();
1.135     albertel  382:     } elsif ($target eq 'modified') {
1.157     albertel  383: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    384: 						     $safeeval,'size',
                    385: 						     'addchars','readonly');
1.135     albertel  386: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.188     albertel  387:     } elsif ($target eq 'tex' 
                    388: 	     && $Apache::lonhomework::type ne 'exam') {
1.135     albertel  389: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    390: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
1.246     onken     391: 	if ($env{'form.pdfFormFields'} eq 'yes') {
                    392:             my $fieldname = $env{'request.symb'}.
                    393:                                  '&part_'. $Apache::inputtags::part.
                    394:                                  '&textresponse'.
                    395:                                  '&HWVAL_' . $Apache::inputtags::response['-1'];
                    396:             $result="\n\\\\\n".'\textField{'.$fieldname.'}{'.$size.'}{12 bp}';
                    397:         } else {
                    398:             $result='\framebox['.$size.'][s]{\tiny\strut}';
                    399:         }
1.188     albertel  400:     } elsif ($target eq 'tex' 
                    401: 	     && $Apache::lonhomework::type eq 'exam'
                    402: 	     && &needs_exam_box($tagstack)) {
                    403: 	$result.=&exam_box($target);
1.135     albertel  404:     }
                    405:     return $result;
1.1       albertel  406: }
                    407: 
                    408: sub end_textline {
1.135     albertel  409:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    410:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    411:     elsif ($target eq 'edit') { return ('','no'); }
1.208     albertel  412:     &end_input();
1.135     albertel  413:     return "";
1.9       albertel  414: }
                    415: 
1.98      albertel  416: sub start_hiddenline {
                    417:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    418:     my $result = "";
1.211     albertel  419:     my $input_id = &start_input($parstack,$safeeval);
1.98      albertel  420:     if ($target eq 'web') {
                    421: 	$Apache::lonxml::evaluate--;
                    422: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    423: 	    my $partid=$Apache::inputtags::part;
                    424: 	    my $id=$Apache::inputtags::response[-1];
1.211     albertel  425: 	    my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    426: 	    if (ref($oldresponse) eq 'ARRAY') {
                    427: 		$oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
                    428: 	    }
                    429: 	    $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
                    430: 
1.98      albertel  431: 	    if ($Apache::lonhomework::type ne 'exam') {
                    432: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
                    433: 		    $oldresponse.'" />';
                    434: 	    }
                    435: 	}
                    436:     } elsif ($target eq 'edit') {
                    437: 	$result=&Apache::edit::tag_start($target,$token);
                    438: 	$result.=&Apache::edit::end_table;
                    439:     }
1.189     albertel  440: 
                    441:     if ( ($target eq 'web' || $target eq 'tex')
                    442: 	 && $Apache::lonhomework::type eq 'exam'
                    443: 	 && &needs_exam_box($tagstack)) {
                    444: 	$result.=&exam_box($target);
                    445:     }
1.98      albertel  446:     return $result;
                    447: }
                    448: 
                    449: sub end_hiddenline {
1.135     albertel  450:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    451:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    452:     elsif ($target eq 'edit') { return ('','no'); }
1.211     albertel  453:     &end_input();
1.135     albertel  454:     return "";
1.98      albertel  455: }
                    456: 
1.249   ! jms       457: =pod
        !           458: 
        !           459: =item file_selector()
        !           460: 
        !           461: $part -> partid
        !           462: $id -> responseid
        !           463: $uploadefiletypes -> comma seperated list of extensions allowed or * for any
        !           464: $which -> 'uploadedonly'  -> only newly uploaded files
        !           465:           'portfolioonly' -> only allow files from portfolio
        !           466:           'both' -> allow files from either location
        !           467: $extratext -> additional text to go between the link and the input box
        !           468: returns a table row <tr> 
        !           469: 
        !           470: =cut
        !           471: 
1.160     albertel  472: sub file_selector {
1.175     albertel  473:     my ($part,$id,$uploadedfiletypes,$which,$extratext)=@_;
1.160     albertel  474:     if (!$uploadedfiletypes) { return ''; }
1.167     albertel  475: 
                    476:     my $jspart=$part;
                    477:     $jspart=~s/\./_/g;
                    478: 
1.160     albertel  479:     my $result;
                    480:     
1.162     albertel  481:     $result.='<tr><td>';
                    482:     if ($uploadedfiletypes ne '*') {
                    483: 	$result.=
                    484: 	    &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
                    485:     }
1.160     albertel  486:     if ($which eq 'uploadonly' || $which eq 'both') { 
                    487: 	$result.=&mt('Submit a file: (only one file can be uploaded)').
                    488: 	    ' <br /><input type="file" size="50" name="HWFILE'.
1.167     albertel  489: 	    $jspart.'_'.$id.'" /><br />';
1.205     albertel  490: 	$result .= &show_past_file_submission($part,$id);
1.160     albertel  491:     }
                    492:     if ( $which eq 'both') { 
                    493: 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
                    494:     }
                    495:     if ($which eq 'portfolioonly' || $which eq 'both') { 
1.227     albertel  496: 	$result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&amp;fieldname='.$env{'form.request.prefix'}.'HWPORT'.$jspart.'_'.$id.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
1.160     albertel  497: 	    &mt('Select Portfolio Files').'</a><br />'.
1.167     albertel  498: 	    '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
1.160     albertel  499: 	    '<br />';
1.205     albertel  500: 	$result .= &show_past_portfile_submission($part,$id);
                    501: 
1.160     albertel  502:     }
                    503:     $result.='</td></tr>'; 
                    504:     return $result;
                    505: }
                    506: 
1.205     albertel  507: sub show_past_file_submission {
                    508:     my ($part,$id) = @_;
                    509:     my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
                    510: 
                    511:     return if (!$uploadedfile);
                    512: 
                    513:     my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
                    514:     &Apache::lonxml::extlink($url);
                    515:     &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
                    516:     my $icon=&Apache::loncommon::icon($url);
                    517:     my $curfile='<a href="'.$url.'"><img src="'.$icon.
                    518: 	'" border="0" />'.$uploadedfile.'</a>';
                    519:     return &mt('Currently submitted: <tt>[_1]</tt>',$curfile);
                    520: 
                    521: }
                    522: 
                    523: sub show_past_portfile_submission {
                    524:     my ($part,$id) = @_;
                    525:     if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}!~/[^\s]/){
                    526: 	return;
                    527:     }
                    528:     my (@file_list,@bad_file_list);
                    529:     foreach my $file (split(/\s*,\s*/,&unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
1.209     albertel  530: 	my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1.205     albertel  531: 	my $url="/uploaded/$domain/$user/portfolio$file";
                    532: 	my $icon=&Apache::loncommon::icon($url);
                    533: 	push(@file_list,'<a href="'.$url.'"><img src="'.$icon.
                    534: 	     '" border="0" />'.$file.'</a>');
                    535: 	if (! &Apache::lonnet::stat_file($url)) {
                    536: 	    &Apache::lonnet::logthis("bad file is $url");
                    537: 	    push(@bad_file_list,'<a href="'.$url.'"><img src="'.$icon.
                    538: 		 '" border="0" />'.$file.'</a>');
                    539: 	}
                    540:     }
                    541:     my $files = '<span class="LC_filename">'.
                    542: 	join('</span>, <span class="LC_filename">',@file_list).
                    543: 	'</span>';
                    544:     my $result = &mt("Portfolio files previously selected: [_1]",$files);
                    545:     if (@bad_file_list) {
                    546: 	my $bad_files = '<span class="LC_filename">'.
                    547: 	    join('</span>, <span class="LC_filename">',@bad_file_list).
                    548: 	    '</span>';
                    549: 	$result.='<br />'.&mt('<span class="LC_error">These file(s) don\'t exist:</span> [_1]',$bad_files);
                    550:     }
                    551:     return $result;
                    552: 
                    553: }
                    554: 
1.179     albertel  555: sub valid_award {
                    556:     my ($award) =@_;
1.182     albertel  557:     foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
                    558: 			       'NO_RESPONSE',
1.179     albertel  559: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
                    560: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
                    561: 			       'UNIT_FAIL', 'NO_UNIT',
                    562: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
                    563: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
                    564: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
1.248     raeburn   565:                                'EXCESS_FILESIZE', 'DRAFT',
                    566: 			       'SUBMITTED', 'ASSIGNED_SCORE',
1.179     albertel  567: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
                    568: 	if ($award eq $possibleaward) { return 1; }
                    569:     }
                    570:     return 0;
                    571: }
                    572: 
1.207     albertel  573: {
                    574:     my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
                    575: 		  'TOO_LONG',
                    576: 		  'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
                    577: 		  'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
                    578: 		  'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',
                    579: 		  'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
1.248     raeburn   580: 		  'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'DRAFT', 'SUBMITTED',
                    581:                   'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
1.207     albertel  582:     my $i=0;
                    583:     my %fwd_awards = map { ($_,$i++) } @awards;
                    584:     my $max=scalar(@awards);
                    585:     @awards=reverse(@awards);
1.208     albertel  586:     $i=0;
1.207     albertel  587:     my %rev_awards = map { ($_,$i++) } @awards;
                    588: 
1.232     albertel  589: sub awarddetail_to_awarded {
                    590:     my ($awarddetail) = @_;
                    591:     if ($awarddetail eq 'EXACT_ANS'
                    592: 	|| $awarddetail eq 'APPROX_ANS') {
                    593: 	return 1;
                    594:     }
                    595:     return 0;
                    596: }
                    597: 
1.233     albertel  598: sub hide_award {
                    599:     my ($award) = @_;
                    600:     if (&Apache::lonhomework::show_no_problem_status()) {
                    601: 	return 1;
                    602:     }
                    603:     if ($award =~
                    604: 	/^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|ASSIGNED_SCORE|INCORRECT)/) {
                    605: 	return 1;
                    606:     }
                    607:     return 0;
                    608: }
                    609: 
1.9       albertel  610: sub finalizeawards {
1.232     albertel  611:     my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
1.207     albertel  612:     my $result;
1.136     albertel  613:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135     albertel  614:     if ($result eq '' ) {
                    615: 	my $blankcount;
1.207     albertel  616: 	foreach my $award (@$awardref) {
1.135     albertel  617: 	    if ($award eq '') {
                    618: 		$result='MISSING_ANSWER';
                    619: 		$blankcount++;
                    620: 	    }
                    621: 	}
1.232     albertel  622: 	if ($blankcount == ($#$awardref + 1)) {
                    623: 	    return ('NO_RESPONSE');
                    624: 	}
1.135     albertel  625:     }
1.232     albertel  626:     if (!$final_scantron && defined($result)) { return ($result); }
1.181     albertel  627: 
1.232     albertel  628:     # if in scantron mode, if the award for any response is 
                    629:     # assigned score, then the part gets an assigned score
                    630:     if ($final_scantron 
                    631: 	&& grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
                    632: 	return ('ASSIGNED_SCORE');
                    633:     }
                    634: 
                    635:     # if in scantron mode, if the award for any response is 
                    636:     # correct and there are non-correct responses,
                    637:     # then the part gets an assigned score
                    638:     if ($final_scantron 
                    639: 	&& (grep { $_ eq 'EXACT_ANS' ||
                    640: 		   $_ eq 'APPROX_ANS'  } (@$awardref))
                    641: 	&& (grep { $_ ne 'EXACT_ANS' &&
                    642: 		   $_ ne 'APPROX_ANS'  } (@$awardref))) {
                    643: 	return ('ASSIGNED_SCORE');
                    644:     }
1.181     albertel  645:     # these awards are ordered from most important error through best correct
1.207     albertel  646:     my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
                    647: 
                    648:     my $best = $max;
                    649:     my $j=0;
                    650:     my $which;
                    651:     foreach my $award (@$awardref) {
                    652: 	if ($awards->{$award} < $best) {
                    653: 	    $best  = $awards->{$award};
                    654: 	    $which = $j;
                    655: 	}
                    656: 	$j++;
                    657:     }
1.232     albertel  658: 
1.207     albertel  659:     if (defined($which)) {
                    660: 	if (ref($nameref)) {
                    661: 	    return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
                    662: 	} else {
                    663: 	    return ($$awardref[$which],$$msgref[$which]);
                    664: 	}
1.135     albertel  665:     }
1.136     albertel  666:     return ('ERROR',undef);
1.9       albertel  667: }
1.207     albertel  668: }
1.9       albertel  669: 
1.10      albertel  670: sub decideoutput {
1.169     albertel  671:     my ($award,$awarded,$awardmsg,$solved,$previous,$target)=@_;
1.135     albertel  672:     my $message='';
                    673:     my $button=0;
                    674:     my $previousmsg;
1.221     albertel  675:     my $css_class='orange';
1.148     albertel  676:     my $added_computer_text=0;
1.221     albertel  677:     my %possible_class =
                    678: 	( 'correct'         => 'LC_answer_correct',
                    679: 	  'charged_try'     => 'LC_answer_charged_try',
                    680: 	  'not_charged_try' => 'LC_answer_not_charged_try',
                    681: 	  'no_grade'        => 'LC_answer_no_grade',
                    682: 	  'no_message'      => 'LC_no_message',
1.135     albertel  683: 	  );
1.169     albertel  684: 
1.180     albertel  685:     my $part = $Apache::inputtags::part;
1.236     raeburn   686:     my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
                    687:     my $handgrade = ('yes' eq lc($tohandgrade)); 
1.180     albertel  688:     
                    689:     my $computer = ($handgrade)? ''
1.203     www       690: 	                       : " ".&mt("Computer's answer now shown above.");
1.180     albertel  691:     &Apache::lonxml::debug("handgrade has :$handgrade:");
                    692: 
1.135     albertel  693:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
                    694:     
1.194     banghart  695:     if ($solved =~ /^correct/) {
1.221     albertel  696:         $css_class=$possible_class{'correct'};
1.170     albertel  697: 	$message=&mt('You are correct.');
                    698: 	if ($awarded < 1 && $awarded > 0) {
                    699: 	    $message=&mt('You are partially correct.');
1.221     albertel  700: 	    $css_class=$possible_class{'not_charged_try'};
1.170     albertel  701: 	} elsif ($awarded < 1) {
                    702: 	    $message=&mt('Incorrect.');
1.221     albertel  703: 	    $css_class=$possible_class{'charged_try'};
1.170     albertel  704: 	}
1.172     albertel  705: 	if ($env{'request.filename'} =~ 
                    706: 	    m|/res/lib/templates/examupload.problem$|) {
                    707: 	    $message = &mt("A score has been assigned.");
                    708: 	    $added_computer_text=1;
1.135     albertel  709: 	} else {
1.172     albertel  710: 	    if ($target eq 'tex') {
                    711: 		$message = '\textbf{'.$message.'}';
                    712: 	    } else {
                    713: 		$message = "<b>".$message."</b>";
1.180     albertel  714: 		$message.= $computer;
1.135     albertel  715: 	    }
1.172     albertel  716: 	    $added_computer_text=1;
1.235     albertel  717: 	    if ($awarded > 0) {
                    718: 		my ($symb) = &Apache::lonnet::whichuser();
                    719: 		if (($symb ne '') 
                    720: 		    &&
                    721: 		    ($env{'course.'.$env{'request.course.id'}.
1.237     www       722: 			      '.disable_receipt_display'} ne 'yes') &&
                    723:                     ($Apache::lonhomework::type ne 'practice')) { 
1.235     albertel  724: 		    $message.=(($target eq 'web')?'<br />':' ').
                    725: 			&mt('Your receipt is [_1]',
                    726: 			    (&Apache::lonnet::receipt($Apache::inputtags::part).
                    727: 			     (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
                    728: 		}
1.135     albertel  729: 	    }
                    730: 	}
                    731: 	$button=0;
                    732: 	$previousmsg='';
                    733:     } elsif ($solved =~ /^excused/) {
                    734: 	if ($target eq 'tex') {
                    735: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
                    736: 	} else {
                    737: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
                    738: 	}
1.221     albertel  739: 	$css_class=$possible_class{'charged_try'};
1.135     albertel  740: 	$button=0;
                    741: 	$previousmsg='';
                    742:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                    743: 	if ($solved =~ /^incorrect/ || $solved eq '') {
1.144     albertel  744: 	    $message = &mt("Incorrect").".";
1.221     albertel  745: 	    $css_class=$possible_class{'charged_try'};
1.135     albertel  746: 	    $button=1;
                    747: 	} else {
1.144     albertel  748: 	    if ($target eq 'tex') {
                    749: 		$message = '\textbf{'.&mt('You are correct.').'}';
                    750: 	    } else {
                    751: 		$message = "<b>".&mt('You are correct.')."</b>";
1.180     albertel  752: 		$message.= $computer;
1.144     albertel  753: 	    }
1.148     albertel  754: 	    $added_computer_text=1;
1.235     albertel  755: 	    if  ($awarded > 0 
                    756: 		 && $env{'course.'.
1.165     albertel  757: 			     $env{'request.course.id'}.
1.235     albertel  758: 			     '.disable_receipt_display'} ne 'yes') { 
1.135     albertel  759: 		$message.=(($target eq 'web')?'<br />':' ').
1.235     albertel  760: 		    &mt('Your receipt is [_1]',
                    761: 			(&Apache::lonnet::receipt($Apache::inputtags::part).
                    762: 			 (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1.135     albertel  763: 	    }
1.221     albertel  764: 	    $css_class=$possible_class{'correct'};
1.135     albertel  765: 	    $button=0;
                    766: 	    $previousmsg='';
                    767: 	}
                    768:     } elsif ($award eq 'NO_RESPONSE') {
                    769: 	$message = '';
1.221     albertel  770: 	$css_class=$possible_class{'no_feedback'};
1.135     albertel  771: 	$button=1;
1.182     albertel  772:     } elsif ($award eq 'EXTRA_ANSWER') {
                    773: 	$message = &mt('Some extra items were submitted.');
1.221     albertel  774: 	$css_class=$possible_class{'not_charged_try'};
1.182     albertel  775: 	$button = 1;
1.135     albertel  776:     } elsif ($award eq 'MISSING_ANSWER') {
1.245     bisitz    777: 	$message = &mt('Some items were not submitted.');
                    778:         if ($target ne 'tex') {
                    779:            $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
                    780:         }
1.221     albertel  781: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  782: 	$button = 1;
                    783:     } elsif ($award eq 'ERROR') {
1.247     bisitz    784: 	$message = &mt('An error occurred while grading your answer.');
1.221     albertel  785: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  786: 	$button = 1;
                    787:     } elsif ($award eq 'TOO_LONG') {
                    788: 	$message = &mt("The submitted answer was too long.");
1.221     albertel  789: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  790: 	$button=1;
                    791:     } elsif ($award eq 'WANTED_NUMERIC') {
                    792: 	$message = &mt("This question expects a numeric answer.");
1.221     albertel  793: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  794: 	$button=1;
                    795:     } elsif ($award eq 'MISORDERED_RANK') {
1.242     bisitz    796:         $message = &mt('You have provided an invalid ranking.');
                    797:         if ($target ne 'tex') {
                    798:             $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
                    799:         }
1.221     albertel  800: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  801: 	$button=1;
1.248     raeburn   802:     } elsif ($award eq 'EXCESS_FILESIZE') {
                    803:         $message = &mt('Submission won\'t be graded. The combined size of submitted files exceeded the amount allowed.');
                    804:         $css_class=$possible_class{'not_charged_try'};
                    805:         $button=1;
                    806: 
1.135     albertel  807:     } elsif ($award eq 'INVALID_FILETYPE') {
1.166     albertel  808: 	$message = &mt('Submission won\'t be graded. The type of file submitted is not allowed.');
1.221     albertel  809: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  810: 	$button=1;
                    811:     } elsif ($award eq 'SIG_FAIL') {
1.145     albertel  812: 	my ($used,$min,$max)=split(':',$awardmsg);
1.212     albertel  813: 	my $word = ($used < $min) ? 'more' : 'fewer';
                    814: 	$message = &mt("Submission not graded.  Use $word digits.",$used);
1.221     albertel  815: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  816: 	$button=1;
1.137     albertel  817:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
                    818: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
                    819: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel  820: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel  821: 	$button=1;
                    822:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155     albertel  823: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137     albertel  824: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel  825: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel  826: 	$button=1;
1.140     matthew   827:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155     albertel  828: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136     albertel  829: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel  830: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  831: 	$button=1;
                    832:     } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155     albertel  833: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.221     albertel  834: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  835: 	$button=1;
                    836:     } elsif ($award eq 'NO_UNIT') {
1.144     albertel  837: 	$message = &mt("Units required").'.';
1.135     albertel  838: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
1.221     albertel  839: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  840: 	$button=1;
1.153     albertel  841:     } elsif ($award eq 'COMMA_FAIL') {
                    842: 	$message = &mt("Proper comma separation is required").'.';
1.221     albertel  843: 	$css_class=$possible_class{'not_charged_try'};
1.153     albertel  844: 	$button=1;
1.135     albertel  845:     } elsif ($award eq 'BAD_FORMULA') {
1.240     www       846: 	$message = &mt("Unable to understand formula").'.';
                    847:         if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
1.221     albertel  848: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  849: 	$button=1;
                    850:     } elsif ($award eq 'INCORRECT') {
1.144     albertel  851: 	$message = &mt("Incorrect").'.';
1.221     albertel  852: 	$css_class=$possible_class{'charged_try'};
1.135     albertel  853: 	$button=1;
                    854:     } elsif ($award eq 'SUBMITTED') {
                    855: 	$message = &mt("Your submission has been recorded.");
1.221     albertel  856: 	$css_class=$possible_class{'no_grade'};
1.135     albertel  857: 	$button=1;
                    858:     } elsif ($award eq 'DRAFT') {
1.144     albertel  859: 	$message = &mt("A draft copy has been saved.");
1.221     albertel  860: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel  861: 	$button=1;
                    862:     } elsif ($award eq 'ASSIGNED_SCORE') {
1.144     albertel  863: 	$message = &mt("A score has been assigned.");
1.221     albertel  864: 	$css_class=$possible_class{'correct'};
1.135     albertel  865: 	$button=0;
1.144     albertel  866:     } elsif ($award eq '') {
1.186     albertel  867: 	if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
                    868: 	    $message = &mt("Nothing submitted.");
1.221     albertel  869: 	    $css_class=$possible_class{'charged_try'};
1.186     albertel  870: 	} else {
1.221     albertel  871: 	    $css_class=$possible_class{'not_charged_try'};
1.186     albertel  872: 	}
1.144     albertel  873: 	$button=1;
1.135     albertel  874:     } else {
                    875: 	$message = &mt("Unknown message").": $award";
                    876: 	$button=1;
                    877:     }
1.209     albertel  878:     my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1.194     banghart  879:     foreach my $resid(@Apache::inputtags::response){
                    880:         if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1.198     albertel  881: 	    $message.='<br />';
                    882: 	    my @files = split(/\s*,\s*/,
                    883: 			      $Apache::lonhomework::history{"resource.$part.$resid.handback"});
                    884: 	    my $file_msg;
                    885: 	    foreach my $file (@files) {
                    886: 		$file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
                    887: 	    }
                    888: 	    $message .= &mt('Returned file(s): [_1]',$file_msg);
                    889: 	}
1.194     banghart  890:     }
                    891: 
1.233     albertel  892:     if (&Apache::lonhomework::hide_problem_status()
                    893: 	&& $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
                    894: 	&& &hide_award($award)) {
1.135     albertel  895: 	$message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
1.221     albertel  896: 	$css_class=$possible_class{'no_grade'};
1.135     albertel  897: 	$button=1;
                    898:     }
1.148     albertel  899:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
1.150     albertel  900: 	!$added_computer_text && $target ne 'tex') {
1.180     albertel  901: 	$message.= $computer;
1.148     albertel  902: 	$added_computer_text=1;
1.144     albertel  903:     }
1.237     www       904:     if ($Apache::lonhomework::type eq 'practice') {
1.244     raeburn   905:        if ($target eq 'web') {
                    906:            $message .= '<br />';
                    907:        } else {
                    908:            $message .= ' ';      
                    909:        }
                    910:        $message.=&mt('Submissions to practice problems are not permanently recorded.');
1.237     www       911:     }
                    912: 
1.221     albertel  913:     return ($button,$css_class,$message,$previousmsg);
1.12      albertel  914: }
                    915: 
1.155     albertel  916: sub markup_unit {
                    917:     my ($unit,$target)=@_;
                    918:     if ($target eq 'tex') {
                    919: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
                    920:     } else {
                    921: 	return "<tt>".$unit."</tt>";
                    922:     }
                    923: }
                    924: 
1.88      albertel  925: sub removealldata {
1.87      albertel  926:     my ($id)=@_;
                    927:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    928: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                    929: 	    &Apache::lonxml::debug("Removing $key");
                    930: 	    delete($Apache::lonhomework::results{$key});
                    931: 	}
                    932:     }
                    933: }
                    934: 
1.142     albertel  935: sub hidealldata {
                    936:     my ($id)=@_;
                    937:     foreach my $key (keys(%Apache::lonhomework::results)) {
                    938: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                    939: 	    &Apache::lonxml::debug("Hidding $key");
                    940: 	    my $newkey=$key;
                    941: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
                    942: 	    $Apache::lonhomework::results{$newkey}=
                    943: 		$Apache::lonhomework::results{$key};
                    944: 	    delete($Apache::lonhomework::results{$key});
                    945: 	}
                    946:     }
                    947: }
                    948: 
1.12      albertel  949: sub setgradedata {
1.136     albertel  950:     my ($award,$msg,$id,$previously_used) = @_;
1.154     albertel  951:     if ($Apache::lonhomework::scantronmode && 
1.165     albertel  952: 	&Apache::lonnet::validCODE($env{'form.CODE'})) {
                    953: 	$Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154     albertel  954:     } elsif ($Apache::lonhomework::scantronmode && 
1.165     albertel  955: 	     $env{'form.CODE'} eq '' &&
1.154     albertel  956: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
                    957: 	$Apache::lonhomework::results{"resource.CODE"}='';
1.141     albertel  958:     }
1.154     albertel  959: 
1.135     albertel  960:     if (!$Apache::lonhomework::scantronmode &&
                    961: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                    962: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                    963: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87      albertel  964: 	return '';
1.135     albertel  965:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
1.233     albertel  966: 	      /^correct/ 
                    967: 	      || $Apache::lonhomework::scantronmode 
                    968: 	      || &Apache::lonhomework::hide_problem_status()  ) {
1.154     albertel  969:         # the student doesn't already have it correct,
                    970: 	# or we are in a mode (scantron orno problem status) where a correct 
                    971:         # can become incorrect
                    972: 	# handle assignment of tries and solved status
1.135     albertel  973: 	my $solvemsg;
                    974: 	if ($Apache::lonhomework::scantronmode) {
                    975: 	    $solvemsg='correct_by_scantron';
                    976: 	} else {
                    977: 	    $solvemsg='correct_by_student';
                    978: 	}
                    979: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                    980: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                    981: 	}
                    982: 	if ( $award eq 'ASSIGNED_SCORE') {
                    983: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                    984: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                    985: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                    986: 		$solvemsg;
                    987: 	    my $numawards=scalar(@Apache::inputtags::response);
                    988: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                    989: 	    foreach my $res (@Apache::inputtags::response) {
1.232     albertel  990: 		if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
                    991: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                    992: 			$Apache::lonhomework::results{"resource.$id.$res.awarded"};
                    993: 		} else {
                    994: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                    995: 			&awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
                    996: 		}
1.135     albertel  997: 	    }
                    998: 	    if ($numawards > 0) {
                    999: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
                   1000: 		    $numawards;
                   1001: 	    }
                   1002: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                   1003: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1004: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1005: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1006: 		$solvemsg;
                   1007: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                   1008: 	} elsif ( $award eq 'INCORRECT' ) {
                   1009: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1010: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.233     albertel 1011: 	    if (&Apache::lonhomework::hide_problem_status()
                   1012: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1013: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1014: 	    }
                   1015: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1016: 		'incorrect_attempted';
                   1017: 	} elsif ( $award eq 'SUBMITTED' ) {
                   1018: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1019: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1020: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1021: 		'ungraded_attempted';
                   1022: 	} elsif ( $award eq 'DRAFT' ) {
                   1023: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
                   1024: 	} elsif ( $award eq 'NO_RESPONSE' ) {
                   1025: 	    #no real response so delete any data that got stored
1.129     albertel 1026: 	    &removealldata($id);
                   1027: 	    return '';
                   1028: 	} else {
1.135     albertel 1029: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1030: 		'incorrect_attempted';
1.233     albertel 1031: 	    if (&Apache::lonhomework::show_no_problem_status()
                   1032: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1033: 		$Apache::lonhomework::results{"resource.$id.tries"} =
                   1034: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1035: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1036: 	    }
1.233     albertel 1037: 
                   1038: 	    if (&Apache::lonhomework::show_some_problem_status()) {
                   1039: 		# clear out the awarded if they had gotten it wrong/right
                   1040: 		# and are now in an error mode	
                   1041: 		$Apache::lonhomework::results{"resource.$id.awarded"} = '';
                   1042: 	    }
1.135     albertel 1043: 	}
1.136     albertel 1044: 	if (defined($msg)) {
                   1045: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
                   1046: 	}
1.135     albertel 1047: 	# did either of the overall awards chage? If so ignore the 
                   1048: 	# previous check
                   1049: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
                   1050: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
                   1051: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
                   1052: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
                   1053: 	    # check if this was a previous submission if it was delete the
                   1054: 	    # unneeded data and update the previously_used attribute
                   1055: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
1.233     albertel 1056: 		if (&Apache::lonhomework::show_problem_status()) {
1.135     albertel 1057: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
                   1058: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                   1059: 		}
                   1060: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
                   1061: 		#delete all data as they student didn't do anything, but save
                   1062: 		#the list of collaborators.
                   1063: 		&removealldata($id);
                   1064: 		#and since they didn't do anything we were never here
                   1065: 		return '';
                   1066: 	    } else {
                   1067: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
                   1068: 	    }
1.101     albertel 1069: 	}
1.135     albertel 1070:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
                   1071: 	      /^correct/ ) {
                   1072: 	#delete all data as they student already has it correct
                   1073: 	&removealldata($id);
                   1074: 	#and since they didn't do anything we were never here
                   1075: 	return '';
1.40      albertel 1076:     }
1.135     albertel 1077:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.184     albertel 1078:     if ($award eq 'SUBMITTED') {
                   1079: 	&Apache::response::add_to_gradingqueue();
                   1080:     }
1.10      albertel 1081: }
                   1082: 
1.219     albertel 1083: sub find_which_previous {
                   1084:     my ($version) = @_;
                   1085:     my $part = $Apache::inputtags::part;
                   1086:     my (@previous_version);
                   1087:     foreach my $resp (@Apache::inputtags::response) {
                   1088: 	my $key = "$version:resource.$part.$resp.submission";
                   1089: 	my $submission = $Apache::lonhomework::history{$key};
                   1090: 	my %previous = &Apache::response::check_for_previous($submission,
                   1091: 							     $part,$resp,
                   1092: 							     $version);
                   1093: 	push(@previous_version,$previous{'version'});
                   1094:     }
                   1095:     return &previous_match(\@previous_version,
                   1096: 			   scalar(@Apache::inputtags::response));
                   1097: }
                   1098: 
                   1099: sub previous_match {
                   1100:     my ($previous_array,$count) = @_;
                   1101:     my $match = 0;
                   1102:     my @matches;
                   1103:     foreach my $versionar (@$previous_array) {
                   1104: 	foreach my $version (@$versionar) {
                   1105: 	    $matches[$version]++;
                   1106: 	}
                   1107:     }
                   1108:     my $which=0;
                   1109:     foreach my $elem (@matches) {
                   1110: 	if ($elem eq $count) {
                   1111: 	    $match=1;
                   1112: 	    last;
                   1113: 	}
                   1114: 	$which++;
                   1115:     }
                   1116:     return ($match,$which);
                   1117: }
                   1118: 
1.9       albertel 1119: sub grade {
1.135     albertel 1120:     my ($target) = @_;
                   1121:     my $id = $Apache::inputtags::part;
                   1122:     my $response='';
1.165     albertel 1123:     if ( defined $env{'form.submitted'}) {
1.136     albertel 1124: 	my (@awards,@msgs);
1.135     albertel 1125: 	foreach $response (@Apache::inputtags::response) {
                   1126: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                   1127: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
                   1128: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
                   1129: 	    push (@awards,$value);
1.136     albertel 1130: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
                   1131: 	    &Apache::lonxml::debug("got message $value from $response for $id");
                   1132: 	    push (@msgs,$value);
1.135     albertel 1133: 	}
1.232     albertel 1134: 	my ($finalaward,$msg) = 
                   1135: 	    &finalizeawards(\@awards,\@msgs,undef,undef,
                   1136: 			    $Apache::lonhomework::scantronmode);
1.135     albertel 1137: 	my $previously_used;
                   1138: 	if ( $#Apache::inputtags::previous eq $#awards ) {
1.219     albertel 1139: 	    my ($match) =
                   1140: 		&previous_match(\@Apache::inputtags::previous_version,
                   1141: 				scalar(@Apache::inputtags::response));
1.244     raeburn  1142: 
1.135     albertel 1143: 	    if ($match) {
                   1144: 		$previously_used = 'PREVIOUSLY_LAST';
                   1145: 		foreach my $value (@Apache::inputtags::previous) {
                   1146: 		    if ($value eq 'PREVIOUSLY_USED' ) {
                   1147: 			$previously_used = $value;
                   1148: 			last;
                   1149: 		    }
1.75      albertel 1150: 		}
                   1151: 	    }
1.43      albertel 1152: 	}
1.136     albertel 1153: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
                   1154: 	&setgradedata($finalaward,$msg,$id,$previously_used);
1.43      albertel 1155:     }
1.135     albertel 1156:     return '';
1.1       albertel 1157: }
                   1158: 
1.217     albertel 1159: sub get_grade_messages {
                   1160:     my ($id,$prefix,$target,$status) = @_;
                   1161: 
                   1162:     my ($message,$latemessage,$trystr,$previousmsg);
                   1163:     my $showbutton = 1;
                   1164: 
                   1165:     my $award = $Apache::lonhomework::history{"$prefix.award"};
                   1166:     my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
                   1167:     my $solved = $Apache::lonhomework::history{"$prefix.solved"};
                   1168:     my $previous = $Apache::lonhomework::history{"$prefix.previous"};
                   1169:     my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
                   1170:     &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
                   1171:     if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
                   1172: 	&Apache::lonxml::debug('Getting message');
1.221     albertel 1173: 	($showbutton,my $css_class,$message,$previousmsg) =
1.217     albertel 1174: 	    &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
                   1175: 			  $target);
                   1176: 	if ($target eq 'tex') {
                   1177: 	    $message='\vskip 2 mm '.$message.' ';
                   1178: 	} else {
1.221     albertel 1179: 	    $message="<td class=\"$css_class\">$message</td>";
1.217     albertel 1180: 	    if ($previousmsg) {
1.221     albertel 1181: 		$previousmsg="<td class=\"LC_answer_previous\">$previousmsg</td>";
1.217     albertel 1182: 	    }
                   1183: 	}
                   1184:     }
                   1185:     my $tries = $Apache::lonhomework::history{"$prefix.tries"};
                   1186:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1187:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                   1188:     #if tries are set to negative turn off the Tries/Button and messages
                   1189:     if (defined($maxtries) && $maxtries < 0) { return ''; }
                   1190:     if ( $tries eq '' ) { $tries = '0'; }
                   1191:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                   1192:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
                   1193:     my $tries_text=&mt('Tries');
                   1194:     if ( $Apache::lonhomework::type eq 'survey' ||
                   1195: 	 $Apache::lonhomework::parsing_a_task) {
                   1196: 	$tries_text=&mt('Submissions');
                   1197:     }
                   1198: 
                   1199:     if ($showbutton) {
                   1200: 	if ($target eq 'tex') {
                   1201: 	    if ($env{'request.state'} ne "construct"
                   1202: 		&& $Apache::lonhomework::type ne 'exam'
                   1203: 		&& $env{'form.suppress_tries'} ne 'yes') {
                   1204: 		$trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.
                   1205: 		    $tries.'/'.$maxtries.'} \vskip 2 mm ';
                   1206: 	    } else {
                   1207: 		$trystr = '\vskip 0 mm ';
                   1208: 	    }
                   1209: 	} else {
                   1210: 	    $trystr = "<td><nobr>".$tries_text." $tries";
                   1211: 	    if ($Apache::lonhomework::parsing_a_task) {
                   1212: 	    } elsif($env{'request.state'} ne 'construct') {
                   1213: 		$trystr.="/$maxtries";
                   1214: 	    } else {
                   1215: 		if (defined($Apache::inputtags::params{'maxtries'})) {
                   1216: 		    $trystr.="/".$Apache::inputtags::params{'maxtries'};
                   1217: 		}
                   1218: 	    }
                   1219: 	    $trystr.="</nobr></td>";
                   1220: 	}
                   1221:     }
1.221     albertel 1222: 
1.217     albertel 1223:     if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
                   1224: 	#last submissions was after due date
                   1225: 	$latemessage=&mt(' The last submission was after the Due Date ');;
                   1226: 	if ($target eq 'web') {
1.221     albertel 1227: 	    $latemessage='<td class="LC_answer_late">'.$latemessage.'</td>';
1.217     albertel 1228: 	}
                   1229:     }
                   1230:     return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
                   1231: }
                   1232: 
1.11      albertel 1233: sub gradestatus {
1.223     albertel 1234:     my ($id,$target,$no_previous) = @_;
1.135     albertel 1235:     my $showbutton = 1;
                   1236:     my $message = '';
                   1237:     my $latemessage = '';
                   1238:     my $trystr='';
                   1239:     my $button='';
                   1240:     my $previousmsg='';
                   1241: 
                   1242:     my $status = $Apache::inputtags::status['-1'];
                   1243:     &Apache::lonxml::debug("gradestatus has :$status:");
1.183     albertel 1244:     if ( $status ne 'CLOSED' 
                   1245: 	 && $status ne 'UNAVAILABLE' 
                   1246: 	 && $status ne 'INVALID_ACCESS' 
                   1247: 	 && $status ne 'NEEDS_CHECKIN' 
                   1248: 	 && $status ne 'NOT_IN_A_SLOT') {  
1.217     albertel 1249: 
                   1250: 	($previousmsg,$latemessage,$message,$trystr) =
                   1251: 	    &get_grade_messages($id,"resource.$id",$target,$status,
                   1252: 				$showbutton);
                   1253: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {
                   1254: 	    $showbutton = 0;
1.164     albertel 1255: 	}
1.218     albertel 1256: 	if ( $status eq 'SHOW_ANSWER') {
                   1257: 	    undef($previousmsg);
                   1258: 	}
1.135     albertel 1259: 	if ( $showbutton ) { 
                   1260: 	    if ($target ne 'tex') {
1.230     albertel 1261: 		$button = 
                   1262: 		    '<input 
                   1263:                           onmouseup="javascript:setSubmittedPart(\''.$id.'\')"
                   1264:                            onsubmit="javascript:setSubmittedPart(\''.$id.'\')"
                   1265:                         type="submit" name="submit_'.$id.'"
                   1266:                          value="'.&mt('Submit Answer').'" />';
1.135     albertel 1267: 	    }
                   1268: 	}
1.217     albertel 1269: 
1.135     albertel 1270:     }
                   1271:     my $output= $previousmsg.$latemessage.$message.$trystr;
                   1272:     if ($output =~ /^\s*$/) {
                   1273: 	return $button;
1.63      sakharuk 1274:     } else {
1.135     albertel 1275: 	if ($target eq 'tex') {
                   1276: 	    return $button.' \vskip 0 mm '.$output.' ';
                   1277: 	} else {
1.223     albertel 1278: 	    $output =
                   1279: 		'<table><tr><td>'.$button.'</td>'.$output;
                   1280: 	    if (!$no_previous) {
                   1281: 		$output.='<td>'.&previous_tries($id,$target).'</td>';
                   1282: 	    }
                   1283: 	    $output.= '</tr></table>';
                   1284: 	    return $output;
1.135     albertel 1285: 	}
1.63      sakharuk 1286:     }
1.11      albertel 1287: }
1.217     albertel 1288: 
                   1289: sub previous_tries {
                   1290:     my ($id,$target) = @_;
                   1291:     my $output;
                   1292:     my $status = $Apache::inputtags::status['-1'];
1.219     albertel 1293: 
                   1294:     my $count;
                   1295:     my %count_lookup;
                   1296: 
1.217     albertel 1297:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                   1298: 	my $prefix = $i.":resource.$id";
                   1299: 
                   1300: 	next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1.219     albertel 1301: 	$count++;
                   1302: 	$count_lookup{$i} = $count;
                   1303: 	
1.217     albertel 1304: 	my ($previousmsg,$latemessage,$message,$trystr);
                   1305: 
                   1306: 	($previousmsg,$latemessage,$message,$trystr) =
                   1307: 	    &get_grade_messages($id,"$prefix",$target,$status);
                   1308: 
1.219     albertel 1309: 	if ($previousmsg ne '') {
                   1310: 	    my ($match,$which) = &find_which_previous($i);
                   1311: 	    $message=$previousmsg;
                   1312: 	    my $previous = $count_lookup{$which};
1.226     albertel 1313: 	    $message =~ s{(</td>)}{ as submission \# $previous $1};
1.221     albertel 1314: 	} elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1.233     albertel 1315: 	    if (!(&Apache::lonhomework::hide_problem_status()
1.225     albertel 1316: 		  && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
                   1317: 		&& $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
                   1318: 		) {
                   1319: 		
1.238     bisitz   1320:                 my $txt_correct = &mt('Correct');
1.221     albertel 1321: 		$message =~ s{(<td.*?>)(.*?)(</td>)}
1.238     bisitz   1322:                              {$1 <strong>$txt_correct</strong>. $3}s;
1.221     albertel 1323: 	    }
1.238     bisitz   1324:             my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1.221     albertel 1325: 	    $message =~ s{(</td>)}{ $trystr $1};
1.219     albertel 1326: 	}
1.221     albertel 1327: 	my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
                   1328: 	$message =~ s{(<td.*?>)}{<td>};
                   1329: 	
1.219     albertel 1330: 
1.221     albertel 1331: 	$output.='<tr class="'.$class.'">';
1.223     albertel 1332: 	$output.='<td align="center">'.$count.'</td>';
1.219     albertel 1333: 	$output.=$message;
1.217     albertel 1334: 
                   1335: 	foreach my $resid (@Apache::inputtags::response) {
                   1336: 	    my $prefix = $prefix.".$resid";
                   1337: 	    if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
                   1338: 		my $submission =
                   1339: 		    $Apache::inputtags::submission_display{"$prefix.submission"};
                   1340: 		if (!defined($submission)) {
                   1341: 		    $submission = 
                   1342: 			$Apache::lonhomework::history{"$prefix.submission"};
                   1343: 		}
                   1344: 		$output.='<td>'.$submission.'</td>';
                   1345: 	    } else {
                   1346: 		$output.='<td></td>';
                   1347: 	    }
                   1348: 	}
1.221     albertel 1349: 	$output.=&Apache::loncommon::end_data_table_row()."\n";
1.217     albertel 1350:     }
                   1351:     return if ($output eq '');
1.219     albertel 1352:     my $headers = 
1.222     albertel 1353: 	'<tr>'.'<th>'.&mt('Submission #').'</th><th>'.&mt('Try').
1.219     albertel 1354: 	'</th><th colspan="'.scalar(@Apache::inputtags::response).'">'.
                   1355: 	&mt('Submitted Answer').'</th>';
                   1356:     $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1.217     albertel 1357:     #return $output;
1.229     albertel 1358:     $output = &Apache::loncommon::js_ready($output); 
1.226     albertel 1359:     $output.='<br /><form action=""><center><input type="button" name="close" value="'.&mt('Close Window').'" onClick="window.close()" /></center></form>';
                   1360: 
1.217     albertel 1361:     my $windowopen=&Apache::lonhtmlcommon::javascript_docopen();
                   1362:     my $start_page =
                   1363: 	&Apache::loncommon::start_page('Previous Tries', undef,
1.229     albertel 1364: 				       {'only_body'      => 1,
                   1365: 					'bgcolor'        => '#FFFFFF',
                   1366: 					'js_ready'       => 1,
                   1367: 				        'inherit_jsmath' => 1, });
1.217     albertel 1368:     my $end_page =
                   1369: 	&Apache::loncommon::end_page({'js_ready' => 1,});
1.231     albertel 1370:     my $prefix = $env{'form.request.prefix'};
                   1371:     $prefix =~ tr{.}{_};
                   1372:     my $function_name = "LONCAPA_previous_tries_".$prefix.
1.234     albertel 1373: 	$Apache::lonxml::curdepth.'_'.$env{'form.counter'};
1.217     albertel 1374:     my $result ="<script type=\"text/javascript\">
                   1375: // <![CDATA[
1.231     albertel 1376:     function $function_name() {newWindow=open('','new_W','width=500,height=500,scrollbars=1,resizable=yes');newWindow.$windowopen;newWindow.document.writeln('$start_page $output $end_page');newWindow.document.close();newWindow.focus()}
1.217     albertel 1377: // ]]>
1.231     albertel 1378: </script><a href=\"javascript:$function_name();void(0);\">".&mt("Previous Tries")."</a><br />";
1.217     albertel 1379:     #use Data::Dumper;
                   1380:     #&Apache::lonnet::logthis(&Dumper(\%Apache::inputtags::submission_display));
                   1381:     return $result;
                   1382: }
                   1383: 
1.1       albertel 1384: 1;
                   1385: __END__
1.249   ! jms      1386: 
        !          1387: =pod
        !          1388: 
        !          1389: =back
        !          1390: 
        !          1391: =cut
1.43      albertel 1392:  

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