File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.177: download - view: text, annotated - select for diffs
Thu Nov 3 11:11:25 2005 UTC (18 years, 7 months ago) by foxr
Branches: MAIN
CVS tags: HEAD
"improve?" some commenting of the global variables so that it may
be easier for th eye to extract what they mean.

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

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