File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.174: download - view: text, annotated - select for diffs
Tue Aug 2 18:15:27 2005 UTC (18 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: version_2_0_1, version_2_0_0, version_1_99_3, HEAD
- lets actually line things up with the correct comment

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

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