Annotation of loncom/homework/caparesponse/caparesponse.pm, revision 1.130
1.3 albertel 1: # The LearningOnline Network with CAPA
2: # caparesponse definition
1.47 albertel 3: #
1.130 ! sakharuk 4: # $Id: caparesponse.pm,v 1.129 2004/02/13 15:09:52 www 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.129 www 32: use Apache::lonlocal;
1.3 albertel 33:
1.50 harris41 34: BEGIN {
1.89 albertel 35: &Apache::lonxml::register('Apache::caparesponse',('caparesponse','numericalresponse','stringresponse','formularesponse'));
1.3 albertel 36: }
37:
1.89 albertel 38: sub start_numericalresponse {
39: my ($target,$token,$tagstack,$parstack,$parser,$safeeval)=@_;
40: my $id = &Apache::response::start_response($parstack,$safeeval);
41: my $result;
42: if ($target eq 'edit') {
43: $result.=&Apache::edit::tag_start($target,$token);
44: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
45: if ($token->[1] eq 'numericalresponse') {
1.122 albertel 46: $result.=&Apache::edit::text_arg('Incorrect Answers:','incorrect',
47: $token);
1.89 albertel 48: $result.=&Apache::edit::text_arg('Unit:','unit',$token,5).
49: &Apache::loncommon::help_open_topic('Physical_Units');
50: $result.=&Apache::edit::text_arg('Format:','format',$token,4).
51: &Apache::loncommon::help_open_topic('Numerical_Response_Format');
52: } elsif ($token->[1] eq 'formularesponse') {
53: $result.=&Apache::edit::text_arg('Sample Points:','samples',
54: $token,40).
55: &Apache::loncommon::help_open_topic('Formula_Response_Sampling');
56: }
57: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
58: } elsif ($target eq 'modified') {
59: my $constructtag;
60: if ($token->[1] eq 'numericalresponse') {
61: $constructtag=&Apache::edit::get_new_args($token,$parstack,
62: $safeeval,'answer',
1.117 albertel 63: 'incorrect','unit',
64: 'format');
1.89 albertel 65: } elsif ($token->[1] eq 'formularesponse') {
66: $constructtag=&Apache::edit::get_new_args($token,$parstack,
67: $safeeval,'answer',
68: 'samples');
69: }
70: if ($constructtag) {
71: $result = &Apache::edit::rebuild_tag($token);
72: $result.=&Apache::edit::handle_insert();
1.22 albertel 73: }
1.89 albertel 74: } elsif ($target eq 'meta') {
75: $result=&Apache::response::meta_package_write('numericalresponse');
76: } elsif ($target eq 'answer' || $target eq 'grade') {
77: &Apache::response::reset_params();
1.96 albertel 78: } elsif ($target eq 'web') {
79: my $partid = $Apache::inputtags::part;
80: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
81: &Apache::lonxml::debug("Got unit $hideunit for $partid $id");
82: #no way to enter units, with radio buttons
83: if (lc($hideunit) eq "yes") {
84: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
85: $safeeval);
86: if ($unit =~ /\S/) { $result.=" (in $unit) "; }
87: }
1.16 albertel 88: }
1.89 albertel 89: return $result;
1.21 albertel 90: }
91:
1.89 albertel 92: sub end_numericalresponse {
93: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
1.90 albertel 94: my $increment=1;
1.89 albertel 95: my $result = '';
96: if (!$Apache::lonxml::default_homework_loaded) {
97: &Apache::lonxml::default_homework_load($safeeval);
1.34 albertel 98: }
1.125 albertel 99: my $tag;
100: if (scalar(@$tagstack)) { $tag=$$tagstack[-1]; }
1.95 albertel 101: if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
1.125 albertel 102: &Apache::response::setup_params($tag);
1.90 albertel 103: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
104: my $partid = $Apache::inputtags::part;
105: my $id = $Apache::inputtags::response['-1'];
1.95 albertel 106: if ($Apache::lonhomework::type eq 'exam' &&
1.125 albertel 107: $tag eq 'formularesponse') {
1.95 albertel 108: $increment=&Apache::response::scored_response($partid,$id);
109: } else {
110: my $response = &Apache::response::getresponse();
111: if ( $response =~ /[^\s]/) {
112: my $ad;
113: my %previous = &Apache::response::check_for_previous($response,$partid,$id);
114: &Apache::lonxml::debug("submitted a $response<br>\n");
115: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
116:
117: if ($ENV{'form.submitted'} eq 'scantron') {
1.117 albertel 118: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
119: if (!$number_of_bubbles) { $number_of_bubbles=8; }
120: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
121: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
122: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
123: my @values=&make_numerical_bubbles($number_of_bubbles,$target,$answers[0],$formats[0],\@incorrect);
1.95 albertel 124: $response=$values[$response];
125: } else {
126: $response =~ s/\\/\\\\/g;
127: $response =~ s/\'/\\\'/g;
128: }
1.115 albertel 129: $Apache::lonhomework::results{"resource.$partid.$id.submission"}=$response;
1.95 albertel 130: &Apache::lonxml::debug("current $response");
131: my $expression="&caparesponse_check_list('".$response."','".
132: $$parstack[-1];
1.96 albertel 133: my $hideunit=&Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.turnoffunit');
1.123 albertel 134:
135: foreach my $key (keys(%Apache::inputtags::params)) {
136: $expression.= ';my $'. #'
137: $key.'="'.$Apache::inputtags::params{$key}.'"';
138: }
139:
1.95 albertel 140: #no way to enter units, with radio buttons
1.96 albertel 141: if ($Apache::lonhomework::type eq 'exam' ||
142: lc($hideunit) eq "yes") {
1.95 albertel 143: $expression.=';my $unit=undef;';
144: }
1.123 albertel 145: #sig fig don't make much sense either
146: if (($Apache::lonhomework::type eq 'exam' ||
147: $ENV{'form.submitted'} eq 'scantron') &&
1.125 albertel 148: $tag eq 'numericalresponse') {
1.123 albertel 149: $expression.=';my $sig=undef;';
1.95 albertel 150: }
1.123 albertel 151:
1.125 albertel 152: if ($tag eq 'formularesponse') {
1.95 albertel 153: $expression.=';my $type="fml";';
1.125 albertel 154: } elsif ($tag eq 'numericalresponse') {
1.95 albertel 155: $expression.=';my $type="float";';
156: }
157: $expression.="');";
1.105 albertel 158: my @answer=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
159: &Apache::lonxml::debug('answer is'.join(':',@answer));
160: @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=@answer;
161:
1.95 albertel 162: $result = &Apache::run::run($expression,$safeeval);
163: my ($awards) = split /:/ , $result;
164: ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
165: &Apache::lonxml::debug("$expression");
166: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
167: &Apache::response::handle_previous(\%previous,$ad);
168: $Apache::lonhomework::results{"resource.$partid.$id.awarddetail"}=$ad;
169: $result='';
1.90 albertel 170: }
1.89 albertel 171: }
172: } elsif ($target eq 'web' || $target eq 'tex') {
1.90 albertel 173: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,
174: $safeeval);
1.89 albertel 175: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
176: my $status = $Apache::inputtags::status['-1'];
1.114 albertel 177: if ( &Apache::response::show_answer() ) {
1.89 albertel 178: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
179: $safeeval);
180: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
181: $safeeval);
182: if ($target eq 'web') {
1.129 www 183: $result="<br />".&mt('The correct answer is')." ";
1.89 albertel 184: }
185: for (my $i=0; $i <= $#answers; $i++) {
186: my $answer=$answers[$i];
187: my $format;
188: if ($#formats > 0) {
189: $format=$formats[$i];
190: } else {
191: $format=$formats[0];
192: }
193: my $formatted;
194: if ((defined($format)) && ($format ne '')) {
1.109 albertel 195: $format=~s/e/E/g;
1.89 albertel 196: &Apache::lonxml::debug("formatting with :$format: answer :$answer:");
197: $formatted=sprintf('%.'.$format,$answer).',';
198: } else {
199: &Apache::lonxml::debug("no format answer :$answer:");
200: $formatted="$answer,";
201: }
202: if ($target eq 'tex') {
203: $formatted='';
204: #$formatted=&Apache::lonxml::latex_special_symbols($formatted);
205: }
206: $result.=$formatted;
207: }
208: chop $result;
209: if ($target eq 'web') {
210: $result.=" $unit.<br />";
211: }
212: }
213: if ($Apache::lonhomework::type eq 'exam') {
1.118 albertel 214: my $partid=$Apache::inputtags::part;
215: my $id=$Apache::inputtags::response[-1];
1.117 albertel 216: my $number_of_bubbles = &Apache::lonnet::EXT('resource.'.$partid.'_'.$id.'.numbubbles');
1.126 albertel 217: if ($Apache::inputtags::params{'numbubbles'}) {
218: $number_of_bubbles = $Apache::inputtags::params{'numbubbles'};
219: }
1.117 albertel 220: if (!$number_of_bubbles) { $number_of_bubbles=8; }
1.126 albertel 221:
1.89 albertel 222: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,
223: $safeeval);
224: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,
225: $safeeval);
1.117 albertel 226: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
1.90 albertel 227: my @bubble_values=&make_numerical_bubbles($number_of_bubbles,
1.91 albertel 228: $target,$answers[0],
1.117 albertel 229: $formats[0],\@incorrect);
1.89 albertel 230: my @alphabet=('A'..'Z');
231: if ($target eq 'web') {
1.125 albertel 232: if ($tag eq 'numericalresponse') {
1.89 albertel 233: if ($unit=~/\S/) {$result.=' (in '.$unit.')<br /><br />';}
234: $result.= '<table border="1"><tr>';
1.116 albertel 235: my $previous=$Apache::lonhomework::history{"resource.$Apache::inputtags::part.$id.submission"};
1.90 albertel 236: for (my $ind=0;$ind<$number_of_bubbles;$ind++) {
1.116 albertel 237: my $checked='';
238: if ($previous eq $bubble_values[$ind]) {
239: $checked=" checked='on' ";
240: }
1.90 albertel 241: $result.='<td><input type="radio" name="HWVAL_'.$id.
1.116 albertel 242: '" value="'.$bubble_values[$ind].'" '.$checked
243: .' /><b>'.$alphabet[$ind].'</b>: '.
244: $bubble_values[$ind].'</td>';
1.89 albertel 245: }
246: $result.='</tr></table>';
1.125 albertel 247: } elsif ($tag eq 'formularesponse') {
1.90 albertel 248: $result.= '<br /><br /><font color="red">
249: <textarea name="HWVAL_'.$id.'" rows="4" cols="50">
250: </textarea></font> <br /><br />';
1.89 albertel 251: }
252: } elsif ($target eq 'tex') {
1.107 sakharuk 253: if ((defined $unit) and ($unit=~/\S/) and ($Apache::lonhomework::type eq 'exam')) {
1.89 albertel 254: $result.=' \textit{(in} \verb|'.$unit.'|\textit{)} ';
255: }
1.125 albertel 256: if ($tag eq 'numericalresponse') {
1.90 albertel 257: my ($celllength,$number_of_tables,@table_range)=
1.128 sakharuk 258: &get_table_sizes($number_of_bubbles,\@bubble_values);
1.89 albertel 259: my $j=0;
260: my $cou=0;
261: $result.='\vskip -1 mm \noindent \begin{enumerate}\item[\textbf{'.$Apache::lonxml::counter.'}.]';
262: for (my $i=0;$i<$number_of_tables;$i++) {
263: $result.='\vskip -1 mm \noindent \begin{tabular}{';
1.90 albertel 264: for (my $ind=0;$ind<$table_range[$j];$ind++) {
1.128 sakharuk 265: $result.='p{3 mm}p{'.$celllength.' mm}';
1.89 albertel 266: }
267: $result.='}';
1.90 albertel 268: for (my $ind=$cou;$ind<$cou+$table_range[$j];$ind++) {
1.128 sakharuk 269: $result.='\hskip -4 mm {\small \textbf{'.$alphabet[$ind].'}}$\bigcirc$ & \hskip -3 mm {\small '.$bubble_values[$ind].'} ';
1.89 albertel 270: if ($ind != $cou+$table_range[$j]-1) {$result.=' & ';}
271: }
272: $cou += $table_range[$j];
273: $j++;
274: $result.='\\\\\end{tabular}\vskip 0 mm ';
275: }
276: $result.='\end{enumerate}';
277: } else {
278: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
1.93 albertel 279: my $repetition = &Apache::response::repetition();
1.89 albertel 280: $result.='\begin{enumerate}';
281: for (my $i=0;$i<$repetition;$i++) {
1.93 albertel 282: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).'}.]\textit{Leave blank on scoring form}\vskip 0 mm';
1.89 albertel 283: }
1.90 albertel 284: $increment=$repetition;
1.89 albertel 285: $result.= '\end{enumerate}';
286: }
287: }
288: }
289: } elsif ($target eq 'edit') {
290: $result.='</td></tr>'.&Apache::edit::end_table;
291: } elsif ($target eq 'answer' || $target eq 'analyze') {
1.58 sakharuk 292:
1.89 albertel 293: my $part_id="$Apache::inputtags::part.$Apache::inputtags::response[-1]";
294: if ($target eq 'analyze') {
295: push (@{ $Apache::lonhomework::analyze{"parts"} },$part_id);
1.125 albertel 296: $Apache::lonhomework::analyze{"$part_id.type"} = $tag;
1.117 albertel 297: my (@incorrect)=&Apache::lonxml::get_param_var('incorrect',$parstack,$safeeval);
298: push (@{ $Apache::lonhomework::analyze{"$part_id.incorrect"} }, @incorrect);
1.89 albertel 299: }
1.125 albertel 300: if (scalar(@$tagstack)) {
301: &Apache::response::setup_params($tag);
302: }
1.89 albertel 303: my (@answers)=&Apache::lonxml::get_param_var('answer',$parstack,$safeeval);
1.58 sakharuk 304: my (@formats)=&Apache::lonxml::get_param_var('format',$parstack,$safeeval);
305: my $unit=&Apache::lonxml::get_param_var('unit',$parstack,$safeeval);
1.89 albertel 306: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
307:
308: if ($target eq 'answer') {
1.125 albertel 309: $result.=&Apache::response::answer_header($tag);
1.89 albertel 310: }
311: for(my $i=0;$i<=$#answers;$i++) {
312: my $ans=$answers[$i];
1.90 albertel 313: my $fmt=$formats[0];
314: if (@formats && $#formats) {$fmt=$formats[$i];}
1.89 albertel 315: my ($high,$low);
316: if ($Apache::inputtags::params{'tol'}) {
317: ($high,$low)=&get_tolrange($ans,$Apache::inputtags::params{'tol'});
318: }
319: my ($sighigh,$siglow);
320: if ($Apache::inputtags::params{'sig'}) {
321: ($sighigh,$siglow)=&get_sigrange($Apache::inputtags::params{'sig'});
322: }
1.125 albertel 323: if ($fmt && $tag eq 'numericalresponse') {
1.112 albertel 324: $fmt=~s/e/E/g;
1.89 albertel 325: $ans = sprintf('%.'.$fmt,$ans);
326: if ($high) {
1.90 albertel 327: $high=sprintf('%.'.$fmt,$high);
328: $low =sprintf('%.'.$fmt,$low);
1.62 sakharuk 329: }
1.60 sakharuk 330: }
1.89 albertel 331: if ($target eq 'answer') {
1.125 albertel 332: if ($high && $tag eq 'numericalresponse') { $ans.=' ['.$low.','.$high.']'; }
333: if ($sighigh && $tag eq 'numericalresponse') {
1.101 albertel 334: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.106 sakharuk 335: $ans.= " Sig $siglow - $sighigh";
1.98 sakharuk 336: } else {
1.101 albertel 337: $ans.= " Sig <i>$siglow - $sighigh</i>";
1.98 sakharuk 338: }
339: }
1.125 albertel 340: $result.=&Apache::response::answer_part($tag,$ans);
1.89 albertel 341: } elsif ($target eq 'analyze') {
342: push (@{ $Apache::lonhomework::analyze{"$part_id.answer"} }, $ans);
343: if ($high) {
344: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_high"} }, $high);
345: push (@{ $Apache::lonhomework::analyze{"$part_id.ans_low"} }, $low);
1.58 sakharuk 346: }
1.79 sakharuk 347: }
1.36 albertel 348: }
1.102 albertel 349: if (defined($unit) and ($unit ne '') and
1.125 albertel 350: $tag eq 'numericalresponse') {
1.89 albertel 351: if ($target eq 'answer') {
1.101 albertel 352: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.125 albertel 353: $result.=&Apache::response::answer_part($tag,
1.106 sakharuk 354: " Unit: $unit ");
1.98 sakharuk 355: } else {
1.125 albertel 356: $result.=&Apache::response::answer_part($tag,
1.98 sakharuk 357: "Unit: <b>$unit</b>");
358: }
1.89 albertel 359: } elsif ($target eq 'analyze') {
1.103 albertel 360: push (@{ $Apache::lonhomework::analyze{"$part_id.unit"} }, $unit);
1.89 albertel 361: }
362: }
1.125 albertel 363: if ($tag eq 'formularesponse' && $target eq 'answer') {
1.89 albertel 364: my $samples=&Apache::lonxml::get_param('samples',$parstack,$safeeval);
1.125 albertel 365: $result.=&Apache::response::answer_part($tag,$samples);
1.89 albertel 366: }
367: if ($target eq 'answer') {
1.125 albertel 368: $result.=&Apache::response::answer_footer($tag);
1.89 albertel 369: }
1.69 sakharuk 370: }
1.121 sakharuk 371: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
1.90 albertel 372: $target eq 'tex' || $target eq 'analyze') {
373: &Apache::lonxml::increment_counter($increment);
374: }
1.89 albertel 375: &Apache::response::end_response;
376: return $result;
1.90 albertel 377: }
378:
379: sub get_table_sizes {
1.128 sakharuk 380: my ($number_of_bubbles,$rbubble_values)=@_;
381: my $scale=2; #mm for one digit
382: my $cell_width=0;
383: foreach my $member (@$rbubble_values) {
384: my $cell_width_real=0;
385: if ($member=~/(\d*)\.?(\d*)(E|e)(\+|-)?(\d*)/) {
386: $cell_width_real=(length($1)+length($2)+length($5)+9)*$scale;
387: } elsif ($member=~/(\d*)\.?(\d*)/) {
388: $cell_width_real=(length($1)+length($2)+2)*$scale;
389: } else {
390: $cell_width_real=(length($member)+1)*$scale;
391: }
392: if ($cell_width_real>$cell_width) {$cell_width=$cell_width_real;}
1.90 albertel 393: }
1.128 sakharuk 394: $cell_width+=8;
1.130 ! sakharuk 395: my $textwidth;
! 396: if ($ENV{'form.textwidth'} ne '') {
! 397: $ENV{'form.textwidth'}=~/(\d+)/;
! 398: $textwidth=$1;
! 399: } else {
! 400: $ENV{'textwidth'}=~/(\d+)/;
! 401: $textwidth=$1;
! 402: }
1.128 sakharuk 403: my $bubbles_per_line=int($textwidth/$cell_width);
404: my $number_of_tables = int($number_of_bubbles/$bubbles_per_line);
1.90 albertel 405: my @table_range = ();
1.128 sakharuk 406: for (my $i=0;$i<$number_of_tables;$i++) {push @table_range,$bubbles_per_line;}
407: if ($number_of_bubbles % $bubbles_per_line) {
1.90 albertel 408: $number_of_tables++;
1.128 sakharuk 409: push @table_range,($number_of_bubbles % $bubbles_per_line);
1.90 albertel 410: }
1.128 sakharuk 411: $cell_width-=8;
412: return ($cell_width,$number_of_tables,@table_range);
1.90 albertel 413: }
414:
415: sub format_number {
416: my ($number,$format,$target)=@_;
417: my $ans;
418: if ($format ne '') {
1.109 albertel 419: $format=~s/e/E/g;
1.90 albertel 420: $ans = sprintf('%.'.$format,$number);
421: } else {
422: my $format = '';
423: #What is the number? (integer,decimal,floating point)
424: if ($number=~/^(\d*\.?\d*)(E|e)(\d*)$/) {
1.113 sakharuk 425: $format = '3e';
1.90 albertel 426: } elsif ($number=~/^(\d*)\.(\d*)$/) {
427: $format = '4f';
428: } elsif ($number=~/^(\d*)$/) {
429: $format = 'd';
430: }
431: $ans = sprintf('%.'.$format,$number);
432: }
433: if ($target eq 'tex') {
434: if ($ans =~ m/([0-9\.\-\+]+)E([0-9\-\+]+)/ ) {
435: my $number = $1;
436: my $power = $2;
437: $power=~s/^\+//;
1.124 sakharuk 438: $power=~s/^(-?)0+(\d+)/$1$2/;
439: $ans=$number.'$\times 10^{'.$power.'}$'; #'stupidemacs
1.90 albertel 440: }
441: }
442: return $ans;
443: }
444:
445: sub make_numerical_bubbles {
1.117 albertel 446: my ($number_of_bubbles,$target,$answer,$format,$incorrect) =@_;
1.91 albertel 447: my @bubble_values = ();
1.119 albertel 448: &Apache::lonxml::debug("answer is $answer incorrect is $incorrect");
449: my @oldseed=&Math::Random::random_get_seed();
1.117 albertel 450: if (defined($incorrect) && ref($incorrect)) {
451: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1 gt $number_of_bubbles));
1.126 albertel 452: if (defined($$incorrect[0]) &&
453: scalar(@$incorrect)+1 >= $number_of_bubbles) {
1.117 albertel 454: &Apache::lonxml::debug("inside ".(scalar(@$incorrect)+1).":$number_of_bubbles");
455: &Apache::response::setrandomnumber();
456: my @rand_inc=&Math::Random::random_permutation(@$incorrect);
457: @bubble_values=@rand_inc[0..($number_of_bubbles-2)];
458: @bubble_values=sort {$a <=> $b} (@bubble_values,$answer);
459: &Apache::lonxml::debug("Answer was :$answer: returning :".$#bubble_values.": whih are :".join(':',@bubble_values));
1.119 albertel 460: &Math::Random::random_set_seed(@oldseed);
1.117 albertel 461: return @bubble_values;
462: }
1.127 albertel 463: if (defined($$incorrect[0]) &&
464: scalar(@$incorrect)+1 < $number_of_bubbles) {
465: &Apache::lonxml::warning("Not enough incorrect answers were specified in the incorrect array, ignoring the specified incorrect answers and instead generating them.");
466: }
1.117 albertel 467: }
1.90 albertel 468: my @factors = (1.13,1.17,1.25,1.33,1.45); #default values of factors
1.126 albertel 469: my @powers = (1..$number_of_bubbles);
1.90 albertel 470: &Apache::response::setrandomnumber();
471: my $ind=&Math::Random::random_uniform_integer(1,0,$#powers);
472: my $power = $powers[$ind];
473: $ind=&Math::Random::random_uniform_integer(1,0,$#factors);
474: my $factor = $factors[$ind];
475: for ($ind=0;$ind<$number_of_bubbles;$ind++) {
1.91 albertel 476: $bubble_values[$ind] = $answer*($factor**($power-$powers[$#powers-$ind]));
477: $bubble_values[$ind] = &format_number($bubble_values[$ind],
478: $format,$target);
479:
1.90 albertel 480: }
1.119 albertel 481: &Math::Random::random_set_seed(@oldseed);
1.91 albertel 482: return @bubble_values;
1.51 albertel 483: }
484:
485: sub get_tolrange {
1.89 albertel 486: my ($ans,$tol)=@_;
487: my ($high,$low);
488: if ($tol =~ /%$/) {
489: chop($tol);
490: my $change=$ans*($tol/100.0);
491: $high=$ans+$change;
492: $low=$ans-$change;
493: } else {
494: $high=$ans+$tol;
495: $low=$ans-$tol;
496: }
497: return ($high,$low);
1.52 albertel 498: }
499:
500: sub get_sigrange {
1.89 albertel 501: my ($sig)=@_;
502: &Apache::lonxml::debug("Got a sig of :$sig:");
503: my $sig_lbound;
504: my $sig_ubound;
505: if ($sig eq '') {
506: $sig_lbound = 0; #SIG_LB_DEFAULT
507: $sig_ubound =15; #SIG_UB_DEFAULT
508: } else {
509: ($sig_lbound,$sig_ubound) = split(/,/,$sig);
510: if (!$sig_lbound) {
511: $sig_lbound = 0; #SIG_LB_DEFAULT
512: $sig_ubound =15; #SIG_UB_DEFAULT
513: }
514: if (!$sig_ubound) { $sig_ubound=$sig_lbound; }
1.52 albertel 515: }
1.89 albertel 516: return ($sig_ubound,$sig_lbound);
1.34 albertel 517: }
518:
519: sub start_stringresponse {
1.89 albertel 520: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
521: my $result;
1.122 albertel 522: my $id = &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 523: if ($target eq 'meta') {
1.104 bowersj2 524: &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 525: $result=&Apache::response::meta_package_write('stringresponse');
1.104 bowersj2 526: &Apache::response::end_response();
1.122 albertel 527: } elsif ($target eq 'edit') {
528: $result.=&Apache::edit::tag_start($target,$token);
529: $result.=&Apache::edit::text_arg('Answer:','answer',$token);
530: $result.=&Apache::edit::select_arg('Type:','type',
531: [['cs','Case Sensitive'],['ci','Case Insensitive'],
532: ['mc','Case Insensitive, Any Order'],
533: ['re','Regular Expression']],$token);
1.129 www 534: $result.=&Apache::edit::checked_arg('Answer Display:','answerdisplay',
535: [['inline','Inline']],$token);
1.122 albertel 536: $result.=&Apache::edit::end_row().&Apache::edit::start_spanning_row();
537: } elsif ($target eq 'modified') {
1.129 www 538: &Apache::edit::get_new_args($token,$parstack,
539: $safeeval,'answer','type');
540: my $answerdisplay=$ENV{'form.'.&Apache::edit::html_element_name('answerdisplay')};
541: if ($answerdisplay) {
542: $token->[2]->{'answerdisplay'}=$answerdisplay;
543: } else {
544: delete $token->[2]->{'answerdisplay'};
1.122 albertel 545: }
1.129 www 546: $result = &Apache::edit::rebuild_tag($token);
547: $result.=&Apache::edit::handle_insert();
1.122 albertel 548: } elsif ($target eq 'answer' || $target eq 'grade') {
549: &Apache::response::reset_params();
1.89 albertel 550: }
551: return $result;
1.34 albertel 552: }
553:
554: sub end_stringresponse {
1.122 albertel 555: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
556: my $increment=1;
557: my $result = '';
558: my $part=$Apache::inputtags::part;
559: my $id=$Apache::inputtags::response[-1];
560: my $answer=&Apache::lonxml::get_param('answer',$parstack,$safeeval);
561: my $type=&Apache::lonxml::get_param('type',$parstack,$safeeval);
1.129 www 562: my $answerdisplay=&Apache::lonxml::get_param('answerdisplay',$parstack,$safeeval);
1.122 albertel 563: &Apache::lonxml::debug("current $answer ".$token->[2]);
564: if (!$Apache::lonxml::default_homework_loaded) {
565: &Apache::lonxml::default_homework_load($safeeval);
566: }
567: if ( $target eq 'grade' && defined($ENV{'form.submitted'})) {
1.125 albertel 568: &Apache::response::setup_params('stringresponse');
1.122 albertel 569: $safeeval->share_from('capa',['&caparesponse_capa_check_answer']);
570: if ($Apache::lonhomework::type eq 'exam' ||
571: $ENV{'form.submitted'} eq 'scantron') {
572: $increment=&Apache::response::scored_response($part,$id);
573: } else {
574: my $response = &Apache::response::getresponse();
575: if ( $response =~ /[^\s]/) {
576: my %previous = &Apache::response::check_for_previous($response,
577: $part,$id);
578: &Apache::lonxml::debug("submitted a $response<br>\n");
579: &Apache::lonxml::debug($$parstack[-1] . "\n<br>");
580:
581: $Apache::lonhomework::results{"resource.$part.$id.submission"}=
582: $response;
583: my $ad;
584: if ($type eq 're' ) {
585: # if the RE wasn't in a var it likely got munged,
586: # thus grab it from the var directly
587: # my $testans=$token->[2]->{'answer'};
588: # if ($testans !~ m/^\s*\$/) {
589: # $answer=$token->[2]->{'answer'};
590: # }
591: ${$safeeval->varglob('LONCAPA_INTERNAL_response')}=
592: $response;
593: $result = &Apache::run::run('return $LONCAPA_INTERNAL_response=~m'.$answer,$safeeval);
594: &Apache::lonxml::debug("current $response");
595: &Apache::lonxml::debug("current $answer");
596: $ad = ($result) ? 'APPROX_ANS' : 'INCORRECT';
597: } else {
598: $response =~ s/\\/\\\\/g;
599: $response =~ s/\'/\\\'/g;
600: &Apache::lonxml::debug("current $response");
601: my $expression="&caparesponse_check_list('".$response."','".
602: $$parstack[-1];
603: foreach my $key (keys(%Apache::inputtags::params)) {
604: $expression.= ';my $'. #'
605: $key.'="'.$Apache::inputtags::params{$key}.'"';
606: }
607: $expression.="');";
608: &Apache::lonxml::debug('answer is'.join(':',$answer));
609: @{$safeeval->varglob('CAPARESPONSE_CHECK_LIST_answer')}=($answer);
610: $result = &Apache::run::run($expression,$safeeval);
611: my ($awards) = split /:/ , $result;
612: ($ad) = &Apache::inputtags::finalizeawards(split /,/ , $awards);
613: &Apache::lonxml::debug("$expression");
614: &Apache::lonxml::debug("\n<br>result:$result:$Apache::lonxml::curdepth<br>\n");
615: }
616: &Apache::response::handle_previous(\%previous,$ad);
617: $Apache::lonhomework::results{"resource.$part.$id.awarddetail"}=$ad;
618: }
619: }
620: } elsif ($target eq 'web' || $target eq 'tex') {
621: my $award = $Apache::lonhomework::history{"resource.$Apache::inputtags::part.solved"};
622: my $status = $Apache::inputtags::status['-1'];
623: if ( &Apache::response::show_answer() ) {
624: if ($target eq 'web') {
1.129 www 625: $result=($answerdisplay eq 'inline'?'':"<br />".&mt('The correct answer is')." ")
626: .$answer;
1.122 albertel 627: # join(', ',@answers).".<br />";
628: }
629: }
630: if ($Apache::lonhomework::type eq 'exam' && $target eq 'tex') {
631: $result.='\fbox{\fbox{\parbox{\textwidth-5mm}{\strut\\\\\strut\\\\\strut\\\\\strut\\\\}}}';
632: $increment = &Apache::response::repetition();
633: $result.='\begin{enumerate}';
634: for (my $i=0;$i<$increment;$i++) {
635: $result.='\item[\textbf{'.($Apache::lonxml::counter+$i).
636: '}.]\textit{Leave blank on scoring form}\vskip 0 mm';
637: }
638: $result.= '\end{enumerate}';
639: }
640: } elsif ($target eq 'answer' || $target eq 'analyze') {
641: if ($target eq 'analyze') {
642: push (@{ $Apache::lonhomework::analyze{"parts"} },"$part.$id");
1.125 albertel 643: $Apache::lonhomework::analyze{"$part.$id.type"} = 'stringresponse';
1.122 albertel 644: }
1.125 albertel 645: &Apache::response::setup_params('stringresponse');
1.122 albertel 646: if ($target eq 'answer') {
1.125 albertel 647: $result.=&Apache::response::answer_header('stringresponse');
1.122 albertel 648: }
649: # foreach my $ans (@answers) {
650: if ($target eq 'answer') {
1.125 albertel 651: $result.=&Apache::response::answer_part('stringresponse',$answer);
1.122 albertel 652: } elsif ($target eq 'analyze') {
653: push (@{ $Apache::lonhomework::analyze{"$part.$id.answer"} },
654: $answer);
655: }
656: # }
657: my $string='Case Insensitive';
658: if ($type eq 'mc') {
659: $string='Multiple Choice';
660: } elsif ($type eq 'cs') {
661: $string='Case Sensitive';
662: } elsif ($type eq 'ci') {
663: $string='Case Insensitive';
664: } elsif ($type eq 're') {
665: $string='Regular Expression';
666: }
667: if ($target eq 'answer') {
668: if ($ENV{'form.answer_output_mode'} eq 'tex') {
1.125 albertel 669: $result.=&Apache::response::answer_part('stringresponse',
1.122 albertel 670: "$string");
671: } else {
1.125 albertel 672: $result.=&Apache::response::answer_part('stringresponse',
1.122 albertel 673: "<b>$string</b>");
674: }
675: } elsif ($target eq 'analyze') {
676: push (@{$Apache::lonhomework::analyze{"$part.$id.str_type"}},
677: $type);
678: }
679: if ($target eq 'answer') {
1.125 albertel 680: $result.=&Apache::response::answer_footer('stringresponse');
1.122 albertel 681: }
682: } elsif ($target eq 'edit') {
683: $result.='</td></tr>'.&Apache::edit::end_table;
684: }
685: if ($target eq 'grade' || $target eq 'web' || $target eq 'answer' ||
686: $target eq 'tex' || $target eq 'analyze') {
687: &Apache::lonxml::increment_counter($increment);
688: }
689: &Apache::response::end_response;
690: return $result;
1.44 albertel 691: }
692:
693: sub start_formularesponse {
1.89 albertel 694: my ($target,$token,$tagstack,$parstack,$parser,$safeeval,$style)=@_;
695: my $result;
696: if ($target eq 'meta') {
1.104 bowersj2 697: &Apache::response::start_response($parstack,$safeeval);
1.89 albertel 698: $result=&Apache::response::meta_package_write('formularesponse');
1.104 bowersj2 699: &Apache::response::end_response();
1.89 albertel 700: } else {
701: $result.=&start_numericalresponse(@_);
702: }
703: return $result;
1.44 albertel 704: }
705:
706: sub end_formularesponse {
1.89 albertel 707: return end_numericalresponse(@_);
1.3 albertel 708: }
709:
1.1 albertel 710: 1;
1.3 albertel 711: __END__
1.89 albertel 712:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>