File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.180: download - view: text, annotated - select for diffs
Tue Nov 15 22:28:30 2005 UTC (18 years, 6 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- Don't say computer's answer shown above on handgraded problems

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

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