File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.48: download - view: text, annotated - select for diffs
Tue Jan 8 21:09:23 2002 UTC (22 years, 4 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
 - renamed <textarea> to <textfield>

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.48 2002/01/08 21:09:23 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: # 2/19 Guy 
   29: 
   30: package Apache::inputtags;
   31: use strict;
   32: 
   33: sub BEGIN {
   34:   &Apache::lonxml::register('Apache::inputtags',('textfield','textline','datasubmission'));
   35: }
   36: 
   37: 
   38: sub initialize_inputtags {
   39:   # list of current input ids
   40:   @Apache::inputtags::input=();
   41:   # list of all input ids seen in this problem
   42:   @Apache::inputtags::inputlist=();
   43:   # list of all current response ids
   44:   @Apache::inputtags::response=();
   45:   # list of all response ids seen in this problem
   46:   @Apache::inputtags::responselist=();
   47:   # list of whether or not a specific response was previously used
   48:   @Apache::inputtags::previous=();
   49:   # id of current part, 0 means that no part is current (inside <problem> only
   50:   $Apache::inputtags::part='';
   51:   # list of problem date statuses, the first element is for <problem>
   52:   # if there is a second element it is for the current <part>
   53:   @Apache::inputtags::status=();
   54:   # hash of defined params for the current response
   55:   %Apache::inputtags::params=();
   56:   # list of all ids, for <import>, these get join()ed and prepended
   57:   @Apache::inputtags::import=();
   58: }
   59: 
   60: sub start_input {
   61:   my ($parstack,$safeeval)=@_;
   62:   my $id = &Apache::lonxml::get_param('id',$parstack,$safeeval);
   63:   if ($id eq '') { $id = $Apache::lonxml::curdepth; }
   64:   push (@Apache::inputtags::input,$id);
   65:   push (@Apache::inputtags::inputlist,$id);
   66:   return $id;
   67: }
   68: 
   69: sub end_input {
   70:   pop @Apache::inputtags::input;
   71:   return '';
   72: }
   73: 
   74: sub start_textfield {
   75:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   76:   my $result = "";
   77:   my $id = &start_input($parstack,$safeeval);
   78:   if ($target eq 'web') {
   79:     my $oldresponse = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.$Apache::inputtags::response['-1'].submission"};
   80:     my $cols = &Apache::lonxml::get_param('cols',$parstack,$safeeval);
   81:     if ( $cols eq '') { $cols = 80; }
   82:     my $rows = &Apache::lonxml::get_param('rows',$parstack,$safeeval);
   83:     if ( $rows eq '') { $rows = 10; }
   84:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
   85:       $result= '<textarea name="HWVAL'.$Apache::inputtags::response['-1'].'" '.
   86: 	"rows=\"$rows\" cols=\"$cols\">".$oldresponse;
   87:     } else {
   88:       $result='<table border="1"><tr><td><i>'.$oldresponse.'</i></td></tr></table>';
   89:     }
   90:     if ($oldresponse ne '') {
   91:       #get rid of any startup text if the user has already responded
   92:       &Apache::lonxml::get_all_text("/textfield",$$parser[$#$parser]);
   93:     }
   94:   }
   95:   return $result;
   96: }
   97: 
   98: sub end_textfield {
   99:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  100:   if ($target eq 'web') {
  101:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  102:       return "</textarea>";
  103:     }
  104:   }
  105:   &end_input;
  106:   return '';
  107: }
  108: 
  109: sub start_textline {
  110:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  111:   my $result = "";
  112:   if ($target eq 'web') {
  113:     my $size = &Apache::lonxml::get_param('size',$parstack,$safeeval);
  114:     if ($size eq '') { $size=20; }
  115:     my $partid=$Apache::inputtags::part;
  116:     my $id=$Apache::inputtags::response['-1'];
  117:     my $oldresponse = 
  118:       $Apache::lonhomework::history{"resource.$partid.$id.submission"};
  119:     if ($Apache::inputtags::status[-1] eq 'CAN_ANSWER') {
  120:       $result= '<input type="text" name="HWVAL'.$id.'" value="'.
  121: 	$oldresponse.'" size="'.$size.'" />';
  122:     } else {
  123:       $result='<i>'.$oldresponse.'</i>';
  124:     }
  125:   } elsif ($target eq 'edit') {
  126:     $result=&Apache::edit::tag_start($target,$token,
  127: 				     &Apache::lonxml::description($token));
  128:     $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
  129:     $result.=&Apache::edit::end_table;
  130:   } elsif ($target eq 'modified') {
  131:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
  132:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  133:   }
  134:   return $result;
  135: }
  136: 
  137: sub end_textline {
  138:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  139:   if ($target eq 'edit') { return ('','no'); }
  140:   return "";
  141: }
  142: 
  143: sub start_datasubmission {
  144:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  145:   my $id = &Apache::response::start_response($parstack,$safeeval);
  146:   my $result;
  147:   if ($target eq 'meta') {
  148:     $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
  149: 						   $token->[2]->{'type'},
  150: 						   $token->[2]->{'display'});
  151:     $result .= &Apache::response::meta_package_write('datasubmission');
  152:   }
  153:   return $result;
  154: }
  155: 
  156: sub end_datasubmission {
  157:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  158:   my $result;
  159:   if ( $target eq 'web' ) {
  160:   } elsif ($target eq 'grade' ) {
  161:     if ( defined $ENV{'form.submitted'}) {
  162:       &Apache::response::setup_params('datasubmission');
  163:       my $partid = $Apache::inputtags::part;
  164:       my $id = $Apache::inputtags::response['-1'];
  165:       my $response = $ENV{'form.HWVAL'.$id};
  166:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  167:       if ( $response =~ /[^\s]/) {
  168: 	$Apache::lonhomework::results{"resource.$partid.$id.$name"}=
  169: 	  $response;
  170:       }
  171:     }
  172:   }
  173:   &Apache::response::end_response;
  174:   return $result;
  175: }
  176: 
  177: sub finalizeawards {
  178:   my $result='';
  179:   my $award;
  180:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
  181:   if ($result eq '' ) {
  182:     foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
  183:   }
  184:   if ($result eq '' ) {
  185:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
  186:   }
  187:   if ($result eq '' ) {
  188:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
  189:   }
  190: 
  191:   if ($result eq '' ) {
  192:     foreach $award (@_) { 
  193:       if ($award eq 'UNIT_FAIL' ||
  194: 	  $award eq 'NO_UNIT' ||
  195: 	  $award eq 'UNIT_NOTNEEDED') {
  196: 	$result=$award; last;
  197:       }
  198:     }
  199:   }
  200:   if ($result eq '' ) {
  201:     foreach $award (@_) { 
  202:       if ($award eq 'WANTED_NUMERIC' || 
  203: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
  204:     }
  205:   }
  206:   if ($result eq '' ) {
  207:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
  208:   }
  209:   if ($result eq '' ) {
  210:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
  211:   }
  212:   if ($result eq '' ) {
  213:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
  214:   }
  215:   if ($result eq '' ) {
  216:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
  217:   }
  218:   if ($result eq '' ) { $result='EXACT_ANS'; }
  219:   return $result
  220: }
  221: 
  222: sub decideoutput {
  223:   my ($award,$solved,$previous)=@_;
  224:   my $message='';
  225:   my $button=0;
  226:   my $previousmsg;
  227: 
  228:   if ($previous) { $previousmsg='You have entered that answer before'; }
  229: 
  230:   if      ($solved =~ /^correct/) {
  231:     $message = "<b>You are correct.</b> Your receipt is ".
  232:       &Apache::lonnet::receipt;
  233:     $button=0;
  234:     $previousmsg='';
  235:   } elsif ($solved =~ /^excused/) {
  236:     $message = "<b>You are excused from the problem.</b>";
  237:     $button=0;
  238:     $previousmsg='';
  239:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  240:     if ($solved =~ /^incorrect/ || $solved eq '') {
  241:       $message = "Incorrect";
  242:       $button=1;
  243:     } else {
  244:       $message = "<b>You are correct.</b> Your receipt is ".
  245: 	&Apache::lonnet::receipt;
  246:       $button=0;
  247:       $previousmsg='';
  248:     }
  249:   } elsif ($award eq 'NO_RESPONSE') {
  250:     $message = '';
  251:     $button=1;
  252:   } elsif ($award eq 'MISSING_ANSWER') {
  253:     $message = 'Some parts were not submitted';
  254:     $button = 1;
  255:   } elsif ($award eq 'WANTED_NUMERIC') {
  256:     $message = "This question expects a numeric answer";
  257:     $button=1;
  258:   } elsif ($award eq 'SIG_FAIL') {
  259:     $message = "Please adjust significant figures.";# you provided %s significant figures";
  260:     $button=1;
  261:   } elsif ($award eq 'UNIT_FAIL') {
  262:     $message = "Units incorrect."; #Computer reads units as %s";
  263:     $button=1;
  264:   } elsif ($award eq 'UNIT_NOTNEEDED') {
  265:     $message = "Only a number required.";# Computer reads units of %s";
  266:     $button=1;
  267:   } elsif ($award eq 'NO_UNIT') {
  268:     $message = "Units required";
  269:     $button=1;
  270:   } elsif ($award eq 'BAD_FORMULA') {
  271:     $message = "Unable to understand formula";
  272:     $button=1;
  273:   } elsif ($award eq 'INCORRECT') {
  274:     $message = "Incorrect";
  275:     $button=1;
  276:   } elsif ($award eq 'SUBMITTED') {
  277:     $message = "Your submission has been recorded.";
  278:     $button=1;
  279:   } else {
  280:     $message = "Unknown message: $award";
  281:     $button=1;
  282:   }
  283:   return ($button,$message,$previousmsg);
  284: }
  285: 
  286: sub setgradedata {
  287:   my ($award,$id,$previously_used) = @_;
  288:   # if the student already has it correct, don't modify the status
  289:   if ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  290:        /^correct/ ) {
  291:     #handle assignment of tries and solved status
  292:     if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  293:       $Apache::lonhomework::results{"resource.$id.tries"} =
  294: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  295:       $Apache::lonhomework::results{"resource.$id.solved"} =
  296: 	'correct_by_student';
  297:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  298:     } elsif ( $award eq 'INCORRECT' ) {
  299:       $Apache::lonhomework::results{"resource.$id.tries"} =
  300: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  301:       $Apache::lonhomework::results{"resource.$id.solved"} =
  302: 	'incorrect_attempted';
  303:     } elsif ( $award eq 'SUBMITTED' ) {
  304:       $Apache::lonhomework::results{"resource.$id.tries"} =
  305: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  306:       $Apache::lonhomework::results{"resource.$id.solved"} =
  307: 	'ungraded_attempted';
  308:     } elsif ( $award eq 'NO_RESPONSE' ) {
  309:       return '';
  310:     } else {
  311:       $Apache::lonhomework::results{"resource.$id.solved"} =
  312: 	'incorrect_attempted';
  313:     }
  314: 
  315:     # check if this was a previous submission if it was delete the
  316:     # unneeded data and update the previously_used attribute
  317:     if ( $previously_used eq 'PREVIOUSLY_USED') {
  318:       delete($Apache::lonhomework::results{"resource.$id.tries"});
  319:       $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  320:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  321:       #delete all data as they student didn't do anything
  322:       foreach my $key (keys(%Apache::lonhomework::results)) {
  323: 	if ($key =~ /^resource\.$id\./) {
  324: 	  &Apache::lonxml::debug("Removing $key");
  325: 	  delete($Apache::lonhomework::results{$key});
  326: 	}
  327:       }
  328:       #and since they didn't do anything we were never here
  329:       return '';
  330:     } else {
  331:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
  332:     }
  333:   }
  334:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
  335: }
  336: 
  337: sub grade {
  338:   my ($target) = @_;
  339:   my $id = $Apache::inputtags::part;
  340:   my $response='';
  341:   if ( defined $ENV{'form.submitted'}) {
  342:     my @awards = ();
  343:     foreach $response (@Apache::inputtags::responselist) {
  344:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  345:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  346:       if ( $value ne '' ) {
  347: 	&Apache::lonxml::debug("keeping $value from $response for $id");
  348: 	push (@awards,$value);
  349:       } else {
  350: 	&Apache::lonxml::debug("skipping $value from $response for $id");
  351:       }
  352:     }
  353:     my $finalaward = &finalizeawards(@awards);
  354:     my $previously_used;
  355:     if ( $#Apache::inputtags::previous eq $#awards ) {
  356:       $previously_used = 'PREVIOUSLY_LAST';
  357:       foreach my $value (@Apache::inputtags::previous) {
  358: 	if ($value eq 'PREVIOUSLY_USED' ) {
  359: 	  $previously_used = $value;
  360: 	  last;
  361: 	}
  362:       }
  363:     }
  364:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
  365:     &setgradedata($finalaward,$id,$previously_used);
  366:   }
  367:   return '';
  368: }
  369: 
  370: sub gradestatus {
  371:   my ($id) = @_;
  372:   my $showbutton = 1;
  373:   my $message = '';
  374:   my $trystr='';
  375:   my $button='';
  376:   my $previousmsg='';
  377: 
  378:   my $status = $Apache::inputtags::status['-1'];
  379:   &Apache::lonxml::debug("gradestatus has :$status:");
  380:   if ( $status ne 'CLOSED' ) {  
  381:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
  382:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  383:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  384:     &Apache::lonxml::debug("Found Award |$award|$solved|");
  385:     if ( $award ne '' ) {
  386:       &Apache::lonxml::debug('Getting message');
  387:       ($showbutton,$message,$previousmsg) =
  388: 	&decideoutput($award,$solved,$previous);
  389:       $message="<td bgcolor=\"#aaffaa\">$message</td>";
  390:       if ($previousmsg) {
  391: 	$previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
  392:       }
  393:     }
  394:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  395:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  396:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
  397:     if ( $tries eq '' ) { $tries = '0'; }
  398:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  399:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  400:     if ( $showbutton ) {
  401:       $trystr = "<td>Tries $tries/$maxtries</td>";
  402:     }
  403:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  404:     if ( $showbutton ) { 
  405:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
  406:     }
  407:   }
  408:   my $output= $previousmsg.$message.$trystr;
  409:   if ($output =~ /^\s*$/) {
  410:     return $button;
  411:   } else {
  412:     return $button.'<table><tr>'.$previousmsg.$message.$trystr.'</tr></table>';
  413:   }
  414: }
  415: 1;
  416: __END__
  417:  

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