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

1.43      albertel    1: # The LearningOnline Network with CAPA
                      2: # input  definitons
1.47      albertel    3: #
1.333.2.9.2.3! (raeburn    4:: # $Id: inputtags.pm,v 1.333.2.9.2.2 2021/09/11 16:46:37 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.265     raeburn    56: use Apache::lonhtmlcommon;
1.115     www        57: use Apache::lonlocal;
1.165     albertel   58: use Apache::lonnet;
1.192     www        59: use LONCAPA;
                     60:  
1.1       albertel   61: 
1.50      harris41   62: BEGIN {
1.286     www        63:     &Apache::lonxml::register('Apache::inputtags',('hiddensubmission','hiddenline','textfield','textline'));
1.1       albertel   64: }
                     65: 
1.249     jms        66: =pod
                     67: 
                     68: =item initialize_inputtags()
                     69: 
                     70: Initializes a set of global variables used during the parse of the problem.
                     71: 
                     72: @Apache::inputtags::input        - List of current input ids.
                     73: @Apache::inputtags::inputlist    - List of all input ids seen this problem.
                     74: @Apache::inputtags::response     - List of all current resopnse ids.
                     75: @Apache::inputtags::responselist - List of all response ids seen this 
                     76:                                      problem.
                     77: @Apache::inputtags::hint         - List of all hint ids.
                     78: @Apache::inputtags::hintlist     - List of all hint ids seen this problem.
                     79: @Apache::inputtags::previous     - List describing if specific responseds
                     80:                                      have been used
                     81: @Apache::inputtags::previous_version - Submission responses were used in.
                     82: $Apache::inputtags::part         - Current part id (valid only in 
                     83:                                      <problem>)
                     84:                                    0 if not in a part.
                     85: @Apache::inputtags::partlist     - List of part ids seen in the current
                     86:                                      <problem>
                     87: @Apache::inputtags::status       - List of problem  statuses. First 
                     88:                                    element is the status of the <problem>
                     89:                                    the remainder are for individual <part>s.
                     90: %Apache::inputtags::params       - Hash of defined parameters for the
                     91:                                    current response.
                     92: @Apache::inputtags::import       - List of all ids for <import> thes get
                     93:                                    join()ed and prepended.
                     94: @Apache::inputtags::importlist   - List of all import ids seen.
                     95: $Apache::inputtags::response_with_no_part
                     96:                                  - Flag set true if we have seen a response
                     97:                                    that is not inside a <part>
                     98: %Apache::inputtags::answertxt    - <*response> tags store correct
                     99:                                    answer strings for display by <textline/>
                    100:                                    in this hash.
                    101: %Apache::inputtags::submission_display
                    102:                                  - <*response> tags store improved display
                    103:                                    of submission strings for display by part
                    104:                                    end.
                    105: 
                    106: =cut
1.178     albertel  107: 
1.1       albertel  108: sub initialize_inputtags {
1.135     albertel  109:     @Apache::inputtags::input=();
                    110:     @Apache::inputtags::inputlist=();
1.174     albertel  111:     @Apache::inputtags::response=();
1.135     albertel  112:     @Apache::inputtags::responselist=();
1.174     albertel  113:     @Apache::inputtags::hint=();
1.173     albertel  114:     @Apache::inputtags::hintlist=();
1.135     albertel  115:     @Apache::inputtags::previous=();
                    116:     @Apache::inputtags::previous_version=();
                    117:     $Apache::inputtags::part='';
                    118:     @Apache::inputtags::partlist=();
                    119:     @Apache::inputtags::status=();
                    120:     %Apache::inputtags::params=();
                    121:     @Apache::inputtags::import=();
                    122:     @Apache::inputtags::importlist=();
                    123:     $Apache::inputtags::response_with_no_part=0;
1.144     albertel  124:     %Apache::inputtags::answertxt=();
1.217     albertel  125:     %Apache::inputtags::submission_display=();
1.103     albertel  126: }
                    127: 
1.310     foxr      128: #
                    129: #  provides the onblur binding for spellchecking.  This could be an
                    130: #  empty string if spellchecking was not enabled.
                    131: #  Jquery selector binding is done rather than setting an onblur
                    132: #  attribute because we'll need to set the element's spellcheck language
                    133: #  option dynamically so we need $(this) to be defined.
                    134: #
                    135: # @param id   - The element id to bind.
                    136: # @param lang - Language in which spellchecking is desired.
                    137: #               if undef, nothing is generated.  
                    138: # @return string - onblur specification to do the requested spellchecking.
                    139: #
                    140: sub spellcheck_onblur {
                    141:     my ($id, $lang) = @_;
                    142:     my $result = '';
                    143:     if ($lang) {
                    144: 
                    145: 	$result = <<JAVASCRIPT;
                    146: <script type="text/javascript">
                    147: \$('\#$id').blur(function() {
                    148:     doSpellcheck('\#$id', '$lang');
                    149:  });
                    150: </script>
                    151: 
                    152: JAVASCRIPT
                    153: 
                    154: 
                    155:     }
                    156:     return $result;
                    157: }
                    158: 
1.103     albertel  159: sub check_for_duplicate_ids {
                    160:     my %check;
                    161:     foreach my $id (@Apache::inputtags::partlist,
                    162: 		    @Apache::inputtags::responselist,
1.173     albertel  163: 		    @Apache::inputtags::hintlist,
1.103     albertel  164: 		    @Apache::inputtags::importlist) {
                    165: 	$check{$id}++;
                    166:     }
                    167:     my @duplicates;
                    168:     foreach my $id (sort(keys(%check))) {
                    169: 	if ($check{$id} > 1) {
                    170: 	    push(@duplicates,$id);
                    171: 	}
                    172:     }
                    173:     if (@duplicates) {
                    174: 	&Apache::lonxml::error("Duplicated ids found, problem will operate incorrectly. Duplicated ids seen: ",join(', ',@duplicates));
                    175:     }
1.1       albertel  176: }
                    177: 
1.14      albertel  178: sub start_input {
1.135     albertel  179:     my ($parstack,$safeeval)=@_;
1.228     albertel  180:     my $id = &Apache::lonxml::get_id($parstack,$safeeval);
1.135     albertel  181:     push (@Apache::inputtags::input,$id);
                    182:     push (@Apache::inputtags::inputlist,$id);
                    183:     return $id;
1.14      albertel  184: }
                    185: 
                    186: sub end_input {
1.135     albertel  187:     pop @Apache::inputtags::input;
                    188:     return '';
1.14      albertel  189: }
                    190: 
1.124     www       191: sub addchars {
                    192:     my ($fieldid,$addchars)=@_;
                    193:     my $output='';
                    194:     foreach (split(/\,/,$addchars)) {
                    195: 	$output.='<a href="javascript:void(document.forms.lonhomework.'.
                    196: 	    $fieldid.'.value+=\''.$_.'\')">'.$_.'</a> ';
                    197:     }
                    198:     return $output;
                    199: }
                    200: 
1.48      albertel  201: sub start_textfield {
1.185     albertel  202:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.135     albertel  203:     my $result = "";
                    204:     my $id = &start_input($parstack,$safeeval);
                    205:     my $resid=$Apache::inputtags::response[-1];
                    206:     if ($target eq 'web') {
                    207: 	$Apache::lonxml::evaluate--;
1.205     albertel  208: 	my $partid=$Apache::inputtags::part;
1.274     raeburn   209:         my ($oldresponse,$newvariation);
                    210:         if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
                    211:              ($Apache::lonhomework::type eq 'randomizetry')) &&
                    212:              ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
                    213:             if ($env{'form.'.$partid.'.rndseed'} ne
                    214:                 $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
                    215:                 $newvariation = 1;
                    216:             }
                    217:         }
                    218:         unless ($newvariation) {
1.317     raeburn   219:             if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
                    220:                 (($env{'form.grade_username'} eq $env{'user.name'}) &&
                    221:                  ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
                    222:                 $oldresponse = $Apache::lonhomework::history{"resource.$partid.$resid.submission"};
                    223:             } elsif (($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurvey') ||
                    224:                     ($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurveycred')) {
1.322     raeburn   225:                 $oldresponse = '* '.&mt('(only shown to submitter)').' *';
1.317     raeburn   226:             } else {
                    227:                 $oldresponse = $Apache::lonhomework::history{"resource.$partid.$resid.submission"};
                    228:             }
1.274     raeburn   229:         }
1.135     albertel  230: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    231: 	    my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    232: 	    if ( $cols eq '') { $cols = 80; }
                    233: 	    my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
1.143     www       234: 	    if ( $rows eq '') { $rows = 16; }
1.135     albertel  235: 	    my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    236: 	    $result='';
1.310     foxr      237: 	    my $tagident = 'HWVAL_' . $resid;
1.326     raeburn   238:             my $itemid = 'HWVAL_'.$partid.'_'.$resid;
1.135     albertel  239: 	    if ($addchars) {
1.310     foxr      240: 		$result.=&addchars($tagident, $addchars);
1.135     albertel  241: 	    }
1.324     raeburn   242:             my $textareaclass;
                    243:             unless (&Apache::londefdef::is_inside_of($tagstack,
                    244:                                                     'externalresponse')) {
                    245:                 $textareaclass = 'class="LC_richDetectHtml spellchecked"';
                    246:             }
1.326     raeburn   247: 	    $result.= '<textarea wrap="hard" name="'.$tagident.'" id="'.$itemid.'" ' .
1.308     foxr      248: 		      'rows="'.$rows.'" cols="'.$cols.'" '.$textareaclass
                    249: 		      .'>'.
1.302     raeburn   250:                       &HTML::Entities::encode($oldresponse,'<>&"');
1.135     albertel  251: 	    if ($oldresponse ne '') {
1.143     www       252: 
1.135     albertel  253: 		#get rid of any startup text if the user has already responded
1.185     albertel  254: 		&Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.135     albertel  255: 	    }
                    256: 	} else {
1.205     albertel  257: 	    #show past answer in the essayresponse case
                    258: 	    if ($oldresponse =~ /\S/
                    259: 		&& &Apache::londefdef::is_inside_of($tagstack,
                    260: 						    'essayresponse') ) {
                    261: 		$result='<table class="LC_pastsubmission"><tr><td>'.
1.323     bisitz    262: 		    &HTML::Entities::encode($oldresponse,'"<>&').
                    263:                     '</td></tr></table>';
1.205     albertel  264: 	    }
1.135     albertel  265: 	    #get rid of any startup text
1.185     albertel  266: 	    &Apache::lonxml::get_all_text("/textfield",$parser,$style);
1.61      albertel  267: 	}
1.135     albertel  268:     } elsif ($target eq 'grade') {
1.185     albertel  269: 	my $seedtext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    270: 						   $style);
1.165     albertel  271: 	if ($seedtext eq $env{'form.HWVAL_'.$resid}) {
1.135     albertel  272: 	    # if the seed text is still there it wasn't a real submission
1.165     albertel  273: 	    $env{'form.HWVAL_'.$resid}='';
1.135     albertel  274: 	}
                    275:     } elsif ($target eq 'edit') {
                    276: 	$result.=&Apache::edit::tag_start($target,$token);
                    277: 	$result.=&Apache::edit::text_arg('Rows:','rows',$token,4);
                    278: 	$result.=&Apache::edit::text_arg('Columns:','cols',$token,4);
                    279: 	$result.=&Apache::edit::text_arg
                    280: 	    ('Click-On Texts (comma sep):','addchars',$token,10);
1.185     albertel  281: 	my $bodytext=&Apache::lonxml::get_all_text("/textfield",$parser,
                    282: 						   $style);
1.135     albertel  283: 	$result.=&Apache::edit::editfield($token->[1],$bodytext,'Text you want to appear by default:',80,2);
1.314     raeburn   284:         my $spell_langs = &spelling_languages();
1.312     raeburn   285: 	$result .= &Apache::edit::select_arg('Spellcheck for:', 'spellcheck',
1.314     raeburn   286: 					     $spell_langs, $token);
1.135     albertel  287:     } elsif ($target eq 'modified') {
                    288: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    289: 						     $safeeval,'rows','cols',
1.311     foxr      290: 						     'addchars', 'spellcheck');
1.135     albertel  291: 	if ($constructtag) {
                    292: 	    $result = &Apache::edit::rebuild_tag($token);
                    293: 	} else {
                    294: 	    $result=$token->[4];
                    295: 	}
                    296: 	$result.=&Apache::edit::modifiedfield("/textfield",$parser);
                    297:     } elsif ($target eq 'tex') {
                    298: 	my $number_of_lines = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
                    299: 	my $width_of_box = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
                    300: 	if ($$tagstack[-2] eq 'essayresponse' and $Apache::lonhomework::type eq 'exam') {
                    301: 	    $result = '\fbox{\fbox{\parbox{\textwidth-5mm}{';
                    302: 	    for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    303: 	    $result.='\strut \\\\\strut \\\\\strut \\\\\strut \\\\}}}';
                    304: 	} else {
1.313     raeburn   305:             if ($env{'form.pdfFormFields'} eq 'yes') {
                    306:                 my $fieldname = $env{'request.symb'}.
                    307:                                 '&part_'. $Apache::inputtags::part.
                    308:                                 '&textresponse'.
                    309:                                 '&HWVAL_' . $Apache::inputtags::response['-1'];
                    310:                 $result.='\TextField[name='.$fieldname.',multiline=true,height=6\baselineskip,width=270,borderwidth=0,backgroundcolor={.85 .85 .85}]\\';
                    311:             } else {
                    312:                 my $TeXwidth=$width_of_box/80;
                    313:                 $result = '\vskip 1 mm \fbox{\fbox{\parbox{'.$TeXwidth.'\textwidth-5mm}{';
                    314:                 for (my $i=0;$i<int $number_of_lines*2;$i++) {$result.='\strut \\\\ ';}
                    315:                 $result.='}}}\vskip 2 mm ';
                    316:             }
1.135     albertel  317: 	}
1.60      albertel  318:     }
1.135     albertel  319:     return $result;
1.6       albertel  320: }
                    321: 
1.48      albertel  322: sub end_textfield {
1.135     albertel  323:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    324:     my $result;
                    325:     if ($target eq 'web') {
1.310     foxr      326: 	my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
1.135     albertel  327: 	$Apache::lonxml::evaluate++;
                    328: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.326     raeburn   329:             my $partid=$Apache::inputtags::part;
1.310     foxr      330: 	    my $resid = $Apache::inputtags::response[-1];
1.326     raeburn   331: 	    my $itemid = 'HWVAL_' . $partid . '_' . $resid;
1.310     foxr      332: 	    my $result =  "</textarea>";
1.326     raeburn   333: 	    $result .= &spellcheck_onblur($itemid, $spellcheck);
1.310     foxr      334: 	    return $result;
1.135     albertel  335: 	}
                    336:     } elsif ($target eq 'edit') {
                    337: 	$result=&Apache::edit::end_table();
                    338:     }
                    339:     &end_input;
                    340:     return $result;
1.6       albertel  341: }
                    342: 
1.190     albertel  343: sub exam_score_line {
1.188     albertel  344:     my ($target) = @_;
1.190     albertel  345: 
1.188     albertel  346:     my $result;
                    347:     if ($target eq 'tex') {
                    348: 	my $repetition = &Apache::response::repetition();
                    349: 	$result.='\begin{enumerate}';
1.190     albertel  350: 	if ($env{'request.state'} eq "construct" ) {$result.='\item[\strut]';}
1.188     albertel  351: 	foreach my $i (0..$repetition-1) {
                    352: 	    $result.='\item[\textbf{'.
                    353: 		($Apache::lonxml::counter+$i).
                    354: 		'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
                    355: 	}
                    356: 	$result.= '\end{enumerate}';
1.190     albertel  357:     }
                    358: 
                    359:     return $result;
                    360: }
                    361: 
                    362: sub exam_box {
                    363:     my ($target) = @_;
                    364:     my $result;
1.188     albertel  365: 
1.190     albertel  366:     if ($target eq 'tex') {
                    367: 	$result .= '\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
                    368: 	$result .= &exam_score_line($target);
1.188     albertel  369:     } elsif ($target eq 'web') {
                    370: 	my $id=$Apache::inputtags::response[-1];
                    371: 	$result.= '<br /><br />
                    372:                    <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
                    373:                    </textarea> <br /><br />';
                    374:     }
                    375:     return $result;
                    376: }
                    377: 
                    378: sub needs_exam_box {
                    379:     my ($tagstack) = @_;
                    380:     my @tags = ('formularesponse',
                    381: 		'stringresponse',
                    382: 		'reactionresponse',
                    383: 		'organicresponse',
                    384: 		);
                    385: 
                    386:     foreach my $tag (@tags) {
                    387: 	if (grep(/\Q$tag\E/,@$tagstack)) {
                    388: 	    return 1;
                    389: 	}
                    390:     }
                    391:     return 0;
                    392: }
                    393: 
1.1       albertel  394: sub start_textline {
1.135     albertel  395:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    396:     my $result = "";
1.210     albertel  397:     my $input_id = &start_input($parstack,$safeeval);
1.310     foxr      398: 
                    399:     # The spellcheck attribute 
                    400:     # 1. enables spellchecking.
                    401:     # 2. Provides the language code in which the spellchecking will be performed.
                    402: 
                    403:     my $spellcheck = &Apache::lonxml::get_param('spellcheck', $parstack, $safeeval);
1.135     albertel  404:     if ($target eq 'web') {
                    405: 	$Apache::lonxml::evaluate--;
                    406: 	my $partid=$Apache::inputtags::part;
                    407: 	my $id=$Apache::inputtags::response[-1];
1.204     albertel  408: 	if (!&Apache::response::show_answer()) {
1.135     albertel  409: 	    my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    410: 	    my $maxlength;
                    411: 	    if ($size eq '') { $size=20; } else {
1.214     albertel  412: 		if ($size < 20) {
                    413: 		    $maxlength = ' maxlength="'.$size.'"';
                    414: 		}
1.135     albertel  415: 	    }
1.274     raeburn   416:             my ($oldresponse,$newvariation);
                    417:             if ((($Apache::lonhomework::history{"resource.$partid.type"} eq 'randomizetry') ||
                    418:                  ($Apache::lonhomework::type eq 'randomizetry')) &&
                    419:                  ($Apache::inputtags::status[-1] eq 'CAN_ANSWER')) {
                    420:                 if ($env{'form.'.$partid.'.rndseed'} ne
                    421:                     $Apache::lonhomework::history{"resource.$partid.rndseed"}) {
                    422:                     $newvariation = 1;
                    423:                 }
                    424:             }
                    425:             unless ($newvariation) {
1.317     raeburn   426:                 if ((($env{'form.grade_username'} eq '') && ($env{'form.grade_domain'} eq '')) ||
                    427:                     (($env{'form.grade_username'} eq $env{'user.name'}) &&
                    428:                      ($env{'form.grade_domain'} eq $env{'user.domain'}))) {
                    429:                     $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    430:                 } elsif (($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurvey') ||
                    431:                         ($Apache::lonhomework::history{"resource.$partid.type"} eq 'anonsurveycred') ||
                    432:                         ($Apache::lonhomework::type eq 'anonsurvey') ||
                    433:                         ($Apache::lonhomework::type eq 'anonsurveycred')) {
1.322     raeburn   434:                         $oldresponse = '* '.&mt('(only shown to submitter)').' *';
1.317     raeburn   435:                 } else {
                    436:                     $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    437:                 }
1.274     raeburn   438: 	        &Apache::lonxml::debug("oldresponse $oldresponse is ".ref($oldresponse));
                    439: 	        if (ref($oldresponse) eq 'ARRAY') {
                    440: 		    $oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
                    441: 	        }
                    442: 	        $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
                    443:                 $oldresponse =~ s/^\s+//;
                    444:                 $oldresponse =~ s/\s+$//;
                    445:                 $oldresponse =~ s/\s+/ /g;
                    446:             }
1.135     albertel  447: 	    if ($Apache::lonhomework::type ne 'exam') {
                    448: 		my $addchars=&Apache::lonxml::get_param('addchars',$parstack,$safeeval);
                    449: 		$result='';
                    450: 		if ($addchars) {
                    451: 		    $result.=&addchars('HWVAL_'.$id,$addchars);
                    452: 		}
1.157     albertel  453: 		my $readonly=&Apache::lonxml::get_param('readonly',$parstack,
                    454: 							$safeeval);
1.193     albertel  455: 		if (lc($readonly) eq 'yes' 
                    456: 		    || $Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
1.157     albertel  457: 		    $readonly=' readonly="readonly" ';
1.158     albertel  458: 		} else {
                    459: 		    $readonly='';
1.157     albertel  460: 		}
1.193     albertel  461: 		my $name = 'HWVAL_'.$id;
1.326     raeburn   462:                 my $itemid = 'HWVAL_'.$partid.'_'.$id;
1.333.2.3  raeburn   463:                 my $input_tag_id = $itemid.'_'.$input_id;
1.193     albertel  464: 		if ($Apache::inputtags::status[-1] eq 'CANNOT_ANSWER') {
                    465: 		    $name = "none";
                    466: 		}
1.310     foxr      467: 		$result.= '<input onkeydown="javascript:setSubmittedPart(\''.$partid.'\');"'
1.333.2.3  raeburn   468: 		     . ' onfocus="javascript:disableAutoComplete(\''.$input_tag_id.'\');"'
1.331     raeburn   469: 		     . ' type="text" '.$readonly.' name="'. $name . '"'
1.333.2.3  raeburn   470: 		     . ' id="' . $input_tag_id . '"'
1.310     foxr      471: 		     . ' value="'.  $oldresponse.'"'
1.331     raeburn   472: 		     . ' class="LC_textline spellchecked"  size="'.$size.'"'.$maxlength.' />';
1.310     foxr      473: 
1.326     raeburn   474: 		$result .= &spellcheck_onblur($itemid, $spellcheck);
1.333.2.3  raeburn   475:                 if (($Apache::inputtags::status['-1'] eq 'CAN_ANSWER') &&
                    476:                     (($tagstack->[-2] eq 'formularesponse') || ($tagstack->[-2] eq 'mathresponse')) &&
                    477:                     (&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffeditor') ne 'yes')) {
                    478:                     $result.=&edit_mathresponse_button($input_tag_id);
                    479:                 }
1.135     albertel  480: 	    }
1.188     albertel  481: 	    if ($Apache::lonhomework::type eq 'exam'
                    482: 		&& &needs_exam_box($tagstack)) {
                    483: 		$result.=&exam_box($target);
                    484: 	    }
1.135     albertel  485: 	} else {
                    486: 	    #right or wrong don't show what was last typed in.
1.208     albertel  487: 	    my $count = scalar(@Apache::inputtags::inputlist)-1;
                    488: 	    $result='<b>'.$Apache::inputtags::answertxt{$id}[$count].'</b>';
1.144     albertel  489: 	    #$result='';
1.135     albertel  490: 	}
                    491:     } elsif ($target eq 'edit') {
                    492: 	$result=&Apache::edit::tag_start($target,$token);
                    493: 	$result.=&Apache::edit::text_arg('Size:','size',$token,'5').
1.157     albertel  494: 	    &Apache::edit::text_arg('Click-On Texts (comma sep):',
                    495: 				    'addchars',$token,10);
                    496:         $result.=&Apache::edit::select_arg('Readonly:','readonly',
                    497: 					   ['no','yes'],$token);
1.314     raeburn   498:         my $spell_langs = &spelling_languages();
1.316     bisitz    499: 	$result.=&Apache::edit::select_arg('Spellcheck for:', 'spellcheck',
1.314     raeburn   500: 					   $spell_langs, $token);
1.157     albertel  501: 	$result.=&Apache::edit::end_row();
                    502: 	$result.=&Apache::edit::end_table();
1.135     albertel  503:     } elsif ($target eq 'modified') {
1.157     albertel  504: 	my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    505: 						     $safeeval,'size',
1.311     foxr      506: 						     'addchars','readonly', 'spellcheck');
1.135     albertel  507: 	if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
1.188     albertel  508:     } elsif ($target eq 'tex' 
                    509: 	     && $Apache::lonhomework::type ne 'exam') {
1.135     albertel  510: 	my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
                    511: 	if ($size != 0) {$size=$size*2; $size.=' mm';} else {$size='40 mm';}
1.257     onken     512: 	if ($env{'form.pdfFormFields'} eq 'yes'
                    513:             && $Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
1.246     onken     514:             my $fieldname = $env{'request.symb'}.
                    515:                                  '&part_'. $Apache::inputtags::part.
                    516:                                  '&textresponse'.
                    517:                                  '&HWVAL_' . $Apache::inputtags::response['-1'];
1.259     onken     518:             $result='\textField{'.$fieldname.'}{'.$size.'}{12 bp}';
1.246     onken     519:         } else {
                    520:             $result='\framebox['.$size.'][s]{\tiny\strut}';
                    521:         }
1.188     albertel  522:     } elsif ($target eq 'tex' 
                    523: 	     && $Apache::lonhomework::type eq 'exam'
                    524: 	     && &needs_exam_box($tagstack)) {
                    525: 	$result.=&exam_box($target);
1.135     albertel  526:     }
                    527:     return $result;
1.1       albertel  528: }
                    529: 
                    530: sub end_textline {
1.135     albertel  531:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    532:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    533:     elsif ($target eq 'edit') { return ('','no'); }
1.208     albertel  534:     &end_input();
1.135     albertel  535:     return "";
1.9       albertel  536: }
                    537: 
1.98      albertel  538: sub start_hiddenline {
                    539:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    540:     my $result = "";
1.211     albertel  541:     my $input_id = &start_input($parstack,$safeeval);
1.98      albertel  542:     if ($target eq 'web') {
                    543: 	$Apache::lonxml::evaluate--;
                    544: 	if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    545: 	    my $partid=$Apache::inputtags::part;
                    546: 	    my $id=$Apache::inputtags::response[-1];
1.211     albertel  547: 	    my $oldresponse = $Apache::lonhomework::history{"resource.$partid.$id.submission"};
                    548: 	    if (ref($oldresponse) eq 'ARRAY') {
                    549: 		$oldresponse = $oldresponse->[$#Apache::inputtags::inputlist];
                    550: 	    }
                    551: 	    $oldresponse = &HTML::Entities::encode($oldresponse,'<>&"');
                    552: 
1.98      albertel  553: 	    if ($Apache::lonhomework::type ne 'exam') {
                    554: 		$result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.
                    555: 		    $oldresponse.'" />';
                    556: 	    }
                    557: 	}
                    558:     } elsif ($target eq 'edit') {
                    559: 	$result=&Apache::edit::tag_start($target,$token);
                    560: 	$result.=&Apache::edit::end_table;
                    561:     }
1.189     albertel  562: 
                    563:     if ( ($target eq 'web' || $target eq 'tex')
                    564: 	 && $Apache::lonhomework::type eq 'exam'
                    565: 	 && &needs_exam_box($tagstack)) {
                    566: 	$result.=&exam_box($target);
                    567:     }
1.98      albertel  568:     return $result;
                    569: }
                    570: 
                    571: sub end_hiddenline {
1.135     albertel  572:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    573:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    574:     elsif ($target eq 'edit') { return ('','no'); }
1.211     albertel  575:     &end_input();
1.135     albertel  576:     return "";
1.98      albertel  577: }
                    578: 
1.286     www       579: 
                    580: sub start_hiddensubmission {
                    581:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    582:     my $result = "";
                    583:     my $input_id = &start_input($parstack,$safeeval);
                    584:     if ($target eq 'web') {
                    585:         $Apache::lonxml::evaluate--;
                    586:         if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    587:             my $partid=$Apache::inputtags::part;
                    588:             my $id=$Apache::inputtags::response[-1];
                    589:             if ($Apache::lonhomework::type ne 'exam') {
                    590:                 my $value = &Apache::lonxml::get_param('value',$parstack,$safeeval);
                    591:                 $value = &HTML::Entities::encode($value,'<>&"');
                    592:                 $result= '<input type="hidden" name="HWVAL_'.$id.'" value="'.$value.'" />';
                    593:             }
                    594:         }
                    595:     } elsif ($target eq 'edit') {
                    596:         $result=&Apache::edit::tag_start($target,$token);
                    597:         $result.=&Apache::edit::text_arg('Value:','value',$token,'15');
                    598:         $result.=&Apache::edit::end_row();
                    599:         $result.=&Apache::edit::end_table();
                    600:     } elsif ($target eq 'modified') {
                    601:         my $constructtag=&Apache::edit::get_new_args($token,$parstack,
                    602:                                                      $safeeval,'value');
                    603:         if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
                    604:     }
                    605: 
                    606:     if ( ($target eq 'web' || $target eq 'tex')
                    607:          && $Apache::lonhomework::type eq 'exam'
                    608:          && &needs_exam_box($tagstack)) {
                    609:         $result.=&exam_box($target);
                    610:     }
                    611:     return $result;
                    612: }
                    613: 
                    614: sub end_hiddensubmission {
                    615:     my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
                    616:     if    ($target eq 'web') { $Apache::lonxml::evaluate++; }
                    617:     elsif ($target eq 'edit') { return ('','no'); }
                    618:     &end_input();
                    619:     return "";
                    620: }
                    621: 
1.249     jms       622: =pod
                    623: 
                    624: =item file_selector()
                    625: 
                    626: $part -> partid
                    627: $id -> responseid
                    628: $uploadefiletypes -> comma seperated list of extensions allowed or * for any
1.252     raeburn   629: $which -> 'uploadonly'  -> only newly uploaded files
1.249     jms       630:           'portfolioonly' -> only allow files from portfolio
                    631:           'both' -> allow files from either location
                    632: $extratext -> additional text to go between the link and the input box
1.252     raeburn   633: $maxfilesize -> maximum cumulative filesize for submitted files (in MB).
1.249     jms       634: returns a table row <tr> 
                    635: 
                    636: =cut
                    637: 
1.160     albertel  638: sub file_selector {
1.252     raeburn   639:     my ($part,$id,$uploadedfiletypes,$which,$extratext,$maxfilesize)=@_;
1.160     albertel  640:     if (!$uploadedfiletypes) { return ''; }
1.167     albertel  641: 
                    642:     my $jspart=$part;
                    643:     $jspart=~s/\./_/g;
                    644: 
1.160     albertel  645:     my $result;
1.265     raeburn   646:     my $current_files_display = &current_file_submissions($part,$id);
                    647:     my $addfiles;
                    648:     if ($current_files_display) {
1.333.2.1  raeburn   649:         $result .= &Apache::lonhtmlcommon::row_title(&mt('Currently submitted files')).
1.265     raeburn   650:                    $current_files_display.
                    651:                    &Apache::lonhtmlcommon::row_closure();
                    652:         $addfiles = &mt('Submit other file(s)');
                    653:     } else {
                    654:         $addfiles = &mt('Choose file(s) to submit');
                    655:     }
                    656:     $result .= &Apache::lonhtmlcommon::row_title($addfiles);
                    657:     my $constraints;
                    658:     if ($uploadedfiletypes ne '*') {
                    659: 	$constraints =
                    660: 	    &mt('Allowed filetypes: [_1]','<b>'.$uploadedfiletypes.'</b>').'<br />';
                    661:     }
                    662:     if ($maxfilesize) {
1.319     bisitz    663:         $constraints .= &mt('Combined size of all files not to exceed: [_1] MB.',
1.265     raeburn   664:                         '<b>'.$maxfilesize.'</b>').'<br />';
                    665:     }
                    666:     if ($constraints) {
                    667:         $result .= $constraints.'<br />';
1.162     albertel  668:     }
1.333.2.7  raeburn   669:     if ($which eq 'uploadonly' || $which eq 'both') {
1.333.2.6  raeburn   670:         my $free_space = $maxfilesize * 1048576;
                    671:         $result .= &mt('Submit a file: (only one file per submission)').
                    672:             ' <br /><input type="file" size="50" name="HWFILE'.$jspart.'_'.$id.
1.333.2.7  raeburn   673:             '" id="HWFILE'.$jspart.'_'.$id.'" class="LC_flUpload LC_hwkfile" />'.
                    674:             '<input type="hidden" id="LC_free_space_'.$jspart.'_'.$id.'"'.
                    675:             ' value="'.$free_space.'" /><br />';
1.160     albertel  676:     }
1.265     raeburn   677:     if ( $which eq 'both') {
1.160     albertel  678: 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
                    679:     }
1.303     raeburn   680:     if ($which eq 'portfolioonly' || $which eq 'both') {
                    681:         my $symb = $env{'request.symb'};
                    682:         (undef,undef,my $res)=&Apache::lonnet::decode_symb($symb);
                    683:         my $showsymb;
                    684:         # If resource is a .task and URL is unencrypted, include symb in query string
                    685:         # for url opened in portfolio file selection window. Can be used to override
                    686:         # blocking of portfolio access resulting from an exam event in a different course. 
                    687:         if ($res =~ /\.task$/i) {
                    688:             my $encsymb = &Apache::lonenc::check_encrypt($symb);
                    689:             if ($symb eq $encsymb) {
                    690:                 $showsymb = $symb;
                    691:             }
                    692:         }
1.306     raeburn   693: 	$result.=$extratext.'<a href='."'".'javascript:void(window.open("/adm/portfolio?mode=selectfile&amp;fieldname='.$env{'form.request.prefix'}.'HWPORT'.$jspart.'_'.$id.'&amp;symb='.$showsymb.'","cat","height=600,width=800,scrollbars=1,resizable=1,menubar=2,location=1"))'."'".'>'.
1.265     raeburn   694: 	    &mt('Select Portfolio Files: (one or more files per submission)').'</a><br />'.
1.167     albertel  695: 	    '<input type="text" size="50" name="HWPORT'.$jspart.'_'.$id.'" value="" />'.
1.160     albertel  696: 	    '<br />';
                    697:     }
1.265     raeburn   698:     $result.=&Apache::lonhtmlcommon::row_closure(1);
1.160     albertel  699:     return $result;
                    700: }
                    701: 
1.265     raeburn   702: sub current_file_submissions {
1.205     albertel  703:     my ($part,$id) = @_;
1.265     raeburn   704:     my $jspart=$part;
                    705:     $jspart=~s/\./_/g;
1.270     raeburn   706:     my $uploadedfile=$Apache::lonhomework::history{"resource.$part.$id.uploadedfile"};
1.265     raeburn   707:     my $portfiles=$Apache::lonhomework::history{"resource.$part.$id.portfiles"};
                    708:     return if (($uploadedfile eq '') && ($portfiles !~/[^\s]/));
1.320     raeburn   709:     my $header = &portpath_popup_js().
                    710:                  &Apache::loncommon::start_data_table().
1.270     raeburn   711:                  &Apache::loncommon::start_data_table_header_row();
                    712:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    713:         $header .= '<th>'.&mt('Delete?').'</th>';
                    714:     }
1.271     raeburn   715:     $header .=   '<th>'.&mt('File').'</th>'.
1.265     raeburn   716:                  '<th>'.&mt('Size (MB)').'</th>'.
                    717:                  '<th>'.&mt('Last Modified').'</th>'.
                    718:                  &Apache::loncommon::end_data_table_header_row();
1.333.2.1  raeburn   719:     my (undef,$crsid,$udom,$uname)=&Apache::lonnet::whichuser();
1.265     raeburn   720:     my ($cdom,$cnum) = ($crsid =~ /^($LONCAPA::match_domain)_($LONCAPA::match_courseid)$/);
1.271     raeburn   721:     my ($result,$header_shown,%okfiles,%rows,%legacy,@bad_file_list);
1.265     raeburn   722:     if ($uploadedfile) {
                    723:         my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
1.270     raeburn   724:         my $link = &HTML::Entities::encode($url,'<>&"');
                    725:         my ($path,$name) = ($url =~ m{^(/uploaded/\Q$udom\E/\Q$uname\E/essayresponse.*/)([^/]+)$});
1.265     raeburn   726:         my ($status,$hashref,$error) =
1.270     raeburn   727:             &current_file_info($url,$link,$name,$path);
1.265     raeburn   728:         if ($status eq 'ok') {
                    729:             push(@{$okfiles{$name}},$url);
                    730:             $rows{$url} = $hashref;
1.271     raeburn   731:             $legacy{$url} = 1;
1.265     raeburn   732:             &Apache::lonxml::extlink($url);
                    733:             &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
                    734:         } else {
                    735:             push(@bad_file_list,$error);
                    736:         }
                    737:     }
1.333.2.1  raeburn   738:     if ($portfiles =~ /[^\s]/) {
1.265     raeburn   739:         my $prefix = "/uploaded/$udom/$uname/portfolio";
1.333.2.1  raeburn   740:         foreach my $file (split(/\s*,\s*/,&unescape($portfiles))) {
1.265     raeburn   741:             my ($path,$name) = ($file =~ m{^(.*/)([^/]+)$});
                    742:             my $url = $prefix.$path.$name;
                    743:             my $uploadedfile = &HTML::Entities::encode($url,'<>&"');
                    744:             my ($status,$hashref,$error) =
                    745:                 &current_file_info($url,$uploadedfile,$name,$path);
                    746:             if ($status eq 'ok') {
                    747:                 push(@{$okfiles{$name}},$url);
                    748:                 $rows{$url} = $hashref;
                    749:             } else {
                    750:                 push(@bad_file_list,$error);
                    751:             }
                    752:         }
                    753:     }
1.276     raeburn   754:     my $num = 0;
1.265     raeburn   755:     foreach my $name (sort(keys(%okfiles))) {
                    756:         if (ref($okfiles{$name}) eq 'ARRAY') {
                    757:             foreach my $url (@{$okfiles{$name}}) {
                    758:                 if (ref($rows{$url}) eq 'HASH') {
                    759:                     my $link = $rows{$url}{link};
                    760:                     my $portfile = $rows{$url}{path}.$rows{$url}{name};
                    761:                     $portfile = &HTML::Entities::encode($portfile,'<>&"');
                    762:                     if ($link) {
                    763:                         my $icon=&Apache::loncommon::icon($url);
                    764:                         unless ($header_shown) {
                    765:                             $result .= $header;
                    766:                             $header_shown = 1;
                    767:                         }
                    768:                         $result.=
1.270     raeburn   769:                             &Apache::loncommon::start_data_table_row()."\n";
                    770:                         if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    771:                             $result .=
                    772:                                  '<td valign="bottom"><input type="checkbox" name="HWFILE'.$jspart.'_'.$id.'_delete"'.
1.273     raeburn   773:                                  ' value="'.$portfile.'" id="HWFILE'.$jspart.'_'.$id.'_'.$num.'_delete" /></td>'."\n";
                    774:                             $num ++;
1.270     raeburn   775:                         }
1.320     raeburn   776:                         my $pathid = 'HWFILE'.$jspart.'_'.$id.'_'.$num.'_path';
                    777:                         my $pathidtext = $pathid.'text';
                    778:                         my ($showname,$showpath);
1.271     raeburn   779:                         if ($legacy{$url}) {
                    780:                             $showname = $name.' '.&mt('not in portfolio');
1.320     raeburn   781:                         } else {
                    782:                             $showname = $name;
                    783:                             $showpath = '<br />'. 
                    784:                                         '<span id="'.$pathidtext.'" class="LC_cusr_subheading">'.
                    785:                                         '<a href="javascript:showPortPath('."'$pathid','$pathidtext'".');" '.
                    786:                                         'class="LC_menubuttons_link">'.
                    787:                                         &mt('(Show path)').'</a></span>'.
                    788:                                         '<div id="'.$pathid.'" class="LC_dccid">'.$rows{$url}{path}.$name.
                    789: '</div>';
1.271     raeburn   790:                         }
1.270     raeburn   791:                         $result .= 
1.265     raeburn   792:                             '<td><a href="'.$link.'"><img src="'.$icon.
1.320     raeburn   793:                             '" border="0" alt="" />'.$showname.'</a>'.$showpath.'</td>'."\n".
1.265     raeburn   794:                             '<td align="right" valign="bottom">'.$rows{$url}{size}.'</td>'."\n".
                    795:                             '<td align="right" valign="bottom">'.$rows{$url}{lastmodified}.'</td>'."\n".
                    796:                             &Apache::loncommon::end_data_table_row();
                    797:                     }
                    798:                 }
                    799:             }
                    800:         }
                    801:     }
                    802:     if ($header_shown) {
1.307     raeburn   803:         $result .= &Apache::loncommon::end_data_table();
                    804:         if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
                    805:             $result .= '<br /><span class="LC_warning">'.
                    806:                        &mt('Exclude existing file(s) from grading by checking the "Delete?" checkbox(es) and clicking "Submit Answer"').'</span>';
                    807:         }
1.205     albertel  808:     }
                    809:     if (@bad_file_list) {
1.265     raeburn   810:         my $bad_files = '<span class="LC_filename">'.
                    811:             join('</span>, <span class="LC_filename">',@bad_file_list).
                    812:             '</span>';
                    813:         $result.='<p class="LC_error">'.
                    814:                  &mt("These file(s) don't exist: [_1]",$bad_files).
                    815:                  '</p>';
1.205     albertel  816:     }
                    817:     return $result;
1.265     raeburn   818: }
1.205     albertel  819: 
1.265     raeburn   820: sub current_file_info {
                    821:     my ($url,$uploadedfile,$name,$path) = @_;
                    822:     my ($status,$error,%info);
                    823:     my @stat = &Apache::lonnet::stat_file($url);
                    824:     if ((@stat) && ($stat[0] ne 'no_such_dir')) {
                    825:         my ($lastmod,$size);
                    826:         if ($stat[9] =~ /^\d+$/) {
                    827:             $lastmod = &Apache::lonlocal::locallocaltime($stat[9]);
                    828:         }
                    829:         $size = $stat[7]/(1024*1024);
                    830:         $size = sprintf("%.3f",$size);
                    831:         %info = (
                    832:                     link         => $uploadedfile,
                    833:                     name         => $name,
                    834:                     path         => $path,
                    835:                     size         => $size,
                    836:                     lastmodified => $lastmod,
                    837:                 );
                    838:         $status = 'ok';
                    839:     } else {
                    840:         &Apache::lonnet::logthis("bad file is $url");
                    841:         my $icon=&Apache::loncommon::icon($url);
                    842:         $error = '<a href="'.$url.'"><img src="'.$icon.
                    843:                  '" border="0" />'.$uploadedfile.'</a>';
                    844:     }
                    845:     return ($status,\%info,$error);
1.205     albertel  846: }
                    847: 
1.320     raeburn   848: sub portpath_popup_js {
                    849:     my %lt = &Apache::lonlocal::texthash(
                    850:                                           show => '(Show path)',
                    851:                                           hide => '(Hide)',
                    852:                                         );
                    853:     return <<"END";
                    854: <script type="text/javascript"> 
                    855: // <![CDATA[
                    856: 
                    857: function showPortPath(id,idtext) {
                    858:     document.getElementById(id).style.display='block';
                    859:     document.getElementById(id).style.textAlign='left';
                    860:     document.getElementById(id).style.textFace='normal';
                    861:     if (document.getElementById(idtext)) {
                    862:         document.getElementById(idtext).innerHTML ='<a href="javascript:hidePortPath(\\''+id+'\\',\\''+idtext+'\\'); '+
                    863:                                                    '"class="LC_menubuttons_link">$lt{'hide'}</a>&nbsp;';
                    864:     }
                    865:     return;
                    866: }
                    867: 
                    868: function hidePortPath(id,idtext) {
                    869:     if (document.getElementById(id)) {
                    870:         document.getElementById(id).style.display='none';
                    871:     }
                    872:     if (document.getElementById(idtext)) {
                    873:         document.getElementById(idtext).innerHTML ='<a href="javascript:showPortPath(\\''+id+'\\',\\''+idtext+'\\');" '+
                    874:                                                    'class="LC_menubuttons_link">$lt{'show'}</a>';
                    875:     }
                    876:     return;
                    877: }
                    878: 
                    879: // ]]>
                    880: </script>
                    881: 
                    882: END
                    883: }
                    884: 
1.179     albertel  885: sub valid_award {
                    886:     my ($award) =@_;
1.182     albertel  887:     foreach my $possibleaward ('EXTRA_ANSWER','MISSING_ANSWER', 'ERROR',
1.328     raeburn   888: 			       'NO_RESPONSE','WRONG_NUMBOXESCHECKED',
1.179     albertel  889: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
                    890: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
                    891: 			       'UNIT_FAIL', 'NO_UNIT',
                    892: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
1.272     www       893: 			       'BAD_FORMULA', 'NOT_FUNCTION', 'WRONG_FORMAT', 
                    894:                                'INTERNAL_ERROR', 'SIG_FAIL', 'INCORRECT', 
1.179     albertel  895: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
1.264     raeburn   896:                                'EXCESS_FILESIZE', 'FILENAME_INUSE', 
                    897: 			       'DRAFT', 'SUBMITTED', 'SUBMITTED_CREDIT', 
1.261     raeburn   898:                                'ANONYMOUS', 'ANONYMOUS_CREDIT',
                    899:                                'ASSIGNED_SCORE', 'APPROX_ANS',
                    900: 			       'EXACT_ANS','COMMA_FAIL') {
1.179     albertel  901: 	if ($award eq $possibleaward) { return 1; }
                    902:     }
                    903:     return 0;
                    904: }
                    905: 
1.207     albertel  906: {
                    907:     my @awards = ('EXTRA_ANSWER', 'MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
1.333.2.2  raeburn   908: 		  'WRONG_NUMBOXESCHECKED','TOO_LONG',
1.207     albertel  909: 		  'UNIT_INVALID_INSTRUCTOR', 'UNIT_INVALID_STUDENT',
                    910: 		  'UNIT_IRRECONCIBLE', 'UNIT_FAIL', 'NO_UNIT',
1.272     www       911: 		  'UNIT_NOTNEEDED', 'WANTED_NUMERIC', 'BAD_FORMULA',  'NOT_FUNCTION', 
                    912:                   'WRONG_FORMAT', 'INTERNAL_ERROR',
1.207     albertel  913: 		  'COMMA_FAIL', 'SIG_FAIL', 'INCORRECT', 'MISORDERED_RANK',
1.264     raeburn   914: 		  'INVALID_FILETYPE', 'EXCESS_FILESIZE', 'FILENAME_INUSE', 
                    915:                   'DRAFT', 'SUBMITTED',
1.261     raeburn   916:                   'SUBMITTED_CREDIT', 'ANONYMOUS', 'ANONYMOUS_CREDIT',
1.248     raeburn   917:                   'ASSIGNED_SCORE', 'APPROX_ANS', 'EXACT_ANS');
1.207     albertel  918:     my $i=0;
                    919:     my %fwd_awards = map { ($_,$i++) } @awards;
                    920:     my $max=scalar(@awards);
                    921:     @awards=reverse(@awards);
1.208     albertel  922:     $i=0;
1.207     albertel  923:     my %rev_awards = map { ($_,$i++) } @awards;
                    924: 
1.232     albertel  925: sub awarddetail_to_awarded {
                    926:     my ($awarddetail) = @_;
                    927:     if ($awarddetail eq 'EXACT_ANS'
                    928: 	|| $awarddetail eq 'APPROX_ANS') {
                    929: 	return 1;
                    930:     }
                    931:     return 0;
                    932: }
                    933: 
1.233     albertel  934: sub hide_award {
                    935:     my ($award) = @_;
                    936:     if (&Apache::lonhomework::show_no_problem_status()) {
                    937: 	return 1;
                    938:     }
                    939:     if ($award =~
1.261     raeburn   940: 	/^(?:EXACT_ANS|APPROX_ANS|SUBMITTED|SUBMITTED_CREDIT|ANONYMOUS|ANONYMOUS_CREDIT|ASSIGNED_SCORE|INCORRECT)/) {
1.233     albertel  941: 	return 1;
                    942:     }
                    943:     return 0;
                    944: }
                    945: 
1.9       albertel  946: sub finalizeawards {
1.232     albertel  947:     my ($awardref,$msgref,$nameref,$reverse,$final_scantron)=@_;
1.207     albertel  948:     my $result;
1.136     albertel  949:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
1.135     albertel  950:     if ($result eq '' ) {
                    951: 	my $blankcount;
1.207     albertel  952: 	foreach my $award (@$awardref) {
1.135     albertel  953: 	    if ($award eq '') {
                    954: 		$result='MISSING_ANSWER';
                    955: 		$blankcount++;
                    956: 	    }
                    957: 	}
1.232     albertel  958: 	if ($blankcount == ($#$awardref + 1)) {
                    959: 	    return ('NO_RESPONSE');
                    960: 	}
1.135     albertel  961:     }
1.251     www       962: 
                    963:     if ($Apache::lonxml::internal_error) { $result='INTERNAL_ERROR'; }
                    964: 
1.232     albertel  965:     if (!$final_scantron && defined($result)) { return ($result); }
1.181     albertel  966: 
1.232     albertel  967:     # if in scantron mode, if the award for any response is 
                    968:     # assigned score, then the part gets an assigned score
                    969:     if ($final_scantron 
                    970: 	&& grep {$_ eq 'ASSIGNED_SCORE'} (@$awardref)) {
                    971: 	return ('ASSIGNED_SCORE');
                    972:     }
                    973: 
                    974:     # if in scantron mode, if the award for any response is 
                    975:     # correct and there are non-correct responses,
                    976:     # then the part gets an assigned score
                    977:     if ($final_scantron 
                    978: 	&& (grep { $_ eq 'EXACT_ANS' ||
                    979: 		   $_ eq 'APPROX_ANS'  } (@$awardref))
                    980: 	&& (grep { $_ ne 'EXACT_ANS' &&
                    981: 		   $_ ne 'APPROX_ANS'  } (@$awardref))) {
                    982: 	return ('ASSIGNED_SCORE');
                    983:     }
1.181     albertel  984:     # these awards are ordered from most important error through best correct
1.207     albertel  985:     my $awards = (!$reverse) ? \%fwd_awards : \%rev_awards ;
                    986: 
                    987:     my $best = $max;
                    988:     my $j=0;
                    989:     my $which;
                    990:     foreach my $award (@$awardref) {
                    991: 	if ($awards->{$award} < $best) {
                    992: 	    $best  = $awards->{$award};
                    993: 	    $which = $j;
                    994: 	}
                    995: 	$j++;
                    996:     }
1.232     albertel  997: 
1.333.2.2  raeburn   998:     # if at least one response item is set to include lenient grading
                    999:     # and that item is partially correct then overall award reflects
                   1000:     # that, unless an award for one of the other response items does
                   1001:     # not fall within the basic awards for correct or incorrect.
                   1002:     if ($Apache::inputtags::leniency) {
                   1003:         if (($$awardref[$which] eq 'INCORRECT')
                   1004:             && (grep { $_ eq 'EXACT_ANS' ||
                   1005:                        $_ eq 'APPROX_ANS' ||
                   1006:                        $_ eq 'ASSIGNED_SCORE' } (@$awardref))
                   1007:             && !((grep { $_ ne 'INCORRECT' &&
                   1008:                          $_ ne 'EXACT_ANS' &&
                   1009:                          $_ ne 'APPROX_ANS' &&
                   1010:                          $_ ne 'ASSIGNED_SCORE' } (@$awardref)))) {
                   1011:             return ('ASSIGNED_SCORE');
                   1012:         }
                   1013:     }
                   1014: 
1.207     albertel 1015:     if (defined($which)) {
                   1016: 	if (ref($nameref)) {
                   1017: 	    return ($$awardref[$which],$$msgref[$which],$$nameref[$which]);
                   1018: 	} else {
                   1019: 	    return ($$awardref[$which],$$msgref[$which]);
                   1020: 	}
1.135     albertel 1021:     }
1.136     albertel 1022:     return ('ERROR',undef);
1.9       albertel 1023: }
1.207     albertel 1024: }
1.9       albertel 1025: 
1.10      albertel 1026: sub decideoutput {
1.325     raeburn  1027:     my ($award,$awarded,$awardmsg,$solved,$previous,$target,$nocorrect,$tdclass)=@_;
1.251     www      1028: 
1.135     albertel 1029:     my $message='';
                   1030:     my $button=0;
                   1031:     my $previousmsg;
1.221     albertel 1032:     my $css_class='orange';
1.148     albertel 1033:     my $added_computer_text=0;
1.221     albertel 1034:     my %possible_class =
                   1035: 	( 'correct'         => 'LC_answer_correct',
                   1036: 	  'charged_try'     => 'LC_answer_charged_try',
                   1037: 	  'not_charged_try' => 'LC_answer_not_charged_try',
                   1038: 	  'no_grade'        => 'LC_answer_no_grade',
                   1039: 	  'no_message'      => 'LC_no_message',
1.333.2.8  raeburn  1040:           'no_charge_warn'  => 'LC_answer_warning',
1.135     albertel 1041: 	  );
1.169     albertel 1042: 
1.180     albertel 1043:     my $part = $Apache::inputtags::part;
1.236     raeburn  1044:     my $tohandgrade = &Apache::lonnet::EXT("resource.$part.handgrade");
                   1045:     my $handgrade = ('yes' eq lc($tohandgrade)); 
1.282     www      1046: #
                   1047: # Should "Computer's Answer" be displayed?
                   1048: # Should not be displayed if still answerable,
                   1049: # if the problem is handgraded,
                   1050: # or if the problem does not give a correct answer
                   1051: #
1.180     albertel 1052:     
1.282     www      1053:     my $computer = ($handgrade || $nocorrect)? ''
1.333.2.4  raeburn  1054: 	                       : &mt("Computer's answer now shown above.");
1.180     albertel 1055:     &Apache::lonxml::debug("handgrade has :$handgrade:");
                   1056: 
1.135     albertel 1057:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
                   1058:     
1.194     banghart 1059:     if ($solved =~ /^correct/) {
1.221     albertel 1060:         $css_class=$possible_class{'correct'};
1.170     albertel 1061: 	$message=&mt('You are correct.');
                   1062: 	if ($awarded < 1 && $awarded > 0) {
                   1063: 	    $message=&mt('You are partially correct.');
1.221     albertel 1064: 	    $css_class=$possible_class{'not_charged_try'};
1.170     albertel 1065: 	} elsif ($awarded < 1) {
                   1066: 	    $message=&mt('Incorrect.');
1.221     albertel 1067: 	    $css_class=$possible_class{'charged_try'};
1.170     albertel 1068: 	}
1.291     www      1069: 	if ($handgrade || 
                   1070:             ($env{'request.filename'}=~/\/res\/lib\/templates\/(examupload|DropBox).problem$/)) {
1.172     albertel 1071: 	    $message = &mt("A score has been assigned.");
                   1072: 	    $added_computer_text=1;
1.135     albertel 1073: 	} else {
1.172     albertel 1074: 	    if ($target eq 'tex') {
                   1075: 		$message = '\textbf{'.$message.'}';
                   1076: 	    } else {
                   1077: 		$message = "<b>".$message."</b>";
1.333.2.4  raeburn  1078:                 if ($computer) {
                   1079:                     $message = "$computer $message";
                   1080:                 }
1.135     albertel 1081: 	    }
1.172     albertel 1082: 	    $added_computer_text=1;
1.235     albertel 1083: 	    if ($awarded > 0) {
                   1084: 		my ($symb) = &Apache::lonnet::whichuser();
                   1085: 		if (($symb ne '') 
                   1086: 		    &&
                   1087: 		    ($env{'course.'.$env{'request.course.id'}.
1.237     www      1088: 			      '.disable_receipt_display'} ne 'yes') &&
                   1089:                     ($Apache::lonhomework::type ne 'practice')) { 
1.235     albertel 1090: 		    $message.=(($target eq 'web')?'<br />':' ').
1.256     biermanm 1091: 			&mt('Your receipt no. is [_1]',
1.235     albertel 1092: 			    (&Apache::lonnet::receipt($Apache::inputtags::part).
                   1093: 			     (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
                   1094: 		}
1.135     albertel 1095: 	    }
                   1096: 	}
1.299     raeburn  1097:         if ($awarded >= 1) {
1.296     raeburn  1098:             $button=0;
1.299     raeburn  1099:         } elsif (&Apache::lonnet::EXT("resource.$part.retrypartial") !~/^1|on|yes$/i) {
1.296     raeburn  1100:             $button=0;
1.299     raeburn  1101:         } else {
1.296     raeburn  1102:             $button=1;
                   1103:         }
1.135     albertel 1104: 	$previousmsg='';
                   1105:     } elsif ($solved =~ /^excused/) {
                   1106: 	if ($target eq 'tex') {
                   1107: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
                   1108: 	} else {
                   1109: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
                   1110: 	}
1.221     albertel 1111: 	$css_class=$possible_class{'charged_try'};
1.135     albertel 1112: 	$button=0;
                   1113: 	$previousmsg='';
                   1114:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
                   1115: 	if ($solved =~ /^incorrect/ || $solved eq '') {
1.144     albertel 1116: 	    $message = &mt("Incorrect").".";
1.221     albertel 1117: 	    $css_class=$possible_class{'charged_try'};
1.135     albertel 1118: 	    $button=1;
                   1119: 	} else {
1.144     albertel 1120: 	    if ($target eq 'tex') {
                   1121: 		$message = '\textbf{'.&mt('You are correct.').'}';
                   1122: 	    } else {
                   1123: 		$message = "<b>".&mt('You are correct.')."</b>";
1.333.2.4  raeburn  1124:                 if ($computer) {
                   1125:                     $message = "$computer $message";
                   1126:                 }
1.144     albertel 1127: 	    }
1.148     albertel 1128: 	    $added_computer_text=1;
1.235     albertel 1129: 	    if  ($awarded > 0 
                   1130: 		 && $env{'course.'.
1.165     albertel 1131: 			     $env{'request.course.id'}.
1.235     albertel 1132: 			     '.disable_receipt_display'} ne 'yes') { 
1.135     albertel 1133: 		$message.=(($target eq 'web')?'<br />':' ').
1.235     albertel 1134: 		    &mt('Your receipt is [_1]',
                   1135: 			(&Apache::lonnet::receipt($Apache::inputtags::part).
                   1136: 			 (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'')));
1.135     albertel 1137: 	    }
1.221     albertel 1138: 	    $css_class=$possible_class{'correct'};
1.135     albertel 1139: 	    $button=0;
                   1140: 	    $previousmsg='';
                   1141: 	}
                   1142:     } elsif ($award eq 'NO_RESPONSE') {
                   1143: 	$message = '';
1.221     albertel 1144: 	$css_class=$possible_class{'no_feedback'};
1.135     albertel 1145: 	$button=1;
1.182     albertel 1146:     } elsif ($award eq 'EXTRA_ANSWER') {
                   1147: 	$message = &mt('Some extra items were submitted.');
1.221     albertel 1148: 	$css_class=$possible_class{'not_charged_try'};
1.182     albertel 1149: 	$button = 1;
1.135     albertel 1150:     } elsif ($award eq 'MISSING_ANSWER') {
1.245     bisitz   1151: 	$message = &mt('Some items were not submitted.');
                   1152:         if ($target ne 'tex') {
                   1153:            $message .= &Apache::loncommon::help_open_topic('Some_Items_Were_Not_Submitted');
                   1154:         }
1.221     albertel 1155: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1156: 	$button = 1;
1.328     raeburn  1157:     } elsif ($award eq 'WRONG_NUMBOXESCHECKED') {
                   1158:         $message = &mt('Number of boxes checked outside permissible range (either too few or too many).');
                   1159:         if ($target ne 'tex') {
                   1160:            $message .= &Apache::loncommon::help_open_topic('Wrong_Num_Boxes_Checked');
                   1161:         }
                   1162:         $css_class=$possible_class{'not_charged_try'};
                   1163:         $button = 1;
1.135     albertel 1164:     } elsif ($award eq 'ERROR') {
1.247     bisitz   1165: 	$message = &mt('An error occurred while grading your answer.');
1.221     albertel 1166: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1167: 	$button = 1;
                   1168:     } elsif ($award eq 'TOO_LONG') {
                   1169: 	$message = &mt("The submitted answer was too long.");
1.221     albertel 1170: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1171: 	$button=1;
                   1172:     } elsif ($award eq 'WANTED_NUMERIC') {
                   1173: 	$message = &mt("This question expects a numeric answer.");
1.221     albertel 1174: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1175: 	$button=1;
                   1176:     } elsif ($award eq 'MISORDERED_RANK') {
1.242     bisitz   1177:         $message = &mt('You have provided an invalid ranking.');
                   1178:         if ($target ne 'tex') {
                   1179:             $message.=' '.&mt('Please refer to [_1]',&Apache::loncommon::help_open_topic('Ranking_Problems',&mt('help on ranking problems')));
                   1180:         }
1.221     albertel 1181: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1182: 	$button=1;
1.248     raeburn  1183:     } elsif ($award eq 'EXCESS_FILESIZE') {
1.321     raeburn  1184:         $message = &mt("Submission won't be graded. The combined size of submitted files exceeded the amount allowed.");
1.248     raeburn  1185:         $css_class=$possible_class{'not_charged_try'};
                   1186:         $button=1;
1.264     raeburn  1187:     } elsif ($award eq 'FILENAME_INUSE') {
                   1188:         $message = &mt('You have already uploaded a file with that filename.');
                   1189:         if ($target eq 'tex') {
                   1190:             $message.= "\\\\\n";
                   1191:         } else {
                   1192:             $message .= '<br />';
                   1193:         }
1.315     bisitz   1194:         $message .= &mt('Please use a different filename.');
1.264     raeburn  1195:         $css_class=$possible_class{'not_charged_try'};
                   1196:         $button=1;
1.135     albertel 1197:     } elsif ($award eq 'INVALID_FILETYPE') {
1.255     bisitz   1198: 	$message = &mt("Submission won't be graded. The type of file submitted is not allowed.");
1.221     albertel 1199: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1200: 	$button=1;
                   1201:     } elsif ($award eq 'SIG_FAIL') {
1.145     albertel 1202: 	my ($used,$min,$max)=split(':',$awardmsg);
1.212     albertel 1203: 	my $word = ($used < $min) ? 'more' : 'fewer';
1.333.2.9  raeburn  1204: 	$message = &mt("Submission not graded. Use $word significant figures.",$used);
1.333.2.8  raeburn  1205:         if (&Apache::lonhomework::show_some_problem_status()) {
                   1206:             $css_class=$possible_class{'no_charge_warn'};
                   1207:         } else {
                   1208:             $css_class=$possible_class{'not_charged_try'};
                   1209:         }
1.135     albertel 1210: 	$button=1;
1.137     albertel 1211:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
                   1212: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
                   1213: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel 1214: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel 1215: 	$button=1;
                   1216:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
1.155     albertel 1217: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
1.137     albertel 1218: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel 1219: 	$css_class=$possible_class{'not_charged_try'};
1.137     albertel 1220: 	$button=1;
1.140     matthew  1221:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
1.155     albertel 1222: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
1.136     albertel 1223: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
1.221     albertel 1224: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1225: 	$button=1;
                   1226:     } elsif ($award eq 'UNIT_NOTNEEDED') {
1.155     albertel 1227: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
1.221     albertel 1228: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1229: 	$button=1;
                   1230:     } elsif ($award eq 'NO_UNIT') {
1.144     albertel 1231: 	$message = &mt("Units required").'.';
1.135     albertel 1232: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
1.221     albertel 1233: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1234: 	$button=1;
1.153     albertel 1235:     } elsif ($award eq 'COMMA_FAIL') {
                   1236: 	$message = &mt("Proper comma separation is required").'.';
1.221     albertel 1237: 	$css_class=$possible_class{'not_charged_try'};
1.153     albertel 1238: 	$button=1;
1.135     albertel 1239:     } elsif ($award eq 'BAD_FORMULA') {
1.240     www      1240: 	$message = &mt("Unable to understand formula").'.';
                   1241:         if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Formula_Answers')};
1.221     albertel 1242: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1243: 	$button=1;
1.272     www      1244:     } elsif ($award eq 'NOT_FUNCTION') {
                   1245:         $message = &mt("Not a function").'.';
                   1246:         $css_class=$possible_class{'not_charged_try'};
                   1247:         $button=1;
                   1248:     } elsif ($award eq 'WRONG_FORMAT') {
                   1249:         $message = &mt("Wrong format").'.';
                   1250:         $css_class=$possible_class{'not_charged_try'};
                   1251:         $button=1;
                   1252:      } elsif ($award eq 'INTERNAL_ERROR') {
1.251     www      1253:         $message = &mt("An internal error occurred while processing your answer. Please try again later.");
                   1254:         $css_class=$possible_class{'not_charged_try'};
                   1255:         $button=1;
1.135     albertel 1256:     } elsif ($award eq 'INCORRECT') {
1.144     albertel 1257: 	$message = &mt("Incorrect").'.';
1.221     albertel 1258: 	$css_class=$possible_class{'charged_try'};
1.135     albertel 1259: 	$button=1;
                   1260:     } elsif ($award eq 'SUBMITTED') {
                   1261: 	$message = &mt("Your submission has been recorded.");
1.221     albertel 1262: 	$css_class=$possible_class{'no_grade'};
1.135     albertel 1263: 	$button=1;
1.261     raeburn  1264:     } elsif ($award eq 'SUBMITTED_CREDIT') {
                   1265:         $message = &mt("Your submission has been recorded, and credit awarded.");
                   1266:         $css_class=$possible_class{'correct'};
                   1267:         $button=1;
                   1268:     } elsif ($award eq 'ANONYMOUS') {
                   1269:         $message = &mt("Your anonymous submission has been recorded.");
                   1270:         $css_class=$possible_class{'no_grade'};
                   1271:         $button=1;
                   1272:     } elsif ($award eq 'ANONYMOUS_CREDIT') {
                   1273:         $message = &mt("Your anonymous submission has been recorded, and credit awarded.");
                   1274:         $css_class=$possible_class{'correct'};
1.301     raeburn  1275:         $button=1;
1.135     albertel 1276:     } elsif ($award eq 'DRAFT') {
1.258     riegler  1277: 	$message = &mt("Copy saved but not submitted.");
1.221     albertel 1278: 	$css_class=$possible_class{'not_charged_try'};
1.135     albertel 1279: 	$button=1;
                   1280:     } elsif ($award eq 'ASSIGNED_SCORE') {
1.144     albertel 1281: 	$message = &mt("A score has been assigned.");
1.221     albertel 1282: 	$css_class=$possible_class{'correct'};
1.135     albertel 1283: 	$button=0;
1.144     albertel 1284:     } elsif ($award eq '') {
1.186     albertel 1285: 	if ($handgrade && $Apache::inputtags::status[-1] eq 'SHOW_ANSWER') {
                   1286: 	    $message = &mt("Nothing submitted.");
1.221     albertel 1287: 	    $css_class=$possible_class{'charged_try'};
1.186     albertel 1288: 	} else {
1.221     albertel 1289: 	    $css_class=$possible_class{'not_charged_try'};
1.186     albertel 1290: 	}
1.144     albertel 1291: 	$button=1;
1.135     albertel 1292:     } else {
                   1293: 	$message = &mt("Unknown message").": $award";
                   1294: 	$button=1;
                   1295:     }
1.209     albertel 1296:     my (undef,undef,$domain,$user)=&Apache::lonnet::whichuser();
1.194     banghart 1297:     foreach my $resid(@Apache::inputtags::response){
                   1298:         if ($Apache::lonhomework::history{"resource.$part.$resid.handback"}) {
1.254     raeburn  1299:             if ($target eq 'tex') {
                   1300:                 $message.= "\\\\\n";
                   1301:             } else {
                   1302:                 $message.='<br />';
                   1303:             }
1.198     albertel 1304: 	    my @files = split(/\s*,\s*/,
                   1305: 			      $Apache::lonhomework::history{"resource.$part.$resid.handback"});
                   1306: 	    my $file_msg;
                   1307: 	    foreach my $file (@files) {
1.254     raeburn  1308:                 if ($target eq 'tex') {
                   1309:                     $file_msg.= "\\\\\n".$file;
                   1310:                 } else {
                   1311:                     $file_msg.= '<br /><a href="/uploaded/'."$domain/$user".'/'.$file.'">'.$file.'</a>';
                   1312:                 }
1.198     albertel 1313: 	    }
                   1314: 	    $message .= &mt('Returned file(s): [_1]',$file_msg);
1.254     raeburn  1315:             if ($target eq 'tex') {
                   1316:                 $message.= "\\\\\n";
                   1317:             } else {
                   1318:                 $message.='<br />';
                   1319:             }
1.198     albertel 1320: 	}
1.194     banghart 1321:     }
                   1322: 
1.233     albertel 1323:     if (&Apache::lonhomework::hide_problem_status()
                   1324: 	&& $Apache::inputtags::status[-1] ne 'SHOW_ANSWER'
                   1325: 	&& &hide_award($award)) {
1.304     raeburn  1326:         $message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
                   1327:         my @interval= &Apache::lonnet::EXT("resource.$part.interval");
                   1328:         if ($interval[0] =~ /\d+/) {
                   1329:             my $first_access=&Apache::lonnet::get_first_access($interval[1]);
                   1330:             if (defined($first_access)) {
                   1331:                 my $due_date= &Apache::lonnet::EXT("resource.$part.duedate");
1.333.2.9.2.1  (raeburn 1332::                 my ($timelimit) = ($interval[0] =~ /^(\d+)/);
                   1333::                 unless (($due_date) && ($due_date < $first_access + $timelimit)) { 
1.304     raeburn  1334:                     $message = &mt("Answer Submitted: Your final submission will be graded when the time limit is reached.");
                   1335:                 }
                   1336:             }
                   1337:         }
1.221     albertel 1338: 	$css_class=$possible_class{'no_grade'};
1.135     albertel 1339: 	$button=1;
                   1340:     }
1.148     albertel 1341:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
1.150     albertel 1342: 	!$added_computer_text && $target ne 'tex') {
1.333.2.4  raeburn  1343:         if ($computer) {
                   1344:             $message = "$computer $message";
                   1345:         }
1.148     albertel 1346: 	$added_computer_text=1;
1.144     albertel 1347:     }
1.237     www      1348:     if ($Apache::lonhomework::type eq 'practice') {
1.244     raeburn  1349:        if ($target eq 'web') {
                   1350:            $message .= '<br />';
                   1351:        } else {
                   1352:            $message .= ' ';      
                   1353:        }
                   1354:        $message.=&mt('Submissions to practice problems are not permanently recorded.');
1.237     www      1355:     }
1.221     albertel 1356:     return ($button,$css_class,$message,$previousmsg);
1.12      albertel 1357: }
                   1358: 
1.155     albertel 1359: sub markup_unit {
                   1360:     my ($unit,$target)=@_;
                   1361:     if ($target eq 'tex') {
                   1362: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
                   1363:     } else {
                   1364: 	return "<tt>".$unit."</tt>";
                   1365:     }
                   1366: }
                   1367: 
1.88      albertel 1368: sub removealldata {
1.87      albertel 1369:     my ($id)=@_;
                   1370:     foreach my $key (keys(%Apache::lonhomework::results)) {
                   1371: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                   1372: 	    &Apache::lonxml::debug("Removing $key");
                   1373: 	    delete($Apache::lonhomework::results{$key});
                   1374: 	}
                   1375:     }
                   1376: }
                   1377: 
1.142     albertel 1378: sub hidealldata {
                   1379:     my ($id)=@_;
                   1380:     foreach my $key (keys(%Apache::lonhomework::results)) {
                   1381: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
                   1382: 	    &Apache::lonxml::debug("Hidding $key");
                   1383: 	    my $newkey=$key;
                   1384: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
                   1385: 	    $Apache::lonhomework::results{$newkey}=
                   1386: 		$Apache::lonhomework::results{$key};
                   1387: 	    delete($Apache::lonhomework::results{$key});
                   1388: 	}
                   1389:     }
                   1390: }
                   1391: 
1.12      albertel 1392: sub setgradedata {
1.136     albertel 1393:     my ($award,$msg,$id,$previously_used) = @_;
1.154     albertel 1394:     if ($Apache::lonhomework::scantronmode && 
1.165     albertel 1395: 	&Apache::lonnet::validCODE($env{'form.CODE'})) {
                   1396: 	$Apache::lonhomework::results{"resource.CODE"}=$env{'form.CODE'};
1.154     albertel 1397:     } elsif ($Apache::lonhomework::scantronmode && 
1.165     albertel 1398: 	     $env{'form.CODE'} eq '' &&
1.154     albertel 1399: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
                   1400: 	$Apache::lonhomework::results{"resource.CODE"}='';
1.141     albertel 1401:     }
1.154     albertel 1402: 
1.135     albertel 1403:     if (!$Apache::lonhomework::scantronmode &&
                   1404: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
                   1405: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
                   1406: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
1.87      albertel 1407: 	return '';
1.284     www      1408:     } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} < 1
1.233     albertel 1409: 	      || $Apache::lonhomework::scantronmode 
                   1410: 	      || &Apache::lonhomework::hide_problem_status()  ) {
1.154     albertel 1411:         # the student doesn't already have it correct,
                   1412: 	# or we are in a mode (scantron orno problem status) where a correct 
                   1413:         # can become incorrect
                   1414: 	# handle assignment of tries and solved status
1.135     albertel 1415: 	my $solvemsg;
                   1416: 	if ($Apache::lonhomework::scantronmode) {
                   1417: 	    $solvemsg='correct_by_scantron';
                   1418: 	} else {
                   1419: 	    $solvemsg='correct_by_student';
                   1420: 	}
                   1421: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
                   1422: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
                   1423: 	}
                   1424: 	if ( $award eq 'ASSIGNED_SCORE') {
                   1425: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1426: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1427: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1428: 		$solvemsg;
                   1429: 	    my $numawards=scalar(@Apache::inputtags::response);
                   1430: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1431: 	    foreach my $res (@Apache::inputtags::response) {
1.232     albertel 1432: 		if (defined($Apache::lonhomework::results{"resource.$id.$res.awarded"})) {
                   1433: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                   1434: 			$Apache::lonhomework::results{"resource.$id.$res.awarded"};
                   1435: 		} else {
                   1436: 		    $Apache::lonhomework::results{"resource.$id.awarded"}+=
                   1437: 			&awarddetail_to_awarded($Apache::lonhomework::results{"resource.$id.$res.awarddetail"});
                   1438: 		}
1.135     albertel 1439: 	    }
                   1440: 	    if ($numawards > 0) {
                   1441: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
                   1442: 		    $numawards;
                   1443: 	    }
                   1444: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
                   1445: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1446: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1447: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1448: 		$solvemsg;
                   1449: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1.261     raeburn  1450:         } elsif ( $award eq 'SUBMITTED_CREDIT' ) {
                   1451:             $Apache::lonhomework::results{"resource.$id.tries"} =
                   1452:                 $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1453:             $Apache::lonhomework::results{"resource.$id.solved"} =
                   1454:                 'credit_attempted';
                   1455:             $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
                   1456:         }  elsif ( $award eq 'ANONYMOUS_CREDIT' ) {
                   1457:             $Apache::lonhomework::results{"resource.$id.tries"} =
                   1458:                 $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1459:             $Apache::lonhomework::results{"resource.$id.solved"} =
                   1460:                 'credit_attempted';
                   1461:             $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
1.135     albertel 1462: 	} elsif ( $award eq 'INCORRECT' ) {
                   1463: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1464: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
1.233     albertel 1465: 	    if (&Apache::lonhomework::hide_problem_status()
                   1466: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1467: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1468: 	    }
                   1469: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1470: 		'incorrect_attempted';
                   1471: 	} elsif ( $award eq 'SUBMITTED' ) {
                   1472: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
                   1473: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1474: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1475: 		'ungraded_attempted';
1.261     raeburn  1476:         }  elsif ( $award eq 'ANONYMOUS' ) {
                   1477:             $Apache::lonhomework::results{"resource.$id.tries"} =
                   1478:                 $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1479:             $Apache::lonhomework::results{"resource.$id.solved"} =
                   1480:                 'ungraded_attempted';
1.135     albertel 1481: 	} elsif ( $award eq 'DRAFT' ) {
                   1482: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
                   1483: 	} elsif ( $award eq 'NO_RESPONSE' ) {
                   1484: 	    #no real response so delete any data that got stored
1.129     albertel 1485: 	    &removealldata($id);
                   1486: 	    return '';
                   1487: 	} else {
1.135     albertel 1488: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
                   1489: 		'incorrect_attempted';
1.233     albertel 1490: 	    if (&Apache::lonhomework::show_no_problem_status()
                   1491: 		|| $Apache::lonhomework::scantronmode) {
1.135     albertel 1492: 		$Apache::lonhomework::results{"resource.$id.tries"} =
                   1493: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
                   1494: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
                   1495: 	    }
1.233     albertel 1496: 
                   1497: 	    if (&Apache::lonhomework::show_some_problem_status()) {
                   1498: 		# clear out the awarded if they had gotten it wrong/right
                   1499: 		# and are now in an error mode	
                   1500: 		$Apache::lonhomework::results{"resource.$id.awarded"} = '';
                   1501: 	    }
1.135     albertel 1502: 	}
1.136     albertel 1503: 	if (defined($msg)) {
                   1504: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
                   1505: 	}
1.135     albertel 1506: 	# did either of the overall awards chage? If so ignore the 
                   1507: 	# previous check
                   1508: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
                   1509: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
                   1510: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
                   1511: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
                   1512: 	    # check if this was a previous submission if it was delete the
                   1513: 	    # unneeded data and update the previously_used attribute
                   1514: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
1.233     albertel 1515: 		if (&Apache::lonhomework::show_problem_status()) {
1.135     albertel 1516: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
                   1517: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
                   1518: 		}
                   1519: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
                   1520: 		#delete all data as they student didn't do anything, but save
                   1521: 		#the list of collaborators.
                   1522: 		&removealldata($id);
                   1523: 		#and since they didn't do anything we were never here
                   1524: 		return '';
                   1525: 	    } else {
                   1526: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
                   1527: 	    }
1.101     albertel 1528: 	}
1.283     www      1529:     } elsif ( $Apache::lonhomework::history{"resource.$id.awarded"} == 1 ) {
1.135     albertel 1530: 	#delete all data as they student already has it correct
                   1531: 	&removealldata($id);
                   1532: 	#and since they didn't do anything we were never here
                   1533: 	return '';
1.40      albertel 1534:     }
1.135     albertel 1535:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
1.184     albertel 1536:     if ($award eq 'SUBMITTED') {
                   1537: 	&Apache::response::add_to_gradingqueue();
                   1538:     }
1.288     www      1539:     $Apache::lonhomework::results{"resource.$id.type"} = $Apache::lonhomework::type;
1.289     www      1540:     $Apache::lonhomework::results{"resource.$id.duedate"} = &Apache::lonnet::EXT("resource.$id.duedate");
                   1541:     $Apache::lonhomework::results{"resource.$id.hinttries"} = &Apache::lonnet::EXT("resource.$id.hinttries");
1.330     raeburn  1542:     $Apache::lonhomework::results{"resource.$id.version"} = &Apache::lonnet::usedversion();
                   1543:     $Apache::lonhomework::results{"resource.$id.maxtries"} = &Apache::lonnet::EXT("resource.$id.maxtries");
1.10      albertel 1544: }
                   1545: 
1.219     albertel 1546: sub find_which_previous {
                   1547:     my ($version) = @_;
                   1548:     my $part = $Apache::inputtags::part;
                   1549:     my (@previous_version);
                   1550:     foreach my $resp (@Apache::inputtags::response) {
                   1551: 	my $key = "$version:resource.$part.$resp.submission";
                   1552: 	my $submission = $Apache::lonhomework::history{$key};
                   1553: 	my %previous = &Apache::response::check_for_previous($submission,
                   1554: 							     $part,$resp,
                   1555: 							     $version);
                   1556: 	push(@previous_version,$previous{'version'});
                   1557:     }
                   1558:     return &previous_match(\@previous_version,
                   1559: 			   scalar(@Apache::inputtags::response));
                   1560: }
                   1561: 
                   1562: sub previous_match {
                   1563:     my ($previous_array,$count) = @_;
                   1564:     my $match = 0;
                   1565:     my @matches;
                   1566:     foreach my $versionar (@$previous_array) {
                   1567: 	foreach my $version (@$versionar) {
                   1568: 	    $matches[$version]++;
                   1569: 	}
                   1570:     }
                   1571:     my $which=0;
                   1572:     foreach my $elem (@matches) {
                   1573: 	if ($elem eq $count) {
                   1574: 	    $match=1;
                   1575: 	    last;
                   1576: 	}
                   1577: 	$which++;
                   1578:     }
                   1579:     return ($match,$which);
                   1580: }
                   1581: 
1.9       albertel 1582: sub grade {
1.135     albertel 1583:     my ($target) = @_;
                   1584:     my $id = $Apache::inputtags::part;
                   1585:     my $response='';
1.165     albertel 1586:     if ( defined $env{'form.submitted'}) {
1.136     albertel 1587: 	my (@awards,@msgs);
1.135     albertel 1588: 	foreach $response (@Apache::inputtags::response) {
                   1589: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
                   1590: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
                   1591: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
                   1592: 	    push (@awards,$value);
1.136     albertel 1593: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
                   1594: 	    &Apache::lonxml::debug("got message $value from $response for $id");
                   1595: 	    push (@msgs,$value);
1.135     albertel 1596: 	}
1.232     albertel 1597: 	my ($finalaward,$msg) = 
                   1598: 	    &finalizeawards(\@awards,\@msgs,undef,undef,
                   1599: 			    $Apache::lonhomework::scantronmode);
1.135     albertel 1600: 	my $previously_used;
                   1601: 	if ( $#Apache::inputtags::previous eq $#awards ) {
1.219     albertel 1602: 	    my ($match) =
                   1603: 		&previous_match(\@Apache::inputtags::previous_version,
                   1604: 				scalar(@Apache::inputtags::response));
1.244     raeburn  1605: 
1.135     albertel 1606: 	    if ($match) {
                   1607: 		$previously_used = 'PREVIOUSLY_LAST';
                   1608: 		foreach my $value (@Apache::inputtags::previous) {
                   1609: 		    if ($value eq 'PREVIOUSLY_USED' ) {
                   1610: 			$previously_used = $value;
                   1611: 			last;
                   1612: 		    }
1.75      albertel 1613: 		}
                   1614: 	    }
1.43      albertel 1615: 	}
1.136     albertel 1616: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
                   1617: 	&setgradedata($finalaward,$msg,$id,$previously_used);
1.43      albertel 1618:     }
1.135     albertel 1619:     return '';
1.1       albertel 1620: }
                   1621: 
1.217     albertel 1622: sub get_grade_messages {
1.325     raeburn  1623:     my ($id,$prefix,$target,$status,$nocorrect,$tdclass) = @_;
1.282     www      1624: # nocorrect suppresses "Computer's answer now shown above"
1.217     albertel 1625:     my ($message,$latemessage,$trystr,$previousmsg);
                   1626:     my $showbutton = 1;
                   1627: 
                   1628:     my $award = $Apache::lonhomework::history{"$prefix.award"};
                   1629:     my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
                   1630:     my $solved = $Apache::lonhomework::history{"$prefix.solved"};
                   1631:     my $previous = $Apache::lonhomework::history{"$prefix.previous"};
                   1632:     my $awardmsg = $Apache::lonhomework::history{"$prefix.awardmsg"};
                   1633:     &Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
                   1634:     if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
                   1635: 	&Apache::lonxml::debug('Getting message');
1.221     albertel 1636: 	($showbutton,my $css_class,$message,$previousmsg) =
1.217     albertel 1637: 	    &decideoutput($award,$awarded,$awardmsg,$solved,$previous,
1.325     raeburn  1638: 			  $target,(($status eq 'CAN_ANSWER') || $nocorrect),$tdclass);
1.217     albertel 1639: 	if ($target eq 'tex') {
                   1640: 	    $message='\vskip 2 mm '.$message.' ';
                   1641: 	} else {
1.325     raeburn  1642: 	    $message="<td class=\"$tdclass $css_class\">$message</td>";
1.217     albertel 1643: 	    if ($previousmsg) {
1.325     raeburn  1644: 		$previousmsg="<td class=\"$tdclass LC_answer_previous\">$previousmsg</td>";
1.217     albertel 1645: 	    }
                   1646: 	}
                   1647:     }
                   1648:     my $tries = $Apache::lonhomework::history{"$prefix.tries"};
                   1649:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
                   1650:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
                   1651:     #if tries are set to negative turn off the Tries/Button and messages
                   1652:     if (defined($maxtries) && $maxtries < 0) { return ''; }
                   1653:     if ( $tries eq '' ) { $tries = '0'; }
                   1654:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
                   1655:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
1.292     christia 1656:     my $tries_text= &get_tries_text();
1.217     albertel 1657:     if ($showbutton) {
                   1658: 	if ($target eq 'tex') {
                   1659: 	    if ($env{'request.state'} ne "construct"
                   1660: 		&& $Apache::lonhomework::type ne 'exam'
                   1661: 		&& $env{'form.suppress_tries'} ne 'yes') {
1.292     christia 1662: 		$trystr ='{\vskip 1 mm \small '
                   1663:                         .&mt('[_1]'.$tries_text.'[_2] [_3]'
                   1664: 				,'\textit{','}',$tries.'/'.$maxtries ) 
                   1665:                         .'} \vskip 2 mm';
1.217     albertel 1666: 	    } else {
                   1667: 		$trystr = '\vskip 0 mm ';
                   1668: 	    }
                   1669: 	} else {
1.292     christia 1670: 	    my $trial =$tries;
1.217     albertel 1671: 	    if ($Apache::lonhomework::parsing_a_task) {
                   1672: 	    } elsif($env{'request.state'} ne 'construct') {
1.292     christia 1673: 		$trial.="/".&Apache::lonhtmlcommon::direct_parm_link($maxtries,$env{'request.symb'},'maxtries',$id,$target);
1.217     albertel 1674: 	    } else {
                   1675: 		if (defined($Apache::inputtags::params{'maxtries'})) {
1.292     christia 1676: 		    $trial.="/".$Apache::inputtags::params{'maxtries'};
1.217     albertel 1677: 		}
                   1678: 	    }
1.325     raeburn  1679: 	    $trystr = '<td class="'.$tdclass.'"><span class="LC_nobreak">'.&mt($tries_text.' [_1]',$trial).'</span></td>';
1.217     albertel 1680: 	}
                   1681:     }
1.221     albertel 1682: 
1.217     albertel 1683:     if ($Apache::lonhomework::history{"$prefix.afterduedate"}) {
                   1684: 	#last submissions was after due date
                   1685: 	$latemessage=&mt(' The last submission was after the Due Date ');;
                   1686: 	if ($target eq 'web') {
1.325     raeburn  1687: 	    $latemessage='<td class="'.$tdclass.' LC_answer_late">'.$latemessage.'</td>';
1.217     albertel 1688: 	}
                   1689:     }
                   1690:     return ($previousmsg,$latemessage,$message,$trystr,$showbutton);
                   1691: }
                   1692: 
1.11      albertel 1693: sub gradestatus {
1.223     albertel 1694:     my ($id,$target,$no_previous) = @_;
1.135     albertel 1695:     my $showbutton = 1;
                   1696:     my $message = '';
                   1697:     my $latemessage = '';
                   1698:     my $trystr='';
                   1699:     my $button='';
                   1700:     my $previousmsg='';
1.325     raeburn  1701:     my $tdclass='';
1.135     albertel 1702: 
                   1703:     my $status = $Apache::inputtags::status['-1'];
                   1704:     &Apache::lonxml::debug("gradestatus has :$status:");
1.183     albertel 1705:     if ( $status ne 'CLOSED' 
                   1706: 	 && $status ne 'UNAVAILABLE' 
                   1707: 	 && $status ne 'INVALID_ACCESS' 
                   1708: 	 && $status ne 'NEEDS_CHECKIN' 
1.300     raeburn  1709: 	 && $status ne 'NOT_IN_A_SLOT'
                   1710:          && $status ne 'RESERVABLE'
                   1711:          && $status ne 'RESERVABLE_LATER'
                   1712:          && $status ne 'NOTRESERVABLE') {
1.217     albertel 1713: 
1.305     raeburn  1714: 	if ($status eq 'SHOW_ANSWER') {
                   1715:             $showbutton = 0;
                   1716:         }
                   1717: 
1.325     raeburn  1718:         unless (($status eq 'SHOW_ANSWER') || ($status eq 'CANNOT_ANSWER')) {
                   1719:             if ($target ne 'tex') {
                   1720:                 $tdclass = 'LC_status_submit_'.$id;
                   1721:             }
                   1722:         }
                   1723: 
1.217     albertel 1724: 	($previousmsg,$latemessage,$message,$trystr) =
                   1725: 	    &get_grade_messages($id,"resource.$id",$target,$status,
1.325     raeburn  1726: 				$showbutton,$tdclass);
1.305     raeburn  1727: 	if ($status eq 'CANNOT_ANSWER') {
1.217     albertel 1728: 	    $showbutton = 0;
1.164     albertel 1729: 	}
1.218     albertel 1730: 	if ( $status eq 'SHOW_ANSWER') {
                   1731: 	    undef($previousmsg);
                   1732: 	}
1.325     raeburn  1733: 	if ( $showbutton ) {
1.135     albertel 1734: 	    if ($target ne 'tex') {
1.230     albertel 1735: 		$button = 
1.262     www      1736:             '<input onmouseup="javascript:setSubmittedPart(\''.$id.'\');this.form.action+=\'#'.&escape($id).'\';"
1.325     raeburn  1737:                     type="submit" name="submit_'.$id.'" id="submit_'.$id.'" class="LC_hwk_submit"
                   1738:                     value="'.&mt('Submit Answer').'" />&nbsp;'.
                   1739:                     '<div id="msg_submit_'.$id.'" style="display:none">'.
                   1740:                     &mt('Processing your submission ...').'</div>';
1.135     albertel 1741: 	    }
                   1742: 	}
1.217     albertel 1743: 
1.135     albertel 1744:     }
                   1745:     my $output= $previousmsg.$latemessage.$message.$trystr;
                   1746:     if ($output =~ /^\s*$/) {
                   1747: 	return $button;
1.63      sakharuk 1748:     } else {
1.135     albertel 1749: 	if ($target eq 'tex') {
                   1750: 	    return $button.' \vskip 0 mm '.$output.' ';
                   1751: 	} else {
1.223     albertel 1752: 	    $output =
                   1753: 		'<table><tr><td>'.$button.'</td>'.$output;
                   1754: 	    if (!$no_previous) {
1.325     raeburn  1755: 		$output.='<td class="'.$tdclass.'">'.&previous_tries($id,$target).'</td>';
1.223     albertel 1756: 	    }
                   1757: 	    $output.= '</tr></table>';
                   1758: 	    return $output;
1.135     albertel 1759: 	}
1.63      sakharuk 1760:     }
1.11      albertel 1761: }
1.217     albertel 1762: 
                   1763: sub previous_tries {
                   1764:     my ($id,$target) = @_;
                   1765:     my $output;
                   1766:     my $status = $Apache::inputtags::status['-1'];
1.219     albertel 1767: 
                   1768:     my $count;
                   1769:     my %count_lookup;
1.333     raeburn  1770:     my ($lastrndseed,$lasttype);
1.318     raeburn  1771:     my $numstamps = 0;
1.219     albertel 1772: 
1.217     albertel 1773:     foreach my $i (1..$Apache::lonhomework::history{'version'}) {
                   1774: 	my $prefix = $i.":resource.$id";
1.333     raeburn  1775:         my $is_anon;
                   1776:         my $curr_type = $Apache::lonhomework::history{"$prefix.type"};    
1.263     raeburn  1777:         if (defined($env{'form.grade_symb'})) {
1.333     raeburn  1778:             if (($curr_type eq 'anonsurvey') || ($curr_type eq 'anonsurveycred')) {
1.263     raeburn  1779:                 $is_anon = 1;
                   1780:             }
                   1781:         }
1.217     albertel 1782: 	next if (!exists($Apache::lonhomework::history{"$prefix.award"}));
1.219     albertel 1783: 	$count++;
                   1784: 	$count_lookup{$i} = $count;
1.274     raeburn  1785:         my $curr_rndseed = $Apache::lonhomework::history{"$prefix.rndseed"};
1.217     albertel 1786: 	my ($previousmsg,$latemessage,$message,$trystr);
                   1787: 
                   1788: 	($previousmsg,$latemessage,$message,$trystr) =
                   1789: 	    &get_grade_messages($id,"$prefix",$target,$status);
                   1790: 
1.219     albertel 1791: 	if ($previousmsg ne '') {
                   1792: 	    my ($match,$which) = &find_which_previous($i);
                   1793: 	    $message=$previousmsg;
                   1794: 	    my $previous = $count_lookup{$which};
1.226     albertel 1795: 	    $message =~ s{(</td>)}{ as submission \# $previous $1};
1.221     albertel 1796: 	} elsif ($Apache::lonhomework::history{"$prefix.tries"}) {
1.233     albertel 1797: 	    if (!(&Apache::lonhomework::hide_problem_status()
1.225     albertel 1798: 		  && $Apache::inputtags::status[-1] ne 'SHOW_ANSWER')
                   1799: 		&& $Apache::lonhomework::history{"$prefix.solved"} =~/^correct/
                   1800: 		) {
                   1801: 		
1.238     bisitz   1802:                 my $txt_correct = &mt('Correct');
1.279     raeburn  1803:                 my $awarded = $Apache::lonhomework::history{"$prefix.awarded"};
                   1804:                 if ($awarded < 1 && $awarded > 0) {
1.280     raeburn  1805:                     $txt_correct=&mt('Partially Correct');
1.279     raeburn  1806:                 } elsif ($awarded < 1) {
                   1807:                     if ($awarded eq '') {
                   1808:                         $txt_correct='';
                   1809:                     } else {
                   1810:                         $txt_correct=&mt('Incorrect');
                   1811:                     }
                   1812:                 }
1.221     albertel 1813: 		$message =~ s{(<td.*?>)(.*?)(</td>)}
1.238     bisitz   1814:                              {$1 <strong>$txt_correct</strong>. $3}s;
1.221     albertel 1815: 	    }
1.238     bisitz   1816:             my $trystr = "(".&mt('Try [_1]',$Apache::lonhomework::history{"$prefix.tries"}).")";
1.332     raeburn  1817:             if (($curr_rndseed ne '') &&  ($lastrndseed ne '')) {
1.333     raeburn  1818:                 if (($curr_rndseed ne $lastrndseed) && 
                   1819:                     (($curr_type eq 'randomizetry') || ($lasttype eq 'randomizetry'))) {
1.274     raeburn  1820:                     $trystr .= '<br /><span style="color: green; white-space: nowrap; font-style: italic; font-weight: bold; font-size: 80%;">'.&mt('New problem variation this try.').'</span>';
                   1821:                 }
                   1822:             } 
1.221     albertel 1823: 	    $message =~ s{(</td>)}{ $trystr $1};
1.219     albertel 1824: 	}
1.221     albertel 1825: 	my ($class) = ($message =~ m{<td.*class="([^"]*)"}); #"
                   1826: 	$message =~ s{(<td.*?>)}{<td>};
                   1827: 	
1.219     albertel 1828: 
1.318     raeburn  1829: 	$output .= '<tr class="'.$class.'">'.
                   1830: 	           '<td align="center">'.$count.'</td>'.$message;
                   1831:         if ((!$is_anon) && ($Apache::lonhomework::history{"$prefix.tries"}) &&
                   1832:             ($Apache::lonhomework::history{"$prefix.award"} ne 'ASSIGNED_SCORE') &&
                   1833:             ($Apache::lonhomework::history{$i.':timestamp'})) {
                   1834:             $output .= '<td>'.&Apache::lonlocal::locallocaltime(
                   1835:                              $Apache::lonhomework::history{$i.':timestamp'}).'</td>';
                   1836:             $numstamps ++;
                   1837:         } else {
                   1838:             $output .= '<td></td>';
                   1839:         }
1.217     albertel 1840: 	foreach my $resid (@Apache::inputtags::response) {
                   1841: 	    my $prefix = $prefix.".$resid";
                   1842: 	    if (exists($Apache::lonhomework::history{"$prefix.submission"})) {
                   1843: 		my $submission =
                   1844: 		    $Apache::inputtags::submission_display{"$prefix.submission"};
                   1845: 		if (!defined($submission)) {
                   1846: 		    $submission = 
                   1847: 			$Apache::lonhomework::history{"$prefix.submission"};
                   1848: 		}
1.263     raeburn  1849:                 if ($is_anon) {
                   1850:                     $output.='<td>'.&mt('(only shown to submitter)').'</td>';
                   1851:                 } else {
                   1852: 		    $output.='<td>'.$submission.'</td>';
                   1853:                 }
1.217     albertel 1854: 	    } else {
                   1855: 		$output.='<td></td>';
                   1856: 	    }
                   1857: 	}
1.221     albertel 1858: 	$output.=&Apache::loncommon::end_data_table_row()."\n";
1.274     raeburn  1859:         $lastrndseed = $curr_rndseed;
1.333     raeburn  1860:         $lasttype = $curr_type;
1.217     albertel 1861:     }
                   1862:     return if ($output eq '');
1.318     raeburn  1863:     my $headers = '<tr>'.
                   1864:                   '<th>'.&mt('Submission #').'</th>'.
                   1865:                   '<th>'.&mt('Try').'</th><th>';
                   1866:     if ($numstamps) {
                   1867:         $headers .= &mt('When');
                   1868:     }
                   1869:     $headers .= '</th>';
                   1870:     my $colspan = scalar(@Apache::inputtags::response);
                   1871:     if ($colspan > 1) {
                   1872:         $headers .= '<th colspan="'.$colspan.'">';
                   1873:     } else {
                   1874:         $headers .= '<th>';
                   1875:     }
                   1876:     $headers .= &mt('Submitted Answer').'</th></tr>';
1.298     www      1877:     $output ='<table class="LC_prior_tries">'.$headers.$output.'</table>';
1.226     albertel 1878: 
1.263     raeburn  1879:     my $tries_text = &get_tries_text('link');
1.231     albertel 1880:     my $prefix = $env{'form.request.prefix'};
                   1881:     $prefix =~ tr{.}{_};
1.333.2.5  raeburn  1882:     my $function_name = 'LONCAPA_previous_tries_'.$prefix;
1.333.2.9.2.3! (raeburn 1883::     if (($env{'request.state'} eq 'construct') || ($id =~ /\W/)) {
1.333.2.5  raeburn  1884:         $function_name .= $Apache::lonxml::curdepth;
                   1885:     } else {
1.333.2.9.2.3! (raeburn 1886::         $function_name .= $id;
1.333.2.5  raeburn  1887:     }
                   1888:     $function_name .= '_'.$Apache::lonxml::counter;
1.333.2.9.2.2  (raeburn 1889::     my $possmathjax = 1;
                   1890::     my $result = &Apache::loncommon::modal_adhoc_window($function_name,420,410,$output,
                   1891::                                                         &mt($tries_text),$possmathjax)."<br />";
1.217     albertel 1892:     return $result;
                   1893: }
                   1894: 
1.263     raeburn  1895: sub get_tries_text {
                   1896:     my ($context) = @_;
                   1897:     my $tries_text;
                   1898:     if ($context eq 'link') {
                   1899:         $tries_text = 'Previous Tries';
                   1900:     } else {
                   1901:         $tries_text = 'Tries';
                   1902:     }
                   1903:     if ( $Apache::lonhomework::type eq 'survey' ||
                   1904:          $Apache::lonhomework::type eq 'surveycred' ||
                   1905:          $Apache::lonhomework::type eq 'anonsurvey' ||
                   1906:          $Apache::lonhomework::type eq 'anonsurveycred' ||
                   1907:          $Apache::lonhomework::parsing_a_task) {
                   1908:         if ($context eq 'link') {
                   1909:             $tries_text = 'Previous Submissions';
                   1910:         } else {
                   1911:             $tries_text = 'Submissions';
                   1912:         }
                   1913:     }
                   1914:     return $tries_text;
                   1915: }
                   1916: 
1.314     raeburn  1917: sub spelling_languages {
                   1918:     my %langchoices;
                   1919:     foreach my $id (&Apache::loncommon::languageids()) {
                   1920:         my $code = &Apache::loncommon::supportedlanguagecode($id);
                   1921:         if ($code ne '') {
                   1922:             $langchoices{$code} =  &Apache::loncommon::plainlanguagedescription($id);
                   1923:         }
                   1924:     }
                   1925:     my @spelllangs = ('none');
                   1926:     foreach my $code ('en','de','he','es','fr','pt','tr') {
                   1927:         push(@spelllangs,[$code,$langchoices{$code}]);
                   1928:     }
                   1929:     return \@spelllangs;
                   1930: }
                   1931: 
1.333.2.3  raeburn  1932: sub edit_mathresponse_button {
                   1933:     my ($field) = @_;
                   1934:     my $eqneditor = 'lcmath';
                   1935:     if ($env{'browser.type'} eq 'safari') {
                   1936:         if ($env{'browser.os'} eq 'mac') {
                   1937:             my ($prefix,$version) = ($env{'browser.version'} =~ /^(\d*)(\d{3})\./);
                   1938:             if ($env{'browser.mobile'}) {
                   1939:                 if (($version < 531) || (($prefix eq '') && ($version < 533))) {
                   1940:                     $eqneditor = '';
                   1941:                 }
                   1942:             } elsif ($version < 533) {
                   1943:                 $eqneditor = 'dragmath';
                   1944:             }
                   1945:         } elsif ($env{'browser.os'} eq 'win') {
                   1946:             if ($env{'browser.version'} < 533) {
                   1947:                 $eqneditor = 'dragmath';
                   1948:             }
                   1949:         }
                   1950:     } elsif ($env{'browser.type'} eq 'explorer') {
                   1951:         if ($env{'browser.version'} < 9) {
                   1952:             $eqneditor = 'dragmath';
                   1953:         }
                   1954:     } elsif ($env{'browser.type'} eq 'mozilla') {
                   1955:         if ($env{'browser.version'} < 5) {
                   1956:             $eqneditor = 'dragmath';
                   1957:         } else {
                   1958:             if ($env{'browser.info'} =~ /^firefox\-([\d\.]+)/) {
                   1959:                 my $firefox = $1;
                   1960:                 if ($firefox < 4) {
                   1961:                     $eqneditor = 'dragmath';
                   1962:                 }
                   1963:             }
                   1964:         }
                   1965:     } elsif ($env{'browser.type'} eq 'chrome') {
                   1966:         if ($env{'browser.version'} < 5) {
                   1967:             $eqneditor = 'dragmath';
                   1968:         }
                   1969:     } elsif ($env{'browser.type'} eq 'opera') {
                   1970:         if ($env{'browser.version'} < 12) {
                   1971:             $eqneditor = 'dragmath';
                   1972:         }
                   1973:     }
                   1974:     if ($eqneditor eq 'lcmath') {
                   1975:         if (($env{'request.course.id'}) && ($env{'request.state'} ne 'construct')) {
                   1976:             if (exists($env{'course.'.$env{'request.course.id'}.'.uselcmath'})) {
                   1977:                 if ($env{'course.'.$env{'request.course.id'}.'.uselcmath'} eq '0') {
                   1978:                     $eqneditor = 'dragmath';
                   1979:                 }
                   1980:             } else {
                   1981:                 my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
                   1982:                 if ($domdefs{'uselcmath'} eq '0') {
                   1983:                     $eqneditor = 'dragmath';
                   1984:                 }
                   1985:             }
                   1986:         } else {
                   1987:             my %domdefs = &Apache::lonnet::get_domain_defaults($env{'course.'.$env{'request.course.id'}.'.domain'});
                   1988:             if ($domdefs{'uselcmath'} eq '0') {
                   1989:                 $eqneditor = 'dragmath';
                   1990:             }
                   1991:         }
                   1992:     }
                   1993:     if ($eqneditor eq 'dragmath') {
                   1994:         # DragMath applet
                   1995:         my $button=&mt('Edit Answer');
                   1996: #       my $helplink=&Apache::loncommon::help_open_topic('Formula_Editor');
                   1997:         my $iconpath=$Apache::lonnet::perlvar{'lonIconsURL'};
                   1998:         return(<<ENDFORMULABUTTON);
                   1999: <script type="text/javascript" language="JavaScript">
                   2000: function LC_mathedit_${field} (LCtextline) {
                   2001:     thenumber = LCtextline;
                   2002:     var thedata = '';
                   2003:     if (document.getElementById(LCtextline)) {
                   2004:         thedata = document.getElementById(LCtextline).value;
                   2005:     }
                   2006:     newwin = window.open("/adm/dragmath/MaximaPopup.html","","width=565,height=400,resizable");
                   2007: }
                   2008: </script>
                   2009: <a href="javascript:LC_mathedit_${field}('${field}');void(0);"><img class="stift" src="$iconpath/stift.gif" alt="$button" title="$button" /></a>
                   2010: ENDFORMULABUTTON
                   2011:     } elsif ($eqneditor eq 'lcmath') {
                   2012:         # LON-CAPA math equation editor
                   2013:         my $mathjaxjs;
                   2014:         unless (lc(&Apache::lontexconvert::tex_engine()) eq 'mathjax') {
                   2015:             $mathjaxjs = <<"MATHJAX_SCRIPT";
                   2016: var mathjaxscript = document.createElement("script");
                   2017:     mathjaxscript.type = "text/javascript";
                   2018:     mathjaxscript.src = "/adm/MathJax/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
                   2019:     document.body.appendChild(mathjaxscript);
                   2020: MATHJAX_SCRIPT
                   2021:         }
                   2022:         return(<<EQ_EDITOR_SCRIPT);
                   2023: <script type="text/javascript">
                   2024:   var LCmathField = document.getElementById('${field}');
                   2025:   LCmathField.className += ' math'; // note the space
                   2026:   LCmathField.setAttribute('data-implicit_operators', 'true');
                   2027:   var LCMATH_started;
                   2028:   if (typeof LCMATH_started === 'undefined') {
                   2029:     $mathjaxjs
                   2030:     LCMATH_started = true;
                   2031:     var script = document.createElement("script");
                   2032:     script.type = "text/javascript";
                   2033:     script.src = "/adm/LC_math_editor/LC_math_editor.min.js";
                   2034:     document.body.appendChild(script);
                   2035:     window.addEventListener('load', function(e) {
                   2036:         LCMATH.initEditors();
                   2037:     }, false);
                   2038:   }
                   2039: </script>
                   2040: EQ_EDITOR_SCRIPT
                   2041:     }
                   2042: }
                   2043: 
1.1       albertel 2044: 1;
                   2045: __END__
1.249     jms      2046: 
                   2047: =pod
                   2048: 
                   2049: =back
                   2050: 
                   2051: =cut
1.43      albertel 2052:  

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