File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.166: download - view: text, annotated - select for diffs
Fri Apr 8 19:39:32 2005 UTC (19 years, 1 month ago) by albertel
Branches: MAIN
CVS tags: version_1_99_0_tmcc, HEAD
- improved wording

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

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