File:  [LON-CAPA] / loncom / homework / inputtags.pm
Revision 1.73: download - view: text, annotated - select for diffs
Fri Nov 8 20:34:10 2002 UTC (21 years, 6 months ago) by sakharuk
Branches: MAIN
CVS tags: HEAD
Added if statement to avoid print input text box in the case of exam.

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

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