File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.65: download - view: text, annotated - select for diffs
Mon Oct 14 20:37:27 2002 UTC (21 years, 7 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- adding analyze to more tags in preperation for analyzing a problems answer distribution

    1: # The LearningOnline Network with CAPA
    2: # caparesponse definition
    3: #
    4: # $Id: caparesponse.pm,v 1.65 2002/10/14 20:37:27 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: 
   29: package Apache::caparesponse;
   30: use strict;
   31: use capa;
   32: 
   33: BEGIN {
   34:   &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
   35: }
   36: 
   37: sub start_caparesponse {
   38:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   39:   my $id = &Apache::response::start_response($parstack,$safeeval);
   40:   return '';
   41: }
   42: 
   43: sub end_caparesponse {
   44:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
   45:   my $result = '';
   46:   if ( $target eq 'grade' ) {
   47:     if ( defined $ENV{'form.submitted'}) {
   48:       $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
   49:       my $response = $ENV{'form.HWVAL'.$Apache::inputtags::response['-1']};
   50:       if ( $response =~ /[^\s]/) {
   51: 	my $id = $Apache::inputtags::response['-1'];
   52: 	my $previous= &Apache::response::check_for_previous($response,$id,$response);
   53: 	if ( $previous ) { 
   54: 	  $result = 'PREVIOUSLY_USED';
   55: 	} else {
   56: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.submission"}=$response;
   57: 	  &Apache::lonxml::debug("submitted a $response<br>\n");
   58: 	  &Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
   59: 	  my $expression="&caparesponse_check('".$response."','".
   60: 	    $$parstack[$#$parstack].
   61: 	      ';my $tol="'.$Apache::inputtags::params{'tol'}.'"'.
   62: 		';my $sig="'.$Apache::inputtags::params{'sig'}.'"'.
   63: 		  "');";
   64: 	  $result = &Apache::run::run($expression,$safeeval);
   65: 	  my ($ad) = split /:/ , $result;
   66: 	  $Apache::lonhomework::results{"resource.$Apache::inputtags::part.$id.awarddetail"}=$ad;
   67: 	  &Apache::lonxml::debug("$expression");
   68: 	  &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
   69: 	}
   70:       }
   71:     }
   72:   } elsif ($target eq 'web') {
   73:     #$result .= "The correct answer is ".&Apache::run::run("{$$parstack['-1'];".'return $answer}',$safeeval)."\n";
   74:   }
   75:   #Apache::lonxml::debug("\n<br>\nreal end caparesponse<br>\n");
   76:   &Apache::response::end_response;
   77:   return '';
   78: }
   79: 
   80: sub start_numericalresponse {
   81:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
   82:   my $id = &Apache::response::start_response($parstack,$safeeval);
   83:   my $result;
   84:   if ($target eq 'edit') {
   85:     $result.=&Apache::edit::tag_start($target,$token);
   86:     $result.=&Apache::edit::text_arg('Answer:','answer',$token);
   87:     if ($token->[1] eq 'numericalresponse') {
   88:       $result.=&Apache::edit::text_arg('Unit:','unit',$token,5);
   89:       $result.=&Apache::edit::text_arg('Format:','format',$token,4);
   90:     } elsif ($token->[1] eq 'stringresponse') {
   91:       $result.=&Apache::edit::select_arg('Type:','type',
   92: 					 ['cs','ci','mc'],$token);
   93:     } elsif ($token->[1] eq 'formularesponse') {
   94:       $result.=&Apache::edit::text_arg('Sample Points:','samples',$token,40);
   95:     }
   96:     $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
   97:   } elsif ($target eq 'modified') {
   98:     my $constructtag;
   99:     if ($token->[1] eq 'numericalresponse') {
  100:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  101: 						'answer','unit','format');
  102:     } elsif ($token->[1] eq 'stringresponse') {
  103:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  104: 						'answer','type');
  105:     } elsif ($token->[1] eq 'formularesponse') {
  106:       $constructtag=&Apache::edit::get_new_args($token,$parstack,$safeeval,
  107: 						'answer','samples');
  108:     }
  109:     if ($constructtag) {
  110:       $result = &Apache::edit::rebuild_tag($token);
  111:       $result.=&Apache::edit::handle_insert();
  112:     }
  113:   } elsif ($target eq 'meta') {
  114:     $result=&Apache::response::meta_package_write('numericalresponse');
  115:   }
  116:   return $result;
  117: }
  118: 
  119: sub end_numericalresponse {
  120:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  121:   my $result = '';
  122:   if ( $target eq 'grade' ) {
  123:     if ( defined $ENV{'form.submitted'}) {
  124:       &Apache::response::setup_params($$tagstack[-1]);
  125:       $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
  126:       my $partid = $Apache::inputtags::part;
  127:       my $id = $Apache::inputtags::response['-1'];
  128:       my $response = $ENV{'form.HWVAL'.$id};
  129:       if ( $response =~ /[^\s]/) {
  130: 	my %previous = &Apache::response::check_for_previous($response,$partid,$id);
  131: 	$Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
  132: 	&Apache::lonxml::debug("submitted a $response<br>\n");
  133: 	&Apache::lonxml::debug($$parstack[$#$parstack] . "\n<br>");
  134: 	$response =~ s/\\/\\\\/g;
  135: 	$response =~ s/\'/\\\'/g;
  136: 	&Apache::lonxml::debug("current $response");
  137: 	my $expression="&caparesponse_check_list('".$response."','".
  138: 	  $$parstack[$#$parstack];
  139: 	foreach my $key (keys(%Apache::inputtags::params)) {
  140: 	  $expression.= ';my $'. #'
  141: 	    $key.'="'.$Apache::inputtags::params{$key}.'"';
  142: 	}
  143: 	if ($$tagstack[-1] eq 'formularesponse') {
  144: 	  $expression.=';my $type="fml";';
  145: 	} elsif ($$tagstack[-1] eq 'numericalresponse') {
  146: 	  $expression.=';my $type="float";';
  147: 	}
  148: 	$expression.="');";
  149: 	$result = &Apache::run::run($expression,$safeeval);
  150: 	my ($awards) = split /:/ , $result;
  151: 	my ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
  152: 	&Apache::response::handle_previous(\%previous,$ad);
  153: 	$Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
  154: 	&Apache::lonxml::debug("$expression");
  155: 	&Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
  156: 	$result='';
  157:       }
  158:     }
  159:   } elsif ($target eq 'web' || $target eq 'tex') {
  160:     if ($Apache::lonhomework::type ne 'exam') {
  161:       my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
  162:       my $status = $Apache::inputtags::status['-1'];
  163:       if ($award =~ /^correct/ || $status eq "SHOW_ANSWER" ) {
  164: 	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
  165: 						     $safeeval);
  166: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
  167: 						     $safeeval);
  168: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
  169: 	
  170: 	if ($target eq 'web') {
  171: 	  $result="<br />The correct answer is ";
  172:         } elsif ($target eq 'tex') {
  173: 	  $result='\vskip 0 mm The correct answer is \\texttt{';
  174:         }
  175: 	for (my $i=0; $i <= $#answers; $i++) {
  176: 	   my $answer=$answers[$i];
  177: 	   my $format;
  178: 	   if ($#formats > 0) {
  179: 	     $format=$formats[$i];
  180: 	   } else {
  181: 	     $format=$formats[0];
  182: 	   }
  183: 	   my $formatted;
  184: 	   if ((defined($format)) && ($format ne '')) {
  185: 	     &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
  186: 	     $formatted=sprintf('%.'.$format,$answer).',';
  187: 	   } else {
  188: 	     &Apache::lonxml::debug("no format answer :$answer:");
  189: 	     $formatted="$answer,";
  190: 	   }
  191: 	   if ($target eq 'tex') {$formatted=&Apache::lonxml::latex_special_symbols($formatted);}
  192: 	     $result.=$formatted;
  193:            }
  194: 	   chop $result;
  195: 	   if ($target eq 'web') {
  196: 	     $result.=" $unit.<br />";
  197: 	   } elsif ($target eq 'tex') {
  198: 	     $result.=&Apache::lonxml::latex_special_symbols($unit);
  199: 	     $result.="}. \\vskip 0 mm ";
  200: 	   }
  201:        }
  202:     } else {
  203:       	my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  204: 	my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  205: 	my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
  206: 	my $number_of_bubbles = 8; #default values for number of bubbles
  207: 	my @bubbles_values = (); 
  208: 	my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
  209: 	my @powers = (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0); #default values for powers
  210: 	my $factors_number = 5; #default values for number of factors
  211: 	my $powers_number = 8; #default values for number of powers
  212: 	my $symb;
  213: 	if ($ENV{'form.symb'}=~/___\d+___/) {$symb=$ENV{'form.symb'};} else {$symb=$ENV{'request.symb'};}
  214: 	my $randomseed = srand(&Apache::lonnet::rndseed($symb,$ENV{'request.course.id'},
  215:                                                               $ENV{'user.domain'},$ENV{'user.name'}));
  216: 	my $ind=1+int(rand()*($factors_number-1));
  217: 	my $factor = $factors[$ind];
  218: 	$ind=1+int(rand()*($powers_number-1));
  219: 	my $power = $powers[$ind];
  220: 	for ($ind=0;$ind<$number_of_bubbles;$ind++) {
  221: 	    $bubbles_values[$ind] = $answers[0]*$factor**($power-$powers[$powers_number-$ind-1]);
  222: 	}	    
  223: 	my @alphabet = ('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  224: 			'Q','R','S','T','U','V','W','X','Y','Z');
  225:         if ($target eq 'web') {
  226: 	    $result.= '<table border="1"><tr>';
  227: 	    for ($ind=0;$ind<$number_of_bubbles;$ind++) {
  228: 		my $ans;
  229: 		if ($formats[0] ne '') {
  230: 		    $ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);
  231: 		} else {
  232: 		    my $badans = $bubbles_values[$ind];
  233: 		    my $format = ''; 
  234:                     #What is the number? (integer,decimal,floating point)
  235:                     if ($badans=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
  236: 			$format = 'e'.$2;
  237: 		    } elsif ($badans=~/^(\d*)\.(\d*)$/) {
  238: 			$format = '4f';
  239: 		    } elsif ($badans=~/^(\d*)$/) {
  240: 			$format = 'd';
  241: 		    }
  242: 		    $ans = sprintf('%.'.$format,$bubbles_values[$ind]);
  243: 		}
  244: 		    $result.='<td>'.$alphabet[$ind].': '.$ans.'</td>';
  245: 	    }
  246: 	    $result.='</tr></table>';
  247: 	} elsif ($target eq 'tex') {
  248: 	    my $max_val = 0;
  249: 	    if ($formats[0]=~m/^(\d+)E([^\d]*)(\d*)$/) {
  250: 		$max_val=$1+$2+4;
  251: 	    } else {
  252: 		$max_val=5;
  253: 	    }
  254: 	    $max_val = int(90/(($max_val+6)*2));
  255:             my $celllength = 90/$max_val-12;
  256: 	    my @table_range = ();
  257: 	    my $number_of_tables = int($number_of_bubbles/$max_val);
  258: 	    for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$max_val;}
  259: 	    if ($number_of_bubbles % $max_val != 0) {
  260: 		$number_of_tables++;
  261: 		push @table_range,($number_of_bubbles % $max_val);
  262: 	    }
  263: 	    my $j=0;
  264: 	    my $cou=0;
  265: 	    $result.='\vskip 2 mm \noindent ';
  266: 	    for (my $i=0;$i<$number_of_tables;$i++) {
  267: 		$result.='\vskip 0 mm \noindent \begin{tabular}{|';
  268: 		for ($ind=0;$ind<$table_range[$j];$ind++) {
  269: 		    $result.='lp{'.$celllength.' mm}|';
  270: 		}
  271: 		$result.='}\hline';
  272: 		for ($ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
  273: 		    my $ans;
  274: 		    if ($formats[0] ne '') {
  275: 			$ans = sprintf('%.'.$formats[0],$bubbles_values[$ind]);
  276: 		    } else {
  277: 			my $badans = $bubbles_values[$ind];
  278: 			my $format = ''; 
  279: 			#What is the number? (integer,decimal,floating point)
  280: 			if ($badans=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
  281: 			    $format = 'e'.$2;
  282: 			} elsif ($badans=~/^(\d*)\.(\d*)$/) {
  283: 			    $format = '4f';
  284: 			} elsif ($badans=~/^(\d*)$/) {
  285: 			    $format = 'd';
  286: 			}
  287: 			$ans = sprintf('%.'.$format,$bubbles_values[$ind]);
  288: 		    }
  289: 		    $result.=' '.$alphabet[$ind].': & '.$ans.' ';
  290: 		    if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
  291: 		}
  292: 		$cou += $table_range[$j];
  293: 		$j++;
  294: 		$result.='\\\\\hline\end{tabular}\vskip 0 mm ';
  295: 	    }	    
  296: 	}
  297:     }
  298:   } elsif ($target eq 'edit') {
  299:     $result.='</td></tr>'.&Apache::edit::end_table;
  300:   } elsif ($target eq 'answer' || $target eq 'analyze') {
  301: 
  302:       my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
  303:       if ($target eq 'analyze') {
  304: 	  push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
  305: 	  $Apache::lonhomework::analyze{"$part_id.type"} = $$tagstack[-1];
  306:       }
  307:       &Apache::response::setup_params($$tagstack[-1]);
  308:       my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
  309:       my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
  310:       my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
  311:       my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
  312: 
  313:       if ($target eq 'answer') {
  314: 	  $result.=&Apache::response::answer_header($$tagstack[-1]);
  315:       }
  316:       for(my $i=0;$i<=$#answers;$i++) {
  317: 	  my $ans=$answers[$i];
  318: 	  my $fmt;
  319: 	  if (@formats) {
  320: 	      if ($#formats) {
  321: 		  $fmt=$formats[$i];
  322: 	      } else {
  323: 		  $fmt=$formats[0];
  324: 	      }
  325: 	  }
  326: 	  my ($high,$low);
  327: 	  if ($Apache::inputtags::params{'tol'}) {
  328: 	      ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
  329: 	  }
  330: 	  my ($sighigh,$siglow);
  331: 	  if ($Apache::inputtags::params{'sig'}) {
  332: 	      ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
  333: 	  }
  334: 	  if ($fmt) {
  335: 	      $ans = sprintf('%.'.$fmt,$ans);
  336: 	      if ($high) {
  337: 		  $high = sprintf('%.'.$fmt,$high); $low = sprintf('%.'.$fmt,$low);
  338: 	      }
  339: 	  }
  340: 	  if ($target eq 'answer') {
  341: 	      if ($high) { $ans.=' ['.$low.','.$high.'] '; }
  342: 	      if ($sighigh) { $ans.= "Sig <i>$siglow - $sighigh</i>"; }
  343: 	      $result.=&Apache::response::answer_part($$tagstack[-1],$ans);
  344: 	  } elsif ($target eq 'analyze') {
  345: 	      push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} },
  346: 		    $ans);
  347: 	      if ($high) {
  348: 		  push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
  349: 		  push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
  350: 	      }
  351: 	  }
  352:       }
  353:       if ($unit) {
  354: 	  if ($target eq 'answer') {
  355: 	      $result.=&Apache::response::answer_part($$tagstack[-1],
  356: 						      "Unit: <b>$unit</b>");
  357: 	  } elsif ($target eq 'analyze') {
  358: 	      push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} },
  359: 		    $unit);
  360: 	  }
  361:       }
  362:       if ($type || $token->[1] eq 'stringresponse') {
  363: 	  my $string='Case Insensitive';
  364: 	  if ($type eq 'mc') {
  365: 	      $string='Multiple Choice';
  366: 	  } elsif ($type eq 'cs') {
  367: 	      $string='Case Sensitive';
  368: 	  } elsif ($type eq 'ci') {
  369: 	      $string='Case Insensitive';
  370: 	  } elsif ($type eq 'fml') {
  371: 	      $string='Formula';
  372: 	  }
  373: 	  if ($target eq 'answer') {
  374: 	      $result.=&Apache::response::answer_part($$tagstack[-1],
  375: 						      '<b>'.$string.'</b>');
  376: 	  } elsif ($target eq 'analyze') {
  377: 	      push (@{ $Apache::lonhomework::analyze{"$part_id.type"} },
  378: 		    $type);
  379: 	  }
  380:       }
  381:       if ($$tagstack[-1] eq 'formularesponse' && $target eq 'answer') {
  382: 	  my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
  383: 	  $result.=&Apache::response::answer_part($$tagstack[-1],$samples);
  384:       }
  385:       if ($target eq 'answer') {
  386: 	  $result.=&Apache::response::answer_footer($$tagstack[-1]);
  387:       }
  388:   }
  389:   &Apache::response::end_response;
  390:   return $result;
  391: }
  392: 
  393: sub get_tolrange {
  394:   my ($ans,$tol)=@_;
  395:   my ($high,$low);
  396:   if ($tol =~ /%$/) {
  397:     chop($tol);
  398:     my $change=$ans*($tol/100.0);
  399:     $high=$ans+$change;
  400:     $low=$ans-$change;
  401:   } else {
  402:     $high=$ans+$tol;
  403:     $low=$ans-$tol;
  404:   }
  405:   return ($high,$low);
  406: }
  407: 
  408: sub get_sigrange {
  409:   my ($sig)=@_;
  410:   &Apache::lonxml::debug("Got a sig of :$sig:");
  411:   my $sig_lbound;
  412:   my $sig_ubound;
  413:   if ($sig eq '') {
  414:     $sig_lbound = 0; #SIG_LB_DEFAULT
  415:     $sig_ubound =15; #SIG_UB_DEFAULT
  416:   } else {
  417:     ($sig_lbound,$sig_ubound) = split(/,/,$sig);
  418:     if (!$sig_lbound) {
  419:       $sig_lbound = 0; #SIG_LB_DEFAULT
  420:       $sig_ubound =15; #SIG_UB_DEFAULT
  421:     }
  422:     if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
  423:   }
  424:   return ($sig_ubound,$sig_lbound);
  425: }
  426: 
  427: sub start_stringresponse {
  428:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  429:   my $result;
  430:   if ($target eq 'meta') {
  431:     $result=&Apache::response::meta_package_write('stringresponse');
  432:   } else {
  433:     $result.=&start_numericalresponse(@_);
  434:   }
  435:   return $result;
  436: }
  437: 
  438: sub end_stringresponse {
  439:   return end_numericalresponse(@_);
  440: }
  441: 
  442: sub start_formularesponse {
  443:   my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
  444:   my $result;
  445:   if ($target eq 'meta') {
  446:     $result=&Apache::response::meta_package_write('formularesponse');
  447:   } else {
  448:     $result.=&start_numericalresponse(@_);
  449:   }
  450:   return $result;
  451: }
  452: 
  453: sub end_formularesponse {
  454:   return end_numericalresponse(@_);
  455: }
  456: 
  457: 1;
  458: __END__
  459:  

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