File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.162: download - view: text, annotated - select for diffs
Thu Mar 31 21:32:52 2005 UTC (19 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- looks better for * filetype

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.162 2005/03/31 21:32:52 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:     if ($uploadedfiletypes ne '*') {
  311: 	$result.=
  312: 	    &mt('Allowed filetypes: <b>[_1]</b>',$uploadedfiletypes).'<br />';
  313:     }
  314:     if ($which eq 'uploadonly' || $which eq 'both') { 
  315: 	$result.=&mt('Submit a file: (only one file can be uploaded)').
  316: 	    ' <br /><input type="file" size="50" name="HWFILE'.
  317: 	    $part.'_'.$id.'" /><br />';
  318: 	my $uploadedfile= &HTML::Entities::encode($Apache::lonhomework::history{"resource.$part.$id.uploadedfile"},'<>&"');
  319: 
  320: 	if ($uploadedfile) {
  321: 	    my $url=$Apache::lonhomework::history{"resource.$part.$id.uploadedurl"};
  322: 	    push (@Apache::lonxml::extlinks,$url);
  323: 	    &Apache::lonnet::allowuploaded('/adm/essayresponse',$url);
  324: 	    my $icon=&Apache::loncommon::icon($url);
  325: 	    my $curfile='<a href="'.$url.'"><img src="'.$icon.
  326: 		'" border="0" />'.$uploadedfile.'</a>';
  327: 	    $result.=&mt('Currently submitted: <tt>[_1]</tt>',$curfile);
  328: 	} else {
  329: 	    #$result.=&mt('(Hand in a file you have prepared on your computer)');
  330: 	}
  331:     }
  332:     if ( $which eq 'both') { 
  333: 	$result.='<br />'.'<strong>'.&mt('OR:').'</strong><br />';
  334:     }
  335:     if ($which eq 'portfolioonly' || $which eq 'both') { 
  336: 	$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"))'."'".'>'.
  337: 	    &mt('Select Portfolio Files').'</a><br />'.
  338: 	    '<input type="text" size="50" name="HWPORT'.$part.'_'.$id.'" value="" />'.
  339: 	    '<br />';
  340: 	if ($Apache::lonhomework::history{"resource.$part.$id.portfiles"}=~/[^\s]/){
  341: 	    my @filelist;
  342: 	    foreach my $file (split(',',&Apache::lonnet::unescape($Apache::lonhomework::history{"resource.$part.$id.portfiles"}))) {
  343: 		my (undef,undef,$domain,$user)=&Apache::lonxml::whichuser();
  344: 		my $url="/uploaded/$domain/$user/portfolio$file";
  345: 		my $icon=&Apache::loncommon::icon($url);
  346: 		push(@filelist,'<a href="'.$url.'"><img src="'.$icon.
  347: 		     '" border="0" />'.$file.'</a>');
  348: 	    }
  349: 	    $result.=&mt("Portfolio files previously selected: <strong>[_1]</strong>",join(', ',@filelist));
  350: 	}
  351:     }
  352:     $result.='</td></tr>'; 
  353:     return $result;
  354: }
  355: 
  356: sub checkstatus {
  357:     my ($value,$awardref,$msgref)=@_;
  358:     for (my $i=0;$i<=$#$awardref;$i++) {
  359: 	if ($$awardref[$i] eq $value) {
  360: 	    return ($$awardref[$i],$$msgref[$i]);
  361: 	}
  362:     }
  363:     return(undef,undef);
  364: }
  365: 
  366: sub finalizeawards {
  367:     my ($awardref,$msgref)=@_;
  368:     my $result=undef;
  369:     my $award;
  370:     my $msg;
  371:     if ($#$awardref == -1) { $result = "NO_RESPONSE"; }
  372:     if ($result eq '' ) {
  373: 	my $blankcount;
  374: 	foreach $award (@$awardref) {
  375: 	    if ($award eq '') {
  376: 		$result='MISSING_ANSWER';
  377: 		$blankcount++;
  378: 	    }
  379: 	}
  380: 	if ($blankcount == ($#$awardref + 1)) { $result = 'NO_RESPONSE'; }
  381:     }
  382:     if (defined($result)) { return ($result,$msg); }
  383:     foreach my $possibleaward ('MISSING_ANSWER', 'ERROR', 'NO_RESPONSE',
  384: 			       'TOO_LONG', 'UNIT_INVALID_INSTRUCTOR',
  385: 			       'UNIT_INVALID_STUDENT', 'UNIT_IRRECONCIBLE',
  386: 			       'UNIT_FAIL', 'NO_UNIT',
  387: 			       'UNIT_NOTNEEDED', 'WANTED_NUMERIC',
  388: 			       'BAD_FORMULA', 'SIG_FAIL', 'INCORRECT', 
  389: 			       'MISORDERED_RANK', 'INVALID_FILETYPE',
  390: 			       'DRAFT', 'SUBMITTED', 'ASSIGNED_SCORE',
  391: 			       'APPROX_ANS', 'EXACT_ANS','COMMA_FAIL') {
  392: 	($result,$msg)=&checkstatus($possibleaward,$awardref,$msgref);
  393: 	if (defined($result)) { return ($result,$msg); }
  394:     }
  395:     return ('ERROR',undef);
  396: }
  397: 
  398: sub decideoutput {
  399:     my ($award,$awardmsg,$solved,$previous,$target)=@_;
  400:     my $message='';
  401:     my $button=0;
  402:     my $previousmsg;
  403:     my $bgcolor='orange';
  404:     my $added_computer_text=0;
  405:     my %possiblecolors =
  406: 	( 'correct' => '#aaffaa',
  407: 	  'charged_try' => '#ffaaaa',
  408: 	  'not_charged_try' => '#ffffaa',
  409: 	  'no_message' => '#fffff',
  410: 	  );
  411:     if ($previous) { $previousmsg=&mt('You have entered that answer before'); }
  412:     
  413:     if      ($solved =~ /^correct/) {
  414: 	if ($award eq 'ASSIGNED_SCORE') {
  415: 	    $message = &mt("A score has been assigned.");
  416: 	} else {
  417: 	    if ($target eq 'tex') {
  418: 		$message = '\textbf{'.&mt('You are correct.').'}';
  419: 	    } else {
  420: 		$message = "<b>".&mt('You are correct.')."</b>";
  421: 		$message.=" ".&mt("Computer's answer now shown above.");
  422: 	    }
  423: 	    $added_computer_text=1;
  424: 	    unless ($ENV{'course.'.
  425: 			     $ENV{'request.course.id'}.
  426: 			     '.disable_receipt_display'} eq 'yes') { 
  427: 		$message.=(($target eq 'web')?'<br />':' ').
  428: 		    &mt('Your receipt is').' '.&Apache::lonnet::receipt($Apache::inputtags::part).
  429: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  430: 	    }
  431: 	}
  432: 	$bgcolor=$possiblecolors{'correct'};
  433: 	$button=0;
  434: 	$previousmsg='';
  435:     } elsif ($solved =~ /^excused/) {
  436: 	if ($target eq 'tex') {
  437: 	    $message = ' \textbf{'.&mt('You are excused from the problem.').'} ';
  438: 	} else {
  439: 	    $message = "<b>".&mt('You are excused from the problem.')."</b>";
  440: 	}
  441: 	$bgcolor=$possiblecolors{'charged_try'};
  442: 	$button=0;
  443: 	$previousmsg='';
  444:     } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  445: 	if ($solved =~ /^incorrect/ || $solved eq '') {
  446: 	    $message = &mt("Incorrect").".";
  447: 	    $bgcolor=$possiblecolors{'charged_try'};
  448: 	    $button=1;
  449: 	} else {
  450: 	    if ($target eq 'tex') {
  451: 		$message = '\textbf{'.&mt('You are correct.').'}';
  452: 	    } else {
  453: 		$message = "<b>".&mt('You are correct.')."</b>";
  454: 		$message.=" ".&mt("Computer's answer now shown above.");
  455: 	    }
  456: 	    $added_computer_text=1;
  457: 	    unless ($ENV{'course.'.
  458: 			     $ENV{'request.course.id'}.
  459: 			     '.disable_receipt_display'} eq 'yes') { 
  460: 		$message.=(($target eq 'web')?'<br />':' ').
  461: 		    'Your receipt is '.&Apache::lonnet::receipt($Apache::inputtags::part).
  462: 		    (($target eq 'web')?&Apache::loncommon::help_open_topic('Receipt'):'');
  463: 	    }
  464: 	    $bgcolor=$possiblecolors{'correct'};
  465: 	    $button=0;
  466: 	    $previousmsg='';
  467: 	}
  468:     } elsif ($award eq 'NO_RESPONSE') {
  469: 	$message = '';
  470: 	$bgcolor=$possiblecolors{'no_feedback'};
  471: 	$button=1;
  472:     } elsif ($award eq 'MISSING_ANSWER') {
  473: 	$message = &mt('Some items were not submitted.');
  474: 	$bgcolor=$possiblecolors{'not_charged_try'};
  475: 	$button = 1;
  476:     } elsif ($award eq 'ERROR') {
  477: 	$message = &mt('An error occured while grading your answer.');
  478: 	$bgcolor=$possiblecolors{'not_charged_try'};
  479: 	$button = 1;
  480:     } elsif ($award eq 'TOO_LONG') {
  481: 	$message = &mt("The submitted answer was too long.");
  482: 	$bgcolor=$possiblecolors{'not_charged_try'};
  483: 	$button=1;
  484:     } elsif ($award eq 'WANTED_NUMERIC') {
  485: 	$message = &mt("This question expects a numeric answer.");
  486: 	$bgcolor=$possiblecolors{'not_charged_try'};
  487: 	$button=1;
  488:     } elsif ($award eq 'MISORDERED_RANK') {
  489: 	$message = &mt('You have provided an invalid ranking');
  490: 	if ($target ne 'tex') {
  491: 	    $message.=', '.&mt('please refer to').' '.&Apache::loncommon::help_open_topic('Ranking_Problems','help on ranking problems');
  492: 	}
  493: 	$bgcolor=$possiblecolors{'not_charged_try'};
  494: 	$button=1;
  495:     } elsif ($award eq 'INVALID_FILETYPE') {
  496: 	$message = &mt('The filetype extension of the file you uploaded is not allowed.');
  497: 	$bgcolor=$possiblecolors{'not_charged_try'};
  498: 	$button=1;
  499:     } elsif ($award eq 'SIG_FAIL') {
  500: 	my ($used,$min,$max)=split(':',$awardmsg);
  501: 	my $word;
  502: 	if ($used < $min) { $word=&mt('more'); }
  503: 	if ($used > $max) { $word=&mt('fewer'); }
  504: 	$message = &mt("Submission not graded.  Use [_2] digits.",$used,$word);
  505: 	$bgcolor=$possiblecolors{'not_charged_try'};
  506: 	$button=1;
  507:     } elsif ($award eq 'UNIT_INVALID_INSTRUCTOR') {
  508: 	$message = &mt('Error in instructor specifed unit. This error has been reported to the instructor.', $awardmsg);
  509: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  510: 	$bgcolor=$possiblecolors{'not_charged_try'};
  511: 	$button=1;
  512:     } elsif ($award eq 'UNIT_INVALID_STUDENT') {
  513: 	$message = &mt('Unable to interpret units. Computer reads units as "[_1]".',&markup_unit($awardmsg,$target));
  514: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  515: 	$bgcolor=$possiblecolors{'not_charged_try'};
  516: 	$button=1;
  517:     } elsif ($award eq 'UNIT_FAIL' || $award eq 'UNIT_IRRECONCIBLE') {
  518: 	$message = &mt('Incompatible units. No conversion found between "[_1]" and the required units.',&markup_unit($awardmsg,$target));
  519: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units');} 
  520: 	$bgcolor=$possiblecolors{'not_charged_try'};
  521: 	$button=1;
  522:     } elsif ($award eq 'UNIT_NOTNEEDED') {
  523: 	$message = &mt('Only a number required. Computer reads units of "[_1]".',&markup_unit($awardmsg,$target));
  524: 	$bgcolor=$possiblecolors{'not_charged_try'};
  525: 	$button=1;
  526:     } elsif ($award eq 'NO_UNIT') {
  527: 	$message = &mt("Units required").'.';
  528: 	if ($target ne 'tex') {$message.=&Apache::loncommon::help_open_topic('Physical_Units')};
  529: 	$bgcolor=$possiblecolors{'not_charged_try'};
  530: 	$button=1;
  531:     } elsif ($award eq 'COMMA_FAIL') {
  532: 	$message = &mt("Proper comma separation is required").'.';
  533: 	$bgcolor=$possiblecolors{'not_charged_try'};
  534: 	$button=1;
  535:     } elsif ($award eq 'BAD_FORMULA') {
  536: 	$message = &mt("Unable to understand formula");
  537: 	$bgcolor=$possiblecolors{'not_charged_try'};
  538: 	$button=1;
  539:     } elsif ($award eq 'INCORRECT') {
  540: 	$message = &mt("Incorrect").'.';
  541: 	$bgcolor=$possiblecolors{'charged_try'};
  542: 	$button=1;
  543:     } elsif ($award eq 'SUBMITTED') {
  544: 	$message = &mt("Your submission has been recorded.");
  545: 	$bgcolor=$possiblecolors{'correct'};
  546: 	$button=1;
  547:     } elsif ($award eq 'DRAFT') {
  548: 	$message = &mt("A draft copy has been saved.");
  549: 	$bgcolor=$possiblecolors{'not_charged_try'};
  550: 	$button=1;
  551:     } elsif ($award eq 'ASSIGNED_SCORE') {
  552: 	$message = &mt("A score has been assigned.");
  553: 	$bgcolor=$possiblecolors{'correct'};
  554: 	$button=0;
  555:     } elsif ($award eq '') {
  556: 	$bgcolor=$possiblecolors{'not_charged_try'};
  557: 	$button=1;
  558:     } else {
  559: 	$message = &mt("Unknown message").": $award";
  560: 	$button=1;
  561:     }
  562:     if (lc($Apache::lonhomework::problemstatus) eq 'no'  && 
  563: 	$Apache::inputtags::status[-1] ne 'SHOW_ANSWER') {
  564: 	$message = &mt("Answer Submitted: Your final submission will be graded after the due date.");
  565: 	$bgcolor=$possiblecolors{'correct'};
  566: 	$button=1;
  567:     }
  568:     if ($Apache::inputtags::status[-1] eq 'SHOW_ANSWER' && 
  569: 	!$added_computer_text && $target ne 'tex') {
  570: 	$message.=" ".&mt("Computer's answer now shown above.");
  571: 	$added_computer_text=1;
  572:     }
  573:     return ($button,$bgcolor,$message,$previousmsg);
  574: }
  575: 
  576: sub markup_unit {
  577:     my ($unit,$target)=@_;
  578:     if ($target eq 'tex') {
  579: 	return '\texttt{'.&Apache::lonxml::latex_special_symbols($unit).'}'; 
  580:     } else {
  581: 	return "<tt>".$unit."</tt>";
  582:     }
  583: }
  584: 
  585: sub removealldata {
  586:     my ($id)=@_;
  587:     foreach my $key (keys(%Apache::lonhomework::results)) {
  588: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  589: 	    &Apache::lonxml::debug("Removing $key");
  590: 	    delete($Apache::lonhomework::results{$key});
  591: 	}
  592:     }
  593: }
  594: 
  595: sub hidealldata {
  596:     my ($id)=@_;
  597:     foreach my $key (keys(%Apache::lonhomework::results)) {
  598: 	if (($key =~ /^resource\.\Q$id\E\./) && ($key !~ /\.collaborators$/)) {
  599: 	    &Apache::lonxml::debug("Hidding $key");
  600: 	    my $newkey=$key;
  601: 	    $newkey=~s/^(resource\.\Q$id\E\.[^\.]+\.)(.*)$/${1}hidden${2}/;
  602: 	    $Apache::lonhomework::results{$newkey}=
  603: 		$Apache::lonhomework::results{$key};
  604: 	    delete($Apache::lonhomework::results{$key});
  605: 	}
  606:     }
  607: }
  608: 
  609: sub setgradedata {
  610:     my ($award,$msg,$id,$previously_used) = @_;
  611:     if ($Apache::lonhomework::scantronmode && 
  612: 	&Apache::lonnet::validCODE($ENV{'form.CODE'})) {
  613: 	$Apache::lonhomework::results{"resource.CODE"}=$ENV{'form.CODE'};
  614:     } elsif ($Apache::lonhomework::scantronmode && 
  615: 	     $ENV{'form.CODE'} eq '' &&
  616: 	     $Apache::lonhomework::history{"resource.CODE"} ne '') {
  617: 	$Apache::lonhomework::results{"resource.CODE"}='';
  618:     }
  619: 
  620:     if (!$Apache::lonhomework::scantronmode &&
  621: 	$Apache::inputtags::status['-1'] ne 'CAN_ANSWER' &&
  622: 	$Apache::inputtags::status['-1'] ne 'CANNOT_ANSWER') {
  623: 	$Apache::lonhomework::results{"resource.$id.afterduedate"}=$award;
  624: 	return '';
  625:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  626: 	      /^correct/ || $Apache::lonhomework::scantronmode ||
  627: 	      lc($Apache::lonhomework::problemstatus) eq 'no') {
  628:         # the student doesn't already have it correct,
  629: 	# or we are in a mode (scantron orno problem status) where a correct 
  630:         # can become incorrect
  631: 	# handle assignment of tries and solved status
  632: 	my $solvemsg;
  633: 	if ($Apache::lonhomework::scantronmode) {
  634: 	    $solvemsg='correct_by_scantron';
  635: 	} else {
  636: 	    $solvemsg='correct_by_student';
  637: 	}
  638: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  639: 	    $Apache::lonhomework::results{"resource.$id.afterduedate"}='';
  640: 	}
  641: 	if ( $award eq 'ASSIGNED_SCORE') {
  642: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  643: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  644: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  645: 		$solvemsg;
  646: 	    my $numawards=scalar(@Apache::inputtags::response);
  647: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  648: 	    foreach my $res (@Apache::inputtags::response) {
  649: 		$Apache::lonhomework::results{"resource.$id.awarded"}+=
  650: 		    $Apache::lonhomework::results{"resource.$id.$res.awarded"};
  651: 	    }
  652: 	    if ($numawards > 0) {
  653: 		$Apache::lonhomework::results{"resource.$id.awarded"}/=
  654: 		    $numawards;
  655: 	    }
  656: 	} elsif ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  657: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  658: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  659: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  660: 		$solvemsg;
  661: 	    $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  662: 	} elsif ( $award eq 'INCORRECT' ) {
  663: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  664: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  665: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  666: 		$Apache::lonhomework::scantronmode) {
  667: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  668: 	    }
  669: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  670: 		'incorrect_attempted';
  671: 	} elsif ( $award eq 'SUBMITTED' ) {
  672: 	    $Apache::lonhomework::results{"resource.$id.tries"} =
  673: 		$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  674: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  675: 		'ungraded_attempted';
  676: 	} elsif ( $award eq 'DRAFT' ) {
  677: 	    $Apache::lonhomework::results{"resource.$id.solved"} = '';
  678: 	} elsif ( $award eq 'NO_RESPONSE' ) {
  679: 	    #no real response so delete any data that got stored
  680: 	    &removealldata($id);
  681: 	    return '';
  682: 	} else {
  683: 	    $Apache::lonhomework::results{"resource.$id.solved"} =
  684: 		'incorrect_attempted';
  685: 	    if (lc($Apache::lonhomework::problemstatus) eq 'no' ||
  686: 		$Apache::lonhomework::scantronmode) {
  687: 		$Apache::lonhomework::results{"resource.$id.tries"} =
  688: 		    $Apache::lonhomework::history{"resource.$id.tries"} + 1;
  689: 		$Apache::lonhomework::results{"resource.$id.awarded"} = 0;
  690: 	    }
  691: 	}
  692: 	if (defined($msg)) {
  693: 	    $Apache::lonhomework::results{"resource.$id.awardmsg"} = $msg;
  694: 	}
  695: 	# did either of the overall awards chage? If so ignore the 
  696: 	# previous check
  697: 	if (($Apache::lonhomework::results{"resource.$id.awarded"} eq
  698: 	     $Apache::lonhomework::history{"resource.$id.awarded"}) &&
  699: 	    ($Apache::lonhomework::results{"resource.$id.solved"} eq
  700: 	     $Apache::lonhomework::history{"resource.$id.solved"})) {
  701: 	    # check if this was a previous submission if it was delete the
  702: 	    # unneeded data and update the previously_used attribute
  703: 	    if ( $previously_used eq 'PREVIOUSLY_USED') {
  704: 		if (lc($Apache::lonhomework::problemstatus) ne 'no') {
  705: 		    delete($Apache::lonhomework::results{"resource.$id.tries"});
  706: 		    $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  707: 		}
  708: 	    } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  709: 		#delete all data as they student didn't do anything, but save
  710: 		#the list of collaborators.
  711: 		&removealldata($id);
  712: 		#and since they didn't do anything we were never here
  713: 		return '';
  714: 	    } else {
  715: 		$Apache::lonhomework::results{"resource.$id.previous"} = '0';
  716: 	    }
  717: 	}
  718:     } elsif ( $Apache::lonhomework::history{"resource.$id.solved"} =~
  719: 	      /^correct/ ) {
  720: 	#delete all data as they student already has it correct
  721: 	&removealldata($id);
  722: 	#and since they didn't do anything we were never here
  723: 	return '';
  724:     }
  725:     $Apache::lonhomework::results{"resource.$id.award"} = $award;
  726: }
  727: 
  728: sub grade {
  729:     my ($target) = @_;
  730:     my $id = $Apache::inputtags::part;
  731:     my $response='';
  732:     if ( defined $ENV{'form.submitted'}) {
  733: 	my (@awards,@msgs);
  734: 	foreach $response (@Apache::inputtags::response) {
  735: 	    &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  736: 	    my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  737: 	    &Apache::lonxml::debug("keeping $value from $response for $id");
  738: 	    push (@awards,$value);
  739: 	    $value=$Apache::lonhomework::results{"resource.$id.$response.awardmsg"};
  740: 	    &Apache::lonxml::debug("got message $value from $response for $id");
  741: 	    push (@msgs,$value);
  742: 	}
  743: 	my ($finalaward,$msg) = &finalizeawards(\@awards,\@msgs);
  744: 	my $previously_used;
  745: 	if ( $#Apache::inputtags::previous eq $#awards ) {
  746: 	    my $match=0;
  747: 	    my @matches;
  748: 	    foreach my $versionar (@Apache::inputtags::previous_version) {
  749: 		foreach my $version (@$versionar) {
  750: 		    $matches[$version]++;
  751: 		}
  752: 	    }
  753: 	    foreach my $elem (@matches) {if ($elem eq ($#awards+1)) {$match=1;}}
  754: 	    if ($match) {
  755: 		$previously_used = 'PREVIOUSLY_LAST';
  756: 		foreach my $value (@Apache::inputtags::previous) {
  757: 		    if ($value eq 'PREVIOUSLY_USED' ) {
  758: 			$previously_used = $value;
  759: 			last;
  760: 		    }
  761: 		}
  762: 	    }
  763: 	}
  764: 	&Apache::lonxml::debug("final award $finalaward, $previously_used, message $msg");
  765: 	&setgradedata($finalaward,$msg,$id,$previously_used);
  766:     }
  767:     return '';
  768: }
  769: 
  770: sub gradestatus {
  771:     my ($id,$target) = @_;
  772:     my $showbutton = 1;
  773:     my $bgcolor = '';
  774:     my $message = '';
  775:     my $latemessage = '';
  776:     my $trystr='';
  777:     my $button='';
  778:     my $previousmsg='';
  779: 
  780:     my $status = $Apache::inputtags::status['-1'];
  781:     &Apache::lonxml::debug("gradestatus has :$status:");
  782:     if ( $status ne 'CLOSED' && $status ne 'UNAVAILABLE' &&
  783: 	 $status ne 'INVALID_ACCESS') {  
  784: 	my $award = $Apache::lonhomework::history{"resource.$id.award"};
  785: 	my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  786: 	my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  787: 	my $awardmsg = $Apache::lonhomework::history{"resource.$id.awardmsg"};
  788: 	&Apache::lonxml::debug("Found Award |$award|$solved|$awardmsg");
  789: 	if ( $award ne '' || $solved ne '' || $status eq 'SHOW_ANSWER') {
  790: 	    &Apache::lonxml::debug('Getting message');
  791: 	    ($showbutton,$bgcolor,$message,$previousmsg) =
  792: 		&decideoutput($award,$awardmsg,$solved,$previous,$target);
  793: 	    if ($target eq 'tex') {
  794: 		$message='\vskip 2 mm '.$message.' ';
  795: 	    } else {
  796: 		$message="<td bgcolor=\"$bgcolor\">$message</td>";
  797: 		if ($previousmsg) {
  798: 		    $previousmsg="<td bgcolor=\"#aaaaff\">$previousmsg</td>";
  799: 		}
  800: 	    }
  801: 	}
  802: 	my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  803: 	my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  804: 	&Apache::lonxml::debug("got maxtries of :$maxtries:");
  805: 	#if tries are set to negative turn off the Tries/Button and messages
  806: 	if (defined($maxtries) && $maxtries < 0) { return ''; }
  807: 	if ( $tries eq '' ) { $tries = '0'; }
  808: 	if ( $maxtries eq '' ) { $maxtries = '2'; } 
  809: 	if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  810: 	my $tries_text=&mt('Tries');
  811: 	if ( $Apache::lonhomework::type eq 'survey') { $tries_text=&mt('Submissions'); }
  812: 	if ( $showbutton ) {
  813: 	    if ($target eq 'tex') {
  814: 		if ($ENV{'request.state'} ne "construct" && $Apache::lonhomework::type ne 'exam' && $ENV{'form.suppress_tries'} ne 'yes') {
  815: 		    $trystr = ' {\vskip 1 mm \small \textit{'.$tries_text.'} '.$tries.'/'.$maxtries.'} \vskip 2 mm ';
  816: 		} else {
  817: 		    $trystr = '\vskip 0 mm ';
  818: 		}
  819: 	    } else {
  820: 		$trystr = "<td><nobr>".$tries_text." $tries";
  821: 		if($ENV{'request.state'} ne 'construct') {
  822: 		    $trystr.="/$maxtries";
  823: 		} else {
  824: 		    if (defined($Apache::inputtags::params{'maxtries'})) {
  825: 			$trystr.="/".$Apache::inputtags::params{'maxtries'};
  826: 		    }
  827: 		}
  828: 		$trystr.="</nobr></td>";
  829: 	    }
  830: 	}
  831: 	if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  832: 	if ( $showbutton ) { 
  833: 	    if ($target ne 'tex') {
  834: 		$button = '<input type="submit" name="submit_'.$id.'" value="'.&mt('Submit Answer').'" />';
  835: 	    }
  836: 	}
  837: 	if ($Apache::lonhomework::history{"resource.$id.afterduedate"}) {
  838: 	    #last submissions was after due date
  839: 	    if ($target eq 'tex') {
  840: 		$latemessage=' The last submission was after the Due Date ';
  841: 	    } else {
  842: 		$latemessage="<td bgcolor=\"#ffaaaa\">The last submission was after the Due Date</td>";
  843: 	    }
  844: 	}
  845:     }
  846:     my $output= $previousmsg.$latemessage.$message.$trystr;
  847:     if ($output =~ /^\s*$/) {
  848: 	return $button;
  849:     } else {
  850: 	if ($target eq 'tex') {
  851: 	    return $button.' \vskip 0 mm '.$output.' ';
  852: 	} else {
  853: 	    return '<table><tr><td>'.$button.'</td>'.$output.'</tr></table>';
  854: 	}
  855:     }
  856: }
  857: 1;
  858: __END__
  859:  

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