File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.49: download - view: text, annotated - select for diffs
Fri Jan 11 16:32:29 2002 UTC (22 years, 4 months ago) by matthew
Branches: MAIN
CVS tags: HEAD
Update to use edit::end_row() and edit::start_spanning_row() as a cleanup
for edit::select_arg and edit::select_or_text_arg.

    1: # The LearningOnline Network with CAPA
    2: # input  definitons
    3: #
    4: # $Id: inputtags.pm,v 1.49 2002/01/11 16:32:29 matthew 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:     $result.=&Apache::edit::text_arg('Size:','size',$token,'5')."</td></tr>";
  128:     $result.=&Apache::edit::end_table;
  129:   } elsif ($target eq 'modified') {
  130:     my $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,'size');
  131:     if ($constructtag) { $result = &Apache::edit::rebuild_tag($token); }
  132:   }
  133:   return $result;
  134: }
  135: 
  136: sub end_textline {
  137:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  138:   if ($target eq 'edit') { return ('','no'); }
  139:   return "";
  140: }
  141: 
  142: sub start_datasubmission {
  143:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  144:   my $id = &Apache::response::start_response($parstack,$safeeval);
  145:   my $result;
  146:   if ($target eq 'meta') {
  147:     $result = &Apache::response::meta_stores_write($token->[2]->{'name'},
  148: 						   $token->[2]->{'type'},
  149: 						   $token->[2]->{'display'});
  150:     $result .= &Apache::response::meta_package_write('datasubmission');
  151:   }
  152:   return $result;
  153: }
  154: 
  155: sub end_datasubmission {
  156:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
  157:   my $result;
  158:   if ( $target eq 'web' ) {
  159:   } elsif ($target eq 'grade' ) {
  160:     if ( defined $ENV{'form.submitted'}) {
  161:       &Apache::response::setup_params('datasubmission');
  162:       my $partid = $Apache::inputtags::part;
  163:       my $id = $Apache::inputtags::response['-1'];
  164:       my $response = $ENV{'form.HWVAL'.$id};
  165:       my $name = &Apache::lonxml::get_param('name',$parstack,$safeeval);
  166:       if ( $response =~ /[^\s]/) {
  167: 	$Apache::lonhomework::results{"resource.$partid.$id.$name"}=
  168: 	  $response;
  169:       }
  170:     }
  171:   }
  172:   &Apache::response::end_response;
  173:   return $result;
  174: }
  175: 
  176: sub finalizeawards {
  177:   my $result='';
  178:   my $award;
  179:   if ($#_ == '-1') { $result = "NO_RESPONSE"; }
  180:   if ($result eq '' ) {
  181:     foreach $award (@_) { if ($award eq '') {$result='MISSING_ANSWER'; last;}}
  182:   }
  183:   if ($result eq '' ) {
  184:     foreach $award (@_) { if ($award eq 'ERROR') {$result='ERROR'; last;}}
  185:   }
  186:   if ($result eq '' ) {
  187:     foreach $award (@_) { if ($award eq 'NO_RESPONSE') {$result='NO_RESPONSE'; last;} }
  188:   }
  189: 
  190:   if ($result eq '' ) {
  191:     foreach $award (@_) { 
  192:       if ($award eq 'UNIT_FAIL' ||
  193: 	  $award eq 'NO_UNIT' ||
  194: 	  $award eq 'UNIT_NOTNEEDED') {
  195: 	$result=$award; last;
  196:       }
  197:     }
  198:   }
  199:   if ($result eq '' ) {
  200:     foreach $award (@_) { 
  201:       if ($award eq 'WANTED_NUMERIC' || 
  202: 	  $award eq 'BAD_FORMULA') {$result=$award; last;}
  203:     }
  204:   }
  205:   if ($result eq '' ) {
  206:     foreach $award (@_) { if ($award eq 'SIG_FAIL') {$result=$award; last;} }
  207:   }
  208:   if ($result eq '' ) {
  209:     foreach $award (@_) { if ($award eq 'INCORRECT') {$result=$award; last;} }
  210:   }
  211:   if ($result eq '' ) {
  212:     foreach $award (@_) { if ($award eq 'SUBMITTED') {$result=$award; last;} }
  213:   }
  214:   if ($result eq '' ) {
  215:     foreach $award (@_) { if ($award eq 'APPROX_ANS') {$result=$award; last;} }
  216:   }
  217:   if ($result eq '' ) { $result='EXACT_ANS'; }
  218:   return $result
  219: }
  220: 
  221: sub decideoutput {
  222:   my ($award,$solved,$previous)=@_;
  223:   my $message='';
  224:   my $button=0;
  225:   my $previousmsg;
  226: 
  227:   if ($previous) { $previousmsg='You have entered that answer before'; }
  228: 
  229:   if      ($solved =~ /^correct/) {
  230:     $message = "<b>You are correct.</b> Your receipt is ".
  231:       &Apache::lonnet::receipt;
  232:     $button=0;
  233:     $previousmsg='';
  234:   } elsif ($solved =~ /^excused/) {
  235:     $message = "<b>You are excused from the problem.</b>";
  236:     $button=0;
  237:     $previousmsg='';
  238:   } elsif ($award eq 'EXACT_ANS' || $award eq 'APPROX_ANS' ) {
  239:     if ($solved =~ /^incorrect/ || $solved eq '') {
  240:       $message = "Incorrect";
  241:       $button=1;
  242:     } else {
  243:       $message = "<b>You are correct.</b> Your receipt is ".
  244: 	&Apache::lonnet::receipt;
  245:       $button=0;
  246:       $previousmsg='';
  247:     }
  248:   } elsif ($award eq 'NO_RESPONSE') {
  249:     $message = '';
  250:     $button=1;
  251:   } elsif ($award eq 'MISSING_ANSWER') {
  252:     $message = 'Some parts were not submitted';
  253:     $button = 1;
  254:   } elsif ($award eq 'WANTED_NUMERIC') {
  255:     $message = "This question expects a numeric answer";
  256:     $button=1;
  257:   } elsif ($award eq 'SIG_FAIL') {
  258:     $message = "Please adjust significant figures.";# you provided %s significant figures";
  259:     $button=1;
  260:   } elsif ($award eq 'UNIT_FAIL') {
  261:     $message = "Units incorrect."; #Computer reads units as %s";
  262:     $button=1;
  263:   } elsif ($award eq 'UNIT_NOTNEEDED') {
  264:     $message = "Only a number required.";# Computer reads units of %s";
  265:     $button=1;
  266:   } elsif ($award eq 'NO_UNIT') {
  267:     $message = "Units required";
  268:     $button=1;
  269:   } elsif ($award eq 'BAD_FORMULA') {
  270:     $message = "Unable to understand formula";
  271:     $button=1;
  272:   } elsif ($award eq 'INCORRECT') {
  273:     $message = "Incorrect";
  274:     $button=1;
  275:   } elsif ($award eq 'SUBMITTED') {
  276:     $message = "Your submission has been recorded.";
  277:     $button=1;
  278:   } else {
  279:     $message = "Unknown message: $award";
  280:     $button=1;
  281:   }
  282:   return ($button,$message,$previousmsg);
  283: }
  284: 
  285: sub setgradedata {
  286:   my ($award,$id,$previously_used) = @_;
  287:   # if the student already has it correct, don't modify the status
  288:   if ( $Apache::lonhomework::history{"resource.$id.solved"} !~
  289:        /^correct/ ) {
  290:     #handle assignment of tries and solved status
  291:     if ( $award eq 'APPROX_ANS' || $award eq 'EXACT_ANS' ) {
  292:       $Apache::lonhomework::results{"resource.$id.tries"} =
  293: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  294:       $Apache::lonhomework::results{"resource.$id.solved"} =
  295: 	'correct_by_student';
  296:       $Apache::lonhomework::results{"resource.$id.awarded"} = '1';
  297:     } elsif ( $award eq 'INCORRECT' ) {
  298:       $Apache::lonhomework::results{"resource.$id.tries"} =
  299: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  300:       $Apache::lonhomework::results{"resource.$id.solved"} =
  301: 	'incorrect_attempted';
  302:     } elsif ( $award eq 'SUBMITTED' ) {
  303:       $Apache::lonhomework::results{"resource.$id.tries"} =
  304: 	$Apache::lonhomework::history{"resource.$id.tries"} + 1;
  305:       $Apache::lonhomework::results{"resource.$id.solved"} =
  306: 	'ungraded_attempted';
  307:     } elsif ( $award eq 'NO_RESPONSE' ) {
  308:       return '';
  309:     } else {
  310:       $Apache::lonhomework::results{"resource.$id.solved"} =
  311: 	'incorrect_attempted';
  312:     }
  313: 
  314:     # check if this was a previous submission if it was delete the
  315:     # unneeded data and update the previously_used attribute
  316:     if ( $previously_used eq 'PREVIOUSLY_USED') {
  317:       delete($Apache::lonhomework::results{"resource.$id.tries"});
  318:       $Apache::lonhomework::results{"resource.$id.previous"} = '1';
  319:     } elsif ( $previously_used eq 'PREVIOUSLY_LAST') {
  320:       #delete all data as they student didn't do anything
  321:       foreach my $key (keys(%Apache::lonhomework::results)) {
  322: 	if ($key =~ /^resource\.$id\./) {
  323: 	  &Apache::lonxml::debug("Removing $key");
  324: 	  delete($Apache::lonhomework::results{$key});
  325: 	}
  326:       }
  327:       #and since they didn't do anything we were never here
  328:       return '';
  329:     } else {
  330:       $Apache::lonhomework::results{"resource.$id.previous"} = '0';
  331:     }
  332:   }
  333:   $Apache::lonhomework::results{"resource.$id.award"} = $award;
  334: }
  335: 
  336: sub grade {
  337:   my ($target) = @_;
  338:   my $id = $Apache::inputtags::part;
  339:   my $response='';
  340:   if ( defined $ENV{'form.submitted'}) {
  341:     my @awards = ();
  342:     foreach $response (@Apache::inputtags::responselist) {
  343:       &Apache::lonxml::debug("looking for response.$id.$response.awarddetail");
  344:       my $value=$Apache::lonhomework::results{"resource.$id.$response.awarddetail"};
  345:       if ( $value ne '' ) {
  346: 	&Apache::lonxml::debug("keeping $value from $response for $id");
  347: 	push (@awards,$value);
  348:       } else {
  349: 	&Apache::lonxml::debug("skipping $value from $response for $id");
  350:       }
  351:     }
  352:     my $finalaward = &finalizeawards(@awards);
  353:     my $previously_used;
  354:     if ( $#Apache::inputtags::previous eq $#awards ) {
  355:       $previously_used = 'PREVIOUSLY_LAST';
  356:       foreach my $value (@Apache::inputtags::previous) {
  357: 	if ($value eq 'PREVIOUSLY_USED' ) {
  358: 	  $previously_used = $value;
  359: 	  last;
  360: 	}
  361:       }
  362:     }
  363:     &Apache::lonxml::debug("final award $finalaward, $previously_used");
  364:     &setgradedata($finalaward,$id,$previously_used);
  365:   }
  366:   return '';
  367: }
  368: 
  369: sub gradestatus {
  370:   my ($id) = @_;
  371:   my $showbutton = 1;
  372:   my $message = '';
  373:   my $trystr='';
  374:   my $button='';
  375:   my $previousmsg='';
  376: 
  377:   my $status = $Apache::inputtags::status['-1'];
  378:   &Apache::lonxml::debug("gradestatus has :$status:");
  379:   if ( $status ne 'CLOSED' ) {  
  380:     my $award = $Apache::lonhomework::history{"resource.$id.award"};
  381:     my $solved = $Apache::lonhomework::history{"resource.$id.solved"};
  382:     my $previous = $Apache::lonhomework::history{"resource.$id.previous"};
  383:     &Apache::lonxml::debug("Found Award |$award|$solved|");
  384:     if ( $award ne '' ) {
  385:       &Apache::lonxml::debug('Getting message');
  386:       ($showbutton,$message,$previousmsg) =
  387: 	&decideoutput($award,$solved,$previous);
  388:       $message="<td bgcolor=\"#aaffaa\">$message</td>";
  389:       if ($previousmsg) {
  390: 	$previousmsg="<td bgcolor=\"#ffaaaa\">$previousmsg</td>";
  391:       }
  392:     }
  393:     my $tries = $Apache::lonhomework::history{"resource.$id.tries"};
  394:     my $maxtries = &Apache::lonnet::EXT("resource.$id.maxtries");
  395:     &Apache::lonxml::debug("got maxtries of :$maxtries:");
  396:     if ( $tries eq '' ) { $tries = '0'; }
  397:     if ( $maxtries eq '' ) { $maxtries = '2'; } 
  398:     if ( $maxtries eq 'con_lost' ) { $maxtries = '0'; } 
  399:     if ( $showbutton ) {
  400:       $trystr = "<td>Tries $tries/$maxtries</td>";
  401:     }
  402:     if ( $status eq 'SHOW_ANSWER' || $status eq 'CANNOT_ANSWER') {$showbutton = 0;}
  403:     if ( $showbutton ) { 
  404:       $button = '<br /><input type="submit" name="submit" value="Submit All Answers" />';
  405:     }
  406:   }
  407:   my $output= $previousmsg.$message.$trystr;
  408:   if ($output =~ /^\s*$/) {
  409:     return $button;
  410:   } else {
  411:     return $button.'<table><tr>'.$previousmsg.$message.$trystr.'</tr></table>';
  412:   }
  413: }
  414: 1;
  415: __END__
  416:  

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