File:  [LON-CAPA] / loncom / homework / caparesponse / caparesponse.pm
Revision 1.90: download - view: text, annotated - select for diffs
Tue Apr 8 03:54:22 2003 UTC (21 years, 2 months ago) by albertel
Branches: MAIN
CVS tags: HEAD
- refactored alot of the bubble printing code

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

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