File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.171: download - view: text, annotated - select for diffs
Mon Jul 11 19:41:53 2005 UTC (18 years, 10 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- BUG#4139 (again)

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

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