File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.178: download - view: text, annotated - select for diffs
Thu Nov 3 17:44:15 2005 UTC (18 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- removing editorial
- reformatting to conform to 80 char line lengths

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

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