Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.88

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

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