File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.160: download - view: text, annotated - select for diffs
Thu Mar 31 21:24:29 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- some cleanups/improvments to the file submission layout
- modularize the file submission code
- allow a user to submit a portfolio file

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

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